Browse Source

added cleanup command

master
Tsia 3 years ago
parent
commit
df7f348544
  1. 1
      README.md
  2. 43
      index.php

1
README.md

@ -44,6 +44,7 @@ Usage:
--rollback switch to previous version
--switch-to VER switch to version VER
--list-versions list available versions
--cleanup N delete old versions but keep the latest N
```
`./index.php --run` will generate a folder structure, render all templates and place the HTML into the appropriate folder.
by default the latest generated files will be accessible via the symlink `versions/current`. Your webserver should use that symlink as webroot.

43
index.php

@ -21,6 +21,7 @@ if(in_array('--help', $argv) || in_array('-h', $argv)){
echo ' --rollback switch to previous version' . PHP_EOL;
echo ' --switch-to VER switch to version VER' . PHP_EOL;
echo ' --list-versions list available versions' . PHP_EOL;
echo ' --cleanup N delete old versions but keep the latest N' . PHP_EOL;
die();
}
@ -139,6 +140,36 @@ if(in_array('--list-versions', $argv)){
die('done.');
}
if(in_array('--cleanup', $argv)){
$count = array_pop($argv);
if(!is_numeric($count)){
throw new Exception('Invalid Argument');
}
$versions = glob(OUTPUT_DIR . '/*');
$totalcount = count($versions);
if($totalcount <= $count){
die('nothing to clean up.');
}
for ($i=0; $i < ($totalcount - $count); $i++) {
if(realpath('current') == $versions[$i]) {
echo 'WARNING! not deleting current version!' . PHP_EOL;
continue;
}
if(realpath('previous') == $versions[$i]) {
echo 'WARNING! deleting previous version!' . PHP_EOL;
unlink('previous');
}
if(realpath('next') == $versions[$i]) {
echo 'WARNING! deleting next version!' . PHP_EOL;
unlink('next');
}
echo 'removing version ' . basename($versions[$i]) . PHP_EOL;
unlinkr($versions[$i]);
}
die('done.');
}
if($argc > 2 || ($argc > 1 && $argv[1] !== '--no-switch' && $argv[1] !== '--run')){
throw new Exception('Invalid Argument');
}
@ -390,3 +421,15 @@ function copyr($source, $destination) {
}
}
}
function unlinkr($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
if(is_dir($dir . '/' . $file)){
unlinkr($dir . '/' . $file);
}else{
unlink($dir . '/' . $file);
}
}
return rmdir($dir);
}
Loading…
Cancel
Save