Exemple #1
0
 /**
  * @param \nochso\WriteMe\Placeholder\Call $call
  *
  * @return \nochso\WriteMe\Markdown\HeaderContent[]
  */
 private function getHeaderContents(Call $call)
 {
     $changelogPath = $this->findChangelog($call->getDocument());
     $changelog = Document::fromFile($changelogPath);
     $parser = new HeaderParser();
     $headerContentList = $parser->extractHeaderContents($changelog);
     $maxChanges = $this->options->getValue('changelog.max-changes');
     $releaseLevel = $this->options->getValue('changelog.release-level');
     $changes = 0;
     /** @var HeaderContent[] $displayHeaders */
     $displayHeaders = [];
     /** @var HeaderContent $headerContent */
     foreach ($headerContentList->getHeaders() as $headerContent) {
         // This header marks a release
         if ($headerContent->getLevel() === $releaseLevel) {
             $changes++;
             // Stop if we reached the max amount of changes.
             if ($changes > $maxChanges) {
                 break;
             }
             $displayHeaders[] = $headerContent;
         }
         // Keep adding sub-HeaderContent if we're within a release
         if ($changes > 0 && $headerContent->getLevel() > $releaseLevel) {
             $displayHeaders[] = $headerContent;
         }
     }
     return $displayHeaders;
 }
Exemple #2
0
 public function run()
 {
     $this->stdio->outln($this->version->getInfo());
     $this->stdio->outln();
     try {
         $getopt = $this->context->getopt($this->getOptions());
         # For the interactive session.
         if ($getopt->get('--init')) {
             $doc = $this->interactiveTemplateToDocument();
             $doc->saveRaw();
             $this->stdio->outln();
             $this->stdio->outln('Customized template written to ' . $doc->getFilepath());
             $this->converter->convert($doc, $this->placeholders);
             $targetPath = $doc->saveTarget();
             $this->stdio->outln('Converted document written to ' . $targetPath);
             exit(Status::SUCCESS);
         }
         $this->validate($getopt);
         $sourceFile = $getopt->get(1);
         if ($sourceFile === null) {
             $this->showHelp();
             exit(Status::USAGE);
         }
         if (!is_file($sourceFile)) {
             throw new \RuntimeException('File not found: ' . $sourceFile);
         }
         $doc = Document::fromFile($sourceFile);
         $this->converter->convert($doc, $this->placeholders);
         $targetFile = $doc->getTargetFilepath($getopt->get('--target'));
         if ($getopt->get('--diff')) {
             $this->showDiff($doc, $targetFile);
         }
         if ($getopt->get('--dry-run')) {
             // Show full output when diff is not wanted
             if (!$getopt->get('--diff')) {
                 $this->stdio->outln('Output:');
                 $this->stdio->outln('<<green>>---<<reset>>');
                 $this->stdio->outln($doc->getContent());
                 $this->stdio->outln('<<green>>---<<reset>>');
             }
         } else {
             $doc->saveTarget($targetFile);
         }
         $this->stdio->outln(sprintf('Saved output from <<green>>%s<<reset>> to <<green>>%s<<reset>>.', $doc->getFilepath(), $targetFile));
     } catch (\Exception $e) {
         $this->stdio->exception($e);
         exit(Status::FAILURE);
     }
 }