/** * @param string|null $overrideTarget * * @return string */ public function getTargetFilepath($overrideTarget = null) { $target = $overrideTarget; // --target is optional. If empty, try the frontmatter key. if ($target === null) { $target = $this->frontmatter->get('target', null); } // Still empty: try replacing WRITEME* with README* if ($target === null) { // Only work with the actual file name $filename = basename($this->filepath); if (preg_match('/^(writeme)((\\..+)?)/i', $filename, $matches)) { $name = $matches[1]; $extension = $matches[2]; if (strtoupper($name) === $name) { $target = 'README' . $extension; } else { $target = 'readme' . $extension; } $target = Path::combine(dirname($this->filepath), $target); } } if ($target === null) { throw new \RuntimeException(sprintf('Could not guess target file name from CLI option, frontmatter key "target" or source file name "%s".', $this->filepath)); } return $target; }
public function testSet() { $fm = new Frontmatter(); $fm->set('foo.bar', 'baz'); $this->assertSame('baz', $fm->get('foo.bar')); $fm->set('foo', 'replaced'); $this->assertSame('replaced', $fm->get('foo')); $this->assertNull($fm->get('foo.bar')); }
/** * askForCustomOption to override frontmatter. * * @param string[] $numberPathMap * @param \nochso\WriteMe\Placeholder\OptionList $defaults * * @return bool True if you should continue asking. False if nothing was entered. */ private function askForCustomOption($numberPathMap, OptionList $defaults) { $number = $this->stdio->ask('Enter the [number] of the option you want to change (leave empty to continue)', ''); // Empty to continue if ($number === '') { return false; } // Ask again for a valid number if (!isset($numberPathMap[$number])) { return true; } $option = $defaults->getOption($numberPathMap[$number]); $currentValue = $this->frontmatter->get($option->getPath(), $option->getDefault()); // Some options take a list of values if (is_array($currentValue)) { $this->askForCustomOptionArray($currentValue, $option); } else { // Otherwise ask for a single value $this->ask($option->getPath(), $option->getDescription(), $currentValue); } // Ask again for another option return true; }
/** * Prepare options by overriding defaults with frontmatter values. * * @param \nochso\WriteMe\Frontmatter $frontmatter */ public function prepare(\nochso\WriteMe\Frontmatter $frontmatter) { foreach ($this->options as $option) { $option->setValue($frontmatter->get($option->getPath(), $option->getDefault())); } }
/** * @return string[] */ public function getVisibilityList() { return $this->frontmatter->get('api.visibility', self::VISIBILITY_DEFAULT); }