Rework build system to reduce repetition

This commit is contained in:
Starbeamrainbowlabs 2015-09-20 12:37:21 +01:00
parent f915df22ff
commit daa8ac2a3b
7 changed files with 136 additions and 123 deletions

View File

@ -5,9 +5,6 @@ php:
- 5.6 - 5.6
- nightly - nightly
before_script: before_script: php build.php
- rm build/index.php
- php rebuild_module_index.php
- php build.php
script: php -l build/index.php script: php -l build/index.php

View File

@ -1,5 +1,2 @@
@echo off @echo off
echo Deleting old index.php php build.php
del build\index.php
php rebuild_module_index.php
php build.php

133
build.php
View File

@ -1,79 +1,54 @@
<?php <?php
if(php_sapi_name() == "cli") echo("*** Preparing environment ***\n");
{
echo("Beginning build...\n"); $build_env = new stdClass();
echo("Reading in module index...\n"); $build_env->target = "build/index.php";
}
if(file_exists($build_env->target))
$module_index = json_decode(file_get_contents("module_index.json")); {
$module_list = []; echo("Deleting old target...\n");
foreach($module_index as $module) unlink($build_env->target);
{ }
$module_list[] = $module->id;
} //////////////////////////////////////////////////////////////////////
//////////////////////// Rebuild Module Index ////////////////////////
if(isset($_GET["modules"])) //////////////////////////////////////////////////////////////////////
{
$module_list = explode(",", $_GET["modules"]); echo("*** Rebuilding module index ***\n");
} $modules = glob("modules/*.php");
$module_index = [];
if(php_sapi_name() != "cli")
{ function register_module($settings)
header("content-type: text/php"); {
} global $module_index;
$newmodule = [
if(php_sapi_name() == "cli") echo("Reading in core files..."); "name" => $settings["name"],
"version" => $settings["version"],
$core = file_get_contents("core.php"); "author" => $settings["author"],
$settings = file_get_contents("settings.fragment.php"); "description" => $settings["description"],
$settings = str_replace([ "<?php", "?>" ], "", $settings); "id" => $settings["id"],
$core = str_replace("{settings}", $settings, $core); "lastupdate" => filemtime("modules/" . $settings["id"] . ".php")
];
$result = $core; $module_index[] = $newmodule;
}
foreach($module_list as $module_id)
{ foreach($modules as $filename)
if($module_id == "") continue; {
echo("Processing $filename\n");
if(php_sapi_name() == "cli") echo("Adding $module_id\n"); require($filename);
}
$module_filepath = "modules/" . preg_replace("[^a-zA-Z0-9\-]", "", $module_id) . ".php";
echo("*** Processing complete ***\n");
//echo("id: $module_id | filepath: $module_filepath\n");
echo("Writing new module index to disk...");
if(!file_exists($module_filepath)) file_put_contents("module_index.json", json_encode($module_index, JSON_PRETTY_PRINT));
{ echo("done\n");
http_response_code(400);
exit("Failed to load module with name: $module_filepath");
} //////////////////////////////////////////////////////////////////////
////////////////////////// Build New Target //////////////////////////
$modulecode = file_get_contents($module_filepath); //////////////////////////////////////////////////////////////////////
$modulecode = str_replace([ "<?php", "?>" ], "", $modulecode); require("pack.php");
$result = str_replace(
"// %next_module% //", ?>
"$modulecode\n// %next_module% //",
$result);
}
if(php_sapi_name() == "cli")
{
if(file_exists("build/index.php"))
{
echo("index.php already exists in the build folder, exiting\n");
exit(1);
}
else
{
echo("Done. Saving to disk...");
file_put_contents("build/index.php", $result);
echo("complete!\n");
echo("*** Build Completed ***\n");
}
}
else
{
exit($result);
}
?>

View File

@ -1,5 +1,2 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo Deleting old index.php
rm build/index.php
php rebuild_module_index.php
php build.php php build.php

View File

@ -76,7 +76,7 @@
function download() function download()
{ {
var url = "build.php?web=true&modules=", var url = "pack.php?web=true&modules=",
checkboxes = document.querySelectorAll("input[type=checkbox]"); checkboxes = document.querySelectorAll("input[type=checkbox]");
for(var i = 0; i < checkboxes.length; i++) for(var i = 0; i < checkboxes.length; i++)
{ {

79
pack.php Normal file
View File

@ -0,0 +1,79 @@
<?php
if(php_sapi_name() == "cli")
{
echo("*** Beginning main build sequence ***\n");
echo("Reading in module index...\n");
}
$module_index = json_decode(file_get_contents("module_index.json"));
$module_list = [];
foreach($module_index as $module)
{
$module_list[] = $module->id;
}
if(isset($_GET["modules"]))
{
$module_list = explode(",", $_GET["modules"]);
}
if(php_sapi_name() != "cli")
{
header("content-type: text/php");
}
if(php_sapi_name() == "cli") echo("Reading in core files...");
$core = file_get_contents("core.php");
$settings = file_get_contents("settings.fragment.php");
$settings = str_replace([ "<?php", "?>" ], "", $settings);
$core = str_replace("{settings}", $settings, $core);
$result = $core;
foreach($module_list as $module_id)
{
if($module_id == "") continue;
if(php_sapi_name() == "cli") echo("Adding $module_id\n");
$module_filepath = "modules/" . preg_replace("[^a-zA-Z0-9\-]", "", $module_id) . ".php";
//echo("id: $module_id | filepath: $module_filepath\n");
if(!file_exists($module_filepath))
{
http_response_code(400);
exit("Failed to load module with name: $module_filepath");
}
$modulecode = file_get_contents($module_filepath);
$modulecode = str_replace([ "<?php", "?>" ], "", $modulecode);
$result = str_replace(
"// %next_module% //",
"$modulecode\n// %next_module% //",
$result);
}
if(php_sapi_name() == "cli")
{
if(file_exists("build/index.php"))
{
echo("index.php already exists in the build folder, exiting\n");
exit(1);
}
else
{
echo("Done. Saving to disk...");
file_put_contents("build/index.php", $result);
echo("complete!\n");
echo("*** Build completed! ***\n");
}
}
else
{
exit($result);
}
?>

View File

@ -1,32 +0,0 @@
<?php
echo("Rebuilding module index...\n");
$modules = glob("modules/*.php");
$module_index = [];
function register_module($settings)
{
global $module_index;
$newmodule = [
"name" => $settings["name"],
"version" => $settings["version"],
"author" => $settings["author"],
"description" => $settings["description"],
"id" => $settings["id"],
"lastupdate" => filemtime("modules/" . $settings["id"] . ".php")
];
$module_index[] = $newmodule;
}
foreach($modules as $filename)
{
echo("Processing $filename\n");
require($filename);
}
echo("*** Processing Complete ***\n");
echo("Writing new module index to disk...");
file_put_contents("module_index.json", json_encode($module_index, JSON_PRETTY_PRINT));
echo("done\n");
?>