xref: /plugin/dev/cli.php (revision c5c85a97d56f32f460f1b227342beeed18e7ef18)
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    {
2436c0b2b4SAndreas Gohr        $options->setHelp(
2536c0b2b4SAndreas Gohr            "CLI to help with plugin and template development\n\n" .
2636c0b2b4SAndreas Gohr            "Run this script from within the extension's directory."
2736c0b2b4SAndreas Gohr        );
2836c0b2b4SAndreas Gohr
2936c0b2b4SAndreas Gohr        $options->registerCommand('init', 'initialize a new plugin or template');
3036c0b2b4SAndreas Gohr        $options->registerCommand('addTest', 'add the testing framework files and optionall a new test file');
3136c0b2b4SAndreas Gohr        $options->registerArgument('test', 'The name of the new test, leave out for general test.', false, 'addTest');
3236c0b2b4SAndreas Gohr        $options->registerCommand('addConf', 'add the configuration files');
3336c0b2b4SAndreas Gohr        $options->registerCommand('addLang', 'add the language files');
3436c0b2b4SAndreas Gohr
3536c0b2b4SAndreas Gohr        $options->registerCommand('addComponent', 'add a component');
3636c0b2b4SAndreas Gohr        $options->registerArgument('type', 'The type of the component. Needs to be one of ' .
3736c0b2b4SAndreas Gohr            join(', ', PluginController::PLUGIN_TYPES),
3836c0b2b4SAndreas Gohr            true, 'addComponent'
3936c0b2b4SAndreas Gohr        );
4036c0b2b4SAndreas Gohr        $options->registerArgument('name', 'Optional name of the component', false, 'addComponent');
4136c0b2b4SAndreas Gohr
4236c0b2b4SAndreas Gohr        $options->registerCommand('deletedFiles', 'create the list of deleted files base on the git history');
43*c5c85a97SAndreas Gohr        $options->registerCommand('rmObsolete', 'delete obsolete files');
4436c0b2b4SAndreas Gohr    }
4536c0b2b4SAndreas Gohr
4636c0b2b4SAndreas Gohr    /** @inheritDoc */
4736c0b2b4SAndreas Gohr    protected function main(Options $options)
4836c0b2b4SAndreas Gohr    {
4936c0b2b4SAndreas Gohr        switch ($options->getCmd()) {
5036c0b2b4SAndreas Gohr            case 'init':
5136c0b2b4SAndreas Gohr                return $this->cmdInit();
5236c0b2b4SAndreas Gohr            case 'addTest':
5336c0b2b4SAndreas Gohr                $args = $options->getArgs();
5436c0b2b4SAndreas Gohr                $test = array_shift($args);
5536c0b2b4SAndreas Gohr                return $this->cmdAddTest($test);
5636c0b2b4SAndreas Gohr            case 'addConf':
5736c0b2b4SAndreas Gohr                return $this->cmdAddConf();
5836c0b2b4SAndreas Gohr            case 'addLang':
5936c0b2b4SAndreas Gohr                return $this->cmdAddLang();
6036c0b2b4SAndreas Gohr            case 'addComponent':
6136c0b2b4SAndreas Gohr                $args = $options->getArgs();
6236c0b2b4SAndreas Gohr                $type = array_shift($args);
6336c0b2b4SAndreas Gohr                $component = array_shift($args);
6436c0b2b4SAndreas Gohr                return $this->cmdAddComponent($type, $component);
6536c0b2b4SAndreas Gohr            case 'deletedFiles':
6636c0b2b4SAndreas Gohr                return $this->cmdDeletedFiles();
67*c5c85a97SAndreas Gohr            case 'rmObsolete':
68*c5c85a97SAndreas Gohr                return $this->rmObsolete();
6936c0b2b4SAndreas Gohr            default:
7036c0b2b4SAndreas Gohr                echo $options->help();
7136c0b2b4SAndreas Gohr                return 0;
7236c0b2b4SAndreas Gohr        }
7336c0b2b4SAndreas Gohr    }
7436c0b2b4SAndreas Gohr
7536c0b2b4SAndreas Gohr    /**
7636c0b2b4SAndreas Gohr     * Get the extension name from the current working directory
7736c0b2b4SAndreas Gohr     *
7836c0b2b4SAndreas Gohr     * @throws CliException if something's wrong
7936c0b2b4SAndreas Gohr     * @param string $dir
8036c0b2b4SAndreas Gohr     * @return string[] name, type
8136c0b2b4SAndreas Gohr     */
8236c0b2b4SAndreas Gohr    protected function getTypedNameFromDir($dir)
8336c0b2b4SAndreas Gohr    {
8436c0b2b4SAndreas Gohr        $pdir = fullpath(DOKU_PLUGIN);
8536c0b2b4SAndreas Gohr        $tdir = fullpath(tpl_incdir() . '../');
8636c0b2b4SAndreas Gohr
8736c0b2b4SAndreas Gohr        if (strpos($dir, $pdir) === 0) {
8836c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($pdir));
8936c0b2b4SAndreas Gohr            $type = 'plugin';
9036c0b2b4SAndreas Gohr        } elseif (strpos($dir, $tdir) === 0) {
9136c0b2b4SAndreas Gohr            $ldir = substr($dir, strlen($tdir));
9236c0b2b4SAndreas Gohr            $type = 'template';
9336c0b2b4SAndreas Gohr        } else {
9436c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be in plugin or template directory');
9536c0b2b4SAndreas Gohr        }
9636c0b2b4SAndreas Gohr
9736c0b2b4SAndreas Gohr        $ldir = trim($ldir, '/');
9836c0b2b4SAndreas Gohr
9936c0b2b4SAndreas Gohr        if (strpos($ldir, '/') !== false) {
10036c0b2b4SAndreas Gohr            throw new CliException('Current directory has to be main extension directory');
10136c0b2b4SAndreas Gohr        }
10236c0b2b4SAndreas Gohr
10336c0b2b4SAndreas Gohr        return [$ldir, $type];
10436c0b2b4SAndreas Gohr    }
10536c0b2b4SAndreas Gohr
10636c0b2b4SAndreas Gohr    /**
10736c0b2b4SAndreas Gohr     * Interactively ask for a value from the user
10836c0b2b4SAndreas Gohr     *
10936c0b2b4SAndreas Gohr     * @param string $prompt
11036c0b2b4SAndreas Gohr     * @param bool $cache cache given value for next time?
11136c0b2b4SAndreas Gohr     * @return string
11236c0b2b4SAndreas Gohr     */
11336c0b2b4SAndreas Gohr    protected function readLine($prompt, $cache = false)
11436c0b2b4SAndreas Gohr    {
11536c0b2b4SAndreas Gohr        $value = '';
11636c0b2b4SAndreas Gohr        $default = '';
11736c0b2b4SAndreas Gohr        $cachename = getCacheName($prompt, '.readline');
11836c0b2b4SAndreas Gohr        if ($cache && file_exists($cachename)) {
11936c0b2b4SAndreas Gohr            $default = file_get_contents($cachename);
12036c0b2b4SAndreas Gohr        }
12136c0b2b4SAndreas Gohr
12236c0b2b4SAndreas Gohr        while ($value === '') {
12336c0b2b4SAndreas Gohr            echo $prompt;
12436c0b2b4SAndreas Gohr            if ($default) echo ' [' . $default . ']';
12536c0b2b4SAndreas Gohr            echo ': ';
12636c0b2b4SAndreas Gohr
12736c0b2b4SAndreas Gohr            $fh = fopen('php://stdin', 'r');
12836c0b2b4SAndreas Gohr            $value = trim(fgets($fh));
12936c0b2b4SAndreas Gohr            fclose($fh);
13036c0b2b4SAndreas Gohr
13136c0b2b4SAndreas Gohr            if ($value === '') $value = $default;
13236c0b2b4SAndreas Gohr        }
13336c0b2b4SAndreas Gohr
13436c0b2b4SAndreas Gohr        if ($cache) {
13536c0b2b4SAndreas Gohr            file_put_contents($cachename, $value);
13636c0b2b4SAndreas Gohr        }
13736c0b2b4SAndreas Gohr
13836c0b2b4SAndreas Gohr        return $value;
13936c0b2b4SAndreas Gohr    }
14036c0b2b4SAndreas Gohr
14136c0b2b4SAndreas Gohr    /**
14236c0b2b4SAndreas Gohr     * Download a skeleton file and do the replacements
14336c0b2b4SAndreas Gohr     *
14436c0b2b4SAndreas Gohr     * @param string $skel Skeleton relative to the skel dir in the repo
14536c0b2b4SAndreas Gohr     * @param string $target Target file relative to the main directory
14636c0b2b4SAndreas Gohr     * @param array $replacements
14736c0b2b4SAndreas Gohr     */
14836c0b2b4SAndreas Gohr    protected function loadSkeleton($skel, $target, $replacements)
14936c0b2b4SAndreas Gohr    {
15036c0b2b4SAndreas Gohr        if (file_exists($target)) {
15136c0b2b4SAndreas Gohr            $this->error($target . ' already exists');
15236c0b2b4SAndreas Gohr            return;
15336c0b2b4SAndreas Gohr        }
15436c0b2b4SAndreas Gohr
15536c0b2b4SAndreas Gohr        $base = 'https://raw.githubusercontent.com/dokufreaks/dokuwiki-plugin-wizard/master/skel/';
15636c0b2b4SAndreas Gohr        $http = new \dokuwiki\HTTP\DokuHTTPClient();
15736c0b2b4SAndreas Gohr        $content = $http->get($base . $skel);
15836c0b2b4SAndreas Gohr
15936c0b2b4SAndreas Gohr        $content = str_replace(
16036c0b2b4SAndreas Gohr            array_keys($replacements),
16136c0b2b4SAndreas Gohr            array_values($replacements),
16236c0b2b4SAndreas Gohr            $content
16336c0b2b4SAndreas Gohr        );
16436c0b2b4SAndreas Gohr
16536c0b2b4SAndreas Gohr        io_makeFileDir($target);
16636c0b2b4SAndreas Gohr        file_put_contents($target, $content);
16736c0b2b4SAndreas Gohr        $this->success('Added ' . $target);
16836c0b2b4SAndreas Gohr    }
16936c0b2b4SAndreas Gohr
17036c0b2b4SAndreas Gohr    /**
17136c0b2b4SAndreas Gohr     * Prepare the string replacements
17236c0b2b4SAndreas Gohr     *
17336c0b2b4SAndreas Gohr     * @param array $replacements override defaults
17436c0b2b4SAndreas Gohr     * @return array
17536c0b2b4SAndreas Gohr     */
17636c0b2b4SAndreas Gohr    protected function prepareReplacements($replacements = [])
17736c0b2b4SAndreas Gohr    {
17836c0b2b4SAndreas Gohr        // defaults
17936c0b2b4SAndreas Gohr        $data = [
18036c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => '',
18136c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => '',
18236c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => '',
18336c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => '',
18436c0b2b4SAndreas Gohr            '@@PLUGIN_URL@@' => '',
18536c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => '',
18636c0b2b4SAndreas Gohr            '@@INSTALL_DIR@@' => 'plugins',
18736c0b2b4SAndreas Gohr            '@@DATE@@' => date('Y-m-d'),
18836c0b2b4SAndreas Gohr        ];
18936c0b2b4SAndreas Gohr
19036c0b2b4SAndreas Gohr        // load from existing plugin.info
19136c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
19236c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
19336c0b2b4SAndreas Gohr        if (file_exists("$type.info.txt")) {
19436c0b2b4SAndreas Gohr            $info = confToHash("$type.info.txt");
19536c0b2b4SAndreas Gohr            $data['@@AUTHOR_NAME@@'] = $info['author'];
19636c0b2b4SAndreas Gohr            $data['@@AUTHOR_MAIL@@'] = $info['email'];
19736c0b2b4SAndreas Gohr            $data['@@PLUGIN_DESC@@'] = $info['desc'];
19836c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] = $info['url'];
19936c0b2b4SAndreas Gohr        }
20036c0b2b4SAndreas Gohr        $data['@@PLUGIN_NAME@@'] = $name;
20136c0b2b4SAndreas Gohr        $data['@@PLUGIN_TYPE@@'] = $type;
20236c0b2b4SAndreas Gohr
20336c0b2b4SAndreas Gohr        if ($type == 'template') {
20436c0b2b4SAndreas Gohr            $data['@@INSTALL_DIR@@'] = 'tpl';
20536c0b2b4SAndreas Gohr        }
20636c0b2b4SAndreas Gohr
20736c0b2b4SAndreas Gohr        // merge given overrides
20836c0b2b4SAndreas Gohr        $data = array_merge($data, $replacements);
20936c0b2b4SAndreas Gohr
21036c0b2b4SAndreas Gohr        // set inherited defaults
21136c0b2b4SAndreas Gohr        if (empty($data['@@PLUGIN_URL@@'])) {
21236c0b2b4SAndreas Gohr            $data['@@PLUGIN_URL@@'] =
21336c0b2b4SAndreas Gohr                'https://www.dokuwiki.org/' .
21436c0b2b4SAndreas Gohr                $data['@@PLUGIN_TYPE@@'] . ':' .
21536c0b2b4SAndreas Gohr                $data['@@PLUGIN_NAME@@'];
21636c0b2b4SAndreas Gohr        }
21736c0b2b4SAndreas Gohr
21836c0b2b4SAndreas Gohr        return $data;
21936c0b2b4SAndreas Gohr    }
22036c0b2b4SAndreas Gohr
22136c0b2b4SAndreas Gohr    /**
22236c0b2b4SAndreas Gohr     * Replacements needed for action components.
22336c0b2b4SAndreas Gohr     *
22436c0b2b4SAndreas Gohr     * Not cool but that' what we need currently
22536c0b2b4SAndreas Gohr     *
22636c0b2b4SAndreas Gohr     * @return string[]
22736c0b2b4SAndreas Gohr     */
22836c0b2b4SAndreas Gohr    protected function actionReplacements()
22936c0b2b4SAndreas Gohr    {
23036c0b2b4SAndreas Gohr        $fn = 'handleEventName';
23136c0b2b4SAndreas Gohr        $register = '        $controller->register_hook(\'EVENT_NAME\', \'AFTER|BEFORE\', $this, \'' . $fn . '\');';
23236c0b2b4SAndreas Gohr        $handler = '    public function ' . $fn . '(Doku_Event $event, $param)' . "\n"
23336c0b2b4SAndreas Gohr            . "    {\n"
23436c0b2b4SAndreas Gohr            . "    }\n";
23536c0b2b4SAndreas Gohr
23636c0b2b4SAndreas Gohr        return [
23736c0b2b4SAndreas Gohr            '@@REGISTER@@' => $register . "\n   ",
23836c0b2b4SAndreas Gohr            '@@HANDLERS@@' => $handler,
23936c0b2b4SAndreas Gohr        ];
24036c0b2b4SAndreas Gohr    }
24136c0b2b4SAndreas Gohr
24236c0b2b4SAndreas Gohr    /**
243*c5c85a97SAndreas Gohr     * Delete the given file if it exists
244*c5c85a97SAndreas Gohr     *
245*c5c85a97SAndreas Gohr     * @param string $file
246*c5c85a97SAndreas Gohr     */
247*c5c85a97SAndreas Gohr    protected function deleteFile($file)
248*c5c85a97SAndreas Gohr    {
249*c5c85a97SAndreas Gohr        if (!file_exists($file)) return;
250*c5c85a97SAndreas Gohr        if (@unlink($file)) {
251*c5c85a97SAndreas Gohr            $this->success('Delete ' . $file);
252*c5c85a97SAndreas Gohr        }
253*c5c85a97SAndreas Gohr    }
254*c5c85a97SAndreas Gohr
255*c5c85a97SAndreas Gohr    /**
256*c5c85a97SAndreas Gohr     * Run git with the given arguments and return the output
257*c5c85a97SAndreas Gohr     *
258*c5c85a97SAndreas Gohr     * @throws CliException when the command can't be run
259*c5c85a97SAndreas Gohr     * @param string ...$args
260*c5c85a97SAndreas Gohr     * @return string[]
261*c5c85a97SAndreas Gohr     */
262*c5c85a97SAndreas Gohr    protected function git(...$args)
263*c5c85a97SAndreas Gohr    {
264*c5c85a97SAndreas Gohr        $args = array_map('escapeshellarg', $args);
265*c5c85a97SAndreas Gohr        $cmd = 'git ' . join(' ', $args);
266*c5c85a97SAndreas Gohr        $output = [];
267*c5c85a97SAndreas Gohr        $result = 0;
268*c5c85a97SAndreas Gohr
269*c5c85a97SAndreas Gohr        $this->info($cmd);
270*c5c85a97SAndreas Gohr        $last = exec($cmd, $output, $result);
271*c5c85a97SAndreas Gohr        if ($last === false || $result !== 0) {
272*c5c85a97SAndreas Gohr            throw new CliException('Running git failed');
273*c5c85a97SAndreas Gohr        }
274*c5c85a97SAndreas Gohr
275*c5c85a97SAndreas Gohr        return $output;
276*c5c85a97SAndreas Gohr    }
277*c5c85a97SAndreas Gohr
278*c5c85a97SAndreas Gohr    // region Commands
279*c5c85a97SAndreas Gohr
280*c5c85a97SAndreas Gohr    /**
28136c0b2b4SAndreas Gohr     * Intialize the current directory as a plugin or template
28236c0b2b4SAndreas Gohr     *
28336c0b2b4SAndreas Gohr     * @return int
28436c0b2b4SAndreas Gohr     */
28536c0b2b4SAndreas Gohr    protected function cmdInit()
28636c0b2b4SAndreas Gohr    {
28736c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
28836c0b2b4SAndreas Gohr        if ((new FilesystemIterator($dir))->valid()) {
28936c0b2b4SAndreas Gohr            throw new CliException('Current directory needs to be empty');
29036c0b2b4SAndreas Gohr        }
29136c0b2b4SAndreas Gohr
29236c0b2b4SAndreas Gohr        [$name, $type] = $this->getTypedNameFromDir($dir);
29336c0b2b4SAndreas Gohr        $user = $this->readLine('Your Name', true);
29436c0b2b4SAndreas Gohr        $mail = $this->readLine('Your E-Mail', true);
29536c0b2b4SAndreas Gohr        $desc = $this->readLine('Short description');
29636c0b2b4SAndreas Gohr
29736c0b2b4SAndreas Gohr        $replacements = [
29836c0b2b4SAndreas Gohr            '@@AUTHOR_NAME@@' => $user,
29936c0b2b4SAndreas Gohr            '@@AUTHOR_MAIL@@' => $mail,
30036c0b2b4SAndreas Gohr            '@@PLUGIN_NAME@@' => $name,
30136c0b2b4SAndreas Gohr            '@@PLUGIN_DESC@@' => $desc,
30236c0b2b4SAndreas Gohr            '@@PLUGIN_TYPE@@' => $type,
30336c0b2b4SAndreas Gohr        ];
30436c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
30536c0b2b4SAndreas Gohr
30636c0b2b4SAndreas Gohr        $this->loadSkeleton('info.skel', $type . '.info.txt', $replacements);
30736c0b2b4SAndreas Gohr        $this->loadSkeleton('README.skel', 'README', $replacements); // fixme needs to be type specific
30836c0b2b4SAndreas Gohr        $this->loadSkeleton('LICENSE.skel', 'LICENSE', $replacements);
30936c0b2b4SAndreas Gohr
3108b06c9ddSAndreas Gohr        try {
3118b06c9ddSAndreas Gohr            $this->git('init');
3128b06c9ddSAndreas Gohr        } catch (CliException $e) {
3138b06c9ddSAndreas Gohr            $this->error($e->getMessage());
3148b06c9ddSAndreas Gohr        }
3158b06c9ddSAndreas Gohr
31636c0b2b4SAndreas Gohr        return 0;
31736c0b2b4SAndreas Gohr    }
31836c0b2b4SAndreas Gohr
31936c0b2b4SAndreas Gohr    /**
32036c0b2b4SAndreas Gohr     * Add test framework
32136c0b2b4SAndreas Gohr     *
32236c0b2b4SAndreas Gohr     * @param string $test Name of the Test to add
32336c0b2b4SAndreas Gohr     * @return int
32436c0b2b4SAndreas Gohr     */
32536c0b2b4SAndreas Gohr    protected function cmdAddTest($test = '')
32636c0b2b4SAndreas Gohr    {
32736c0b2b4SAndreas Gohr        $test = ucfirst(strtolower($test));
32836c0b2b4SAndreas Gohr
32936c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements(['@@TEST@@' => $test]);
33036c0b2b4SAndreas Gohr        $this->loadSkeleton('.github/workflows/phpTestLinux.skel', '.github/workflows/phpTestLinux.yml', $replacements);
33136c0b2b4SAndreas Gohr        if ($test) {
33236c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/StandardTest.skel', '_test/' . $test . 'Test.php', $replacements);
33336c0b2b4SAndreas Gohr        } else {
33436c0b2b4SAndreas Gohr            $this->loadSkeleton('_test/GeneralTest.skel', '_test/GeneralTest.php', $replacements);
33536c0b2b4SAndreas Gohr        }
33636c0b2b4SAndreas Gohr
33736c0b2b4SAndreas Gohr        return 0;
33836c0b2b4SAndreas Gohr    }
33936c0b2b4SAndreas Gohr
34036c0b2b4SAndreas Gohr    /**
34136c0b2b4SAndreas Gohr     * Add configuration
34236c0b2b4SAndreas Gohr     *
34336c0b2b4SAndreas Gohr     * @return int
34436c0b2b4SAndreas Gohr     */
34536c0b2b4SAndreas Gohr    protected function cmdAddConf()
34636c0b2b4SAndreas Gohr    {
34736c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
34836c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/default.skel', 'conf/default.php', $replacements);
34936c0b2b4SAndreas Gohr        $this->loadSkeleton('conf/metadata.skel', 'conf/metadata.php', $replacements);
35036c0b2b4SAndreas Gohr        if (is_dir('lang')) {
35136c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
35236c0b2b4SAndreas Gohr        }
35336c0b2b4SAndreas Gohr
35436c0b2b4SAndreas Gohr        return 0;
35536c0b2b4SAndreas Gohr    }
35636c0b2b4SAndreas Gohr
35736c0b2b4SAndreas Gohr    /**
35836c0b2b4SAndreas Gohr     * Add language
35936c0b2b4SAndreas Gohr     *
36036c0b2b4SAndreas Gohr     * @return int
36136c0b2b4SAndreas Gohr     */
36236c0b2b4SAndreas Gohr    protected function cmdAddLang()
36336c0b2b4SAndreas Gohr    {
36436c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements();
36536c0b2b4SAndreas Gohr        $this->loadSkeleton('lang/lang.skel', 'lang/en/lang.php', $replacements);
36636c0b2b4SAndreas Gohr        if (is_dir('conf')) {
36736c0b2b4SAndreas Gohr            $this->loadSkeleton('lang/settings.skel', 'lang/en/settings.php', $replacements);
36836c0b2b4SAndreas Gohr        }
36936c0b2b4SAndreas Gohr
37036c0b2b4SAndreas Gohr        return 0;
37136c0b2b4SAndreas Gohr    }
37236c0b2b4SAndreas Gohr
37336c0b2b4SAndreas Gohr    /**
37436c0b2b4SAndreas Gohr     * Add another component to the plugin
37536c0b2b4SAndreas Gohr     *
37636c0b2b4SAndreas Gohr     * @param string $type
37736c0b2b4SAndreas Gohr     * @param string $component
37836c0b2b4SAndreas Gohr     */
37936c0b2b4SAndreas Gohr    protected function cmdAddComponent($type, $component = '')
38036c0b2b4SAndreas Gohr    {
38136c0b2b4SAndreas Gohr        $dir = fullpath(getcwd());
38236c0b2b4SAndreas Gohr        list($plugin, $extension) = $this->getTypedNameFromDir($dir);
38336c0b2b4SAndreas Gohr        if ($extension != 'plugin') throw  new CliException('Components can only be added to plugins');
38436c0b2b4SAndreas Gohr        if (!in_array($type, PluginController::PLUGIN_TYPES)) {
38536c0b2b4SAndreas Gohr            throw new CliException('Invalid type ' . $type);
38636c0b2b4SAndreas Gohr        }
38736c0b2b4SAndreas Gohr
38836c0b2b4SAndreas Gohr        if ($component) {
38936c0b2b4SAndreas Gohr            $path = $type . '/' . $component . '.php';
39036c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin . '_' . $component;
39136c0b2b4SAndreas Gohr            $self = $plugin . '_' . $component;
39236c0b2b4SAndreas Gohr        } else {
39336c0b2b4SAndreas Gohr            $path = $type . '.php';
39436c0b2b4SAndreas Gohr            $class = $type . '_plugin_' . $plugin;
39536c0b2b4SAndreas Gohr            $self = $plugin;
39636c0b2b4SAndreas Gohr        }
39736c0b2b4SAndreas Gohr
39836c0b2b4SAndreas Gohr        $replacements = $this->actionReplacements();
39936c0b2b4SAndreas Gohr        $replacements['@@PLUGIN_COMPONENT_NAME@@'] = $class;
40036c0b2b4SAndreas Gohr        $replacements['@@SYNTAX_COMPONENT_NAME@@'] = $self;
40136c0b2b4SAndreas Gohr        $replacements = $this->prepareReplacements($replacements);
40236c0b2b4SAndreas Gohr        $this->loadSkeleton($type . '.skel', $path, $replacements);
40336c0b2b4SAndreas Gohr
40436c0b2b4SAndreas Gohr        return 0;
40536c0b2b4SAndreas Gohr    }
40636c0b2b4SAndreas Gohr
40736c0b2b4SAndreas Gohr    /**
40836c0b2b4SAndreas Gohr     * Generate a list of deleted files from git
40936c0b2b4SAndreas Gohr     *
41036c0b2b4SAndreas Gohr     * @link https://stackoverflow.com/a/6018049/172068
41136c0b2b4SAndreas Gohr     */
41236c0b2b4SAndreas Gohr    protected function cmdDeletedFiles()
41336c0b2b4SAndreas Gohr    {
4148b06c9ddSAndreas Gohr        if (!is_dir('.git')) throw new CliException('This extension seems not to be managed by git');
41536c0b2b4SAndreas Gohr
4168b06c9ddSAndreas Gohr        $output = $this->git('log', '--no-renames', '--pretty=format:', '--name-only', '--diff-filter=D');
41736c0b2b4SAndreas Gohr        $output = array_map('trim', $output);
41836c0b2b4SAndreas Gohr        $output = array_filter($output);
41936c0b2b4SAndreas Gohr        $output = array_unique($output);
42036c0b2b4SAndreas Gohr        $output = array_filter($output, function ($item) {
42136c0b2b4SAndreas Gohr            return !file_exists($item);
42236c0b2b4SAndreas Gohr        });
42336c0b2b4SAndreas Gohr        sort($output);
42436c0b2b4SAndreas Gohr
42536c0b2b4SAndreas Gohr        if (!count($output)) {
42636c0b2b4SAndreas Gohr            $this->info('No deleted files found');
42736c0b2b4SAndreas Gohr            return 0;
42836c0b2b4SAndreas Gohr        }
42936c0b2b4SAndreas Gohr
43036c0b2b4SAndreas Gohr        $content = "# This is a list of files that were present in previous releases\n" .
43136c0b2b4SAndreas Gohr            "# but were removed later. They should not exist in your installation.\n" .
43236c0b2b4SAndreas Gohr            join("\n", $output) . "\n";
43336c0b2b4SAndreas Gohr
43436c0b2b4SAndreas Gohr        file_put_contents('deleted.files', $content);
43536c0b2b4SAndreas Gohr        $this->success('written deleted.files');
43636c0b2b4SAndreas Gohr        return 0;
43736c0b2b4SAndreas Gohr    }
4388b06c9ddSAndreas Gohr
4398b06c9ddSAndreas Gohr    /**
440*c5c85a97SAndreas Gohr     * Remove files that shouldn't be here anymore
4418b06c9ddSAndreas Gohr     */
442*c5c85a97SAndreas Gohr    protected function rmObsolete()
4438b06c9ddSAndreas Gohr    {
444*c5c85a97SAndreas Gohr        $this->deleteFile('_test/general.test.php');
445*c5c85a97SAndreas Gohr        $this->deleteFile('.travis.yml');
4468b06c9ddSAndreas Gohr
447*c5c85a97SAndreas Gohr        return 0;
4488b06c9ddSAndreas Gohr    }
4498b06c9ddSAndreas Gohr
450*c5c85a97SAndreas Gohr    //endregion
45136c0b2b4SAndreas Gohr}
452