<?php
if(count($argv) == 1){
echo "\nERROR - You must specify a configuration file.\n";
die();
}
$configFile = $argv[1];
$config = file_get_contents($configFile);
$json = json_decode($config, true);
$inputPackage = $json["files"];
$resultCode = "";
$toLoad = array();
function packageToFile($package){
$filename = str_replace("rhy.", "src.", trim($package));
$filename = str_replace(".","\\", $filename);
$filename = dirname(__FILE__)."\\".$filename.".rhy.js";
return $filename;
}
function getDependencies($package, $current){
$content = file_get_contents(packageToFile($package));
$matches = array();
if(preg_match("#.* @dependencies (.*)#", $content, $matches) == 0)
return;
$dependencies = $matches[1];
$dependencies = explode(",", $dependencies);
foreach($dependencies as $key => $dependency){
$dependencies[$key] = trim($dependency);
}
$result = $dependencies;
foreach($dependencies as $dependency){
if(!in_array($dependency, $current)){
$current = array_unique(array_merge($current, array($dependency)));
$new = getDependencies($dependency, $current);
if($new != null)
$result = array_merge($result, $new);
}
}
return array_unique($result);
}
foreach($inputPackage as $package){
$deps = getDependencies($package, $toLoad);
if($deps != null)
$toLoad = array_merge($toLoad, $deps);
$toLoad = array_unique($toLoad);
}
$toLoad = array_merge($toLoad, $inputPackage);
$toLoad = array_unique($toLoad);
echo "\nCOMPLETE - Generated following content: \n";
print_r($toLoad);
$rawBegin = file_get_contents(dirname(__FILE__)."\\src\\rhy.js");
$rawCore = "";
$rawExtra = "";
$rawEnding = "";
foreach($toLoad as $file){
$code = file_get_contents(packageToFile($file));
if(preg_match("#.* @ending.*#", $code) > 0){
$rawEnding .= $code;
}
else if(strpos($file, ".core.") !== FALSE){
$rawCore .= $code;
}
else{
$rawExtra .= $code;
}
}
$rawCode = $rawBegin.$rawCore.$rawExtra.$rawEnding;
file_put_contents(dirname(__FILE__)."\\build\\rhy.".$json['name'].".js", $rawCode);
exec("java -jar yuicompressor.jar -o build\\rhy.".$json['name'].".min.js build\\rhy.".$json['name'].".js");
?>