Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- $options = getopt("l");
- $config = [
- 'input_path' => 'source', 'output_path' => 'build',
- 'exclude_files' => [], 'exclude_extentions' => [],
- 'ignore_files' => [], 'ignore_extensions' => [],
- 'sign' => '{%}', 'vars' => []
- ];
- echo PHP_EOL;
- if(file_exists('config.json')){
- $custom_config = json_decode(file_get_contents('config.json'), true);
- if($error = json_last_error()) die('There is an error in your config: ' . $error);
- $config = array_replace_recursive($config, $custom_config);
- }
- if(!file_exists($config['input_path'])) die('Input path invalid');
- if(file_exists($config['output_path'])){
- echo 'Cleaning previous build files...' . PHP_EOL;
- if(is_dir($config['output_path'])){
- delTree($config['output_path']) || die('Could not clean target directory');
- }else{
- unlink($config['output_path']) || die('Please remove the file named as the target directory');
- }
- }
- mkdir($config['output_path']);
- $filemtime = [];
- $n = 0;
- echo 'Processing files...' . PHP_EOL;
- do{
- $files = processDir($config['input_path']);
- if(isset($options['l']) && !($n++)) echo 'Done. Checking for changes...' . PHP_EOL;
- }while(isset($options['l']));
- echo 'Done. File list:' . PHP_EOL;
- print_r($files);
- // Functions
- function delTree($dir){
- if(!$dir || !is_dir($dir)) return false;
- foreach(array_diff(scandir($dir), ['.', '..']) as $value){
- $path = $dir . DIRECTORY_SEPARATOR . $value;
- if(is_dir($path)) delTree($path);
- else unlink($path);
- }
- return rmdir($dir);
- }
- function processDir($dir, $ignore = false){
- global $config, $filemtime;
- clearstatcache();
- $result = [];
- foreach(array_diff(scandir($dir), ['.', '..']) as $value){
- $path = $dir . DIRECTORY_SEPARATOR . $value;
- $relpath = substr($path, strlen($config['input_path'])+1);
- $outpath = $config['output_path'] . DIRECTORY_SEPARATOR . $relpath;
- $ignorepath = $ignore || in_array($relpath, $config['ignore_files']);
- if(in_array($relpath, $config['exclude_files'])) continue;
- if(is_dir($path)){
- if(!file_exists($outpath)) mkdir($outpath);
- $result[$value] = processDir($path, $ignorepath);
- }else{
- if(isset($filemtime[$relpath]) && filemtime($path) == $filemtime[$relpath]) continue;
- if(isset($filemtime[$relpath])) echo date("Y-m-d H:i:s") . ' | Change detected: ' . $relpath . PHP_EOL;
- $filemtime[$relpath] = filemtime($path);
- $ext = pathinfo($path, PATHINFO_EXTENSION);
- if(in_array($ext, $config['exclude_extentions'])) continue;
- if($ignorepath || in_array($ext, $config['ignore_extensions'])){
- copy($path, $outpath);
- }else{
- file_put_contents($outpath, processFile($path, $config['sign'], $config['vars'])['content']);
- }
- $result[] = $value;
- }
- }
- return $result;
- }
- function processFile($path, $sign, $vars = []){
- if(!file_exists($path)) throw new Exception('Included file not found: ' . $path);
- $finalcontent = '';
- $dir = dirname($path) . DIRECTORY_SEPARATOR;
- $content = file_get_contents($path);
- $parts = explode($sign, $content);
- for($i=0; $i<count($parts); $i++){
- if(($i+1)%2){
- $finalcontent .= $parts[$i];
- }else{
- $cmd = json_decode($parts[$i], true);
- if(json_last_error()) throw new Exception('Syntax error: ' . $path);
- if(isset($cmd['vars']) && is_array($cmd['vars'])) $vars = array_merge($vars, $cmd['vars']);
- if(isset($cmd['var']) && array_key_exists($cmd['var'], $vars)) $finalcontent .= $vars[$cmd['var']];
- if(isset($cmd['include'])){
- $result = processFile($dir . $cmd['include'], $sign, $vars);
- $finalcontent .= $result['content'];
- $vars = array_merge($vars, $result['vars']);
- }
- }
- }
- return ['content' => $finalcontent, 'vars' => $vars];
- }
Advertisement
Add Comment
Please, Sign In to add comment