Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
generate() now cleans public dir first. Fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolsen committed Oct 26, 2013
1 parent 5411e3f commit 21dd4dc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
6 changes: 4 additions & 2 deletions builder/builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down Expand Up @@ -40,7 +41,7 @@
$g->generate();
print "your site has been generated...\n";

} elseif (isset($args["w"])) {
} else if (isset($args["w"])) {

// initiate the w (watch) switch

Expand All @@ -60,7 +61,8 @@
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";
Expand Down
72 changes: 72 additions & 0 deletions builder/lib/builder.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,78 @@ protected function updateChangeTime() {

}

/**
* 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
Expand Down
7 changes: 5 additions & 2 deletions builder/lib/generator.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function __construct() {
*/
public function generate() {

// clean the public directory to remove old files
$this->cleanPublic();

// gather data
$this->gatherData();

Expand All @@ -52,7 +55,7 @@ public function generate() {

}

// 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
Expand All @@ -73,7 +76,7 @@ public function generate() {
}

// 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);
}

Expand Down

0 comments on commit 21dd4dc

Please sign in to comment.