mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-04 23:35:40 +08:00
add windows support
This commit is contained in:
@@ -160,78 +160,35 @@ class FileSystem
|
||||
}
|
||||
logger()->info("extracting {$name} source to " . ($move_path ?? SOURCE_PATH . "/{$name}") . ' ...');
|
||||
try {
|
||||
$target = $move_path ?? (SOURCE_PATH . "/{$name}");
|
||||
$target = self::convertPath($move_path ?? (SOURCE_PATH . "/{$name}"));
|
||||
// Git source, just move
|
||||
if (is_dir($filename)) {
|
||||
self::copyDir($filename, $target);
|
||||
if (is_dir(self::convertPath($filename))) {
|
||||
self::copyDir(self::convertPath($filename), $target);
|
||||
self::emitSourceExtractHook($name);
|
||||
return;
|
||||
}
|
||||
if (f_mkdir(directory: $target, recursive: true) !== true) {
|
||||
throw new FileSystemException('create ' . $name . 'source dir failed');
|
||||
}
|
||||
|
||||
if (in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'BSD'])) {
|
||||
if (f_mkdir(directory: $target, recursive: true) !== true) {
|
||||
throw new FileSystemException('create ' . $name . 'source dir failed');
|
||||
}
|
||||
switch (self::extname($filename)) {
|
||||
case 'xz':
|
||||
case 'txz':
|
||||
f_passthru("tar -xf {$filename} -C {$target} --strip-components 1");
|
||||
// f_passthru("cat {$filename} | xz -d | tar -x -C " . SOURCE_PATH . "/{$name} --strip-components 1");
|
||||
break;
|
||||
case 'gz':
|
||||
case 'tgz':
|
||||
f_passthru("tar -xzf {$filename} -C {$target} --strip-components 1");
|
||||
break;
|
||||
case 'bz2':
|
||||
f_passthru("tar -xjf {$filename} -C {$target} --strip-components 1");
|
||||
break;
|
||||
case 'zip':
|
||||
f_passthru("unzip {$filename} -d {$target}");
|
||||
break;
|
||||
// case 'zstd':
|
||||
// case 'zst':
|
||||
// passthru('cat ' . $filename . ' | zstd -d | tar -x -C ".SOURCE_PATH . "/' . $name . ' --strip-components 1', $ret);
|
||||
// break;
|
||||
case 'tar':
|
||||
f_passthru("tar -xf {$filename} -C {$target} --strip-components 1");
|
||||
break;
|
||||
default:
|
||||
throw new FileSystemException('unknown archive format: ' . $filename);
|
||||
}
|
||||
match (self::extname($filename)) {
|
||||
'tar', 'xz', 'txz' => f_passthru("tar -xf {$filename} -C {$target} --strip-components 1"),
|
||||
'tgz', 'gz' => f_passthru("tar -xzf {$filename} -C {$target} --strip-components 1"),
|
||||
'bz2' => f_passthru("tar -xjf {$filename} -C {$target} --strip-components 1"),
|
||||
'zip' => f_passthru("unzip {$filename} -d {$target}"),
|
||||
default => throw new FileSystemException('unknown archive format: ' . $filename),
|
||||
};
|
||||
} elseif (PHP_OS_FAMILY === 'Windows') {
|
||||
// find 7z
|
||||
$_7zExe = self::findCommandPath('7z', [
|
||||
'C:\Program Files\7-Zip-Zstandard',
|
||||
'C:\Program Files (x86)\7-Zip-Zstandard',
|
||||
'C:\Program Files\7-Zip',
|
||||
'C:\Program Files (x86)\7-Zip',
|
||||
]);
|
||||
if (!$_7zExe) {
|
||||
throw new FileSystemException('windows needs 7z to unpack');
|
||||
}
|
||||
// use php-sdk-binary-tools/bin/7za.exe
|
||||
$_7z = self::convertPath(PHP_SDK_PATH . '/bin/7za.exe');
|
||||
f_mkdir(SOURCE_PATH . "/{$name}", recursive: true);
|
||||
switch (self::extname($filename)) {
|
||||
case 'zstd':
|
||||
case 'zst':
|
||||
if (!str_contains($_7zExe, 'Zstandard')) {
|
||||
throw new FileSystemException("zstd is not supported: {$filename}");
|
||||
}
|
||||
// no break
|
||||
case 'xz':
|
||||
case 'txz':
|
||||
case 'gz':
|
||||
case 'tgz':
|
||||
case 'bz2':
|
||||
f_passthru("\"{$_7zExe}\" x -so {$filename} | tar -f - -x -C {$target} --strip-components 1");
|
||||
break;
|
||||
case 'tar':
|
||||
f_passthru("tar -xf {$filename} -C {$target} --strip-components 1");
|
||||
break;
|
||||
case 'zip':
|
||||
f_passthru("\"{$_7zExe}\" x {$filename} -o{$target}");
|
||||
break;
|
||||
default:
|
||||
throw new FileSystemException("unknown archive format: {$filename}");
|
||||
}
|
||||
match (self::extname($filename)) {
|
||||
'tar' => f_passthru("tar -xf {$filename} -C {$target} --strip-components 1"),
|
||||
'xz', 'txz', 'gz', 'tgz', 'bz2' => f_passthru("\"{$_7z}\" x -so {$filename} | tar -f - -x -C {$target} --strip-components 1"),
|
||||
'zip' => f_passthru("\"{$_7z}\" x {$filename} -o{$target} -y"),
|
||||
default => throw new FileSystemException("unknown archive format: {$filename}"),
|
||||
};
|
||||
}
|
||||
self::emitSourceExtractHook($name);
|
||||
} catch (RuntimeException $e) {
|
||||
@@ -398,7 +355,7 @@ class FileSystem
|
||||
public static function createDir(string $path): void
|
||||
{
|
||||
if (!is_dir($path) && !f_mkdir($path, 0755, true) && !is_dir($path)) {
|
||||
throw new FileSystemException(sprintf('无法建立目录:%s', $path));
|
||||
throw new FileSystemException(sprintf('Unable to create dir: %s', $path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,7 +365,7 @@ class FileSystem
|
||||
*/
|
||||
public static function writeFile(string $path, mixed $content, ...$args): bool|int|string
|
||||
{
|
||||
$dir = pathinfo($path, PATHINFO_DIRNAME);
|
||||
$dir = pathinfo(self::convertPath($path), PATHINFO_DIRNAME);
|
||||
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
|
||||
throw new FileSystemException('Write file failed, cannot create parent directory: ' . $dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user