added support for windows
This commit is contained in:
parent
583ac36a30
commit
a7ac30b380
1 changed files with 142 additions and 54 deletions
184
files.php
184
files.php
|
@ -258,9 +258,97 @@
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function systemify_path($path) {
|
||||||
|
return str_replace("/", DIRECTORY_SEPARATOR, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
function join_path(...$paths) {
|
||||||
|
$result_path = "";
|
||||||
|
foreach ($paths as $path) {
|
||||||
|
if(strlen($result_path) > 0)
|
||||||
|
$result_path .= DIRECTORY_SEPARATOR . $path;
|
||||||
|
else
|
||||||
|
$result_path = $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
function create_directories(&$error, $path, $dry_run = false) {
|
||||||
|
if(strpos(PHP_OS, "Linux") !== false) {
|
||||||
|
$command = "mkdir -p " . $path;
|
||||||
|
} else if(strpos(PHP_OS, "WINNT") !== false) {
|
||||||
|
$command = "mkdir " . $path; /* default path tree */
|
||||||
|
} else {
|
||||||
|
$error = "unsupported system";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $command . PHP_EOL;
|
||||||
|
if(!$dry_run) {
|
||||||
|
exec($command, $error, $state);
|
||||||
|
if($state) {
|
||||||
|
$error = "Command execution results in " . $state . ": " . implode(' ', $error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete_directories(&$error, $path, $dry_run = false) {
|
||||||
|
if(strpos(PHP_OS, "Linux") !== false) {
|
||||||
|
$command = "rm -r " . $path;
|
||||||
|
} else if(strpos(PHP_OS, "WINNT") !== false) {
|
||||||
|
$command = "rm -r " . $path;
|
||||||
|
} else {
|
||||||
|
$error = "unsupported system";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $command . PHP_EOL;
|
||||||
|
if(!$dry_run) {
|
||||||
|
$state = 0;
|
||||||
|
exec($command, $output, $state);
|
||||||
|
|
||||||
|
if($state !== 0) {
|
||||||
|
$error = "Command execution results in " . $state . ": " . implode(' ', $output);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function create_link(&$error, $source, $target, $dry_run = false) {
|
||||||
|
if(strpos(PHP_OS, "Linux") !== false) {
|
||||||
|
$command = "ln -s " . $source . " " . $target;
|
||||||
|
} else if(strpos(PHP_OS, "WINNT") !== false) {
|
||||||
|
$command = "mklink " . (is_dir($target) ? "/D " : "") . " " . $target . " " . $source;
|
||||||
|
} else {
|
||||||
|
$error = "unsupported system";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $command . PHP_EOL;
|
||||||
|
if(!$dry_run) {
|
||||||
|
$state = 0;
|
||||||
|
exec($command, $output, $state);
|
||||||
|
|
||||||
|
if($state !== 0) {
|
||||||
|
$error = "Command execution results in " . $state . ": " . implode(' ', $output);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function list_dir($base_dir, $match = null, $depth = -1, &$results = array(), $dir = "") {
|
function list_dir($base_dir, $match = null, $depth = -1, &$results = array(), $dir = "") {
|
||||||
if($depth == 0) return $results;
|
if($depth == 0) return $results;
|
||||||
|
|
||||||
|
if(!is_dir($base_dir . $dir)) {
|
||||||
|
echo "Skipping directory " . $base_dir . $dir . PHP_EOL;
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
$files = scandir($base_dir . $dir);
|
$files = scandir($base_dir . $dir);
|
||||||
|
|
||||||
foreach($files as $key => $value){
|
foreach($files as $key => $value){
|
||||||
|
@ -279,12 +367,14 @@
|
||||||
class AppFile {
|
class AppFile {
|
||||||
public $type;
|
public $type;
|
||||||
public $name;
|
public $name;
|
||||||
public $path;
|
|
||||||
public $local_path;
|
public $target_path; /* relative path to the target file viewed from the file root */
|
||||||
|
public $local_path; /* absolute path to local file */
|
||||||
|
|
||||||
public $hash;
|
public $hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
function find_files($flag = 0b11, $local_path_prefix = "./", $type = "dev", $args = []) { //TODO Use cache here!
|
function find_files($flag = 0b11, $local_path_prefix = "." . DIRECTORY_SEPARATOR, $type = "dev", $args = []) { //TODO Use cache here!
|
||||||
global $APP_FILE_LIST;
|
global $APP_FILE_LIST;
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
|
@ -303,23 +393,22 @@
|
||||||
if(!$valid)
|
if(!$valid)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$entries = list_dir($local_path_prefix . $entry["local-path"], $entry["search-pattern"], isset($entry["search-depth"]) ? $entry["search-depth"] : -1);
|
$entries = list_dir(
|
||||||
|
systemify_path($local_path_prefix . $entry["local-path"]),
|
||||||
|
$entry["search-pattern"],
|
||||||
|
isset($entry["search-depth"]) ? $entry["search-depth"] : -1
|
||||||
|
);
|
||||||
foreach ($entries as $f_entry) {
|
foreach ($entries as $f_entry) {
|
||||||
if(isset($entry["search-exclude"]) && preg_match($entry["search-exclude"], $f_entry)) continue;
|
if(isset($entry["search-exclude"]) && preg_match($entry["search-exclude"], $f_entry)) continue;
|
||||||
$file = new AppFile;
|
$file = new AppFile;
|
||||||
|
|
||||||
$idx_sep = strrpos($f_entry, DIRECTORY_SEPARATOR);
|
$f_info = pathinfo($f_entry);
|
||||||
$file->path = "./" . $entry["path"] . "/";
|
$file->target_path = systemify_path($entry["path"]) . DIRECTORY_SEPARATOR . $f_info["dirname"] . DIRECTORY_SEPARATOR;
|
||||||
if($idx_sep > 0) {
|
$file->local_path = getcwd() . DIRECTORY_SEPARATOR . systemify_path($entry["local-path"]) . DIRECTORY_SEPARATOR . $f_info["dirname"] . DIRECTORY_SEPARATOR;
|
||||||
$file->name = substr($f_entry, strrpos($f_entry, DIRECTORY_SEPARATOR) + 1);
|
|
||||||
$file->path = $file->path . substr($f_entry, 0, strrpos($f_entry, DIRECTORY_SEPARATOR));
|
|
||||||
} else {
|
|
||||||
$file->name = $f_entry;
|
|
||||||
}
|
|
||||||
|
|
||||||
$file->local_path = $local_path_prefix . $entry["local-path"] . DIRECTORY_SEPARATOR . $f_entry;
|
$file->name = $f_info["basename"];
|
||||||
$file->type = $entry["type"];
|
$file->type = $entry["type"];
|
||||||
$file->hash = sha1_file($file->local_path);
|
$file->hash = sha1_file($file->local_path . DIRECTORY_SEPARATOR . $file->name);
|
||||||
|
|
||||||
if(strlen($file->hash) > 0) {
|
if(strlen($file->hash) > 0) {
|
||||||
foreach ($result as $e)
|
foreach ($result as $e)
|
||||||
|
@ -334,10 +423,17 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($_SERVER["argv"])) { //Executed by command line
|
if(isset($_SERVER["argv"])) { //Executed by command line
|
||||||
if(strpos(PHP_OS, "Linux") == -1) {
|
$supported = false;
|
||||||
error_log("Invalid operating system! Help tool only available under linux!");
|
if(strpos(PHP_OS, "Linux") !== false) {
|
||||||
|
$supported = true;
|
||||||
|
} else if(strpos(PHP_OS, "WIN") !== false) {
|
||||||
|
$supported = true;
|
||||||
|
}
|
||||||
|
if(!$supported) {
|
||||||
|
error_log("Invalid operating system (" . PHP_OS . ")! Help tool only available under linux!");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($_SERVER["argv"]) < 2) {
|
if(count($_SERVER["argv"]) < 2) {
|
||||||
error_log("Invalid parameters!");
|
error_log("Invalid parameters!");
|
||||||
goto help;
|
goto help;
|
||||||
|
@ -358,10 +454,10 @@
|
||||||
if($_SERVER["argv"][3] == "dev" || $_SERVER["argv"][3] == "development") {
|
if($_SERVER["argv"][3] == "dev" || $_SERVER["argv"][3] == "development") {
|
||||||
if ($_SERVER["argv"][2] == "web") {
|
if ($_SERVER["argv"][2] == "web") {
|
||||||
$flagset = 0b01;
|
$flagset = 0b01;
|
||||||
$environment = "web/environment/development";
|
$environment = join_path("web", "environment", "development");
|
||||||
} else if ($_SERVER["argv"][2] == "client") {
|
} else if ($_SERVER["argv"][2] == "client") {
|
||||||
$flagset = 0b10;
|
$flagset = 0b10;
|
||||||
$environment = "client-api/environment/ui-files/raw";
|
$environment = join_path("client-api", "environment", "ui-files", "raw");
|
||||||
} else {
|
} else {
|
||||||
error_log("Invalid type!");
|
error_log("Invalid type!");
|
||||||
goto help;
|
goto help;
|
||||||
|
@ -370,10 +466,10 @@
|
||||||
$type = "rel";
|
$type = "rel";
|
||||||
if ($_SERVER["argv"][2] == "web") {
|
if ($_SERVER["argv"][2] == "web") {
|
||||||
$flagset = 0b01;
|
$flagset = 0b01;
|
||||||
$environment = "web/environment/release";
|
$environment = join_path("web", "environment", "release");
|
||||||
} else if ($_SERVER["argv"][2] == "client") {
|
} else if ($_SERVER["argv"][2] == "client") {
|
||||||
$flagset = 0b10;
|
$flagset = 0b10;
|
||||||
$environment = "client-api/environment/ui-files/raw";
|
$environment = join_path("client-api", "environment", "ui-files", "raw");
|
||||||
} else {
|
} else {
|
||||||
error_log("Invalid type!");
|
error_log("Invalid type!");
|
||||||
goto help;
|
goto help;
|
||||||
|
@ -385,37 +481,29 @@
|
||||||
|
|
||||||
{
|
{
|
||||||
if(!$dry_run) {
|
if(!$dry_run) {
|
||||||
exec($command = "rm -r " . $environment, $output, $state);
|
if(delete_directories($error, $environment) === false)
|
||||||
exec($command = "mkdir -p " . $environment, $output, $state); if($state) goto handle_error;
|
goto handle_error;
|
||||||
|
|
||||||
|
if(create_directories($error, $environment) === false)
|
||||||
|
goto handle_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
$files = find_files($flagset, "./", $type, array_slice($_SERVER["argv"], 4));
|
$files = find_files($flagset, "." . DIRECTORY_SEPARATOR, $type, array_slice($_SERVER["argv"], 4));
|
||||||
$original_path = realpath(".");
|
$original_path = realpath(".");
|
||||||
if(!chdir($environment)) {
|
if(!chdir($environment)) {
|
||||||
error_log("Failed to enter directory " . $environment . "!");
|
error_log("Failed to enter directory " . $environment . "!");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var AppFile $file */
|
||||||
foreach($files as $file) {
|
foreach($files as $file) {
|
||||||
if(!$dry_run && !is_dir($file->path)) {
|
if(!$dry_run && !is_dir($file->target_path) && strlen($file->target_path) > 0) {
|
||||||
exec($command = "mkdir -p " . $file->path, $output, $state);
|
if(create_directories($error, $file->target_path, $dry_run) === false)
|
||||||
if($state) goto handle_error;
|
goto handle_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parent_base = substr_count(realpath($file->path), DIRECTORY_SEPARATOR) - substr_count(realpath('.'), DIRECTORY_SEPARATOR);
|
if(create_link($output, $file->local_path . $file->name, $file->target_path . $file->name, $dry_run) === false)
|
||||||
$parent_file = substr_count(realpath("."), DIRECTORY_SEPARATOR) - substr_count($original_path, DIRECTORY_SEPARATOR); //Current to parent
|
goto handle_error;
|
||||||
$parent = $parent_base + $parent_file;
|
|
||||||
|
|
||||||
$path = "";
|
|
||||||
for($index = 0; $index < $parent; $index++)
|
|
||||||
$path = $path . "../";
|
|
||||||
|
|
||||||
$command = "ln -s " . $path . $file->local_path . " " . $file->path;
|
|
||||||
if(!$dry_run) {
|
|
||||||
exec($command, $output, $state);
|
|
||||||
if($state) goto handle_error;
|
|
||||||
}
|
|
||||||
echo $command . PHP_EOL;
|
|
||||||
}
|
}
|
||||||
if(!chdir($original_path)) {
|
if(!chdir($original_path)) {
|
||||||
error_log("Failed to reset directory!");
|
error_log("Failed to reset directory!");
|
||||||
|
@ -425,18 +513,20 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$dry_run) {
|
if(!$dry_run) {
|
||||||
exec("./scripts/git_index.sh sort-tag", $output, $state);
|
exec("." . DIRECTORY_SEPARATOR . "scripts" . DIRECTORY_SEPARATOR . "git_index.sh sort-tag", $output, $state);
|
||||||
file_put_contents($environment . DIRECTORY_SEPARATOR . "version", $output);
|
file_put_contents($environment . DIRECTORY_SEPARATOR . "version", $output);
|
||||||
|
|
||||||
if ($_SERVER["argv"][2] == "client") {
|
if ($_SERVER["argv"][2] == "client") {
|
||||||
if(!chdir("client-api/environment")) {
|
if(!chdir("client-api" . DIRECTORY_SEPARATOR . "environment")) {
|
||||||
error_log("Failed to enter directory client-api/environment!");
|
error_log("Failed to enter directory client-api/environment!");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if(!is_dir("versions/beta"))
|
if(!is_dir("versions" . DIRECTORY_SEPARATOR . "beta")) {
|
||||||
exec($command = "mkdir -p versions/beta", $output, $state); if($state) goto handle_error;
|
exec($command = "mkdir -p versions/beta", $output, $state); if($state) goto handle_error;
|
||||||
if(!is_dir("versions/stable"))
|
}
|
||||||
|
if(!is_dir("versions/stable")) {
|
||||||
exec($command = "mkdir -p versions/beta", $output, $state); if($state) goto handle_error;
|
exec($command = "mkdir -p versions/beta", $output, $state); if($state) goto handle_error;
|
||||||
|
}
|
||||||
|
|
||||||
exec($command = "ln -s ../api.php ./", $output, $state); $state = 0; //Dont handle an error here!
|
exec($command = "ln -s ../api.php ./", $output, $state); $state = 0; //Dont handle an error here!
|
||||||
if($state) goto handle_error;
|
if($state) goto handle_error;
|
||||||
|
@ -445,10 +535,8 @@
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
handle_error:
|
handle_error:
|
||||||
error_log("Failed to execute command '" . $command . "'!");
|
error_log("Command execution failed!");
|
||||||
error_log("Command returned code " . $state . ". Output: " . PHP_EOL);
|
error_log("Error message: " . $error);
|
||||||
foreach ($output as $line)
|
|
||||||
error_log($line);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue