xref: /plugin/dev/cli.php (revision 9273840774e623180ba408f4f4f3ba5468aaa5c8)
136c0b2b4SAndreas Gohr#!/usr/bin/env php
236c0b2b4SAndreas Gohr<?php
336c0b2b4SAndreas Gohr
436c0b2b4SAndreas Gohruse dokuwiki\Extension\CLIPlugin;
536c0b2b4SAndreas Gohruse dokuwiki\Extension\PluginController;
61a23d1dbSAndreas Gohruse dokuwiki\plugin\dev\SVGIcon;
736c0b2b4SAndreas Gohruse splitbrain\phpcli\Exception as CliException;
836c0b2b4SAndreas Gohruse splitbrain\phpcli\Options;
936c0b2b4SAndreas Gohr
1036c0b2b4SAndreas Gohr/**
1136c0b2b4SAndreas Gohr * @license GPL2
1236c0b2b4SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
1336c0b2b4SAndreas Gohr */
1436c0b2b4SAndreas Gohrclass cli_plugin_dev extends CLIPlugin
1536c0b2b4SAndreas Gohr{
1636c0b2b4SAndreas Gohr
1736c0b2b4SAndreas Gohr    /**
1836c0b2b4SAndreas Gohr     * Register options and arguments on the given $options object
1936c0b2b4SAndreas Gohr     *
2036c0b2b4SAndreas Gohr     * @param Options $options
2136c0b2b4SAndreas Gohr     * @return void
2236c0b2b4SAndreas Gohr     */
2336c0b2b4SAndreas Gohr    protected function setup(Options $options)
2436c0b2b4SAndreas Gohr    {
25f2576912SAndreas Gohr        $options->useCompactHelp();
2636c0b2b4SAndreas Gohr        $options->setHelp(
27f2576912SAndreas Gohr            "CLI to help with DokuWiki plugin and template development.\n\n" .
2836c0b2b4SAndreas Gohr            "Run this script from within the extension's directory."
2936c0b2b4SAndreas Gohr        );
3036c0b2b4SAndreas Gohr
31f2576912SAndreas Gohr        $options->registerCommand('init', 'Initialize a new plugin or template in the current (empty) directory.');
32f2576912SAndreas Gohr        $options->registerCommand('addTest', 'Add the testing framework files and a test. (_test/)');
33f2576912SAndreas Gohr        $options->registerArgument('test', 'Optional name of the new test. Defaults to the general test.', false,
34f2576912SAndreas Gohr            'addTest');
35f2576912SAndreas Gohr        $options->registerCommand('addConf', 'Add the configuration files. (conf/)');
36f2576912SAndreas Gohr        $options->registerCommand('addLang', 'Add the language files. (lang/)');
3736c0b2b4SAndreas Gohr
38f2576912SAndreas Gohr        $types = PluginController::PLUGIN_TYPES;
39f2576912SAndreas Gohr        array_walk(
40f2576912SAndreas Gohr            $types,
41f2576912SAndreas Gohr            function (&$item) {
42f2576912SAndreas Gohr                $item = $this->colors->wrap($item, $this->colors::C_BROWN);
43f2576912SAndreas Gohr            }
4436c0b2b4SAndreas Gohr        );
4536c0b2b4SAndreas Gohr
46f2576912SAndreas Gohr        $options->registerCommand('addComponent', 'Add a new plugin component.');
47f2576912SAndreas Gohr        $options->registerArgument('type', 'Type of the component. Needs to be one of ' . join(', ', $types), true,
48f2576912SAndreas Gohr            'addComponent');
49f2576912SAndreas Gohr        $options->registerArgument('name', 'Optional name of the component. Defaults to a base component.', false,
50f2576912SAndreas Gohr            'addComponent');
51f2576912SAndreas Gohr
52f2576912SAndreas Gohr        $options->registerCommand('deletedFiles', 'Create the list of deleted files based on the git history.');
53f2576912SAndreas Gohr        $options->registerCommand('rmObsolete', 'Delete obsolete files.');
541a23d1dbSAndreas Gohr
551a23d1dbSAndreas Gohr        $prefixes = array_keys(SVGIcon::SOURCES);
561a23d1dbSAndreas Gohr        array_walk(
571a23d1dbSAndreas Gohr            $prefixes,
581a23d1dbSAndreas Gohr            function (&$item) {
591a23d1dbSAndreas Gohr                $item = $this->colors->wrap($item, $this->colors::C_BROWN);
601a23d1dbSAndreas Gohr            }
611a23d1dbSAndreas Gohr        );
621a23d1dbSAndreas Gohr
631a23d1dbSAndreas Gohr        $options->registerCommand('downloadSvg', 'Download an SVG file from a known icon repository.');
641a23d1dbSAndreas Gohr        $options->registerArgument('prefix:name',
651a23d1dbSAndreas Gohr            'Colon-prefixed name of the icon. Available prefixes: ' . join(', ', $prefixes), true, 'downloadSvg');
661a23d1dbSAndreas Gohr        $options->registerArgument('output', 'File to save, defaults to <name>.svg in current dir', false,
671a23d1dbSAndreas Gohr            'downloadSvg');
68*92738407SAndreas Gohr        $options->registerOption('keep-ns', 'Keep the SVG namespace. Use when the file is not inlined into HTML.', 'k',
69*92738407SAndreas Gohr            false, 'downloadSvg');
701a23d1dbSAndreas Gohr
711a23d1dbSAndreas Gohr        $options->registerCommand('cleanSvg', 'Clean an existing SVG file to reduce file size.');
721a23d1dbSAndreas Gohr        $options->registerArgument('file', 'The file to clean (will be overwritten)', true, 'cleanSvg');
73*92738407SAndreas Gohr        $options->registerOption('keep-ns', 'Keep the SVG namespace. Use when the file is not inlined into HTML.', 'k',
74*92738407SAndreas Gohr            false, 'cleanSvg');
7536c0b2b4SAndreas Gohr    }
7636c0b2b4SAndreas Gohr
7736c0b2b4SAndreas Gohr    /** @inheritDoc */
7836c0b2b4SAndreas Gohr    protected function main(Options $options)
7936c0b2b4SAndreas Gohr    {
801a23d1dbSAndreas Gohr        $args = $options->getArgs();
811a23d1dbSAndreas Gohr
8236c0b2b4SAndreas Gohr        switch ($options->getCmd()) {
8336c0b2b4SAndreas Gohr            case 'init':
8436c0b2b4SAndreas Gohr                return $this->cmdInit();
8536c0b2b4SAndreas Gohr            case 'addTest':
8636c0b2b4SAndreas Gohr                $test = array_shift($args);
8736c0b2b4SAndreas Gohr                return $this->cmdAddTest($test);
8836c0b2b4SAndreas Gohr            case 'addConf':
8936c0b2b4SAndreas Gohr                return $this->cmdAddConf();
9036c0b2b4SAndreas Gohr            case 'addLang':
9136c0b2b4SAndreas Gohr                return $this->cmdAddLang();
9236c0b2b4SAndreas Gohr            case 'addComponent':
9336c0b2b4SAndreas Gohr                $type = array_shift($args);
9436c0b2b4SAndreas Gohr                $component = array_shift($args);
9536c0b2b4SAndreas Gohr                return $this->cmdAddComponent($type, $component);
9636c0b2b4SAndreas Gohr            case 'deletedFiles':
9736c0b2b4SAndreas Gohr                return $this->cmdDeletedFiles();
98c5c85a97SAndreas Gohr            case 'rmObsolete':
991a23d1dbSAndreas Gohr                return $this->cmdRmObsolete();
1001a23d1dbSAndreas Gohr            case 'downloadSvg':
1011a23d1dbSAndreas Gohr                $ident = array_shift($args);
1021a23d1dbSAndreas Gohr                $save = array_shift($args);
103*92738407SAndreas Gohr                $keep = $options->getOpt('keep-ns', false);
104*92738407SAndreas Gohr                return $this->cmdDownloadSVG($ident, $save, $keep);
1051a23d1dbSAndreas Gohr            case 'cleanSvg':
1061a23d1dbSAndreas Gohr                $file = array_shift($args);
107*92738407SAndreas Gohr                $keep = $options->getOpt('keep-ns', false);
108*92738407SAndreas Gohr                return $this->cmdCleanSVG($file, $keep);
10936c0b2b4SAndreas Gohr            default:
1101a23d1dbSAndreas Gohr                $this->error('Unknown command');
11136c0b2b4SAndreas Gohr                echo $options->help();
11236c0b2b4SAndreas Gohr                return 0;
11336c0b2b4SAndreas Gohr        }
11436c0b2b4SAndreas Gohr    }
11536c0b2b4SAndreas Gohr
11636c0b2b4SAndreas Gohr    /**
11736c0b2b4SAndreas Gohr     * Get the extension name from the current working directory
11836c0b2b4SAndreas Gohr     *
11936c0b2b4SAndreas Gohr     * @throws CliException if something's wrong
12036c0b2b4SAndreas Gohr     * @param string $dir
12136c0b2b4SAndreas Gohr     * @return string[] name, type
12236c0b2b4SAndreas Gohr     */
12336c0b2b4SAndreas Gohr    protected function getTypedNameFromDir($dir)
12436c0b2b4SAndreas Gohr    {
12536c0b2b4SAndreas Gohr        $pdir = fullpath(DOKU_PLUGIN);
12636c0b2b4SAndreas Gohr        $tdir = fullpath(tpl_incdir() . '../');
12736c0b2b4SAndreas Gohr
12836c0b2b4SAndreas Gohr        if (strpos($dir, $pdir) === 0) {
12936c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($pdir));
13036c0b2b4SAndreas Gohr            $type = 'plugin';
13136c0b2b4SAndreas Gohr        } elseif (strpos($dir, $tdir) === 0) {
13236c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($tdir));
13336c0b2b4SAndreas Gohr            $type = 'template';
13436c0b2b4SAndreas Gohr        } else {
13536c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be in plugin or template directory');
13636c0b2b4SAndreas Gohr        }
13736c0b2b4SAndreas Gohr
13836c0b2b4SAndreas Gohr        $ldir = trim($ldir, '/');
13936c0b2b4SAndreas Gohr
14036c0b2b4SAndreas Gohr        if (strpos($ldir, '/') !== false) {
14136c0b2b4SAndreas Gohr            throw new CliException('Current directory has to be main extension directory');
14236c0b2b4SAndreas Gohr        }
14336c0b2b4SAndreas Gohr
14436c0b2b4SAndreas Gohr        return [$ldir, $type];
14536c0b2b4SAndreas Gohr    }
14636c0b2b4SAndreas Gohr
14736c0b2b4SAndreas Gohr    /**
14836c0b2b4SAndreas Gohr     * Interactively ask for a value from the user
14936c0b2b4SAndreas Gohr     *
15036c0b2b4SAndreas Gohr     * @param string $prompt
15136c0b2b4SAndreas Gohr     * @param bool $cache cache given value for next time?
15236c0b2b4SAndreas Gohr     * @return string
15336c0b2b4SAndreas Gohr     */
15436c0b2b4SAndreas Gohr    protected function readLine($prompt, $cache = false)
15536c0b2b4SAndreas Gohr    {
15636c0b2b4SAndreas Gohr        $value = '';
15736c0b2b4SAndreas Gohr        $default = '';
15836c0b2b4SAndreas Gohr        $cachename = getCacheName($prompt, '.readline');
15936c0b2b4SAndreas Gohr        if ($cache && file_exists($cachename)) {
16036c0b2b4SAndreas Gohr            $default = file_get_contents($cachename);
16136c0b2b4SAndreas Gohr        }
16236c0b2b4SAndreas Gohr
16336c0b2b4SAndreas Gohr        while ($value === '') {
16436c0b2b4SAndreas Gohr            echo $prompt;
16536c0b2b4SAndreas Gohr            if ($default) echo ' [' . $default . ']';
16636c0b2b4SAndreas Gohr            echo ': ';
16736c0b2b4SAndreas Gohr
16836c0b2b4SAndreas Gohr            $fh = fopen('php://stdin', 'r');
16936c0b2b4SAndreas Gohr            $value = trim(fgets($fh));
17036c0b2b4SAndreas Gohr            fclose($fh);
17136c0b2b4SAndreas Gohr
17236c0b2b4SAndreas Gohr            if ($value === '') $value = $default;
17336c0b2b4SAndreas Gohr        }
17436c0b2b4SAndreas Gohr
17536c0b2b4SAndreas Gohr        if ($cache) {
17636c0b2b4SAndreas Gohr            file_put_contents($cachename, $value);
17736c0b2b4SAndreas Gohr        }
17836c0b2b4SAndreas Gohr
17936c0b2b4SAndreas Gohr        return $value;
18036c0b2b4SAndreas Gohr    }
18136c0b2b4SAndreas Gohr
18236c0b2b4SAndreas Gohr    /**
18336c0b2b4SAndreas Gohr     * Download a skeleton file and do the replacements
18436c0b2b4SAndreas Gohr     *
18536c0b2b4SAndreas Gohr     * @param string $skel Skeleton relative to the skel dir in the repo
18636c0b2b4SAndreas Gohr     * @param string $target Target file relative to the main directory
18736c0b2b4SAndreas Gohr     * @param array $replacements
18836c0b2b4SAndreas Gohr     */
18936c0b2b4SAndreas Gohr    protected function loadSkeleton($skel, $target, $replacements)
19036c0b2b4SAndreas Gohr    {
19136c0b2b4SAndreas Gohr        if (file_exists($target)) {
19236c0b2b4SAndreas Gohr            $this->error($target . ' already exists');
19336c0b2b4SAndreas Gohr            return;
19436c0b2b4SAndreas Gohr        }
19536c0b2b4SAndreas Gohr
19636c0b2b4SAndreas Gohr        $base = 'https://raw.githubusercontent.com/dokufreaks/dokuwiki-plugin-wizard/master/skel/';
19736c0b2b4SAndreas Gohr        $http = new \dokuwiki\HTTP\DokuHTTPClient();
19836c0b2b4SAndreas Gohr        $content = $http->get($base . $skel);
19936c0b2b4SAndreas Gohr
20036c0b2b4SAndreas Gohr        $content = str_replace(
20136c0b2b4SAndreas Gohr            array_keys($replacements),
20236c0b2b4SAndreas Gohr            array_values($replacements),
20336c0b2b4SAndreas Gohr            $content
20436c0b2b4SAndreas Gohr        );
20536c0b2b4SAndreas Gohr
20636c0b2b4SAndreas Gohr        io_makeFileDir($target);
20736c0b2b4SAndreas Gohr        file_put_contents($target, $content);
20836c0b2b4SAndreas Gohr        $this->success('Added ' . $target);
20936c0b2b4SAndreas Gohr    }
21036c0b2b4SAndreas Gohr
21136c0b2b4SAndreas Gohr    /**
21236c0b2b4SAndreas Gohr     * Prepare the string replacements
21336c0b2b4SAndreas Gohr     *
21436c0b2b4SAndreas Gohr     * @param array $replacements override defaults
21536c0b2b4SAndreas Gohr     * @return array
21636c0b2b4SAndreas Gohr     */
21736c0b2b4SAndreas Gohr    protected function prepareReplacements($replacements = [])
21836c0b2b4SAndreas Gohr    {
21936c0b2b4SAndreas Gohr        // defaults
22036c0b2b4SAndreas Gohr        $data = [
22136c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => '',
22236c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => '',
22336c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => '',
22436c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => '',
22536c0b2b4SAndreas Gohr            '@@PLUGIN_URL@@' => '',
22636c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => '',
22736c0b2b4SAndreas Gohr            '@@INSTALL_DIR@@' => 'plugins',
22836c0b2b4SAndreas Gohr            '@@DATE@@' => date('Y-m-d'),
22936c0b2b4SAndreas Gohr        ];
23036c0b2b4SAndreas Gohr
23136c0b2b4SAndreas Gohr        // load from existing plugin.info
23236c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
23336c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
23436c0b2b4SAndreas Gohr        if (file_exists("$type.info.txt")) {
23536c0b2b4SAndreas Gohr            $info = confToHash("$type.info.txt");
23636c0b2b4SAndreas Gohr            $data['@@AUTHOR_NAME@@'] = $info['author'];
23736c0b2b4SAndreas Gohr            $data['@@AUTHOR_MAIL@@'] = $info['email'];
23836c0b2b4SAndreas Gohr            $data['@@PLUGIN_DESC@@'] = $info['desc'];
23936c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] = $info['url'];
24036c0b2b4SAndreas Gohr        }
24136c0b2b4SAndreas Gohr        $data['@@PLUGIN_NAME@@'] = $name;
24236c0b2b4SAndreas Gohr        $data['@@PLUGIN_TYPE@@'] = $type;
24336c0b2b4SAndreas Gohr
24436c0b2b4SAndreas Gohr        if ($type == 'template') {
24536c0b2b4SAndreas Gohr            $data['@@INSTALL_DIR@@'] = 'tpl';
24636c0b2b4SAndreas Gohr        }
24736c0b2b4SAndreas Gohr
24836c0b2b4SAndreas Gohr        // merge given overrides
24936c0b2b4SAndreas Gohr        $data = array_merge($data, $replacements);
25036c0b2b4SAndreas Gohr
25136c0b2b4SAndreas Gohr        // set inherited defaults
25236c0b2b4SAndreas Gohr        if (empty($data['@@PLUGIN_URL@@'])) {
25336c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] =
25436c0b2b4SAndreas Gohr                'https://www.dokuwiki.org/' .
25536c0b2b4SAndreas Gohr                $data['@@PLUGIN_TYPE@@'] . ':' .
25636c0b2b4SAndreas Gohr                $data['@@PLUGIN_NAME@@'];
25736c0b2b4SAndreas Gohr        }
25836c0b2b4SAndreas Gohr
25936c0b2b4SAndreas Gohr        return $data;
26036c0b2b4SAndreas Gohr    }
26136c0b2b4SAndreas Gohr
26236c0b2b4SAndreas Gohr    /**
26336c0b2b4SAndreas Gohr     * Replacements needed for action components.
26436c0b2b4SAndreas Gohr     *
26536c0b2b4SAndreas Gohr     * Not cool but that' what we need currently
26636c0b2b4SAndreas Gohr     *
26736c0b2b4SAndreas Gohr     * @return string[]
26836c0b2b4SAndreas Gohr     */
26936c0b2b4SAndreas Gohr    protected function actionReplacements()
27036c0b2b4SAndreas Gohr    {
27136c0b2b4SAndreas Gohr        $fn = 'handleEventName';
27236c0b2b4SAndreas Gohr        $register = '        $controller->register_hook(\'EVENT_NAME\', \'AFTER|BEFORE\', $this, \'' . $fn . '\');';
27336c0b2b4SAndreas Gohr        $handler = '    public function ' . $fn . '(Doku_Event $event, $param)' . "\n"
27436c0b2b4SAndreas Gohr            . "    {\n"
27536c0b2b4SAndreas Gohr            . "    }\n";
27636c0b2b4SAndreas Gohr
27736c0b2b4SAndreas Gohr        return [
27836c0b2b4SAndreas Gohr            '@@REGISTER@@' => $register . "\n   ",
27936c0b2b4SAndreas Gohr            '@@HANDLERS@@' => $handler,
28036c0b2b4SAndreas Gohr        ];
28136c0b2b4SAndreas Gohr    }
28236c0b2b4SAndreas Gohr
28336c0b2b4SAndreas Gohr    /**
284c5c85a97SAndreas Gohr     * Delete the given file if it exists
285c5c85a97SAndreas Gohr     *
286c5c85a97SAndreas Gohr     * @param string $file
287c5c85a97SAndreas Gohr     */
288c5c85a97SAndreas Gohr    protected function deleteFile($file)
289c5c85a97SAndreas Gohr    {
290c5c85a97SAndreas Gohr        if (!file_exists($file)) return;
291c5c85a97SAndreas Gohr        if (@unlink($file)) {
292c5c85a97SAndreas Gohr            $this->success('Delete ' . $file);
293c5c85a97SAndreas Gohr        }
294c5c85a97SAndreas Gohr    }
295c5c85a97SAndreas Gohr
296c5c85a97SAndreas Gohr    /**
297c5c85a97SAndreas Gohr     * Run git with the given arguments and return the output
298c5c85a97SAndreas Gohr     *
299c5c85a97SAndreas Gohr     * @throws CliException when the command can't be run
300c5c85a97SAndreas Gohr     * @param string ...$args
301c5c85a97SAndreas Gohr     * @return string[]
302c5c85a97SAndreas Gohr     */
303c5c85a97SAndreas Gohr    protected function git(...$args)
304c5c85a97SAndreas Gohr    {
305c5c85a97SAndreas Gohr        $args = array_map('escapeshellarg', $args);
306c5c85a97SAndreas Gohr        $cmd = 'git ' . join(' ', $args);
307c5c85a97SAndreas Gohr        $output = [];
308c5c85a97SAndreas Gohr        $result = 0;
309c5c85a97SAndreas Gohr
310c5c85a97SAndreas Gohr        $this->info($cmd);
311c5c85a97SAndreas Gohr        $last = exec($cmd, $output, $result);
312c5c85a97SAndreas Gohr        if ($last === false || $result !== 0) {
313c5c85a97SAndreas Gohr            throw new CliException('Running git failed');
314c5c85a97SAndreas Gohr        }
315c5c85a97SAndreas Gohr
316c5c85a97SAndreas Gohr        return $output;
317c5c85a97SAndreas Gohr    }
318c5c85a97SAndreas Gohr
319c5c85a97SAndreas Gohr    // region Commands
320c5c85a97SAndreas Gohr
321c5c85a97SAndreas Gohr    /**
32236c0b2b4SAndreas Gohr     * Intialize the current directory as a plugin or template
32336c0b2b4SAndreas Gohr     *
32436c0b2b4SAndreas Gohr     * @return int
32536c0b2b4SAndreas Gohr     */
32636c0b2b4SAndreas Gohr    protected function cmdInit()
32736c0b2b4SAndreas Gohr    {
32836c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
32936c0b2b4SAndreas Gohr        if ((new FilesystemIterator($dir))->valid()) {
33036c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be empty');
33136c0b2b4SAndreas Gohr        }
33236c0b2b4SAndreas Gohr
33336c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
33436c0b2b4SAndreas Gohr        $user = $this->readLine('Your Name', true);
33536c0b2b4SAndreas Gohr        $mail = $this->readLine('Your E-Mail', true);
33636c0b2b4SAndreas Gohr        $desc = $this->readLine('Short description');
33736c0b2b4SAndreas Gohr
33836c0b2b4SAndreas Gohr        $replacements = [
33936c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => $user,
34036c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => $mail,
34136c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => $name,
34236c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => $desc,
34336c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => $type,
34436c0b2b4SAndreas Gohr        ];
34536c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
34636c0b2b4SAndreas Gohr
34736c0b2b4SAndreas Gohr        $this->loadSkeleton('info.skel', $type . '.info.txt', $replacements);
34836c0b2b4SAndreas Gohr        $this->loadSkeleton('README.skel', 'README', $replacements); // fixme needs to be type specific
34936c0b2b4SAndreas Gohr        $this->loadSkeleton('LICENSE.skel', 'LICENSE', $replacements);
35036c0b2b4SAndreas Gohr
3518b06c9ddSAndreas Gohr        try {
3528b06c9ddSAndreas Gohr            $this->git('init');
3538b06c9ddSAndreas Gohr        } catch (CliException $e) {
3548b06c9ddSAndreas Gohr            $this->error($e->getMessage());
3558b06c9ddSAndreas Gohr        }
3568b06c9ddSAndreas Gohr
35736c0b2b4SAndreas Gohr        return 0;
35836c0b2b4SAndreas Gohr    }
35936c0b2b4SAndreas Gohr
36036c0b2b4SAndreas Gohr    /**
36136c0b2b4SAndreas Gohr     * Add test framework
36236c0b2b4SAndreas Gohr     *
36336c0b2b4SAndreas Gohr     * @param string $test Name of the Test to add
36436c0b2b4SAndreas Gohr     * @return int
36536c0b2b4SAndreas Gohr     */
36636c0b2b4SAndreas Gohr    protected function cmdAddTest($test = '')
36736c0b2b4SAndreas Gohr    {
36836c0b2b4SAndreas Gohr        $test = ucfirst(strtolower($test));
36936c0b2b4SAndreas Gohr
37036c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements(['@@TEST@@' => $test]);
37136c0b2b4SAndreas Gohr        $this->loadSkeleton('.github/workflows/phpTestLinux.skel', '.github/workflows/phpTestLinux.yml', $replacements);
37236c0b2b4SAndreas Gohr        if ($test) {
37336c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/StandardTest.skel', '_test/' . $test . 'Test.php', $replacements);
37436c0b2b4SAndreas Gohr        } else {
37536c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/GeneralTest.skel', '_test/GeneralTest.php', $replacements);
37636c0b2b4SAndreas Gohr        }
37736c0b2b4SAndreas Gohr
37836c0b2b4SAndreas Gohr        return 0;
37936c0b2b4SAndreas Gohr    }
38036c0b2b4SAndreas Gohr
38136c0b2b4SAndreas Gohr    /**
38236c0b2b4SAndreas Gohr     * Add configuration
38336c0b2b4SAndreas Gohr     *
38436c0b2b4SAndreas Gohr     * @return int
38536c0b2b4SAndreas Gohr     */
38636c0b2b4SAndreas Gohr    protected function cmdAddConf()
38736c0b2b4SAndreas Gohr    {
38836c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
38936c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/default.skel', 'conf/default.php', $replacements);
39036c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/metadata.skel', 'conf/metadata.php', $replacements);
39136c0b2b4SAndreas Gohr        if (is_dir('lang')) {
39236c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
39336c0b2b4SAndreas Gohr        }
39436c0b2b4SAndreas Gohr
39536c0b2b4SAndreas Gohr        return 0;
39636c0b2b4SAndreas Gohr    }
39736c0b2b4SAndreas Gohr
39836c0b2b4SAndreas Gohr    /**
39936c0b2b4SAndreas Gohr     * Add language
40036c0b2b4SAndreas Gohr     *
40136c0b2b4SAndreas Gohr     * @return int
40236c0b2b4SAndreas Gohr     */
40336c0b2b4SAndreas Gohr    protected function cmdAddLang()
40436c0b2b4SAndreas Gohr    {
40536c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
40636c0b2b4SAndreas Gohr        $this->loadSkeleton('lang/lang.skel', 'lang/en/lang.php', $replacements);
40736c0b2b4SAndreas Gohr        if (is_dir('conf')) {
40836c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
40936c0b2b4SAndreas Gohr        }
41036c0b2b4SAndreas Gohr
41136c0b2b4SAndreas Gohr        return 0;
41236c0b2b4SAndreas Gohr    }
41336c0b2b4SAndreas Gohr
41436c0b2b4SAndreas Gohr    /**
41536c0b2b4SAndreas Gohr     * Add another component to the plugin
41636c0b2b4SAndreas Gohr     *
41736c0b2b4SAndreas Gohr     * @param string $type
41836c0b2b4SAndreas Gohr     * @param string $component
41936c0b2b4SAndreas Gohr     */
42036c0b2b4SAndreas Gohr    protected function cmdAddComponent($type, $component = '')
42136c0b2b4SAndreas Gohr    {
42236c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
42336c0b2b4SAndreas Gohr        list($plugin, $extension) = $this->getTypedNameFromDir($dir);
42436c0b2b4SAndreas Gohr        if ($extension != 'plugin') throw  new CliException('Components can only be added to plugins');
42536c0b2b4SAndreas Gohr        if (!in_array($type, PluginController::PLUGIN_TYPES)) {
42636c0b2b4SAndreas Gohr            throw new CliException('Invalid type ' . $type);
42736c0b2b4SAndreas Gohr        }
42836c0b2b4SAndreas Gohr
42936c0b2b4SAndreas Gohr        if ($component) {
43036c0b2b4SAndreas Gohr            $path = $type . '/' . $component . '.php';
43136c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin . '_' . $component;
43236c0b2b4SAndreas Gohr            $self = $plugin . '_' . $component;
43336c0b2b4SAndreas Gohr        } else {
43436c0b2b4SAndreas Gohr            $path = $type . '.php';
43536c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin;
43636c0b2b4SAndreas Gohr            $self = $plugin;
43736c0b2b4SAndreas Gohr        }
43836c0b2b4SAndreas Gohr
43936c0b2b4SAndreas Gohr        $replacements = $this->actionReplacements();
44036c0b2b4SAndreas Gohr        $replacements['@@PLUGIN_COMPONENT_NAME@@'] = $class;
44136c0b2b4SAndreas Gohr        $replacements['@@SYNTAX_COMPONENT_NAME@@'] = $self;
44236c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
44336c0b2b4SAndreas Gohr        $this->loadSkeleton($type . '.skel', $path, $replacements);
44436c0b2b4SAndreas Gohr
44536c0b2b4SAndreas Gohr        return 0;
44636c0b2b4SAndreas Gohr    }
44736c0b2b4SAndreas Gohr
44836c0b2b4SAndreas Gohr    /**
44936c0b2b4SAndreas Gohr     * Generate a list of deleted files from git
45036c0b2b4SAndreas Gohr     *
45136c0b2b4SAndreas Gohr     * @link https://stackoverflow.com/a/6018049/172068
45236c0b2b4SAndreas Gohr     */
45336c0b2b4SAndreas Gohr    protected function cmdDeletedFiles()
45436c0b2b4SAndreas Gohr    {
4558b06c9ddSAndreas Gohr        if (!is_dir('.git')) throw new CliException('This extension seems not to be managed by git');
45636c0b2b4SAndreas Gohr
4578b06c9ddSAndreas Gohr        $output = $this->git('log', '--no-renames', '--pretty=format:', '--name-only', '--diff-filter=D');
45836c0b2b4SAndreas Gohr        $output = array_map('trim', $output);
45936c0b2b4SAndreas Gohr        $output = array_filter($output);
46036c0b2b4SAndreas Gohr        $output = array_unique($output);
46136c0b2b4SAndreas Gohr        $output = array_filter($output, function ($item) {
46236c0b2b4SAndreas Gohr            return !file_exists($item);
46336c0b2b4SAndreas Gohr        });
46436c0b2b4SAndreas Gohr        sort($output);
46536c0b2b4SAndreas Gohr
46636c0b2b4SAndreas Gohr        if (!count($output)) {
46736c0b2b4SAndreas Gohr            $this->info('No deleted files found');
46836c0b2b4SAndreas Gohr            return 0;
46936c0b2b4SAndreas Gohr        }
47036c0b2b4SAndreas Gohr
47136c0b2b4SAndreas Gohr        $content = "# This is a list of files that were present in previous releases\n" .
47236c0b2b4SAndreas Gohr            "# but were removed later. They should not exist in your installation.\n" .
47336c0b2b4SAndreas Gohr            join("\n", $output) . "\n";
47436c0b2b4SAndreas Gohr
47536c0b2b4SAndreas Gohr        file_put_contents('deleted.files', $content);
47636c0b2b4SAndreas Gohr        $this->success('written deleted.files');
47736c0b2b4SAndreas Gohr        return 0;
47836c0b2b4SAndreas Gohr    }
4798b06c9ddSAndreas Gohr
4808b06c9ddSAndreas Gohr    /**
481c5c85a97SAndreas Gohr     * Remove files that shouldn't be here anymore
4828b06c9ddSAndreas Gohr     */
4831a23d1dbSAndreas Gohr    protected function cmdRmObsolete()
4848b06c9ddSAndreas Gohr    {
485c5c85a97SAndreas Gohr        $this->deleteFile('_test/general.test.php');
486c5c85a97SAndreas Gohr        $this->deleteFile('.travis.yml');
4878b06c9ddSAndreas Gohr
488c5c85a97SAndreas Gohr        return 0;
4898b06c9ddSAndreas Gohr    }
4908b06c9ddSAndreas Gohr
4911a23d1dbSAndreas Gohr    /**
4921a23d1dbSAndreas Gohr     * Download a remote icon
4931a23d1dbSAndreas Gohr     *
4941a23d1dbSAndreas Gohr     * @param string $ident
4951a23d1dbSAndreas Gohr     * @param string $save
496*92738407SAndreas Gohr     * @param bool $keep
4971a23d1dbSAndreas Gohr     * @return int
4981a23d1dbSAndreas Gohr     * @throws Exception
4991a23d1dbSAndreas Gohr     */
500*92738407SAndreas Gohr    protected function cmdDownloadSVG($ident, $save = '', $keep = false)
5011a23d1dbSAndreas Gohr    {
5021a23d1dbSAndreas Gohr        $svg = new SVGIcon($this);
503*92738407SAndreas Gohr        $svg->keepNamespace($keep);
5041a23d1dbSAndreas Gohr        return (int)$svg->downloadRemoteIcon($ident, $save);
5051a23d1dbSAndreas Gohr    }
5061a23d1dbSAndreas Gohr
5071a23d1dbSAndreas Gohr    /**
5081a23d1dbSAndreas Gohr     * @param string $file
509*92738407SAndreas Gohr     * @param bool $keep
5101a23d1dbSAndreas Gohr     * @return int
5111a23d1dbSAndreas Gohr     * @throws Exception
5121a23d1dbSAndreas Gohr     */
513*92738407SAndreas Gohr    protected function cmdCleanSVG($file, $keep = false)
5141a23d1dbSAndreas Gohr    {
5151a23d1dbSAndreas Gohr        $svg = new SVGIcon($this);
516*92738407SAndreas Gohr        $svg->keepNamespace($keep);
5171a23d1dbSAndreas Gohr        return (int)$svg->cleanSVGFile($file);
5181a23d1dbSAndreas Gohr    }
5191a23d1dbSAndreas Gohr
520c5c85a97SAndreas Gohr    //endregion
52136c0b2b4SAndreas Gohr}
522