1
0
mirror of https://github.com/pattern-lab/patternlab-php.git synced 2025-01-18 06:38:20 +01:00

generate() now cleans public dir first. Fixes #15

This commit is contained in:
Dave Olsen 2013-10-26 19:55:09 -04:00
parent 5411e3f917
commit 21dd4dc936
3 changed files with 81 additions and 4 deletions

View File

@ -10,6 +10,7 @@
*
* php builder.php -g
* Iterates over the 'source' directories & files and generates the entire site a single time.
* It also cleans the 'public' directory.
*
* php builder.php -w
* Generates the site like the -g flag and then watches for changes in the 'source' directories &
@ -40,7 +41,7 @@ if (php_sapi_name() == 'cli') {
$g->generate();
print "your site has been generated...\n";
} elseif (isset($args["w"])) {
} else if (isset($args["w"])) {
// initiate the w (watch) switch
@ -60,7 +61,8 @@ if (php_sapi_name() == 'cli') {
print "\n";
print "Usage:\n\n";
print " php ".$_SERVER["PHP_SELF"]." -g\n";
print " Iterates over the 'source' directories & files and generates the entire site a single time.\n\n";
print " Iterates over the 'source' directories & files and generates the entire site a single time.\n";
print " It also cleans the 'public' directory.\n\n";
print " php ".$_SERVER["PHP_SELF"]." -w\n";
print " Generates the site like the -g flag and then watches for changes in the 'source' directories &\n";
print " files. Will re-generate files if they've changed.\n\n";

View File

@ -727,6 +727,78 @@ class Buildr {
}
/**
* Delete patterns and user-created directories and files in public/
*/
protected function cleanPublic() {
// find all of the patterns in public/. sort by the children first
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__."/../../public/patterns/"), RecursiveIteratorIterator::CHILD_FIRST);
// make sure dots are skipped
$objects->setFlags(FilesystemIterator::SKIP_DOTS);
// for each file figure out what to do with it
foreach($objects as $name => $object) {
if ($object->isDir()) {
// if this is a directory remove it
rmdir($name);
} else if ($object->isFile() && ($object->getFilename() != "README")) {
// if this is a file remove it
unlink($name);
}
}
// scan source/ & public/ to figure out what directories might need to be cleaned up
$sourceDirs = glob(__DIR__."/../../source/*",GLOB_ONLYDIR);
$publicDirs = glob(__DIR__."/../../public/*",GLOB_ONLYDIR);
// make sure some directories aren't deleted
$ignoreDirs = array("listeners","styleguide");
foreach ($ignoreDirs as $ignoreDir) {
$key = array_search(__DIR__."/../../public/".$ignoreDir,$publicDirs);
if ($key !== false){
unset($publicDirs[$key]);
}
}
// compare source dirs against public. remove those dirs w/ an underscore in source/ from the public/ list
foreach ($sourceDirs as $sourceDir) {
$cleanDir = str_replace(__DIR__."/../../source/","",$sourceDir);
if ($cleanDir[0] == "_") {
$key = array_search(__DIR__."/../../public/".str_replace("_","",$cleanDir),$publicDirs);
if ($key !== false){
unset($publicDirs[$key]);
}
}
}
// for the remaining dirs in public delete them and their files
foreach ($publicDirs as $dir) {
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
// make sure dots are skipped
$objects->setFlags(FilesystemIterator::SKIP_DOTS);
foreach($objects as $name => $object) {
if ($object->isDir()) {
rmdir($name);
} else if ($object->isFile()) {
unlink($name);
}
}
rmdir($dir);
}
}
/**
* Copies a file from the given source path to the given public path.
* THIS IS NOT FOR PATTERNS

View File

@ -28,6 +28,9 @@ class Generatr extends Buildr {
*/
public function generate() {
// clean the public directory to remove old files
$this->cleanPublic();
// gather data
$this->gatherData();
@ -52,7 +55,7 @@ class Generatr extends Buildr {
}
// iterate over all of the other files in the source directory and move them if their modified time has changed
// iterate over all of the other files in the source directory
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__."/../../source/"), RecursiveIteratorIterator::SELF_FIRST);
// make sure dots are skipped
@ -73,7 +76,7 @@ class Generatr extends Buildr {
}
// check to see if it's a new file or a file that has changed
if (!$ignoreDir && $object->isFile() && (!file_exists(__DIR__."/../../public/".$fileName) || ($object->getMTime() > filemtime(__DIR__."/../../public/".$fileName)))) {
if (!$ignoreDir && $object->isFile() && (!file_exists(__DIR__."/../../public/".$fileName))) {
$this->moveStaticFile($fileName);
}