Skip to content

Commit

Permalink
show step annotations for manual triggers
Browse files Browse the repository at this point in the history
annotate `--show` and `--show-pipelines` steps by `*M` for any step with a
manual trigger (`trigger: manual`).

streamline display of unnamed steps as `no-name` to prevent hiding steps
with `--show-pipelines`.

for that:

extract file-info modules. the file-shower should concentrate on traversing
and arranging the information in tables, not so much about data-point
string acquisition and formatting.

let file-info do that.
  • Loading branch information
ktomk committed Dec 18, 2021
1 parent d2d13ca commit 3c51fe5
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ The format is based on [Keep a Changelog] and Pipelines adheres to
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [unreleased]
### Add
- `--show` and `--show-pipelines`: Annotate steps with manual triggers
with `*M`
### Change
- `--show-pipelines`: Show unnamed steps as `no-name` (like `--show`)
### Fix
- Fix internal file descriptor system paths mapping flaws, since
[0.0.39](#0039---2020-06-02)
Expand Down
109 changes: 109 additions & 0 deletions src/File/Info/StepInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/* this file is part of pipelines */

namespace Ktomk\Pipelines\File\Info;

use Ktomk\Pipelines\File\File;
use Ktomk\Pipelines\File\Pipeline\Step;

/**
* info about a step (for show output)
*/
final class StepInfo
{
const NO_NAME = 'no-name';
const CHAR_MANUAL = 'M';

/**
* @var Step
*/
private $step;
private $index;

/**
* @param Step $step
* @param int $index (zero-based)
*/
public function __construct(Step $step, $index)
{
$this->step = $step;
$this->index = (int)$index;
}

/**
* @param string $string
* @param string $separator [optional]
*
* @return string
*/
public function annotate($string, $separator = ' *')
{
$buffer = (string)$string;
$annotations = $this->getAnnotations();
if ($annotations) {
$buffer .= $separator . implode('', $annotations);
}

return $buffer;
}

/**
* @return array
*/
public function getAnnotations()
{
$annotations = array();

$this->step->isManual() && $annotations[] = self::CHAR_MANUAL;

return $annotations;
}

/**
* @return \Ktomk\Pipelines\File\Image
*/
public function getImage()
{
return $this->step->getImage();
}

/**
* @return string
*/
public function getImageName()
{
return $this->step->getImage()->getName();
}

/**
* @return string
*/
public function getName()
{
$name = $this->step->getName();

return null === $name ? self::NO_NAME : sprintf('"%s"', $name);
}

public function getStep()
{
return $this->step;
}

/**
* @return int
*/
public function getStepNumber()
{
return $this->index + 1;
}

/**
* @return bool
*/
public function hasDefaultImage()
{
return File::DEFAULT_IMAGE === $this->getImageName();
}
}
106 changes: 106 additions & 0 deletions src/File/Info/StepsInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/* this file is part of pipelines */

namespace Ktomk\Pipelines\File\Info;

use Ktomk\Pipelines\File\Pipeline;
use Ktomk\Pipelines\File\Pipeline\Steps;

/**
* info about steps (for show output)
*
* info about steps include {@see StepInfo}.
*/
final class StepsInfo implements \Countable
{
/**
* @var ?Steps
*/
private $steps;

public static function fromPipeline(Pipeline $pipeline = null)
{
return new self($pipeline ? $pipeline->getSteps() : null);
}

/**
* @param ?Steps $steps
*/
public function __construct(Steps $steps = null)
{
$this->steps = $steps;
}

/**
* @return array|string[] image names (unique), w/o the default image name
*/
public function getImageNames()
{
$images = array();
foreach (new StepsStepInfoIterator($this->steps) as $info) {
if (false === $info->hasDefaultImage()) {
$images[] = $info->getImageName();
}
}

return array_unique($images);
}

public function getImagesAsString($separator = ', ')
{
$images = $this->getImageNames();

return $images ? implode($separator, $images) : '';
}

public function getSummary()
{
list($names, $annotations) = $this->getNamesAndAnnotations();

return sprintf(
'%d%s',
$this->count(),
$names ? ' (' . implode('; ', $this->annotate($names, ' *', $annotations)) . ')' : ''
);
}

public function count()
{
return $this->steps ? count($this->steps) : 0;
}

/**
* @return array [string[] images, string[] names, array<string>[] annotations]
*/
private function getNamesAndAnnotations()
{
$names = array();
$annotations = array();

foreach (new StepsStepInfoIterator($this->steps) as $info) {
$names[] = $info->getName();
$annotations[] = $info->getAnnotations();
}

return array($names, $annotations);
}

/**
* @param array<string> $names
* @param string $annotator
* @param array<string> $annotations
*
* @return array|string[]
*/
private function annotate(array $names, $annotator, array $annotations)
{
return array_map(function ($name, $annotations) use ($annotator) {
if ($annotations) {
$name .= $annotator . implode('', $annotations);
}

return $name;
}, $names, $annotations);
}
}
33 changes: 33 additions & 0 deletions src/File/Info/StepsStepInfoIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/* this file is part of pipelines */

namespace Ktomk\Pipelines\File\Info;

use Ktomk\Pipelines\File\Pipeline\Steps;

final class StepsStepInfoIterator extends \IteratorIterator
{
/**
* @var ?Steps
*/
private $steps;

/**
* @param ?Steps $steps
*/
public function __construct(Steps $steps = null)
{
$this->steps = $steps;
parent::__construct(Steps::fullIter($steps));
}

#[\ReturnTypeWillChange]
/**
* @return StepInfo
*/
public function current()
{
return new StepInfo(parent::current(), (int)$this->key());
}
}
55 changes: 12 additions & 43 deletions src/Utility/Show/FileShower.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Ktomk\Pipelines\File\Definitions\Service;
use Ktomk\Pipelines\File\Definitions\Services;
use Ktomk\Pipelines\File\File;
use Ktomk\Pipelines\File\Info\StepsInfo;
use Ktomk\Pipelines\File\Info\StepsStepInfoIterator;
use Ktomk\Pipelines\File\ParseException;
use Ktomk\Pipelines\File\Pipeline\Step;
use Ktomk\Pipelines\File\Pipeline\Steps;
Expand Down Expand Up @@ -47,11 +49,10 @@ public function showImages()
*/
public function showPipelineIds()
{
$pipelines = $this->file->getPipelines();

foreach ($pipelines->getPipelineIds() as $id) {
$this->info($id);
}
array_map(
array($this, 'info'),
$this->file->getPipelines()->getPipelineIds()
);

return 0;
}
Expand Down Expand Up @@ -87,12 +88,8 @@ public function showPipelines()
$table = new FileTable(array('PIPELINE ID', 'IMAGES', 'STEPS'));

foreach ($this->tablePipelineIdsPipelines($pipelines, $table) as $id => $pipeline) {
$steps = (null === $pipeline) ? null : $pipeline->getSteps();
list($images, $names) = $this->getImagesAndNames($steps);

$images = $images ? implode(', ', $images) : '';
$steps = sprintf('%d%s', count($steps), $names ? ' ("' . implode('"; "', $names) . '")' : '');
$table->addRow(array($id, $images, $steps));
$info = StepsInfo::fromPipeline($pipeline);
$table->addRow(array($id, $info->getImagesAsString(), $info->getSummary()));
}

return $this->outputTableAndReturn($table);
Expand Down Expand Up @@ -128,14 +125,10 @@ public function showServices()
*/
private function tableFileSteps($steps, $id, FileTable $table)
{
foreach ($steps::fullIter($steps) as $index => $step) {
$stepNumber = (int)$index + 1;
$name = $step->getName();
$name = null === $name ? 'no-name' : sprintf('"%s"', $name);

$table->addRow(array($id, $stepNumber, $step->getImage(), $name));
$this->tableFileStepsCaches($step, $id, $stepNumber, $table);
$this->tableFileStepsServices($step, $id, $stepNumber, $table);
foreach (new StepsStepInfoIterator($steps) as $info) {
$table->addRow(array($id, $info->annotate($number = $info->getStepNumber()), $info->getImage(), $info->getName()));
$this->tableFileStepsCaches($step = $info->getStep(), $id, $number, $table);
$this->tableFileStepsServices($step, $id, $number, $table);
}
}

Expand Down Expand Up @@ -245,28 +238,4 @@ private function getAllStepsWithServices(File $file)

return $return;
}

/**
* @param Steps $steps
*
* @return array
*/
private function getImagesAndNames(Steps $steps = null)
{
$images = array();
$names = array();

foreach (Steps::fullIter($steps) as $step) {
$image = $step->getImage()->getName();
if (File::DEFAULT_IMAGE !== $image) {
$images[] = $image;
}
$name = $step->getName();
(null !== $name) && $names[] = $name;
}

$images = array_unique($images);

return array($images, $names);
}
}
8 changes: 4 additions & 4 deletions src/Utility/Show/FileShowerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
class FileShowerAbstract
{
/**
* @var callable
* @var File
*/
protected $output;
protected $file;

/**
* @var File
* @var callable
*/
protected $file;
private $output;

/**
* FileInfo constructor.
Expand Down
2 changes: 1 addition & 1 deletion test/data/yml/steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pipelines:
script:
- printenv | sort
- step:
name: 'step #4'
# this step has no name
trigger: manual
script:
- printenv | sort
Loading

0 comments on commit 3c51fe5

Please sign in to comment.