From 488ef24d9ef214c2bed26389511af13389278a2a Mon Sep 17 00:00:00 2001 From: Salem Ghoweri Date: Sun, 1 Oct 2017 17:20:51 -0400 Subject: [PATCH 1/2] Working fix to get Pattern Lineages working again when using custom Twig namespaces. --- PATCHES.txt | 23 +++++++ src/PatternLab/Builder.php.rej | 64 +++++++++++++++++++ .../PatternData/Helpers/LineageHelper.php | 50 ++++++++++----- 3 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 PATCHES.txt create mode 100644 src/PatternLab/Builder.php.rej diff --git a/PATCHES.txt b/PATCHES.txt new file mode 100644 index 00000000..2aa7d63f --- /dev/null +++ b/PATCHES.txt @@ -0,0 +1,23 @@ +This file was automatically generated by Composer Patches (https://github.com/cweagans/composer-patches) +Patches applied to this directory: + +[FIX] Fix PL Lineages when using Twig namespaces plugin. +Source: .patches/fixPatternLabLineagesWhenUsingTwigNamespaces.patch + + +[FEATURE] Compile embedded templates in Markdown files +Source: .patches/compileTemplatesInMarkdownFiles.patch + + +[FIX] Allow PL to Ignore Certain Folders like Node Modules. +Source: .patches/ignoreCertainFolders.patch + + +[FEATURE] Allow default Pattern Data configuration rules to be disabled. Also adds ability to add on extra Pattern Data rules to customize and extend default Pattern Lab behavior. +Source: .patches/configurablePatternDataRules.patch + + +[FEATURE] Automatically re-generate the Pattern Lab public folder with styleguidekit assets if the public folder ever gets cleaned. PR #15 on DPL PL Core +Source: .patches/regeneratePatternLabStyleguideKit.patch + + diff --git a/src/PatternLab/Builder.php.rej b/src/PatternLab/Builder.php.rej new file mode 100644 index 00000000..bd6fe776 --- /dev/null +++ b/src/PatternLab/Builder.php.rej @@ -0,0 +1,64 @@ +*************** +*** 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 diff --git a/src/PatternLab/PatternData/Helpers/LineageHelper.php b/src/PatternLab/PatternData/Helpers/LineageHelper.php index 1d5676e5..3e34967b 100644 --- a/src/PatternLab/PatternData/Helpers/LineageHelper.php +++ b/src/PatternLab/PatternData/Helpers/LineageHelper.php @@ -43,17 +43,19 @@ public function run() { $store = PatternData::get(); foreach ($store as $patternStoreKey => $patternStoreData) { - if (($patternStoreData["category"] == "pattern") && (!isset($patternStoreData["pseudo"]))) { + // Check for lineages in patterns AND sub-patterns + if (($patternStoreData["category"] == "pattern" || $patternStoreData["category"] == "patternSubtype") && (!isset($patternStoreData["pseudo"]))) { $patternLineages = array(); $fileData = isset($patternStoreData["patternRaw"]) ? $patternStoreData["patternRaw"] : ""; + $foundLineages = $this->findLineages($fileData); if (!empty($foundLineages)) { foreach ($foundLineages as $lineage) { - - /** + + /** * Fix for Pattern Lab Lineages when using Twig Namespaces. * Converts the full file path to PL-friendly shorthand so * they are internally registered. @@ -98,8 +100,24 @@ public function run() { $lineage = $patternType . "-" . $patternName; /* [11] */ } - if (PatternData::getOption($lineage)) { + + // If we're having trouble finding a pattern's lineage (ex. due to using Twig namespaces that don't match a pattern's parent directory), try finding a pattern with the same pattern name. Solves https://github.com/EvanLovely/plugin-twig-namespaces/issues/4 + if (PatternData::getOption($lineage) == false){ + // Strip off the original lineage's pattern type so we can compare the original pattern name to the new one if we find a potential match. + $origPatternName = list($before, $after) = explode('-', $lineage, 2)[1]; + //Loop through the patterns available and compare each pattern's patternName with the original lineage match that was found + foreach ($store as $altPatternStoreKey => $nestedPatternStoreData) { + $altPatternName = list($before, $after) = explode('-', $altPatternStoreKey, 2)[1]; + + // If we can't figure out the original pattern's lineage but found a match, use that instead. + if ($origPatternName == $altPatternName){ + $lineage = $altPatternStoreKey; + } + } + } + + if (PatternData::getOption($lineage)) { $patternLineages[] = array("lineagePattern" => $lineage, "lineagePath" => "../../patterns/".$patternStoreData["pathDash"]."/".$patternStoreData["pathDash"].$suffixRendered.".html"); @@ -109,18 +127,18 @@ public function run() { $fileName = $patternStoreData["pathName"].".".$patternExtension; Console::writeWarning("you may have a typo in ".$fileName.". `".$lineage."` is not a valid pattern..."); } - + } - + } // add the lineages to the PatternData::$store PatternData::setPatternOption($patternStoreKey,"lineages",$patternLineages); } - + } - + } // handle all of those pseudo patterns @@ -141,7 +159,7 @@ public function run() { $store = PatternData::get(); foreach ($store as $patternStoreKey => $patternStoreData) { - if (($patternStoreData["category"] == "pattern") && (!isset($patternStoreData["pseudo"])) && isset($patternStoreData["partial"])) { + if (($patternStoreData["category"] == "pattern" || $patternStoreData["category"] == "patternSubtype") && (!isset($patternStoreData["pseudo"])) && isset($patternStoreData["partial"])) { $patternLineagesR = array(); @@ -173,21 +191,21 @@ public function run() { "lineagePath" => "../../patterns/".$path."/".$path.$suffixRendered.".html"); } - + } - + } - + } - + } - + } - + PatternData::setPatternOption($patternStoreKey,"lineagesR",$patternLineagesR); } - + } // handle all of those pseudo patterns From 0ec956294f1f25dc5cc9552c106e6b45506e9f1f Mon Sep 17 00:00:00 2001 From: Salem Ghoweri Date: Sun, 1 Oct 2017 17:21:25 -0400 Subject: [PATCH 2/2] Removing extra files --- PATCHES.txt | 23 ------------ src/PatternLab/Builder.php.rej | 64 ---------------------------------- 2 files changed, 87 deletions(-) delete mode 100644 PATCHES.txt delete mode 100644 src/PatternLab/Builder.php.rej diff --git a/PATCHES.txt b/PATCHES.txt deleted file mode 100644 index 2aa7d63f..00000000 --- a/PATCHES.txt +++ /dev/null @@ -1,23 +0,0 @@ -This file was automatically generated by Composer Patches (https://github.com/cweagans/composer-patches) -Patches applied to this directory: - -[FIX] Fix PL Lineages when using Twig namespaces plugin. -Source: .patches/fixPatternLabLineagesWhenUsingTwigNamespaces.patch - - -[FEATURE] Compile embedded templates in Markdown files -Source: .patches/compileTemplatesInMarkdownFiles.patch - - -[FIX] Allow PL to Ignore Certain Folders like Node Modules. -Source: .patches/ignoreCertainFolders.patch - - -[FEATURE] Allow default Pattern Data configuration rules to be disabled. Also adds ability to add on extra Pattern Data rules to customize and extend default Pattern Lab behavior. -Source: .patches/configurablePatternDataRules.patch - - -[FEATURE] Automatically re-generate the Pattern Lab public folder with styleguidekit assets if the public folder ever gets cleaned. PR #15 on DPL PL Core -Source: .patches/regeneratePatternLabStyleguideKit.patch - - diff --git a/src/PatternLab/Builder.php.rej b/src/PatternLab/Builder.php.rej deleted file mode 100644 index bd6fe776..00000000 --- a/src/PatternLab/Builder.php.rej +++ /dev/null @@ -1,64 +0,0 @@ -*************** -*** 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