bitsy-bundler/bundler.php

69 lines
2.2 KiB
PHP

<?php
error_reporting(E_ALL);
echo "Starting hack bundler...\n";
echo "Getting hacks file...\n";
$hacks_file = file_get_contents('hacks.json');
$time_start = microtime(true);
if (!$hacks_file) {
echo "hacks.json file not found. creating file.";
file_put_contents('hacks.json', '{"sources": ["seleb/bitsy-hacks/dist"],"local": "hacks","hacks": []}');
$hacks_file = file_get_contents('hacks.json');
}
$hacks = json_decode($hacks_file);
if (!$hacks) {
echo "hacks.json file malformed or improperly formatted. you may need to delete it and re-create it.";
die(1);
}
$hacks_accumulator = "";
foreach ($hacks->hacks as $hack) {
echo "Getting hack {$hack}...\n";
preg_match('/https:\/\//', $hack, $output_match);
if (count($output_match) > 0) {
// it's a url, so just curl it
$handle = curl_init($hack);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($handle);
if (!$ret) {
echo `WARNING: Error getting {$hack} from URL. Hack will be skipped.\n`;
} else {
$hacks_accumulator .= $ret."\n\n";
}
} else {
// first check sources
$found = false;
foreach ($hacks->sources as $source) {
$handle = curl_init('https://cdn.jsdelivr.net/gh/'.$source.'/'.$hack.'.js');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($handle);
if ($ret) {
$hacks_accumulator .= $ret."\n\n";
$found = true;
break;
}
}
if (!$found) {
echo `{$hack} not found in remote libraries; checking local.\n`;
// check in local hacks folder
$hack_content = file_get_contents($hacks->local.'/'.$hack.'.js');
if (!$hack_content) {
echo `WARNING: Hack {$hack} skipped; not found in remote sources or local.\n`;
} else {
$hacks_accumulator .= $hack_content."\n\n";
}
}
}
}
echo "Writing to file...\n";
file_put_contents('hacks.js', $hacks_accumulator);
$time_end = microtime(true);
$totaltime = number_format($time_end - $time_start, 2, ".", ",");
$hackcount = count($hacks->hacks);
echo "{$hackcount} hacks processed in {$totaltime}ms.\n";
echo "Add the following to your Bitsy HTML file, just above the body tag, if you haven't already:\n";
echo '<script src="hacks.js" type="text/javascript"></script>';