jawira/phing-visualizer

View on GitHub
bin/phing-visualizer

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php declare(strict_types=1);

namespace Jawira\PhingVisualizer;

use Exception;
use Jawira\MiniGetopt\MiniGetopt;
use Throwable;


/**
 * Finds and loads autoload file
 *
 * @see https://www.tomasvotruba.cz/blog/2018/08/02/5-gotchas-of-the-bin-file-in-php-cli-applications/
 * @return void
 * @throws \Exception
 */
function loadAutoload(): void
{
    $candidates = [
        __DIR__ . '/../vendor/autoload.php',    // production
        __DIR__ . '/../../../autoload.php',     // development
    ];

    $eureka = false;
    foreach ($candidates as $c) {
        if (file_exists($c)) {
            /** @noinspection PhpIncludeInspection */
            require_once $c;
            $eureka = true;
            break;
        }
    }

    if (!$eureka) {
        throw new Exception("autoload.php not found; try 'composer dump-autoload' first.");
    }
}

/**
 * Run cli command to generate Phing diagrams
 *
 * @throws \Jawira\PhingVisualizer\DiagramException
 * @throws \Exception
 */
function runPhingVisualizer(): void
{
    $mg = new MiniGetopt();
    $mg->addRequired('i', 'input');
    $mg->addRequired('o', 'output');
    $mg->addRequired('f', 'format');
    $mg->addNoValue('h', 'help');

    $input  = $mg->getOption('i', 'input', Diagram::BUILDFILE_DEFAULT);
    $output = $mg->getOption('o', 'output');
    $format = $mg->getOption('f', 'format', Diagram::FORMAT_PNG);
    $help   = $mg->getOption('h', 'help');

    if (false === $help) {
        help();
        exit(0);
    }

    $diagram = new Diagram($input);
    $diagram->save($format, $output);
}

/**
 * Show binary help
 */
function help(): void
{
    echo <<<'HELP'
NAME
    phing-visualizer - visualize Phing's buildfile

SYNOPSIS
    phing-visualizer [-i <buildfile>] [-f <png|svg|puml|eps>] [-o <path>] 

DESCRIPTION
    With phing-visualizer you can generate a diagram that 
    represents Phing's targets, calls, and dependencies.

OPTIONS
    -i, --input <buildfile>
        The buildfile location. It could be an absolute or 
        relative path.
    
    -f, --format <png|svg|puml|eps>
        The format of the diagram to generate. 
    
    -o, --output <path>
        The location for the diagram to be generated, it could 
        be a filepath or dirpath. It defaults to same location 
        as --input.
        
    -h, --help
        Shows this message.

EXAMPLES
    $ phing-visualizer
    $ phing-visualizer -i build.xml -f png -o images/
    $ phing-visualizer --input /projects/build.xml --format svg
    
AUTHOR
    Jawira Portugal


HELP;
}

/**
 * Main function
 */
function main(): void
{
    try {
        loadAutoload();
        runPhingVisualizer();
    } catch (Throwable $th) {
        exit('⚠ ' . $th->getMessage() . PHP_EOL);
    }
}

main();