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

Commit 136744c

Browse files
committed
Merge branch 'feature-cleanpublic' into dev
2 parents 5411e3f + 21dd4dc commit 136744c

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

builder/builder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*
1111
* php builder.php -g
1212
* Iterates over the 'source' directories & files and generates the entire site a single time.
13+
* It also cleans the 'public' directory.
1314
*
1415
* php builder.php -w
1516
* Generates the site like the -g flag and then watches for changes in the 'source' directories &
@@ -40,7 +41,7 @@
4041
$g->generate();
4142
print "your site has been generated...\n";
4243

43-
} elseif (isset($args["w"])) {
44+
} else if (isset($args["w"])) {
4445

4546
// initiate the w (watch) switch
4647

@@ -60,7 +61,8 @@
6061
print "\n";
6162
print "Usage:\n\n";
6263
print " php ".$_SERVER["PHP_SELF"]." -g\n";
63-
print " Iterates over the 'source' directories & files and generates the entire site a single time.\n\n";
64+
print " Iterates over the 'source' directories & files and generates the entire site a single time.\n";
65+
print " It also cleans the 'public' directory.\n\n";
6466
print " php ".$_SERVER["PHP_SELF"]." -w\n";
6567
print " Generates the site like the -g flag and then watches for changes in the 'source' directories &\n";
6668
print " files. Will re-generate files if they've changed.\n\n";

builder/lib/builder.lib.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,78 @@ protected function updateChangeTime() {
727727

728728
}
729729

730+
/**
731+
* Delete patterns and user-created directories and files in public/
732+
*/
733+
protected function cleanPublic() {
734+
735+
// find all of the patterns in public/. sort by the children first
736+
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__."/../../public/patterns/"), RecursiveIteratorIterator::CHILD_FIRST);
737+
738+
// make sure dots are skipped
739+
$objects->setFlags(FilesystemIterator::SKIP_DOTS);
740+
741+
// for each file figure out what to do with it
742+
foreach($objects as $name => $object) {
743+
744+
if ($object->isDir()) {
745+
// if this is a directory remove it
746+
rmdir($name);
747+
} else if ($object->isFile() && ($object->getFilename() != "README")) {
748+
// if this is a file remove it
749+
unlink($name);
750+
}
751+
752+
}
753+
754+
// scan source/ & public/ to figure out what directories might need to be cleaned up
755+
$sourceDirs = glob(__DIR__."/../../source/*",GLOB_ONLYDIR);
756+
$publicDirs = glob(__DIR__."/../../public/*",GLOB_ONLYDIR);
757+
758+
// make sure some directories aren't deleted
759+
$ignoreDirs = array("listeners","styleguide");
760+
foreach ($ignoreDirs as $ignoreDir) {
761+
$key = array_search(__DIR__."/../../public/".$ignoreDir,$publicDirs);
762+
if ($key !== false){
763+
unset($publicDirs[$key]);
764+
}
765+
}
766+
767+
// compare source dirs against public. remove those dirs w/ an underscore in source/ from the public/ list
768+
foreach ($sourceDirs as $sourceDir) {
769+
$cleanDir = str_replace(__DIR__."/../../source/","",$sourceDir);
770+
if ($cleanDir[0] == "_") {
771+
$key = array_search(__DIR__."/../../public/".str_replace("_","",$cleanDir),$publicDirs);
772+
if ($key !== false){
773+
unset($publicDirs[$key]);
774+
}
775+
}
776+
}
777+
778+
// for the remaining dirs in public delete them and their files
779+
foreach ($publicDirs as $dir) {
780+
781+
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
782+
783+
// make sure dots are skipped
784+
$objects->setFlags(FilesystemIterator::SKIP_DOTS);
785+
786+
foreach($objects as $name => $object) {
787+
788+
if ($object->isDir()) {
789+
rmdir($name);
790+
} else if ($object->isFile()) {
791+
unlink($name);
792+
}
793+
794+
}
795+
796+
rmdir($dir);
797+
798+
}
799+
800+
}
801+
730802
/**
731803
* Copies a file from the given source path to the given public path.
732804
* THIS IS NOT FOR PATTERNS

builder/lib/generator.lib.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function __construct() {
2828
*/
2929
public function generate() {
3030

31+
// clean the public directory to remove old files
32+
$this->cleanPublic();
33+
3134
// gather data
3235
$this->gatherData();
3336

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

5356
}
5457

55-
// iterate over all of the other files in the source directory and move them if their modified time has changed
58+
// iterate over all of the other files in the source directory
5659
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__."/../../source/"), RecursiveIteratorIterator::SELF_FIRST);
5760

5861
// make sure dots are skipped
@@ -73,7 +76,7 @@ public function generate() {
7376
}
7477

7578
// check to see if it's a new file or a file that has changed
76-
if (!$ignoreDir && $object->isFile() && (!file_exists(__DIR__."/../../public/".$fileName) || ($object->getMTime() > filemtime(__DIR__."/../../public/".$fileName)))) {
79+
if (!$ignoreDir && $object->isFile() && (!file_exists(__DIR__."/../../public/".$fileName))) {
7780
$this->moveStaticFile($fileName);
7881
}
7982

0 commit comments

Comments
 (0)