xref: /plugin/dev/cli.php (revision f2576912a59238828e1ac61840fc15c82fc7beeb)
136c0b2b4SAndreas Gohr#!/usr/bin/env php
236c0b2b4SAndreas Gohr<?php
336c0b2b4SAndreas Gohr
436c0b2b4SAndreas Gohruse dokuwiki\Extension\CLIPlugin;
536c0b2b4SAndreas Gohruse dokuwiki\Extension\PluginController;
636c0b2b4SAndreas Gohruse splitbrain\phpcli\Exception as CliException;
736c0b2b4SAndreas Gohruse splitbrain\phpcli\Options;
836c0b2b4SAndreas Gohr
936c0b2b4SAndreas Gohr/**
1036c0b2b4SAndreas Gohr * @license GPL2
1136c0b2b4SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
1236c0b2b4SAndreas Gohr */
1336c0b2b4SAndreas Gohrclass cli_plugin_dev extends CLIPlugin
1436c0b2b4SAndreas Gohr{
1536c0b2b4SAndreas Gohr
1636c0b2b4SAndreas Gohr    /**
1736c0b2b4SAndreas Gohr     * Register options and arguments on the given $options object
1836c0b2b4SAndreas Gohr     *
1936c0b2b4SAndreas Gohr     * @param Options $options
2036c0b2b4SAndreas Gohr     * @return void
2136c0b2b4SAndreas Gohr     */
2236c0b2b4SAndreas Gohr    protected function setup(Options $options)
2336c0b2b4SAndreas Gohr    {
24*f2576912SAndreas Gohr        $options->useCompactHelp();
2536c0b2b4SAndreas Gohr        $options->setHelp(
26*f2576912SAndreas Gohr            "CLI to help with DokuWiki plugin and template development.\n\n" .
2736c0b2b4SAndreas Gohr            "Run this script from within the extension's directory."
2836c0b2b4SAndreas Gohr        );
2936c0b2b4SAndreas Gohr
30*f2576912SAndreas Gohr        $options->registerCommand('init', 'Initialize a new plugin or template in the current (empty) directory.');
31*f2576912SAndreas Gohr        $options->registerCommand('addTest', 'Add the testing framework files and a test. (_test/)');
32*f2576912SAndreas Gohr        $options->registerArgument('test', 'Optional name of the new test. Defaults to the general test.', false,
33*f2576912SAndreas Gohr            'addTest');
34*f2576912SAndreas Gohr        $options->registerCommand('addConf', 'Add the configuration files. (conf/)');
35*f2576912SAndreas Gohr        $options->registerCommand('addLang', 'Add the language files. (lang/)');
3636c0b2b4SAndreas Gohr
37*f2576912SAndreas Gohr        $types = PluginController::PLUGIN_TYPES;
38*f2576912SAndreas Gohr        array_walk(
39*f2576912SAndreas Gohr            $types,
40*f2576912SAndreas Gohr            function (&$item) {
41*f2576912SAndreas Gohr                $item = $this->colors->wrap($item, $this->colors::C_BROWN);
42*f2576912SAndreas Gohr            }
4336c0b2b4SAndreas Gohr        );
4436c0b2b4SAndreas Gohr
45*f2576912SAndreas Gohr        $options->registerCommand('addComponent', 'Add a new plugin component.');
46*f2576912SAndreas Gohr        $options->registerArgument('type', 'Type of the component. Needs to be one of ' . join(', ', $types), true,
47*f2576912SAndreas Gohr            'addComponent');
48*f2576912SAndreas Gohr        $options->registerArgument('name', 'Optional name of the component. Defaults to a base component.', false,
49*f2576912SAndreas Gohr            'addComponent');
50*f2576912SAndreas Gohr
51*f2576912SAndreas Gohr        $options->registerCommand('deletedFiles', 'Create the list of deleted files based on the git history.');
52*f2576912SAndreas Gohr        $options->registerCommand('rmObsolete', 'Delete obsolete files.');
5336c0b2b4SAndreas Gohr    }
5436c0b2b4SAndreas Gohr
5536c0b2b4SAndreas Gohr    /** @inheritDoc */
5636c0b2b4SAndreas Gohr    protected function main(Options $options)
5736c0b2b4SAndreas Gohr    {
5836c0b2b4SAndreas Gohr        switch ($options->getCmd()) {
5936c0b2b4SAndreas Gohr            case 'init':
6036c0b2b4SAndreas Gohr                return $this->cmdInit();
6136c0b2b4SAndreas Gohr            case 'addTest':
6236c0b2b4SAndreas Gohr                $args = $options->getArgs();
6336c0b2b4SAndreas Gohr                $test = array_shift($args);
6436c0b2b4SAndreas Gohr                return $this->cmdAddTest($test);
6536c0b2b4SAndreas Gohr            case 'addConf':
6636c0b2b4SAndreas Gohr                return $this->cmdAddConf();
6736c0b2b4SAndreas Gohr            case 'addLang':
6836c0b2b4SAndreas Gohr                return $this->cmdAddLang();
6936c0b2b4SAndreas Gohr            case 'addComponent':
7036c0b2b4SAndreas Gohr                $args = $options->getArgs();
7136c0b2b4SAndreas Gohr                $type = array_shift($args);
7236c0b2b4SAndreas Gohr                $component = array_shift($args);
7336c0b2b4SAndreas Gohr                return $this->cmdAddComponent($type, $component);
7436c0b2b4SAndreas Gohr            case 'deletedFiles':
7536c0b2b4SAndreas Gohr                return $this->cmdDeletedFiles();
76c5c85a97SAndreas Gohr            case 'rmObsolete':
77c5c85a97SAndreas Gohr                return $this->rmObsolete();
7836c0b2b4SAndreas Gohr            default:
7936c0b2b4SAndreas Gohr                echo $options->help();
8036c0b2b4SAndreas Gohr                return 0;
8136c0b2b4SAndreas Gohr        }
8236c0b2b4SAndreas Gohr    }
8336c0b2b4SAndreas Gohr
8436c0b2b4SAndreas Gohr    /**
8536c0b2b4SAndreas Gohr     * Get the extension name from the current working directory
8636c0b2b4SAndreas Gohr     *
8736c0b2b4SAndreas Gohr     * @throws CliException if something's wrong
8836c0b2b4SAndreas Gohr     * @param string $dir
8936c0b2b4SAndreas Gohr     * @return string[] name, type
9036c0b2b4SAndreas Gohr     */
9136c0b2b4SAndreas Gohr    protected function getTypedNameFromDir($dir)
9236c0b2b4SAndreas Gohr    {
9336c0b2b4SAndreas Gohr        $pdir = fullpath(DOKU_PLUGIN);
9436c0b2b4SAndreas Gohr        $tdir = fullpath(tpl_incdir() . '../');
9536c0b2b4SAndreas Gohr
9636c0b2b4SAndreas Gohr        if (strpos($dir, $pdir) === 0) {
9736c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($pdir));
9836c0b2b4SAndreas Gohr            $type = 'plugin';
9936c0b2b4SAndreas Gohr        } elseif (strpos($dir, $tdir) === 0) {
10036c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($tdir));
10136c0b2b4SAndreas Gohr            $type = 'template';
10236c0b2b4SAndreas Gohr        } else {
10336c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be in plugin or template directory');
10436c0b2b4SAndreas Gohr        }
10536c0b2b4SAndreas Gohr
10636c0b2b4SAndreas Gohr        $ldir = trim($ldir, '/');
10736c0b2b4SAndreas Gohr
10836c0b2b4SAndreas Gohr        if (strpos($ldir, '/') !== false) {
10936c0b2b4SAndreas Gohr            throw new CliException('Current directory has to be main extension directory');
11036c0b2b4SAndreas Gohr        }
11136c0b2b4SAndreas Gohr
11236c0b2b4SAndreas Gohr        return [$ldir, $type];
11336c0b2b4SAndreas Gohr    }
11436c0b2b4SAndreas Gohr
11536c0b2b4SAndreas Gohr    /**
11636c0b2b4SAndreas Gohr     * Interactively ask for a value from the user
11736c0b2b4SAndreas Gohr     *
11836c0b2b4SAndreas Gohr     * @param string $prompt
11936c0b2b4SAndreas Gohr     * @param bool $cache cache given value for next time?
12036c0b2b4SAndreas Gohr     * @return string
12136c0b2b4SAndreas Gohr     */
12236c0b2b4SAndreas Gohr    protected function readLine($prompt, $cache = false)
12336c0b2b4SAndreas Gohr    {
12436c0b2b4SAndreas Gohr        $value = '';
12536c0b2b4SAndreas Gohr        $default = '';
12636c0b2b4SAndreas Gohr        $cachename = getCacheName($prompt, '.readline');
12736c0b2b4SAndreas Gohr        if ($cache && file_exists($cachename)) {
12836c0b2b4SAndreas Gohr            $default = file_get_contents($cachename);
12936c0b2b4SAndreas Gohr        }
13036c0b2b4SAndreas Gohr
13136c0b2b4SAndreas Gohr        while ($value === '') {
13236c0b2b4SAndreas Gohr            echo $prompt;
13336c0b2b4SAndreas Gohr            if ($default) echo ' [' . $default . ']';
13436c0b2b4SAndreas Gohr            echo ': ';
13536c0b2b4SAndreas Gohr
13636c0b2b4SAndreas Gohr            $fh = fopen('php://stdin', 'r');
13736c0b2b4SAndreas Gohr            $value = trim(fgets($fh));
13836c0b2b4SAndreas Gohr            fclose($fh);
13936c0b2b4SAndreas Gohr
14036c0b2b4SAndreas Gohr            if ($value === '') $value = $default;
14136c0b2b4SAndreas Gohr        }
14236c0b2b4SAndreas Gohr
14336c0b2b4SAndreas Gohr        if ($cache) {
14436c0b2b4SAndreas Gohr            file_put_contents($cachename, $value);
14536c0b2b4SAndreas Gohr        }
14636c0b2b4SAndreas Gohr
14736c0b2b4SAndreas Gohr        return $value;
14836c0b2b4SAndreas Gohr    }
14936c0b2b4SAndreas Gohr
15036c0b2b4SAndreas Gohr    /**
15136c0b2b4SAndreas Gohr     * Download a skeleton file and do the replacements
15236c0b2b4SAndreas Gohr     *
15336c0b2b4SAndreas Gohr     * @param string $skel Skeleton relative to the skel dir in the repo
15436c0b2b4SAndreas Gohr     * @param string $target Target file relative to the main directory
15536c0b2b4SAndreas Gohr     * @param array $replacements
15636c0b2b4SAndreas Gohr     */
15736c0b2b4SAndreas Gohr    protected function loadSkeleton($skel, $target, $replacements)
15836c0b2b4SAndreas Gohr    {
15936c0b2b4SAndreas Gohr        if (file_exists($target)) {
16036c0b2b4SAndreas Gohr            $this->error($target . ' already exists');
16136c0b2b4SAndreas Gohr            return;
16236c0b2b4SAndreas Gohr        }
16336c0b2b4SAndreas Gohr
16436c0b2b4SAndreas Gohr        $base = 'https://raw.githubusercontent.com/dokufreaks/dokuwiki-plugin-wizard/master/skel/';
16536c0b2b4SAndreas Gohr        $http = new \dokuwiki\HTTP\DokuHTTPClient();
16636c0b2b4SAndreas Gohr        $content = $http->get($base . $skel);
16736c0b2b4SAndreas Gohr
16836c0b2b4SAndreas Gohr        $content = str_replace(
16936c0b2b4SAndreas Gohr            array_keys($replacements),
17036c0b2b4SAndreas Gohr            array_values($replacements),
17136c0b2b4SAndreas Gohr            $content
17236c0b2b4SAndreas Gohr        );
17336c0b2b4SAndreas Gohr
17436c0b2b4SAndreas Gohr        io_makeFileDir($target);
17536c0b2b4SAndreas Gohr        file_put_contents($target, $content);
17636c0b2b4SAndreas Gohr        $this->success('Added ' . $target);
17736c0b2b4SAndreas Gohr    }
17836c0b2b4SAndreas Gohr
17936c0b2b4SAndreas Gohr    /**
18036c0b2b4SAndreas Gohr     * Prepare the string replacements
18136c0b2b4SAndreas Gohr     *
18236c0b2b4SAndreas Gohr     * @param array $replacements override defaults
18336c0b2b4SAndreas Gohr     * @return array
18436c0b2b4SAndreas Gohr     */
18536c0b2b4SAndreas Gohr    protected function prepareReplacements($replacements = [])
18636c0b2b4SAndreas Gohr    {
18736c0b2b4SAndreas Gohr        // defaults
18836c0b2b4SAndreas Gohr        $data = [
18936c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => '',
19036c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => '',
19136c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => '',
19236c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => '',
19336c0b2b4SAndreas Gohr            '@@PLUGIN_URL@@' => '',
19436c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => '',
19536c0b2b4SAndreas Gohr            '@@INSTALL_DIR@@' => 'plugins',
19636c0b2b4SAndreas Gohr            '@@DATE@@' => date('Y-m-d'),
19736c0b2b4SAndreas Gohr        ];
19836c0b2b4SAndreas Gohr
19936c0b2b4SAndreas Gohr        // load from existing plugin.info
20036c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
20136c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
20236c0b2b4SAndreas Gohr        if (file_exists("$type.info.txt")) {
20336c0b2b4SAndreas Gohr            $info = confToHash("$type.info.txt");
20436c0b2b4SAndreas Gohr            $data['@@AUTHOR_NAME@@'] = $info['author'];
20536c0b2b4SAndreas Gohr            $data['@@AUTHOR_MAIL@@'] = $info['email'];
20636c0b2b4SAndreas Gohr            $data['@@PLUGIN_DESC@@'] = $info['desc'];
20736c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] = $info['url'];
20836c0b2b4SAndreas Gohr        }
20936c0b2b4SAndreas Gohr        $data['@@PLUGIN_NAME@@'] = $name;
21036c0b2b4SAndreas Gohr        $data['@@PLUGIN_TYPE@@'] = $type;
21136c0b2b4SAndreas Gohr
21236c0b2b4SAndreas Gohr        if ($type == 'template') {
21336c0b2b4SAndreas Gohr            $data['@@INSTALL_DIR@@'] = 'tpl';
21436c0b2b4SAndreas Gohr        }
21536c0b2b4SAndreas Gohr
21636c0b2b4SAndreas Gohr        // merge given overrides
21736c0b2b4SAndreas Gohr        $data = array_merge($data, $replacements);
21836c0b2b4SAndreas Gohr
21936c0b2b4SAndreas Gohr        // set inherited defaults
22036c0b2b4SAndreas Gohr        if (empty($data['@@PLUGIN_URL@@'])) {
22136c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] =
22236c0b2b4SAndreas Gohr                'https://www.dokuwiki.org/' .
22336c0b2b4SAndreas Gohr                $data['@@PLUGIN_TYPE@@'] . ':' .
22436c0b2b4SAndreas Gohr                $data['@@PLUGIN_NAME@@'];
22536c0b2b4SAndreas Gohr        }
22636c0b2b4SAndreas Gohr
22736c0b2b4SAndreas Gohr        return $data;
22836c0b2b4SAndreas Gohr    }
22936c0b2b4SAndreas Gohr
23036c0b2b4SAndreas Gohr    /**
23136c0b2b4SAndreas Gohr     * Replacements needed for action components.
23236c0b2b4SAndreas Gohr     *
23336c0b2b4SAndreas Gohr     * Not cool but that' what we need currently
23436c0b2b4SAndreas Gohr     *
23536c0b2b4SAndreas Gohr     * @return string[]
23636c0b2b4SAndreas Gohr     */
23736c0b2b4SAndreas Gohr    protected function actionReplacements()
23836c0b2b4SAndreas Gohr    {
23936c0b2b4SAndreas Gohr        $fn = 'handleEventName';
24036c0b2b4SAndreas Gohr        $register = '        $controller->register_hook(\'EVENT_NAME\', \'AFTER|BEFORE\', $this, \'' . $fn . '\');';
24136c0b2b4SAndreas Gohr        $handler = '    public function ' . $fn . '(Doku_Event $event, $param)' . "\n"
24236c0b2b4SAndreas Gohr            . "    {\n"
24336c0b2b4SAndreas Gohr            . "    }\n";
24436c0b2b4SAndreas Gohr
24536c0b2b4SAndreas Gohr        return [
24636c0b2b4SAndreas Gohr            '@@REGISTER@@' => $register . "\n   ",
24736c0b2b4SAndreas Gohr            '@@HANDLERS@@' => $handler,
24836c0b2b4SAndreas Gohr        ];
24936c0b2b4SAndreas Gohr    }
25036c0b2b4SAndreas Gohr
25136c0b2b4SAndreas Gohr    /**
252c5c85a97SAndreas Gohr     * Delete the given file if it exists
253c5c85a97SAndreas Gohr     *
254c5c85a97SAndreas Gohr     * @param string $file
255c5c85a97SAndreas Gohr     */
256c5c85a97SAndreas Gohr    protected function deleteFile($file)
257c5c85a97SAndreas Gohr    {
258c5c85a97SAndreas Gohr        if (!file_exists($file)) return;
259c5c85a97SAndreas Gohr        if (@unlink($file)) {
260c5c85a97SAndreas Gohr            $this->success('Delete ' . $file);
261c5c85a97SAndreas Gohr        }
262c5c85a97SAndreas Gohr    }
263c5c85a97SAndreas Gohr
264c5c85a97SAndreas Gohr    /**
265c5c85a97SAndreas Gohr     * Run git with the given arguments and return the output
266c5c85a97SAndreas Gohr     *
267c5c85a97SAndreas Gohr     * @throws CliException when the command can't be run
268c5c85a97SAndreas Gohr     * @param string ...$args
269c5c85a97SAndreas Gohr     * @return string[]
270c5c85a97SAndreas Gohr     */
271c5c85a97SAndreas Gohr    protected function git(...$args)
272c5c85a97SAndreas Gohr    {
273c5c85a97SAndreas Gohr        $args = array_map('escapeshellarg', $args);
274c5c85a97SAndreas Gohr        $cmd = 'git ' . join(' ', $args);
275c5c85a97SAndreas Gohr        $output = [];
276c5c85a97SAndreas Gohr        $result = 0;
277c5c85a97SAndreas Gohr
278c5c85a97SAndreas Gohr        $this->info($cmd);
279c5c85a97SAndreas Gohr        $last = exec($cmd, $output, $result);
280c5c85a97SAndreas Gohr        if ($last === false || $result !== 0) {
281c5c85a97SAndreas Gohr            throw new CliException('Running git failed');
282c5c85a97SAndreas Gohr        }
283c5c85a97SAndreas Gohr
284c5c85a97SAndreas Gohr        return $output;
285c5c85a97SAndreas Gohr    }
286c5c85a97SAndreas Gohr
287c5c85a97SAndreas Gohr    // region Commands
288c5c85a97SAndreas Gohr
289c5c85a97SAndreas Gohr    /**
29036c0b2b4SAndreas Gohr     * Intialize the current directory as a plugin or template
29136c0b2b4SAndreas Gohr     *
29236c0b2b4SAndreas Gohr     * @return int
29336c0b2b4SAndreas Gohr     */
29436c0b2b4SAndreas Gohr    protected function cmdInit()
29536c0b2b4SAndreas Gohr    {
29636c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
29736c0b2b4SAndreas Gohr        if ((new FilesystemIterator($dir))->valid()) {
29836c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be empty');
29936c0b2b4SAndreas Gohr        }
30036c0b2b4SAndreas Gohr
30136c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
30236c0b2b4SAndreas Gohr        $user = $this->readLine('Your Name', true);
30336c0b2b4SAndreas Gohr        $mail = $this->readLine('Your E-Mail', true);
30436c0b2b4SAndreas Gohr        $desc = $this->readLine('Short description');
30536c0b2b4SAndreas Gohr
30636c0b2b4SAndreas Gohr        $replacements = [
30736c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => $user,
30836c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => $mail,
30936c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => $name,
31036c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => $desc,
31136c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => $type,
31236c0b2b4SAndreas Gohr        ];
31336c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
31436c0b2b4SAndreas Gohr
31536c0b2b4SAndreas Gohr        $this->loadSkeleton('info.skel', $type . '.info.txt', $replacements);
31636c0b2b4SAndreas Gohr        $this->loadSkeleton('README.skel', 'README', $replacements); // fixme needs to be type specific
31736c0b2b4SAndreas Gohr        $this->loadSkeleton('LICENSE.skel', 'LICENSE', $replacements);
31836c0b2b4SAndreas Gohr
3198b06c9ddSAndreas Gohr        try {
3208b06c9ddSAndreas Gohr            $this->git('init');
3218b06c9ddSAndreas Gohr        } catch (CliException $e) {
3228b06c9ddSAndreas Gohr            $this->error($e->getMessage());
3238b06c9ddSAndreas Gohr        }
3248b06c9ddSAndreas Gohr
32536c0b2b4SAndreas Gohr        return 0;
32636c0b2b4SAndreas Gohr    }
32736c0b2b4SAndreas Gohr
32836c0b2b4SAndreas Gohr    /**
32936c0b2b4SAndreas Gohr     * Add test framework
33036c0b2b4SAndreas Gohr     *
33136c0b2b4SAndreas Gohr     * @param string $test Name of the Test to add
33236c0b2b4SAndreas Gohr     * @return int
33336c0b2b4SAndreas Gohr     */
33436c0b2b4SAndreas Gohr    protected function cmdAddTest($test = '')
33536c0b2b4SAndreas Gohr    {
33636c0b2b4SAndreas Gohr        $test = ucfirst(strtolower($test));
33736c0b2b4SAndreas Gohr
33836c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements(['@@TEST@@' => $test]);
33936c0b2b4SAndreas Gohr        $this->loadSkeleton('.github/workflows/phpTestLinux.skel', '.github/workflows/phpTestLinux.yml', $replacements);
34036c0b2b4SAndreas Gohr        if ($test) {
34136c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/StandardTest.skel', '_test/' . $test . 'Test.php', $replacements);
34236c0b2b4SAndreas Gohr        } else {
34336c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/GeneralTest.skel', '_test/GeneralTest.php', $replacements);
34436c0b2b4SAndreas Gohr        }
34536c0b2b4SAndreas Gohr
34636c0b2b4SAndreas Gohr        return 0;
34736c0b2b4SAndreas Gohr    }
34836c0b2b4SAndreas Gohr
34936c0b2b4SAndreas Gohr    /**
35036c0b2b4SAndreas Gohr     * Add configuration
35136c0b2b4SAndreas Gohr     *
35236c0b2b4SAndreas Gohr     * @return int
35336c0b2b4SAndreas Gohr     */
35436c0b2b4SAndreas Gohr    protected function cmdAddConf()
35536c0b2b4SAndreas Gohr    {
35636c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
35736c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/default.skel', 'conf/default.php', $replacements);
35836c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/metadata.skel', 'conf/metadata.php', $replacements);
35936c0b2b4SAndreas Gohr        if (is_dir('lang')) {
36036c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
36136c0b2b4SAndreas Gohr        }
36236c0b2b4SAndreas Gohr
36336c0b2b4SAndreas Gohr        return 0;
36436c0b2b4SAndreas Gohr    }
36536c0b2b4SAndreas Gohr
36636c0b2b4SAndreas Gohr    /**
36736c0b2b4SAndreas Gohr     * Add language
36836c0b2b4SAndreas Gohr     *
36936c0b2b4SAndreas Gohr     * @return int
37036c0b2b4SAndreas Gohr     */
37136c0b2b4SAndreas Gohr    protected function cmdAddLang()
37236c0b2b4SAndreas Gohr    {
37336c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
37436c0b2b4SAndreas Gohr        $this->loadSkeleton('lang/lang.skel', 'lang/en/lang.php', $replacements);
37536c0b2b4SAndreas Gohr        if (is_dir('conf')) {
37636c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
37736c0b2b4SAndreas Gohr        }
37836c0b2b4SAndreas Gohr
37936c0b2b4SAndreas Gohr        return 0;
38036c0b2b4SAndreas Gohr    }
38136c0b2b4SAndreas Gohr
38236c0b2b4SAndreas Gohr    /**
38336c0b2b4SAndreas Gohr     * Add another component to the plugin
38436c0b2b4SAndreas Gohr     *
38536c0b2b4SAndreas Gohr     * @param string $type
38636c0b2b4SAndreas Gohr     * @param string $component
38736c0b2b4SAndreas Gohr     */
38836c0b2b4SAndreas Gohr    protected function cmdAddComponent($type, $component = '')
38936c0b2b4SAndreas Gohr    {
39036c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
39136c0b2b4SAndreas Gohr        list($plugin, $extension) = $this->getTypedNameFromDir($dir);
39236c0b2b4SAndreas Gohr        if ($extension != 'plugin') throw  new CliException('Components can only be added to plugins');
39336c0b2b4SAndreas Gohr        if (!in_array($type, PluginController::PLUGIN_TYPES)) {
39436c0b2b4SAndreas Gohr            throw new CliException('Invalid type ' . $type);
39536c0b2b4SAndreas Gohr        }
39636c0b2b4SAndreas Gohr
39736c0b2b4SAndreas Gohr        if ($component) {
39836c0b2b4SAndreas Gohr            $path = $type . '/' . $component . '.php';
39936c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin . '_' . $component;
40036c0b2b4SAndreas Gohr            $self = $plugin . '_' . $component;
40136c0b2b4SAndreas Gohr        } else {
40236c0b2b4SAndreas Gohr            $path = $type . '.php';
40336c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin;
40436c0b2b4SAndreas Gohr            $self = $plugin;
40536c0b2b4SAndreas Gohr        }
40636c0b2b4SAndreas Gohr
40736c0b2b4SAndreas Gohr        $replacements = $this->actionReplacements();
40836c0b2b4SAndreas Gohr        $replacements['@@PLUGIN_COMPONENT_NAME@@'] = $class;
40936c0b2b4SAndreas Gohr        $replacements['@@SYNTAX_COMPONENT_NAME@@'] = $self;
41036c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
41136c0b2b4SAndreas Gohr        $this->loadSkeleton($type . '.skel', $path, $replacements);
41236c0b2b4SAndreas Gohr
41336c0b2b4SAndreas Gohr        return 0;
41436c0b2b4SAndreas Gohr    }
41536c0b2b4SAndreas Gohr
41636c0b2b4SAndreas Gohr    /**
41736c0b2b4SAndreas Gohr     * Generate a list of deleted files from git
41836c0b2b4SAndreas Gohr     *
41936c0b2b4SAndreas Gohr     * @link https://stackoverflow.com/a/6018049/172068
42036c0b2b4SAndreas Gohr     */
42136c0b2b4SAndreas Gohr    protected function cmdDeletedFiles()
42236c0b2b4SAndreas Gohr    {
4238b06c9ddSAndreas Gohr        if (!is_dir('.git')) throw new CliException('This extension seems not to be managed by git');
42436c0b2b4SAndreas Gohr
4258b06c9ddSAndreas Gohr        $output = $this->git('log', '--no-renames', '--pretty=format:', '--name-only', '--diff-filter=D');
42636c0b2b4SAndreas Gohr        $output = array_map('trim', $output);
42736c0b2b4SAndreas Gohr        $output = array_filter($output);
42836c0b2b4SAndreas Gohr        $output = array_unique($output);
42936c0b2b4SAndreas Gohr        $output = array_filter($output, function ($item) {
43036c0b2b4SAndreas Gohr            return !file_exists($item);
43136c0b2b4SAndreas Gohr        });
43236c0b2b4SAndreas Gohr        sort($output);
43336c0b2b4SAndreas Gohr
43436c0b2b4SAndreas Gohr        if (!count($output)) {
43536c0b2b4SAndreas Gohr            $this->info('No deleted files found');
43636c0b2b4SAndreas Gohr            return 0;
43736c0b2b4SAndreas Gohr        }
43836c0b2b4SAndreas Gohr
43936c0b2b4SAndreas Gohr        $content = "# This is a list of files that were present in previous releases\n" .
44036c0b2b4SAndreas Gohr            "# but were removed later. They should not exist in your installation.\n" .
44136c0b2b4SAndreas Gohr            join("\n", $output) . "\n";
44236c0b2b4SAndreas Gohr
44336c0b2b4SAndreas Gohr        file_put_contents('deleted.files', $content);
44436c0b2b4SAndreas Gohr        $this->success('written deleted.files');
44536c0b2b4SAndreas Gohr        return 0;
44636c0b2b4SAndreas Gohr    }
4478b06c9ddSAndreas Gohr
4488b06c9ddSAndreas Gohr    /**
449c5c85a97SAndreas Gohr     * Remove files that shouldn't be here anymore
4508b06c9ddSAndreas Gohr     */
451c5c85a97SAndreas Gohr    protected function rmObsolete()
4528b06c9ddSAndreas Gohr    {
453c5c85a97SAndreas Gohr        $this->deleteFile('_test/general.test.php');
454c5c85a97SAndreas Gohr        $this->deleteFile('.travis.yml');
4558b06c9ddSAndreas Gohr
456c5c85a97SAndreas Gohr        return 0;
4578b06c9ddSAndreas Gohr    }
4588b06c9ddSAndreas Gohr
459c5c85a97SAndreas Gohr    //endregion
46036c0b2b4SAndreas Gohr}
461