Skip to content

Commit

Permalink
Add a --watch option which automatically builds the file again after …
Browse files Browse the repository at this point in the history
…a change

Uses filemtime polling now. Would be awesome to use inotify/tail -f or something similar eventually if possible
  • Loading branch information
Arian committed Aug 22, 2011
1 parent a1b0a05 commit c561d67
Showing 1 changed file with 72 additions and 33 deletions.
105 changes: 72 additions & 33 deletions packager-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
$method = 'output';
$output_file = null;
$graph_file = null;
$watch = false;

function help(){
echo "\npackager-cli.php [options] <modules>\n\n"
. "Options:\n"
. " -h --help Show this help\n"
. " --options Specify another options file (defaults to options.php)\n"
. " -o --options Specify another options file (defaults to options.php)\n"
. " --output The file the output should be written to\n"
. " --modules --list List the modules\n"
. " --dependencies List the dependencies map\n"
. " --graph Create a structural dependency graph\n"
. " and write it to this file\n"
. " --watch Watches the files \n"
. "\n";
exit;
}
Expand All @@ -39,7 +41,7 @@ function warn($message){
case '--options':
$options_file = $args[++$i];
break;
case '--output':
case '--output': case '-o':
$output_file = $args[++$i];
break;
case '--graph':
Expand All @@ -52,6 +54,9 @@ function warn($message){
case '--dependencies':
$method = 'dependencies';
break;
case '--watch':
$watch = true;
break;
default:
$requires[] = $arg;
break;
Expand All @@ -60,49 +65,83 @@ function warn($message){

if (empty($requires)) help();

$packager = new Packager;
function packager(){
global $options_file, $requires;

$options = $options_file ? (include $options_file) : array();
$packager = new Packager;

$packager->setBaseUrl(
isset($options['baseurl']) ? $options['baseurl'] : getcwd()
);
$options = $options_file ? (include $options_file) : array();

$packager->setBaseUrl(
isset($options['baseurl']) ? $options['baseurl'] : getcwd()
);

if (isset($options['paths'])) foreach ($options['paths'] as $alias => $path){
$packager->addAlias($alias, $path);
}

if (!empty($options['loader'])) array_unshift($requires, dirname(__FILE__) . '/loader.js');
if (isset($options['paths'])) foreach ($options['paths'] as $alias => $path){
$packager->addAlias($alias, $path);
}

$builder = $packager->req($requires);
if (!empty($options['loader'])) array_unshift($requires, dirname(__FILE__) . '/loader.js');

if ($method == 'output' || $method == 'modules'){
warn("\nLoaded Modules:\n " . implode("\n ", $builder->modules()) . "\n\n");
return $packager;
}

if ($method == 'output'){
$output = $builder->output();
if ($output_file) file_put_contents ($output_file, $output);
else echo $output;
} elseif ($method == 'dependencies'){
$modules = $builder->dependencies();

$str = '';
foreach ($modules as $id => $deps){
$str .= "\n " . $id;
foreach ($deps as $dep){
$str .= "\n - " . $dep;
function build($builder){
global $method, $output_file, $graph_file;

if ($method == 'output' || $method == 'modules'){
warn("\nLoaded Modules:\n " . implode("\n ", $builder->modules()) . "\n\n");
}

if ($method == 'output'){
$output = $builder->output();
if ($output_file) file_put_contents ($output_file, $output);
else echo $output;
} elseif ($method == 'dependencies'){
$modules = $builder->dependencies();

$str = '';
foreach ($modules as $id => $deps){
$str .= "\n " . $id;
foreach ($deps as $dep){
$str .= "\n - " . $dep;
}
}
$str .= "\n\n";

warn($str);
}

if ($graph_file){
include_once dirname(__FILE__) . '/lib/Graph.php';
$graph = new Packager_Graph($builder);
$graph->output($graph_file);
warn("The dependency graph has been written to '" . $graph_file . "'\n");
}
$str .= "\n\n";

warn($str);
}

if ($graph_file){
include_once dirname(__FILE__) . '/lib/Graph.php';
$graph = new Packager_Graph($builder);
$graph->output($graph_file);
warn("The dependency graph has been written to '" . $graph_file . "'\n");
$builder = packager()->req($requires);

if ($watch){
$files = $builder->files();
$times = array();
while (true){
foreach ($files as $file){
$time = filemtime($file);
if (empty($times[$file])){
$times[$file] = $time;
} elseif ($time != $times[$file]){
$times[$file] = $time;
warn("\nThe file '" . $file . "' has changed\n");
$builder = packager()->req($requires);
$files = $builder->files();
build($builder);
break;
}
}
sleep(1);
}
} else {
build($builder);
}

0 comments on commit c561d67

Please sign in to comment.