Exemple #1
0
 /**
  * @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;
 }
Exemple #2
0
 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'));
 }
 private function askForCustomOptionArray($currentValue, Option $option)
 {
     $da = new DotArray($currentValue);
     $this->stdio->outln($option->getPath());
     $this->stdio->outln($option->getDescription());
     $continue = true;
     while ($continue) {
         $this->stdio->displayList($da->flatten());
         $actions = ['delete', 'clear all', 'add', 'replace', 'skip'];
         $action = $this->stdio->chooseAction($actions);
         switch ($action) {
             case 'delete':
                 $index = $this->stdio->chooseFromList($da->flatten(), 'Item to delete', false);
                 if ($index !== null) {
                     $da->remove($index);
                 }
                 break;
             case 'clear all':
                 $da = new DotArray();
                 break;
             case 'add':
                 $newValue = $this->stdio->ask('Enter value of the new item');
                 $da = new DotArray(array_merge($da->getArray(), [$newValue]));
                 break;
             case 'replace':
                 $index = $this->stdio->chooseFromList($da->flatten(), 'Item to replace', false);
                 if ($index !== null) {
                     $da->set($index, $this->stdio->ask('Enter new value'));
                 }
                 break;
             case 'skip':
                 $continue = false;
                 break;
         }
     }
     $this->frontmatter->set($option->getPath(), $da->getArray());
 }
Exemple #4
0
 /**
  * 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()));
     }
 }
Exemple #5
0
 /**
  * @return string[]
  */
 public function getVisibilityList()
 {
     return $this->frontmatter->get('api.visibility', self::VISIBILITY_DEFAULT);
 }