First version of the build system

This commit is contained in:
Pierre 2018-04-29 20:58:01 +02:00
parent 67acbbab4e
commit 4f9946063d
5 changed files with 298 additions and 10 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
# ---> VisualStudioCode # ---> VisualStudioCode
.settings .settings
# Build directory
output/*

195
build Executable file
View File

@ -0,0 +1,195 @@
#!/usr/bin/env php
###########################
# ComunicWeb build script #
# #
# @author Pierre HUBERT #
###########################
<?php
//Defines some utilities
function notice(string $message, bool $new_section = false) {
echo ($new_section ? "\n\n" : "").$message,"\n";
}
/**
* Append a string at the begining of each entry of an array
*
* @param string $input The string to append to each array entry
* @param array $array The array to process
* @return array Updated array
*/
function array_put_begining(string $input, array $array){
foreach($array as $num => $val)
$array[$num] = $input.$val;
return $array;
}
/**
* Copy an array of file into a specific target file
*
* @param array $files The list of file to copy
* @param string $target The target file to create
* @param bool TRUE for a success / FALSE else
*/
function files_to_file(array $files, string $target) : bool {
$source = "";
foreach($files as $file){
$source .= file_get_contents($file)."\n";
}
return file_put_contents($target, $source) != FALSE;
}
/**
* Delete the entire content of directory
*
* @param string $path The path of the directory to delete
*/
function delDir(string $path){
if(is_dir($path) == TRUE){
$rootFolder = scandir($path);
if(sizeof($rootFolder) > 2){
foreach($rootFolder as $folder){
if($folder != "." && $folder != ".."){
//Pass the subfolder to function
delDir($path."/".$folder);
}
}
//On the end of foreach the directory will be cleaned, and you will can use rmdir, to remove it
rmdir($path);
}
}
else {
if(file_exists($path) == TRUE){
//Suppression du fichier
unlink($path);
}
}
}
// copies files and non-empty directories
function rcopy(string $src, string $dst) {
if (is_dir($src)) {
mkdir($dst, 0777, true);
$files = scandir($src);
foreach ($files as $file)
if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file");
}
else if (file_exists($src)) copy($src, $dst);
}
//Initialize page
require_once __DIR__."/system/system.php";
//Defines some variables
$output = __DIR__."/output/";
$debug_conf = "dev";
$release_conf = "build";
//Load configurations
notice("Load configurations.", TRUE);
notice("Debug config: ".$debug_conf);
notice("Release config: ".$release_conf);
load_config($debug_conf);
$debug = new $debug_conf;
$path_debug_assets = __DIR__."/".$debug::PATH_ASSETS;
load_config($release_conf);
$release = new $release_conf;
$path_release_assets = $output.$release::PATH_ASSETS;
//Clean directory
notice("Clean build directory", TRUE);
if(file_exists($output))
delDir($output);
mkdir($output, 0777, true);
mkdir($path_release_assets, 0777, true);
//Create unminified version
notice("Create unminified files versions", TRUE);
//3rd party CSS
notice("Third Party CSS");
$thirdPartyDebugFiles = array_put_begining($path_debug_assets, $debug::THIRD_PARTY_CSS);
$targetThirdPartyCSS = $path_release_assets.$release::THIRD_PARTY_CSS;
files_to_file($thirdPartyDebugFiles, $targetThirdPartyCSS);
//3rd party JS
notice("Third Party JS");
$thirdPartyDebugFiles = array_put_begining($path_debug_assets, $debug::THIRD_PARTY_JS);
$targetThirdPartyJS = $path_release_assets.$release::THIRD_PARTY_JS;
files_to_file($thirdPartyDebugFiles, $targetThirdPartyJS);
//App CSS
notice("App CSS");
$appDebugFiles = array_put_begining($path_debug_assets, $debug::APP_CSS);
$targetAppCSS = $path_release_assets.$release::APP_CSS;
files_to_file($appDebugFiles, $targetAppCSS);
//App JS
notice("App JS");
$appDebugFiles = array_put_begining($path_debug_assets, $debug::APP_JS);
$targetAppJS = $path_release_assets.$release::APP_JS;
files_to_file($appDebugFiles, $targetAppJS);
//Make some adpations on third party files
$source = file_get_contents($targetThirdPartyCSS);
$source = str_replace("../fonts/fontawesome", "fontawesome_fonts/fontawesome", $source);
file_put_contents($targetThirdPartyCSS, $source);
//Copy font awesome files and twemojies files + Google Fonts
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/font-awesome/fonts", $path_release_assets."fontawesome_fonts");
rcopy($path_debug_assets."3rdparty/twemoji/2/72x72/", $path_release_assets."3rdparty/twemoji/2/72x72/");
rcopy($path_debug_assets."3rdparty/adminLTE/plugins/googleFonts/googleFonts/", $path_release_assets."googleFonts/");
rcopy($path_debug_assets."3rdparty/wdt-emoji/sheets/", $path_release_assets."3rdparty/wdt-emoji/sheets/");
//Copy images and templates
rcopy($path_debug_assets."img/", $path_release_assets."img/");
rcopy($path_debug_assets."templates/", $path_release_assets."templates/");
//Create main HTML file
notice("Generate PHP root file");
$page_src = '<?php
//We check if it is a redirection to handle 404 errors
if(isset($_SERVER["REDIRECT_URL"])){
//We check if it is an asset request
if(preg_match("<assets>", $_SERVER["REDIRECT_URL"])){
//This is a 404 not found error...
echo "<p>Error! 404 not found</p>";
http_response_code(404);
exit();
}
} ?>';
$page_src .= load_page("build");
file_put_contents($output."index.php", $page_src);
// Add .htaccess file
$htaccess = '<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
';
file_put_contents($output.".htaccess", $htaccess);

View File

@ -0,0 +1,69 @@
<?php
/**
* PHP build config for the website
*
* @author Pierre HUBERT
*/
class Build {
/**
* Site URL
*/
const SITE_URL = "http://devweb.local/comunic/v2/output/";
/**
* Site production mode
*/
const PROD_MODE = TRUE;
/**
* Path to assets (relative to the build folder)
*/
const PATH_ASSETS = "assets/";
/**
* Path to assets (URL)
*/
const ASSETS_URL = "http://devweb.local/comunic/v2/output/assets/";
/**
* Third party CSS files
*/
const THIRD_PARTY_CSS = "third_party_css.css";
/**
* Third party Javascript files
*/
const THIRD_PARTY_JS = "third_party.js";
/**
* Third party Javascript files (bundle)
*/
const THIRD_PARTY_BUNDLE_JS = "third_party.bundle.js";
/**
* Application CSS files
*/
const APP_CSS = "app.css";
/**
* Application JS files
*/
const APP_JS = "app.js";
/**
* Application JS files (bundles)
*/
const APP_BUNDLE_JS = "app.bundle.js";
/**
* Language settings
*/
const DEFAULT_LANGUAGE = "en";
/**
* Templates settings
*/
const TEMPLATES_PATH = "templates/";
}

View File

@ -18,7 +18,12 @@ class Dev {
const PROD_MODE = false; const PROD_MODE = false;
/** /**
* Path to assets * Path to assets (relative to the base project)
*/
const PATH_ASSETS = "assets/";
/**
* URL to assets
*/ */
const ASSETS_URL = "http://devweb.local/comunic/v2/assets/"; const ASSETS_URL = "http://devweb.local/comunic/v2/assets/";
@ -30,6 +35,7 @@ class Dev {
"3rdparty/adminLTE/bootstrap/css/bootstrap.min.css", "3rdparty/adminLTE/bootstrap/css/bootstrap.min.css",
"3rdparty/adminLTE/plugins/font-awesome/css/font-awesome.min.css", "3rdparty/adminLTE/plugins/font-awesome/css/font-awesome.min.css",
"3rdparty/adminLTE/plugins/ionicons/css/ionicons.min.css", "3rdparty/adminLTE/plugins/ionicons/css/ionicons.min.css",
"3rdparty/adminLTE/plugins/googleFonts/css.css",
//iCheck //iCheck
"3rdparty/adminLTE/plugins/iCheck/square/blue.css", "3rdparty/adminLTE/plugins/iCheck/square/blue.css",

View File

@ -16,10 +16,10 @@ require_once __DIR__."/config/global.config.php";
* @param string $config The configuration to use to load a page * @param string $config The configuration to use to load a page
* @return string Generated source page * @return string Generated source page
*/ */
function load_page(string $config) : string{ function load_page(string $config) : string {
//Load configuration //Load configuration
require __DIR__."/config/".$config.".config.php"; load_config($config);
$conf = new $config(); $conf = new $config();
//Load page template //Load page template
@ -32,14 +32,31 @@ function load_page(string $config) : string{
$source = str_replace("{js_config}", get_javascript_config($conf), $source); $source = str_replace("{js_config}", get_javascript_config($conf), $source);
//Update assets inclusion //Update assets inclusion
if(is_array($conf::THIRD_PARTY_CSS)){
$source = str_replace("{THIRD_PARTY_CSS}", src_inc_list_css($conf::ASSETS_URL, $conf::THIRD_PARTY_CSS), $source); $source = str_replace("{THIRD_PARTY_CSS}", src_inc_list_css($conf::ASSETS_URL, $conf::THIRD_PARTY_CSS), $source);
$source = str_replace("{APP_CSS}", src_inc_list_css($conf::ASSETS_URL, $conf::APP_CSS), $source); $source = str_replace("{APP_CSS}", src_inc_list_css($conf::ASSETS_URL, $conf::APP_CSS), $source);
$source = str_replace("{THIRD_PARTY_JS}", src_inc_list_js($conf::ASSETS_URL, $conf::THIRD_PARTY_JS), $source); $source = str_replace("{THIRD_PARTY_JS}", src_inc_list_js($conf::ASSETS_URL, $conf::THIRD_PARTY_JS), $source);
$source = str_replace("{APP_JS}", src_inc_list_js($conf::ASSETS_URL, $conf::APP_JS), $source); $source = str_replace("{APP_JS}", src_inc_list_js($conf::ASSETS_URL, $conf::APP_JS), $source);
}
else {
$source = str_replace("{THIRD_PARTY_CSS}", src_inc_css($conf::ASSETS_URL.$conf::THIRD_PARTY_CSS), $source);
$source = str_replace("{APP_CSS}", src_inc_css($conf::ASSETS_URL.$conf::APP_CSS), $source);
$source = str_replace("{THIRD_PARTY_JS}", src_inc_js($conf::ASSETS_URL.$conf::THIRD_PARTY_JS), $source);
$source = str_replace("{APP_JS}", src_inc_js($conf::ASSETS_URL.$conf::APP_JS), $source);
}
return $source; return $source;
} }
/**
* Load a configuration
*
* @param string $name The name of the configuration to load
*/
function load_config(string $config){
require_once __DIR__."/config/".$config.".config.php";
}
/** /**
* Get javascript configuration * Get javascript configuration
* *
@ -74,8 +91,8 @@ function get_javascript_config($config) : string {
//Default language //Default language
defaultLanguage: '".$config::DEFAULT_LANGUAGE."', defaultLanguage: '".$config::DEFAULT_LANGUAGE."',
//LanguagesPath "/*LanguagesPath
languagesPath: '".$config::ASSETS_URL.$config::LANGUAGE_PATH."', "languagesPath: '".$config::ASSETS_URL.$config::LANGUAGE_PATH."', */."
}; };