Permalink
Fetching contributors…
Cannot retrieve contributors at this time
93 lines (69 sloc) 2.54 KB
#!/opt/local/bin/php
<?php
header('Content-Type: text/plain');
// Make the final vanilla2export.php file from the other sources.
$path = dirname(__FILE__) . '/vanilla2export.php';
if (file_exists($path)) {
$r = unlink($path);
if (!$r) {
echo "Could not delete $path.\n";
die();
}
}
// Open the file.
echo "Opening $path\n";
$fp = fopen($path, 'w');
fwrite($fp, "<?php /* This file was automatically generated by make.php. DO NOT EDIT. */ ?>\n\n");
addFile($fp, 'index.php');
fclose($fp);
echo "Make Complete.\n";
/// Functions ///
function addFile($fp, $filename) {
// Recursively build file
$contents = getFile($filename);
// Include individual software porters (undo MAKESKIP)
$paths = glob('packages/*.php');
$exporters = '';
foreach ($paths as $path) {
$exporters .= getFile($path);
}
$contents = str_replace('// [EXPORTERS]', ' ?>' . $exporters . '<?php ', $contents);
// Write the all-in-one file
fwrite($fp, $contents);
}
function getFile($filename, $endPhp = false) {
$path = dirname(__FILE__) . '/' . $filename;
echo "Including file $path\n";
$contents = file_get_contents($path);
// MAKESKIP
$contents = preg_replace('#MAKESKIPSTART(.*)MAKESKIPEND#s', '[EXPORTERS]', $contents);
// Inline any stylesheet includes.
$contents = preg_replace_callback('#<link.+?href=[\'"](.*?)[\'"].*?/>#i', 'replaceStyleCallback', $contents);
// Inline any script includes.
$contents = preg_replace_callback('#<script.+?src=[\'"](.*?)[\'"].*?></script>#i', 'replaceScriptCallback', $contents);
// Inline any includes.
$contents = preg_replace_callback('/include_once [\'"](.*?)[\'"]\;/', 'replaceIncludeCallback', $contents);
// End and begin the php context.
if ($endPhp) {
$contents = "\n/* Contents included from $filename */\n?>" . $contents . "<?php\n";
}
return $contents;
}
function replaceIncludeCallback($matches) {
$path = $matches[1];
$contents = getFile($path, true);
$result = $contents;
return $result;
}
function replaceStyleCallback($matches) {
$path = $matches[1];
$contents = file_get_contents(dirname(__FILE__) . '/' . $path);
$result = "<!-- Contents included from $path -->\n<style>\n" . $contents . "\n</style>";
return $result;
}
function replaceScriptCallback($matches) {
$path = $matches[1];
$contents = file_get_contents(dirname(__FILE__) . '/' . $path);
$result = "<!-- Contents included from $path -->\n<script>\n" . $contents . "\n</script>";
return $result;
}