Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Package/Artifact/go_xcaddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Package\Artifact;

use SPC\util\GlobalEnvManager;
use StaticPHP\Artifact\ArtifactDownloader;
use StaticPHP\Artifact\Downloader\DownloadResult;
use StaticPHP\Attribute\Artifact\AfterBinaryExtract;
use StaticPHP\Attribute\Artifact\CustomBinary;
use StaticPHP\Exception\DownloaderException;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Util\GlobalEnvManager;
use StaticPHP\Util\System\LinuxUtil;

class go_xcaddy
Expand Down
98 changes: 98 additions & 0 deletions src/Package/Artifact/zig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Package\Artifact;

use StaticPHP\Artifact\ArtifactDownloader;
use StaticPHP\Artifact\Downloader\DownloadResult;
use StaticPHP\Attribute\Artifact\AfterBinaryExtract;
use StaticPHP\Attribute\Artifact\CustomBinary;
use StaticPHP\Exception\DownloaderException;
use StaticPHP\Runtime\SystemTarget;

class zig
{
#[CustomBinary('zig', [
'linux-x86_64',
'linux-aarch64',
'macos-x86_64',
'macos-aarch64',
])]
public function downBinary(ArtifactDownloader $downloader): DownloadResult
{
$index_json = default_shell()->executeCurl('https://ziglang.org/download/index.json', retries: $downloader->getRetry());
$index_json = json_decode($index_json ?: '', true);
$latest_version = null;
foreach ($index_json as $version => $data) {
$latest_version = $version;
break;
}

if (!$latest_version) {
throw new DownloaderException('Could not determine latest Zig version');
}
$zig_arch = SystemTarget::getTargetArch();
$zig_os = match (SystemTarget::getTargetOS()) {
'Windows' => 'win',
'Darwin' => 'macos',
'Linux' => 'linux',
default => throw new DownloaderException('Unsupported OS for Zig: ' . SystemTarget::getTargetOS()),
};
$platform_key = "{$zig_arch}-{$zig_os}";
if (!isset($index_json[$latest_version][$platform_key])) {
throw new DownloaderException("No download available for {$platform_key} in Zig version {$latest_version}");
}
$download_info = $index_json[$latest_version][$platform_key];
$url = $download_info['tarball'];
$sha256 = $download_info['shasum'];
$filename = basename($url);
$path = DOWNLOAD_PATH . DIRECTORY_SEPARATOR . $filename;
default_shell()->executeCurlDownload($url, $path, retries: $downloader->getRetry());
// verify hash
$file_hash = hash_file('sha256', $path);
if ($file_hash !== $sha256) {
throw new DownloaderException("Hash mismatch for downloaded Zig binary. Expected {$sha256}, got {$file_hash}");
}
return DownloadResult::archive(basename($path), ['url' => $url, 'version' => $latest_version], extract: PKG_ROOT_PATH . '/zig', verified: true, version: $latest_version);
}

#[AfterBinaryExtract('zig', [
'linux-x86_64',
'linux-aarch64',
'macos-x86_64',
'macos-aarch64',
])]
public function postExtractZig(string $target_path): void
{
$files = ['zig', 'zig-cc', 'zig-c++', 'zig-ar', 'zig-ld.lld', 'zig-ranlib', 'zig-objcopy'];
$all_exist = true;
foreach ($files as $file) {
if (!file_exists("{$target_path}/{$file}")) {
$all_exist = false;
break;
}
}
if ($all_exist) {
return;
}

$script_path = ROOT_DIR . '/src/globals/scripts/zig-cc.sh';
$script_content = file_get_contents($script_path);

file_put_contents("{$target_path}/zig-cc", $script_content);
chmod("{$target_path}/zig-cc", 0755);

$script_content = str_replace('zig cc', 'zig c++', $script_content);
file_put_contents("{$target_path}/zig-c++", $script_content);
file_put_contents("{$target_path}/zig-ar", "#!/usr/bin/env bash\nexec zig ar $@");
file_put_contents("{$target_path}/zig-ld.lld", "#!/usr/bin/env bash\nexec zig ld.lld $@");
file_put_contents("{$target_path}/zig-ranlib", "#!/usr/bin/env bash\nexec zig ranlib $@");
file_put_contents("{$target_path}/zig-objcopy", "#!/usr/bin/env bash\nexec zig objcopy $@");
chmod("{$target_path}/zig-c++", 0755);
chmod("{$target_path}/zig-ar", 0755);
chmod("{$target_path}/zig-ld.lld", 0755);
chmod("{$target_path}/zig-ranlib", 0755);
chmod("{$target_path}/zig-objcopy", 0755);
}
}
2 changes: 1 addition & 1 deletion src/Package/Target/go_xcaddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Package\Target;

use SPC\util\GlobalEnvManager;
use StaticPHP\Attribute\Package\InitPackage;
use StaticPHP\Attribute\Package\Target;
use StaticPHP\Util\GlobalEnvManager;

#[Target('go-xcaddy')]
class go_xcaddy
Expand Down
14 changes: 7 additions & 7 deletions src/StaticPHP/Artifact/ArtifactDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function __construct(protected array $options = [])
foreach ($ls as $name) {
$this->fetch_prefs[$name] = Artifact::FETCH_PREFER_SOURCE;
}
} elseif ($options['prefer-source'] === null) {
} elseif ($options['prefer-source'] === null || $options['prefer-source'] === true) {
$this->default_fetch_pref = Artifact::FETCH_PREFER_SOURCE;
}
}
Expand All @@ -124,7 +124,7 @@ public function __construct(protected array $options = [])
foreach ($ls as $name) {
$this->fetch_prefs[$name] = Artifact::FETCH_PREFER_BINARY;
}
} elseif ($options['prefer-binary'] === null) {
} elseif ($options['prefer-binary'] === null || $options['prefer-binary'] === true) {
$this->default_fetch_pref = Artifact::FETCH_PREFER_BINARY;
}
}
Expand All @@ -134,7 +134,7 @@ public function __construct(protected array $options = [])
foreach ($ls as $name) {
$this->fetch_prefs[$name] = Artifact::FETCH_PREFER_BINARY;
}
} elseif ($options['prefer-pre-built'] === null) {
} elseif ($options['prefer-pre-built'] === null || $options['prefer-pre-built'] === true) {
$this->default_fetch_pref = Artifact::FETCH_PREFER_BINARY;
}
}
Expand All @@ -145,7 +145,7 @@ public function __construct(protected array $options = [])
foreach ($ls as $name) {
$this->fetch_prefs[$name] = Artifact::FETCH_ONLY_SOURCE;
}
} elseif ($options['source-only'] === null) {
} elseif ($options['source-only'] === null || $options['source-only'] === true) {
$this->default_fetch_pref = Artifact::FETCH_ONLY_SOURCE;
}
}
Expand All @@ -156,23 +156,23 @@ public function __construct(protected array $options = [])
foreach ($ls as $name) {
$this->fetch_prefs[$name] = Artifact::FETCH_ONLY_BINARY;
}
} elseif ($options['binary-only'] === null) {
} elseif ($options['binary-only'] === null || $options['binary-only'] === true) {
$this->default_fetch_pref = Artifact::FETCH_ONLY_BINARY;
}
}
// Ignore cache
if (array_key_exists('ignore-cache', $options)) {
if (is_string($options['ignore-cache'])) {
$this->ignore_cache = parse_comma_list($options['ignore-cache']);
} elseif ($options['ignore-cache'] === null) {
} elseif ($options['ignore-cache'] === null || $options['ignore-cache'] === true) {
$this->ignore_cache = true;
}
}
// backward compatibility for ignore-cache-sources
if (array_key_exists('ignore-cache-sources', $options)) {
if (is_string($options['ignore-cache-sources'])) {
$this->ignore_cache = parse_comma_list($options['ignore-cache-sources']);
} elseif ($options['ignore-cache-sources'] === null) {
} elseif ($options['ignore-cache-sources'] === null || $options['ignore-cache-sources'] === true) {
$this->ignore_cache = true;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/StaticPHP/Command/Dev/IsInstalledCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace StaticPHP\Command\Dev;

use StaticPHP\Command\BaseCommand;
use StaticPHP\Package\PackageInstaller;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;

#[AsCommand('dev:is-installed', 'Check if a package is installed correctly', ['is-installed'], true)]
class IsInstalledCommand extends BaseCommand
{
public function configure(): void
{
$this->no_motd = true;
$this->addArgument('package', InputArgument::REQUIRED, 'The package name to check installation status');
}

public function handle(): int
{
$installer = new PackageInstaller();
$package = $this->input->getArgument('package');
$installer->addInstallPackage($package);
$installed = $installer->isPackageInstalled($package);
if ($installed) {
$this->output->writeln("<info>Package [{$package}] is installed correctly.</info>");
return static::SUCCESS;
}
$this->output->writeln("<error>Package [{$package}] is not installed.</error>");
return static::FAILURE;
}
}
33 changes: 33 additions & 0 deletions src/StaticPHP/Command/Dev/ShellCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace StaticPHP\Command\Dev;

use StaticPHP\Command\BaseCommand;
use StaticPHP\Runtime\SystemTarget;
use StaticPHP\Util\GlobalEnvManager;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand('dev:shell')]
class ShellCommand extends BaseCommand
{
public function handle(): int
{
// need to init global env first
GlobalEnvManager::afterInit();

$this->output->writeln("Entering interactive shell. Type 'exit' to leave.");

if (SystemTarget::isUnix()) {
passthru('PS1=\'[StaticPHP] > \' /bin/bash', $code);
return $code;
}
if (SystemTarget::getTargetOS() === 'Windows') {
passthru('cmd.exe', $code);
return $code;
}
$this->output->writeln('<error>Unsupported OS for shell command.</error>');
return static::FAILURE;
}
}
6 changes: 6 additions & 0 deletions src/StaticPHP/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use StaticPHP\Command\BuildLibsCommand;
use StaticPHP\Command\BuildTargetCommand;
use StaticPHP\Command\Dev\IsInstalledCommand;
use StaticPHP\Command\Dev\ShellCommand;
use StaticPHP\Command\DoctorCommand;
use StaticPHP\Command\DownloadCommand;
use StaticPHP\Command\ExtractCommand;
Expand Down Expand Up @@ -47,6 +49,10 @@ public function __construct()
new BuildLibsCommand(),
new ExtractCommand(),
new SPCConfigCommand(),

// dev commands
new ShellCommand(),
new IsInstalledCommand(),
]);

// add additional commands from registries
Expand Down
Loading