xref: /plugin/dev/cli.php (revision 1a23d1dba84779f6c148b47f8352f1ea9cc5b271)
136c0b2b4SAndreas Gohr#!/usr/bin/env php
236c0b2b4SAndreas Gohr<?php
336c0b2b4SAndreas Gohr
436c0b2b4SAndreas Gohruse dokuwiki\Extension\CLIPlugin;
536c0b2b4SAndreas Gohruse dokuwiki\Extension\PluginController;
6*1a23d1dbSAndreas 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.');
54*1a23d1dbSAndreas Gohr
55*1a23d1dbSAndreas Gohr        $prefixes = array_keys(SVGIcon::SOURCES);
56*1a23d1dbSAndreas Gohr        array_walk(
57*1a23d1dbSAndreas Gohr            $prefixes,
58*1a23d1dbSAndreas Gohr            function (&$item) {
59*1a23d1dbSAndreas Gohr                $item = $this->colors->wrap($item, $this->colors::C_BROWN);
60*1a23d1dbSAndreas Gohr            }
61*1a23d1dbSAndreas Gohr        );
62*1a23d1dbSAndreas Gohr
63*1a23d1dbSAndreas Gohr        $options->registerCommand('downloadSvg', 'Download an SVG file from a known icon repository.');
64*1a23d1dbSAndreas Gohr        $options->registerArgument('prefix:name',
65*1a23d1dbSAndreas Gohr            'Colon-prefixed name of the icon. Available prefixes: ' . join(', ', $prefixes), true, 'downloadSvg');
66*1a23d1dbSAndreas Gohr        $options->registerArgument('output', 'File to save, defaults to <name>.svg in current dir', false,
67*1a23d1dbSAndreas Gohr            'downloadSvg');
68*1a23d1dbSAndreas Gohr
69*1a23d1dbSAndreas Gohr        $options->registerCommand('cleanSvg', 'Clean an existing SVG file to reduce file size.');
70*1a23d1dbSAndreas Gohr        $options->registerArgument('file', 'The file to clean (will be overwritten)', true, 'cleanSvg');
7136c0b2b4SAndreas Gohr    }
7236c0b2b4SAndreas Gohr
7336c0b2b4SAndreas Gohr    /** @inheritDoc */
7436c0b2b4SAndreas Gohr    protected function main(Options $options)
7536c0b2b4SAndreas Gohr    {
76*1a23d1dbSAndreas Gohr        $args = $options->getArgs();
77*1a23d1dbSAndreas Gohr
7836c0b2b4SAndreas Gohr        switch ($options->getCmd()) {
7936c0b2b4SAndreas Gohr            case 'init':
8036c0b2b4SAndreas Gohr                return $this->cmdInit();
8136c0b2b4SAndreas Gohr            case 'addTest':
8236c0b2b4SAndreas Gohr                $test = array_shift($args);
8336c0b2b4SAndreas Gohr                return $this->cmdAddTest($test);
8436c0b2b4SAndreas Gohr            case 'addConf':
8536c0b2b4SAndreas Gohr                return $this->cmdAddConf();
8636c0b2b4SAndreas Gohr            case 'addLang':
8736c0b2b4SAndreas Gohr                return $this->cmdAddLang();
8836c0b2b4SAndreas Gohr            case 'addComponent':
8936c0b2b4SAndreas Gohr                $type = array_shift($args);
9036c0b2b4SAndreas Gohr                $component = array_shift($args);
9136c0b2b4SAndreas Gohr                return $this->cmdAddComponent($type, $component);
9236c0b2b4SAndreas Gohr            case 'deletedFiles':
9336c0b2b4SAndreas Gohr                return $this->cmdDeletedFiles();
94c5c85a97SAndreas Gohr            case 'rmObsolete':
95*1a23d1dbSAndreas Gohr                return $this->cmdRmObsolete();
96*1a23d1dbSAndreas Gohr            case 'downloadSvg':
97*1a23d1dbSAndreas Gohr                $ident = array_shift($args);
98*1a23d1dbSAndreas Gohr                $save = array_shift($args);
99*1a23d1dbSAndreas Gohr                return $this->cmdDownloadSVG($ident, $save);
100*1a23d1dbSAndreas Gohr            case 'cleanSvg':
101*1a23d1dbSAndreas Gohr                $file = array_shift($args);
102*1a23d1dbSAndreas Gohr                return $this->cmdCleanSVG($file);
10336c0b2b4SAndreas Gohr            default:
104*1a23d1dbSAndreas Gohr                $this->error('Unknown command');
10536c0b2b4SAndreas Gohr                echo $options->help();
10636c0b2b4SAndreas Gohr                return 0;
10736c0b2b4SAndreas Gohr        }
10836c0b2b4SAndreas Gohr    }
10936c0b2b4SAndreas Gohr
11036c0b2b4SAndreas Gohr    /**
11136c0b2b4SAndreas Gohr     * Get the extension name from the current working directory
11236c0b2b4SAndreas Gohr     *
11336c0b2b4SAndreas Gohr     * @throws CliException if something's wrong
11436c0b2b4SAndreas Gohr     * @param string $dir
11536c0b2b4SAndreas Gohr     * @return string[] name, type
11636c0b2b4SAndreas Gohr     */
11736c0b2b4SAndreas Gohr    protected function getTypedNameFromDir($dir)
11836c0b2b4SAndreas Gohr    {
11936c0b2b4SAndreas Gohr        $pdir = fullpath(DOKU_PLUGIN);
12036c0b2b4SAndreas Gohr        $tdir = fullpath(tpl_incdir() . '../');
12136c0b2b4SAndreas Gohr
12236c0b2b4SAndreas Gohr        if (strpos($dir, $pdir) === 0) {
12336c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($pdir));
12436c0b2b4SAndreas Gohr            $type = 'plugin';
12536c0b2b4SAndreas Gohr        } elseif (strpos($dir, $tdir) === 0) {
12636c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($tdir));
12736c0b2b4SAndreas Gohr            $type = 'template';
12836c0b2b4SAndreas Gohr        } else {
12936c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be in plugin or template directory');
13036c0b2b4SAndreas Gohr        }
13136c0b2b4SAndreas Gohr
13236c0b2b4SAndreas Gohr        $ldir = trim($ldir, '/');
13336c0b2b4SAndreas Gohr
13436c0b2b4SAndreas Gohr        if (strpos($ldir, '/') !== false) {
13536c0b2b4SAndreas Gohr            throw new CliException('Current directory has to be main extension directory');
13636c0b2b4SAndreas Gohr        }
13736c0b2b4SAndreas Gohr
13836c0b2b4SAndreas Gohr        return [$ldir, $type];
13936c0b2b4SAndreas Gohr    }
14036c0b2b4SAndreas Gohr
14136c0b2b4SAndreas Gohr    /**
14236c0b2b4SAndreas Gohr     * Interactively ask for a value from the user
14336c0b2b4SAndreas Gohr     *
14436c0b2b4SAndreas Gohr     * @param string $prompt
14536c0b2b4SAndreas Gohr     * @param bool $cache cache given value for next time?
14636c0b2b4SAndreas Gohr     * @return string
14736c0b2b4SAndreas Gohr     */
14836c0b2b4SAndreas Gohr    protected function readLine($prompt, $cache = false)
14936c0b2b4SAndreas Gohr    {
15036c0b2b4SAndreas Gohr        $value = '';
15136c0b2b4SAndreas Gohr        $default = '';
15236c0b2b4SAndreas Gohr        $cachename = getCacheName($prompt, '.readline');
15336c0b2b4SAndreas Gohr        if ($cache && file_exists($cachename)) {
15436c0b2b4SAndreas Gohr            $default = file_get_contents($cachename);
15536c0b2b4SAndreas Gohr        }
15636c0b2b4SAndreas Gohr
15736c0b2b4SAndreas Gohr        while ($value === '') {
15836c0b2b4SAndreas Gohr            echo $prompt;
15936c0b2b4SAndreas Gohr            if ($default) echo ' [' . $default . ']';
16036c0b2b4SAndreas Gohr            echo ': ';
16136c0b2b4SAndreas Gohr
16236c0b2b4SAndreas Gohr            $fh = fopen('php://stdin', 'r');
16336c0b2b4SAndreas Gohr            $value = trim(fgets($fh));
16436c0b2b4SAndreas Gohr            fclose($fh);
16536c0b2b4SAndreas Gohr
16636c0b2b4SAndreas Gohr            if ($value === '') $value = $default;
16736c0b2b4SAndreas Gohr        }
16836c0b2b4SAndreas Gohr
16936c0b2b4SAndreas Gohr        if ($cache) {
17036c0b2b4SAndreas Gohr            file_put_contents($cachename, $value);
17136c0b2b4SAndreas Gohr        }
17236c0b2b4SAndreas Gohr
17336c0b2b4SAndreas Gohr        return $value;
17436c0b2b4SAndreas Gohr    }
17536c0b2b4SAndreas Gohr
17636c0b2b4SAndreas Gohr    /**
17736c0b2b4SAndreas Gohr     * Download a skeleton file and do the replacements
17836c0b2b4SAndreas Gohr     *
17936c0b2b4SAndreas Gohr     * @param string $skel Skeleton relative to the skel dir in the repo
18036c0b2b4SAndreas Gohr     * @param string $target Target file relative to the main directory
18136c0b2b4SAndreas Gohr     * @param array $replacements
18236c0b2b4SAndreas Gohr     */
18336c0b2b4SAndreas Gohr    protected function loadSkeleton($skel, $target, $replacements)
18436c0b2b4SAndreas Gohr    {
18536c0b2b4SAndreas Gohr        if (file_exists($target)) {
18636c0b2b4SAndreas Gohr            $this->error($target . ' already exists');
18736c0b2b4SAndreas Gohr            return;
18836c0b2b4SAndreas Gohr        }
18936c0b2b4SAndreas Gohr
19036c0b2b4SAndreas Gohr        $base = 'https://raw.githubusercontent.com/dokufreaks/dokuwiki-plugin-wizard/master/skel/';
19136c0b2b4SAndreas Gohr        $http = new \dokuwiki\HTTP\DokuHTTPClient();
19236c0b2b4SAndreas Gohr        $content = $http->get($base . $skel);
19336c0b2b4SAndreas Gohr
19436c0b2b4SAndreas Gohr        $content = str_replace(
19536c0b2b4SAndreas Gohr            array_keys($replacements),
19636c0b2b4SAndreas Gohr            array_values($replacements),
19736c0b2b4SAndreas Gohr            $content
19836c0b2b4SAndreas Gohr        );
19936c0b2b4SAndreas Gohr
20036c0b2b4SAndreas Gohr        io_makeFileDir($target);
20136c0b2b4SAndreas Gohr        file_put_contents($target, $content);
20236c0b2b4SAndreas Gohr        $this->success('Added ' . $target);
20336c0b2b4SAndreas Gohr    }
20436c0b2b4SAndreas Gohr
20536c0b2b4SAndreas Gohr    /**
20636c0b2b4SAndreas Gohr     * Prepare the string replacements
20736c0b2b4SAndreas Gohr     *
20836c0b2b4SAndreas Gohr     * @param array $replacements override defaults
20936c0b2b4SAndreas Gohr     * @return array
21036c0b2b4SAndreas Gohr     */
21136c0b2b4SAndreas Gohr    protected function prepareReplacements($replacements = [])
21236c0b2b4SAndreas Gohr    {
21336c0b2b4SAndreas Gohr        // defaults
21436c0b2b4SAndreas Gohr        $data = [
21536c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => '',
21636c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => '',
21736c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => '',
21836c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => '',
21936c0b2b4SAndreas Gohr            '@@PLUGIN_URL@@' => '',
22036c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => '',
22136c0b2b4SAndreas Gohr            '@@INSTALL_DIR@@' => 'plugins',
22236c0b2b4SAndreas Gohr            '@@DATE@@' => date('Y-m-d'),
22336c0b2b4SAndreas Gohr        ];
22436c0b2b4SAndreas Gohr
22536c0b2b4SAndreas Gohr        // load from existing plugin.info
22636c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
22736c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
22836c0b2b4SAndreas Gohr        if (file_exists("$type.info.txt")) {
22936c0b2b4SAndreas Gohr            $info = confToHash("$type.info.txt");
23036c0b2b4SAndreas Gohr            $data['@@AUTHOR_NAME@@'] = $info['author'];
23136c0b2b4SAndreas Gohr            $data['@@AUTHOR_MAIL@@'] = $info['email'];
23236c0b2b4SAndreas Gohr            $data['@@PLUGIN_DESC@@'] = $info['desc'];
23336c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] = $info['url'];
23436c0b2b4SAndreas Gohr        }
23536c0b2b4SAndreas Gohr        $data['@@PLUGIN_NAME@@'] = $name;
23636c0b2b4SAndreas Gohr        $data['@@PLUGIN_TYPE@@'] = $type;
23736c0b2b4SAndreas Gohr
23836c0b2b4SAndreas Gohr        if ($type == 'template') {
23936c0b2b4SAndreas Gohr            $data['@@INSTALL_DIR@@'] = 'tpl';
24036c0b2b4SAndreas Gohr        }
24136c0b2b4SAndreas Gohr
24236c0b2b4SAndreas Gohr        // merge given overrides
24336c0b2b4SAndreas Gohr        $data = array_merge($data, $replacements);
24436c0b2b4SAndreas Gohr
24536c0b2b4SAndreas Gohr        // set inherited defaults
24636c0b2b4SAndreas Gohr        if (empty($data['@@PLUGIN_URL@@'])) {
24736c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] =
24836c0b2b4SAndreas Gohr                'https://www.dokuwiki.org/' .
24936c0b2b4SAndreas Gohr                $data['@@PLUGIN_TYPE@@'] . ':' .
25036c0b2b4SAndreas Gohr                $data['@@PLUGIN_NAME@@'];
25136c0b2b4SAndreas Gohr        }
25236c0b2b4SAndreas Gohr
25336c0b2b4SAndreas Gohr        return $data;
25436c0b2b4SAndreas Gohr    }
25536c0b2b4SAndreas Gohr
25636c0b2b4SAndreas Gohr    /**
25736c0b2b4SAndreas Gohr     * Replacements needed for action components.
25836c0b2b4SAndreas Gohr     *
25936c0b2b4SAndreas Gohr     * Not cool but that' what we need currently
26036c0b2b4SAndreas Gohr     *
26136c0b2b4SAndreas Gohr     * @return string[]
26236c0b2b4SAndreas Gohr     */
26336c0b2b4SAndreas Gohr    protected function actionReplacements()
26436c0b2b4SAndreas Gohr    {
26536c0b2b4SAndreas Gohr        $fn = 'handleEventName';
26636c0b2b4SAndreas Gohr        $register = '        $controller->register_hook(\'EVENT_NAME\', \'AFTER|BEFORE\', $this, \'' . $fn . '\');';
26736c0b2b4SAndreas Gohr        $handler = '    public function ' . $fn . '(Doku_Event $event, $param)' . "\n"
26836c0b2b4SAndreas Gohr            . "    {\n"
26936c0b2b4SAndreas Gohr            . "    }\n";
27036c0b2b4SAndreas Gohr
27136c0b2b4SAndreas Gohr        return [
27236c0b2b4SAndreas Gohr            '@@REGISTER@@' => $register . "\n   ",
27336c0b2b4SAndreas Gohr            '@@HANDLERS@@' => $handler,
27436c0b2b4SAndreas Gohr        ];
27536c0b2b4SAndreas Gohr    }
27636c0b2b4SAndreas Gohr
27736c0b2b4SAndreas Gohr    /**
278c5c85a97SAndreas Gohr     * Delete the given file if it exists
279c5c85a97SAndreas Gohr     *
280c5c85a97SAndreas Gohr     * @param string $file
281c5c85a97SAndreas Gohr     */
282c5c85a97SAndreas Gohr    protected function deleteFile($file)
283c5c85a97SAndreas Gohr    {
284c5c85a97SAndreas Gohr        if (!file_exists($file)) return;
285c5c85a97SAndreas Gohr        if (@unlink($file)) {
286c5c85a97SAndreas Gohr            $this->success('Delete ' . $file);
287c5c85a97SAndreas Gohr        }
288c5c85a97SAndreas Gohr    }
289c5c85a97SAndreas Gohr
290c5c85a97SAndreas Gohr    /**
291c5c85a97SAndreas Gohr     * Run git with the given arguments and return the output
292c5c85a97SAndreas Gohr     *
293c5c85a97SAndreas Gohr     * @throws CliException when the command can't be run
294c5c85a97SAndreas Gohr     * @param string ...$args
295c5c85a97SAndreas Gohr     * @return string[]
296c5c85a97SAndreas Gohr     */
297c5c85a97SAndreas Gohr    protected function git(...$args)
298c5c85a97SAndreas Gohr    {
299c5c85a97SAndreas Gohr        $args = array_map('escapeshellarg', $args);
300c5c85a97SAndreas Gohr        $cmd = 'git ' . join(' ', $args);
301c5c85a97SAndreas Gohr        $output = [];
302c5c85a97SAndreas Gohr        $result = 0;
303c5c85a97SAndreas Gohr
304c5c85a97SAndreas Gohr        $this->info($cmd);
305c5c85a97SAndreas Gohr        $last = exec($cmd, $output, $result);
306c5c85a97SAndreas Gohr        if ($last === false || $result !== 0) {
307c5c85a97SAndreas Gohr            throw new CliException('Running git failed');
308c5c85a97SAndreas Gohr        }
309c5c85a97SAndreas Gohr
310c5c85a97SAndreas Gohr        return $output;
311c5c85a97SAndreas Gohr    }
312c5c85a97SAndreas Gohr
313c5c85a97SAndreas Gohr    // region Commands
314c5c85a97SAndreas Gohr
315c5c85a97SAndreas Gohr    /**
31636c0b2b4SAndreas Gohr     * Intialize the current directory as a plugin or template
31736c0b2b4SAndreas Gohr     *
31836c0b2b4SAndreas Gohr     * @return int
31936c0b2b4SAndreas Gohr     */
32036c0b2b4SAndreas Gohr    protected function cmdInit()
32136c0b2b4SAndreas Gohr    {
32236c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
32336c0b2b4SAndreas Gohr        if ((new FilesystemIterator($dir))->valid()) {
32436c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be empty');
32536c0b2b4SAndreas Gohr        }
32636c0b2b4SAndreas Gohr
32736c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
32836c0b2b4SAndreas Gohr        $user = $this->readLine('Your Name', true);
32936c0b2b4SAndreas Gohr        $mail = $this->readLine('Your E-Mail', true);
33036c0b2b4SAndreas Gohr        $desc = $this->readLine('Short description');
33136c0b2b4SAndreas Gohr
33236c0b2b4SAndreas Gohr        $replacements = [
33336c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => $user,
33436c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => $mail,
33536c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => $name,
33636c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => $desc,
33736c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => $type,
33836c0b2b4SAndreas Gohr        ];
33936c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
34036c0b2b4SAndreas Gohr
34136c0b2b4SAndreas Gohr        $this->loadSkeleton('info.skel', $type . '.info.txt', $replacements);
34236c0b2b4SAndreas Gohr        $this->loadSkeleton('README.skel', 'README', $replacements); // fixme needs to be type specific
34336c0b2b4SAndreas Gohr        $this->loadSkeleton('LICENSE.skel', 'LICENSE', $replacements);
34436c0b2b4SAndreas Gohr
3458b06c9ddSAndreas Gohr        try {
3468b06c9ddSAndreas Gohr            $this->git('init');
3478b06c9ddSAndreas Gohr        } catch (CliException $e) {
3488b06c9ddSAndreas Gohr            $this->error($e->getMessage());
3498b06c9ddSAndreas Gohr        }
3508b06c9ddSAndreas Gohr
35136c0b2b4SAndreas Gohr        return 0;
35236c0b2b4SAndreas Gohr    }
35336c0b2b4SAndreas Gohr
35436c0b2b4SAndreas Gohr    /**
35536c0b2b4SAndreas Gohr     * Add test framework
35636c0b2b4SAndreas Gohr     *
35736c0b2b4SAndreas Gohr     * @param string $test Name of the Test to add
35836c0b2b4SAndreas Gohr     * @return int
35936c0b2b4SAndreas Gohr     */
36036c0b2b4SAndreas Gohr    protected function cmdAddTest($test = '')
36136c0b2b4SAndreas Gohr    {
36236c0b2b4SAndreas Gohr        $test = ucfirst(strtolower($test));
36336c0b2b4SAndreas Gohr
36436c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements(['@@TEST@@' => $test]);
36536c0b2b4SAndreas Gohr        $this->loadSkeleton('.github/workflows/phpTestLinux.skel', '.github/workflows/phpTestLinux.yml', $replacements);
36636c0b2b4SAndreas Gohr        if ($test) {
36736c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/StandardTest.skel', '_test/' . $test . 'Test.php', $replacements);
36836c0b2b4SAndreas Gohr        } else {
36936c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/GeneralTest.skel', '_test/GeneralTest.php', $replacements);
37036c0b2b4SAndreas Gohr        }
37136c0b2b4SAndreas Gohr
37236c0b2b4SAndreas Gohr        return 0;
37336c0b2b4SAndreas Gohr    }
37436c0b2b4SAndreas Gohr
37536c0b2b4SAndreas Gohr    /**
37636c0b2b4SAndreas Gohr     * Add configuration
37736c0b2b4SAndreas Gohr     *
37836c0b2b4SAndreas Gohr     * @return int
37936c0b2b4SAndreas Gohr     */
38036c0b2b4SAndreas Gohr    protected function cmdAddConf()
38136c0b2b4SAndreas Gohr    {
38236c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
38336c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/default.skel', 'conf/default.php', $replacements);
38436c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/metadata.skel', 'conf/metadata.php', $replacements);
38536c0b2b4SAndreas Gohr        if (is_dir('lang')) {
38636c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
38736c0b2b4SAndreas Gohr        }
38836c0b2b4SAndreas Gohr
38936c0b2b4SAndreas Gohr        return 0;
39036c0b2b4SAndreas Gohr    }
39136c0b2b4SAndreas Gohr
39236c0b2b4SAndreas Gohr    /**
39336c0b2b4SAndreas Gohr     * Add language
39436c0b2b4SAndreas Gohr     *
39536c0b2b4SAndreas Gohr     * @return int
39636c0b2b4SAndreas Gohr     */
39736c0b2b4SAndreas Gohr    protected function cmdAddLang()
39836c0b2b4SAndreas Gohr    {
39936c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
40036c0b2b4SAndreas Gohr        $this->loadSkeleton('lang/lang.skel', 'lang/en/lang.php', $replacements);
40136c0b2b4SAndreas Gohr        if (is_dir('conf')) {
40236c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
40336c0b2b4SAndreas Gohr        }
40436c0b2b4SAndreas Gohr
40536c0b2b4SAndreas Gohr        return 0;
40636c0b2b4SAndreas Gohr    }
40736c0b2b4SAndreas Gohr
40836c0b2b4SAndreas Gohr    /**
40936c0b2b4SAndreas Gohr     * Add another component to the plugin
41036c0b2b4SAndreas Gohr     *
41136c0b2b4SAndreas Gohr     * @param string $type
41236c0b2b4SAndreas Gohr     * @param string $component
41336c0b2b4SAndreas Gohr     */
41436c0b2b4SAndreas Gohr    protected function cmdAddComponent($type, $component = '')
41536c0b2b4SAndreas Gohr    {
41636c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
41736c0b2b4SAndreas Gohr        list($plugin, $extension) = $this->getTypedNameFromDir($dir);
41836c0b2b4SAndreas Gohr        if ($extension != 'plugin') throw  new CliException('Components can only be added to plugins');
41936c0b2b4SAndreas Gohr        if (!in_array($type, PluginController::PLUGIN_TYPES)) {
42036c0b2b4SAndreas Gohr            throw new CliException('Invalid type ' . $type);
42136c0b2b4SAndreas Gohr        }
42236c0b2b4SAndreas Gohr
42336c0b2b4SAndreas Gohr        if ($component) {
42436c0b2b4SAndreas Gohr            $path = $type . '/' . $component . '.php';
42536c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin . '_' . $component;
42636c0b2b4SAndreas Gohr            $self = $plugin . '_' . $component;
42736c0b2b4SAndreas Gohr        } else {
42836c0b2b4SAndreas Gohr            $path = $type . '.php';
42936c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin;
43036c0b2b4SAndreas Gohr            $self = $plugin;
43136c0b2b4SAndreas Gohr        }
43236c0b2b4SAndreas Gohr
43336c0b2b4SAndreas Gohr        $replacements = $this->actionReplacements();
43436c0b2b4SAndreas Gohr        $replacements['@@PLUGIN_COMPONENT_NAME@@'] = $class;
43536c0b2b4SAndreas Gohr        $replacements['@@SYNTAX_COMPONENT_NAME@@'] = $self;
43636c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
43736c0b2b4SAndreas Gohr        $this->loadSkeleton($type . '.skel', $path, $replacements);
43836c0b2b4SAndreas Gohr
43936c0b2b4SAndreas Gohr        return 0;
44036c0b2b4SAndreas Gohr    }
44136c0b2b4SAndreas Gohr
44236c0b2b4SAndreas Gohr    /**
44336c0b2b4SAndreas Gohr     * Generate a list of deleted files from git
44436c0b2b4SAndreas Gohr     *
44536c0b2b4SAndreas Gohr     * @link https://stackoverflow.com/a/6018049/172068
44636c0b2b4SAndreas Gohr     */
44736c0b2b4SAndreas Gohr    protected function cmdDeletedFiles()
44836c0b2b4SAndreas Gohr    {
4498b06c9ddSAndreas Gohr        if (!is_dir('.git')) throw new CliException('This extension seems not to be managed by git');
45036c0b2b4SAndreas Gohr
4518b06c9ddSAndreas Gohr        $output = $this->git('log', '--no-renames', '--pretty=format:', '--name-only', '--diff-filter=D');
45236c0b2b4SAndreas Gohr        $output = array_map('trim', $output);
45336c0b2b4SAndreas Gohr        $output = array_filter($output);
45436c0b2b4SAndreas Gohr        $output = array_unique($output);
45536c0b2b4SAndreas Gohr        $output = array_filter($output, function ($item) {
45636c0b2b4SAndreas Gohr            return !file_exists($item);
45736c0b2b4SAndreas Gohr        });
45836c0b2b4SAndreas Gohr        sort($output);
45936c0b2b4SAndreas Gohr
46036c0b2b4SAndreas Gohr        if (!count($output)) {
46136c0b2b4SAndreas Gohr            $this->info('No deleted files found');
46236c0b2b4SAndreas Gohr            return 0;
46336c0b2b4SAndreas Gohr        }
46436c0b2b4SAndreas Gohr
46536c0b2b4SAndreas Gohr        $content = "# This is a list of files that were present in previous releases\n" .
46636c0b2b4SAndreas Gohr            "# but were removed later. They should not exist in your installation.\n" .
46736c0b2b4SAndreas Gohr            join("\n", $output) . "\n";
46836c0b2b4SAndreas Gohr
46936c0b2b4SAndreas Gohr        file_put_contents('deleted.files', $content);
47036c0b2b4SAndreas Gohr        $this->success('written deleted.files');
47136c0b2b4SAndreas Gohr        return 0;
47236c0b2b4SAndreas Gohr    }
4738b06c9ddSAndreas Gohr
4748b06c9ddSAndreas Gohr    /**
475c5c85a97SAndreas Gohr     * Remove files that shouldn't be here anymore
4768b06c9ddSAndreas Gohr     */
477*1a23d1dbSAndreas Gohr    protected function cmdRmObsolete()
4788b06c9ddSAndreas Gohr    {
479c5c85a97SAndreas Gohr        $this->deleteFile('_test/general.test.php');
480c5c85a97SAndreas Gohr        $this->deleteFile('.travis.yml');
4818b06c9ddSAndreas Gohr
482c5c85a97SAndreas Gohr        return 0;
4838b06c9ddSAndreas Gohr    }
4848b06c9ddSAndreas Gohr
485*1a23d1dbSAndreas Gohr    /**
486*1a23d1dbSAndreas Gohr     * Download a remote icon
487*1a23d1dbSAndreas Gohr     *
488*1a23d1dbSAndreas Gohr     * @param string $ident
489*1a23d1dbSAndreas Gohr     * @param string $save
490*1a23d1dbSAndreas Gohr     * @return int
491*1a23d1dbSAndreas Gohr     * @throws Exception
492*1a23d1dbSAndreas Gohr     */
493*1a23d1dbSAndreas Gohr    protected function cmdDownloadSVG($ident, $save = '')
494*1a23d1dbSAndreas Gohr    {
495*1a23d1dbSAndreas Gohr        $svg = new SVGIcon($this);
496*1a23d1dbSAndreas Gohr        return (int)$svg->downloadRemoteIcon($ident, $save);
497*1a23d1dbSAndreas Gohr    }
498*1a23d1dbSAndreas Gohr
499*1a23d1dbSAndreas Gohr    /**
500*1a23d1dbSAndreas Gohr     * @param string $file
501*1a23d1dbSAndreas Gohr     * @return int
502*1a23d1dbSAndreas Gohr     * @throws Exception
503*1a23d1dbSAndreas Gohr     */
504*1a23d1dbSAndreas Gohr    protected function cmdCleanSVG($file)
505*1a23d1dbSAndreas Gohr    {
506*1a23d1dbSAndreas Gohr        $svg = new SVGIcon($this);
507*1a23d1dbSAndreas Gohr        return (int)$svg->cleanSVGFile($file);
508*1a23d1dbSAndreas Gohr    }
509*1a23d1dbSAndreas Gohr
510c5c85a97SAndreas Gohr    //endregion
51136c0b2b4SAndreas Gohr}
512