*************** *** 78,87 **** */ protected function generateIndex() { - // bomb if missing index.html if (!file_exists(Config::getOption("publicDir")."/index.html")) { $index = Console::getHumanReadablePath(Config::getOption("publicDir")).DIRECTORY_SEPARATOR."index.html"; - Console::writeError("".$index." is missing. grab a copy from your StyleguideKit..."); } // set-up the dispatcher --- 78,128 ---- */ protected function generateIndex() { + /** + * Handle missing index.html. Solves https://github.com/drupal-pattern-lab/patternlab-php-core/issues/14 + * Could also be used to re-add missing styleguidekit assets with a few edits? + * + * 1. @TODO: Figure out a better way to future-proof path resolution for styleguidekit `dist` folder + * 2. Recusirively copy files from styleguidekit to publicDir via https://stackoverflow.com/a/7775949 + * 3. Make sure we only try to create new directories if they don't already exist + * 4. Only copy files if they are missing (vs changed, etc) + */ if (!file_exists(Config::getOption("publicDir")."/index.html")) { $index = Console::getHumanReadablePath(Config::getOption("publicDir")).DIRECTORY_SEPARATOR."index.html"; + Console::writeWarning($index . " is missing. No biggie. Grabbing a fresh copy from your StyleguideKit..."); + + $baseDir = Config::getOption("baseDir") . '/vendor'; + $finder = new Finder(); + + // Locate the current theme's styleguidekit assets via the patternlab-styleguidekit `type` in composer.json + $finder->files()->name("composer.json")->in($baseDir)->contains('patternlab-styleguidekit')->sortByName(); + + foreach ($finder as $file) { + $src = dirname($file->getRealPath()) . DIRECTORY_SEPARATOR . 'dist'; /* [1] */ + $dest= Config::getOption("publicDir"); + + if (is_dir($src)){ + + if(!is_dir($dest)) { + mkdir($dest, 0755); + } + + foreach ( /* [2] */ + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item + ) { + if ($item->isDir()) { + if(!is_dir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) { /* [3] */ + mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + } + } else { + if(!file_exists($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) { /* [4] */ + copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); + } + } + } + } + } } // set-up the dispatcher