136c0b2b4SAndreas Gohr#!/usr/bin/env php 236c0b2b4SAndreas Gohr<?php 336c0b2b4SAndreas Gohr 436c0b2b4SAndreas Gohruse dokuwiki\Extension\CLIPlugin; 536c0b2b4SAndreas Gohruse dokuwiki\Extension\PluginController; 65586e97bSAndreas Gohruse dokuwiki\plugin\dev\LangProcessor; 770316b84SAndreas Gohruse dokuwiki\plugin\dev\Skeletor; 81a23d1dbSAndreas Gohruse dokuwiki\plugin\dev\SVGIcon; 936c0b2b4SAndreas Gohruse splitbrain\phpcli\Exception as CliException; 1036c0b2b4SAndreas Gohruse splitbrain\phpcli\Options; 1136c0b2b4SAndreas Gohr 1236c0b2b4SAndreas Gohr/** 1336c0b2b4SAndreas Gohr * @license GPL2 1436c0b2b4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1536c0b2b4SAndreas Gohr */ 1636c0b2b4SAndreas Gohrclass cli_plugin_dev extends CLIPlugin 1736c0b2b4SAndreas Gohr{ 1836c0b2b4SAndreas Gohr /** 1936c0b2b4SAndreas Gohr * Register options and arguments on the given $options object 2036c0b2b4SAndreas Gohr * 2136c0b2b4SAndreas Gohr * @param Options $options 2236c0b2b4SAndreas Gohr * @return void 2336c0b2b4SAndreas Gohr */ 2436c0b2b4SAndreas Gohr protected function setup(Options $options) 2536c0b2b4SAndreas Gohr { 26f2576912SAndreas Gohr $options->useCompactHelp(); 2736c0b2b4SAndreas Gohr $options->setHelp( 28f2576912SAndreas Gohr "CLI to help with DokuWiki plugin and template development.\n\n" . 2936c0b2b4SAndreas Gohr "Run this script from within the extension's directory." 3036c0b2b4SAndreas Gohr ); 3136c0b2b4SAndreas Gohr 32fcb8165bSAndreas Gohr $options->registerCommand('init', 'Initialize a new plugin or template in the current directory.'); 33f2576912SAndreas Gohr $options->registerCommand('addTest', 'Add the testing framework files and a test. (_test/)'); 34f2576912SAndreas Gohr $options->registerArgument('test', 'Optional name of the new test. Defaults to the general test.', false, 35f2576912SAndreas Gohr 'addTest'); 36f2576912SAndreas Gohr $options->registerCommand('addConf', 'Add the configuration files. (conf/)'); 37f2576912SAndreas Gohr $options->registerCommand('addLang', 'Add the language files. (lang/)'); 38*719b4841SAndreas Gohr $options->registerCommand('addAgents', 'Add an initial AGENTS.md file for guiding LLM coding agents'); 3936c0b2b4SAndreas Gohr 40f2576912SAndreas Gohr $types = PluginController::PLUGIN_TYPES; 41f2576912SAndreas Gohr array_walk( 42f2576912SAndreas Gohr $types, 43f2576912SAndreas Gohr function (&$item) { 44f2576912SAndreas Gohr $item = $this->colors->wrap($item, $this->colors::C_BROWN); 45f2576912SAndreas Gohr } 4636c0b2b4SAndreas Gohr ); 4736c0b2b4SAndreas Gohr 48f2576912SAndreas Gohr $options->registerCommand('addComponent', 'Add a new plugin component.'); 49f2576912SAndreas Gohr $options->registerArgument('type', 'Type of the component. Needs to be one of ' . join(', ', $types), true, 50f2576912SAndreas Gohr 'addComponent'); 51f2576912SAndreas Gohr $options->registerArgument('name', 'Optional name of the component. Defaults to a base component.', false, 52f2576912SAndreas Gohr 'addComponent'); 53f2576912SAndreas Gohr 54f2576912SAndreas Gohr $options->registerCommand('deletedFiles', 'Create the list of deleted files based on the git history.'); 55f2576912SAndreas Gohr $options->registerCommand('rmObsolete', 'Delete obsolete files.'); 561a23d1dbSAndreas Gohr 571a23d1dbSAndreas Gohr $prefixes = array_keys(SVGIcon::SOURCES); 581a23d1dbSAndreas Gohr array_walk( 591a23d1dbSAndreas Gohr $prefixes, 601a23d1dbSAndreas Gohr function (&$item) { 611a23d1dbSAndreas Gohr $item = $this->colors->wrap($item, $this->colors::C_BROWN); 621a23d1dbSAndreas Gohr } 631a23d1dbSAndreas Gohr ); 641a23d1dbSAndreas Gohr 651a23d1dbSAndreas Gohr $options->registerCommand('downloadSvg', 'Download an SVG file from a known icon repository.'); 661a23d1dbSAndreas Gohr $options->registerArgument('prefix:name', 671a23d1dbSAndreas Gohr 'Colon-prefixed name of the icon. Available prefixes: ' . join(', ', $prefixes), true, 'downloadSvg'); 681a23d1dbSAndreas Gohr $options->registerArgument('output', 'File to save, defaults to <name>.svg in current dir', false, 691a23d1dbSAndreas Gohr 'downloadSvg'); 7092738407SAndreas Gohr $options->registerOption('keep-ns', 'Keep the SVG namespace. Use when the file is not inlined into HTML.', 'k', 7192738407SAndreas Gohr false, 'downloadSvg'); 721a23d1dbSAndreas Gohr 738f82d673SAndreas Gohr $options->registerCommand('cleanSvg', 'Clean a existing SVG files to reduce their file size.'); 748f82d673SAndreas Gohr $options->registerArgument('files...', 'The files to clean (will be overwritten)', true, 'cleanSvg'); 7592738407SAndreas Gohr $options->registerOption('keep-ns', 'Keep the SVG namespace. Use when the file is not inlined into HTML.', 'k', 7692738407SAndreas Gohr false, 'cleanSvg'); 775586e97bSAndreas Gohr 785586e97bSAndreas Gohr $options->registerCommand('cleanLang', 795586e97bSAndreas Gohr 'Clean language files from unused language strings. Detecting which strings are truly in use may ' . 805586e97bSAndreas Gohr 'not always correctly work. Use with caution.'); 81ec0a687bSAndreas Gohr 8257732a2dSAndreas Gohr $options->registerCommand( 8357732a2dSAndreas Gohr 'test', 8457732a2dSAndreas Gohr 'Run the unit tests for this extension. (calls phpunit using the proper config and group)' 8557732a2dSAndreas Gohr ); 8657732a2dSAndreas Gohr $options->registerOption( 8757732a2dSAndreas Gohr 'filter', 8857732a2dSAndreas Gohr 'Filter tests to run by a given string. (passed to phpunit)', 8957732a2dSAndreas Gohr null, 9057732a2dSAndreas Gohr true, 9157732a2dSAndreas Gohr 'test' 9257732a2dSAndreas Gohr ); 9357732a2dSAndreas Gohr $options->registerArgument('files...', 'The test files to run. Defaults to all.', false, 'test'); 94ec0a687bSAndreas Gohr 95ec0a687bSAndreas Gohr $options->registerCommand('check', 'Check for code style violations.'); 96ec0a687bSAndreas Gohr $options->registerArgument('files...', 'The files to check. Defaults to the whole extension.', false, 'check'); 97ec0a687bSAndreas Gohr 98ec0a687bSAndreas Gohr $options->registerCommand('fix', 'Fix code style violations and refactor outdated code.'); 99ec0a687bSAndreas Gohr $options->registerArgument('files...', 'The files to check. Defaults to the whole extension.', false, 'fix'); 10036c0b2b4SAndreas Gohr } 10136c0b2b4SAndreas Gohr 10236c0b2b4SAndreas Gohr /** @inheritDoc */ 10336c0b2b4SAndreas Gohr protected function main(Options $options) 10436c0b2b4SAndreas Gohr { 1051a23d1dbSAndreas Gohr $args = $options->getArgs(); 1061a23d1dbSAndreas Gohr 10736c0b2b4SAndreas Gohr switch ($options->getCmd()) { 10836c0b2b4SAndreas Gohr case 'init': 10936c0b2b4SAndreas Gohr return $this->cmdInit(); 11036c0b2b4SAndreas Gohr case 'addTest': 11136c0b2b4SAndreas Gohr $test = array_shift($args); 11236c0b2b4SAndreas Gohr return $this->cmdAddTest($test); 11336c0b2b4SAndreas Gohr case 'addConf': 11436c0b2b4SAndreas Gohr return $this->cmdAddConf(); 11536c0b2b4SAndreas Gohr case 'addLang': 11636c0b2b4SAndreas Gohr return $this->cmdAddLang(); 117*719b4841SAndreas Gohr case 'addAgents': 118*719b4841SAndreas Gohr return $this->cmdAddAgents(); 11936c0b2b4SAndreas Gohr case 'addComponent': 12036c0b2b4SAndreas Gohr $type = array_shift($args); 12136c0b2b4SAndreas Gohr $component = array_shift($args); 12236c0b2b4SAndreas Gohr return $this->cmdAddComponent($type, $component); 12336c0b2b4SAndreas Gohr case 'deletedFiles': 12436c0b2b4SAndreas Gohr return $this->cmdDeletedFiles(); 125c5c85a97SAndreas Gohr case 'rmObsolete': 1261a23d1dbSAndreas Gohr return $this->cmdRmObsolete(); 1271a23d1dbSAndreas Gohr case 'downloadSvg': 1281a23d1dbSAndreas Gohr $ident = array_shift($args); 1291a23d1dbSAndreas Gohr $save = array_shift($args); 13070316b84SAndreas Gohr $keep = $options->getOpt('keep-ns'); 13192738407SAndreas Gohr return $this->cmdDownloadSVG($ident, $save, $keep); 1321a23d1dbSAndreas Gohr case 'cleanSvg': 13370316b84SAndreas Gohr $keep = $options->getOpt('keep-ns'); 1348f82d673SAndreas Gohr return $this->cmdCleanSVG($args, $keep); 1355586e97bSAndreas Gohr case 'cleanLang': 1365586e97bSAndreas Gohr return $this->cmdCleanLang(); 137ec0a687bSAndreas Gohr case 'test': 13857732a2dSAndreas Gohr $filter = $options->getOpt('filter'); 13957732a2dSAndreas Gohr return $this->cmdTest($filter, $args); 140ec0a687bSAndreas Gohr case 'check': 141ec0a687bSAndreas Gohr return $this->cmdCheck($args); 142ec0a687bSAndreas Gohr case 'fix': 143ec0a687bSAndreas Gohr return $this->cmdFix(); 14436c0b2b4SAndreas Gohr default: 1451a23d1dbSAndreas Gohr $this->error('Unknown command'); 14636c0b2b4SAndreas Gohr echo $options->help(); 14736c0b2b4SAndreas Gohr return 0; 14836c0b2b4SAndreas Gohr } 14936c0b2b4SAndreas Gohr } 15036c0b2b4SAndreas Gohr 15136c0b2b4SAndreas Gohr /** 15236c0b2b4SAndreas Gohr * Get the extension name from the current working directory 15336c0b2b4SAndreas Gohr * 15436c0b2b4SAndreas Gohr * @throws CliException if something's wrong 15536c0b2b4SAndreas Gohr * @param string $dir 15636c0b2b4SAndreas Gohr * @return string[] name, type 15736c0b2b4SAndreas Gohr */ 15836c0b2b4SAndreas Gohr protected function getTypedNameFromDir($dir) 15936c0b2b4SAndreas Gohr { 16036c0b2b4SAndreas Gohr $pdir = fullpath(DOKU_PLUGIN); 16136c0b2b4SAndreas Gohr $tdir = fullpath(tpl_incdir() . '../'); 16236c0b2b4SAndreas Gohr 16336c0b2b4SAndreas Gohr if (strpos($dir, $pdir) === 0) { 16436c0b2b4SAndreas Gohr $ldir = substr($dir, strlen($pdir)); 16536c0b2b4SAndreas Gohr $type = 'plugin'; 16636c0b2b4SAndreas Gohr } elseif (strpos($dir, $tdir) === 0) { 16736c0b2b4SAndreas Gohr $ldir = substr($dir, strlen($tdir)); 16836c0b2b4SAndreas Gohr $type = 'template'; 16936c0b2b4SAndreas Gohr } else { 17036c0b2b4SAndreas Gohr throw new CliException('Current directory needs to be in plugin or template directory'); 17136c0b2b4SAndreas Gohr } 17236c0b2b4SAndreas Gohr 17336c0b2b4SAndreas Gohr $ldir = trim($ldir, '/'); 17436c0b2b4SAndreas Gohr 17536c0b2b4SAndreas Gohr if (strpos($ldir, '/') !== false) { 17636c0b2b4SAndreas Gohr throw new CliException('Current directory has to be main extension directory'); 17736c0b2b4SAndreas Gohr } 17836c0b2b4SAndreas Gohr 17936c0b2b4SAndreas Gohr return [$ldir, $type]; 18036c0b2b4SAndreas Gohr } 18136c0b2b4SAndreas Gohr 18236c0b2b4SAndreas Gohr /** 18336c0b2b4SAndreas Gohr * Interactively ask for a value from the user 18436c0b2b4SAndreas Gohr * 18536c0b2b4SAndreas Gohr * @param string $prompt 18636c0b2b4SAndreas Gohr * @param bool $cache cache given value for next time? 18736c0b2b4SAndreas Gohr * @return string 18836c0b2b4SAndreas Gohr */ 18936c0b2b4SAndreas Gohr protected function readLine($prompt, $cache = false) 19036c0b2b4SAndreas Gohr { 19136c0b2b4SAndreas Gohr $value = ''; 19236c0b2b4SAndreas Gohr $default = ''; 19336c0b2b4SAndreas Gohr $cachename = getCacheName($prompt, '.readline'); 19436c0b2b4SAndreas Gohr if ($cache && file_exists($cachename)) { 19536c0b2b4SAndreas Gohr $default = file_get_contents($cachename); 19636c0b2b4SAndreas Gohr } 19736c0b2b4SAndreas Gohr 19836c0b2b4SAndreas Gohr while ($value === '') { 19936c0b2b4SAndreas Gohr echo $prompt; 20036c0b2b4SAndreas Gohr if ($default) echo ' [' . $default . ']'; 20136c0b2b4SAndreas Gohr echo ': '; 20236c0b2b4SAndreas Gohr 20336c0b2b4SAndreas Gohr $fh = fopen('php://stdin', 'r'); 20436c0b2b4SAndreas Gohr $value = trim(fgets($fh)); 20536c0b2b4SAndreas Gohr fclose($fh); 20636c0b2b4SAndreas Gohr 20736c0b2b4SAndreas Gohr if ($value === '') $value = $default; 20836c0b2b4SAndreas Gohr } 20936c0b2b4SAndreas Gohr 21036c0b2b4SAndreas Gohr if ($cache) { 21136c0b2b4SAndreas Gohr file_put_contents($cachename, $value); 21236c0b2b4SAndreas Gohr } 21336c0b2b4SAndreas Gohr 21436c0b2b4SAndreas Gohr return $value; 21536c0b2b4SAndreas Gohr } 21636c0b2b4SAndreas Gohr 21736c0b2b4SAndreas Gohr /** 21870316b84SAndreas Gohr * Create the given files with their given content 21936c0b2b4SAndreas Gohr * 22070316b84SAndreas Gohr * Ignores all files that already exist 22170316b84SAndreas Gohr * 22270316b84SAndreas Gohr * @param array $files A File array as created by Skeletor::getFiles() 22336c0b2b4SAndreas Gohr */ 224fcb8165bSAndreas Gohr protected function createFiles($files) 225fcb8165bSAndreas Gohr { 22670316b84SAndreas Gohr foreach ($files as $path => $content) { 22770316b84SAndreas Gohr if (file_exists($path)) { 22870316b84SAndreas Gohr $this->error($path . ' already exists'); 22970316b84SAndreas Gohr continue; 23036c0b2b4SAndreas Gohr } 23136c0b2b4SAndreas Gohr 23270316b84SAndreas Gohr io_makeFileDir($path); 23370316b84SAndreas Gohr file_put_contents($path, $content); 23470316b84SAndreas Gohr $this->success($path . ' created'); 23536c0b2b4SAndreas Gohr } 23636c0b2b4SAndreas Gohr } 23736c0b2b4SAndreas Gohr 23836c0b2b4SAndreas Gohr /** 239c5c85a97SAndreas Gohr * Delete the given file if it exists 240c5c85a97SAndreas Gohr * 241c5c85a97SAndreas Gohr * @param string $file 242c5c85a97SAndreas Gohr */ 243c5c85a97SAndreas Gohr protected function deleteFile($file) 244c5c85a97SAndreas Gohr { 245c5c85a97SAndreas Gohr if (!file_exists($file)) return; 246c5c85a97SAndreas Gohr if (@unlink($file)) { 247c5c85a97SAndreas Gohr $this->success('Delete ' . $file); 248c5c85a97SAndreas Gohr } 249c5c85a97SAndreas Gohr } 250c5c85a97SAndreas Gohr 251c5c85a97SAndreas Gohr /** 252c5c85a97SAndreas Gohr * Run git with the given arguments and return the output 253c5c85a97SAndreas Gohr * 254c5c85a97SAndreas Gohr * @throws CliException when the command can't be run 255c5c85a97SAndreas Gohr * @param string ...$args 256c5c85a97SAndreas Gohr * @return string[] 257c5c85a97SAndreas Gohr */ 258c5c85a97SAndreas Gohr protected function git(...$args) 259c5c85a97SAndreas Gohr { 260c5c85a97SAndreas Gohr $args = array_map('escapeshellarg', $args); 261c5c85a97SAndreas Gohr $cmd = 'git ' . join(' ', $args); 262c5c85a97SAndreas Gohr $output = []; 263c5c85a97SAndreas Gohr $result = 0; 264c5c85a97SAndreas Gohr 265c5c85a97SAndreas Gohr $this->info($cmd); 266c5c85a97SAndreas Gohr $last = exec($cmd, $output, $result); 267c5c85a97SAndreas Gohr if ($last === false || $result !== 0) { 268c5c85a97SAndreas Gohr throw new CliException('Running git failed'); 269c5c85a97SAndreas Gohr } 270c5c85a97SAndreas Gohr 271c5c85a97SAndreas Gohr return $output; 272c5c85a97SAndreas Gohr } 273c5c85a97SAndreas Gohr 274c5c85a97SAndreas Gohr // region Commands 275c5c85a97SAndreas Gohr 276c5c85a97SAndreas Gohr /** 27736c0b2b4SAndreas Gohr * Intialize the current directory as a plugin or template 27836c0b2b4SAndreas Gohr * 27936c0b2b4SAndreas Gohr * @return int 28036c0b2b4SAndreas Gohr */ 28136c0b2b4SAndreas Gohr protected function cmdInit() 28236c0b2b4SAndreas Gohr { 28336c0b2b4SAndreas Gohr $dir = fullpath(getcwd()); 28436c0b2b4SAndreas Gohr if ((new FilesystemIterator($dir))->valid()) { 285fcb8165bSAndreas Gohr // existing directory, initialize from info file 286fcb8165bSAndreas Gohr $skeletor = Skeletor::fromDir($dir); 287fcb8165bSAndreas Gohr } else { 288fcb8165bSAndreas Gohr // new directory, ask for info 28970316b84SAndreas Gohr [$base, $type] = $this->getTypedNameFromDir($dir); 29036c0b2b4SAndreas Gohr $user = $this->readLine('Your Name', true); 29136c0b2b4SAndreas Gohr $mail = $this->readLine('Your E-Mail', true); 29236c0b2b4SAndreas Gohr $desc = $this->readLine('Short description'); 29370316b84SAndreas Gohr $skeletor = new Skeletor($type, $base, $desc, $user, $mail); 294fcb8165bSAndreas Gohr } 29570316b84SAndreas Gohr $skeletor->addBasics(); 29670316b84SAndreas Gohr $this->createFiles($skeletor->getFiles()); 29736c0b2b4SAndreas Gohr 298fcb8165bSAndreas Gohr if (!is_dir("$dir/.git")) { 2998b06c9ddSAndreas Gohr try { 3008b06c9ddSAndreas Gohr $this->git('init'); 3018b06c9ddSAndreas Gohr } catch (CliException $e) { 3028b06c9ddSAndreas Gohr $this->error($e->getMessage()); 3038b06c9ddSAndreas Gohr } 304fcb8165bSAndreas Gohr } 3058b06c9ddSAndreas Gohr 30636c0b2b4SAndreas Gohr return 0; 30736c0b2b4SAndreas Gohr } 30836c0b2b4SAndreas Gohr 30936c0b2b4SAndreas Gohr /** 31036c0b2b4SAndreas Gohr * Add test framework 31136c0b2b4SAndreas Gohr * 31236c0b2b4SAndreas Gohr * @param string $test Name of the Test to add 31336c0b2b4SAndreas Gohr * @return int 31436c0b2b4SAndreas Gohr */ 31536c0b2b4SAndreas Gohr protected function cmdAddTest($test = '') 31636c0b2b4SAndreas Gohr { 31770316b84SAndreas Gohr $skeletor = Skeletor::fromDir(getcwd()); 31870316b84SAndreas Gohr $skeletor->addTest($test); 31970316b84SAndreas Gohr $this->createFiles($skeletor->getFiles()); 32036c0b2b4SAndreas Gohr return 0; 32136c0b2b4SAndreas Gohr } 32236c0b2b4SAndreas Gohr 32336c0b2b4SAndreas Gohr /** 32436c0b2b4SAndreas Gohr * Add configuration 32536c0b2b4SAndreas Gohr * 32636c0b2b4SAndreas Gohr * @return int 32736c0b2b4SAndreas Gohr */ 32836c0b2b4SAndreas Gohr protected function cmdAddConf() 32936c0b2b4SAndreas Gohr { 33070316b84SAndreas Gohr $skeletor = Skeletor::fromDir(getcwd()); 33170316b84SAndreas Gohr $skeletor->addConf(is_dir('lang')); 33270316b84SAndreas Gohr $this->createFiles($skeletor->getFiles()); 33336c0b2b4SAndreas Gohr return 0; 33436c0b2b4SAndreas Gohr } 33536c0b2b4SAndreas Gohr 33636c0b2b4SAndreas Gohr /** 33736c0b2b4SAndreas Gohr * Add language 33836c0b2b4SAndreas Gohr * 33936c0b2b4SAndreas Gohr * @return int 34036c0b2b4SAndreas Gohr */ 34136c0b2b4SAndreas Gohr protected function cmdAddLang() 34236c0b2b4SAndreas Gohr { 34370316b84SAndreas Gohr $skeletor = Skeletor::fromDir(getcwd()); 34470316b84SAndreas Gohr $skeletor->addLang(is_dir('conf')); 34570316b84SAndreas Gohr $this->createFiles($skeletor->getFiles()); 34636c0b2b4SAndreas Gohr return 0; 34736c0b2b4SAndreas Gohr } 34836c0b2b4SAndreas Gohr 34936c0b2b4SAndreas Gohr /** 350*719b4841SAndreas Gohr * Add AGENTS.md 351*719b4841SAndreas Gohr * 352*719b4841SAndreas Gohr * @return int 353*719b4841SAndreas Gohr */ 354*719b4841SAndreas Gohr protected function cmdAddAgents() 355*719b4841SAndreas Gohr { 356*719b4841SAndreas Gohr $skeletor = Skeletor::fromDir(getcwd()); 357*719b4841SAndreas Gohr $skeletor->addAgents(); 358*719b4841SAndreas Gohr $this->createFiles($skeletor->getFiles()); 359*719b4841SAndreas Gohr return 0; 360*719b4841SAndreas Gohr } 361*719b4841SAndreas Gohr 362*719b4841SAndreas Gohr /** 36336c0b2b4SAndreas Gohr * Add another component to the plugin 36436c0b2b4SAndreas Gohr * 36536c0b2b4SAndreas Gohr * @param string $type 36636c0b2b4SAndreas Gohr * @param string $component 36736c0b2b4SAndreas Gohr */ 36836c0b2b4SAndreas Gohr protected function cmdAddComponent($type, $component = '') 36936c0b2b4SAndreas Gohr { 37070316b84SAndreas Gohr $skeletor = Skeletor::fromDir(getcwd()); 37170316b84SAndreas Gohr $skeletor->addComponent($type, $component); 37270316b84SAndreas Gohr $this->createFiles($skeletor->getFiles()); 37336c0b2b4SAndreas Gohr return 0; 37436c0b2b4SAndreas Gohr } 37536c0b2b4SAndreas Gohr 37636c0b2b4SAndreas Gohr /** 37736c0b2b4SAndreas Gohr * Generate a list of deleted files from git 37836c0b2b4SAndreas Gohr * 37936c0b2b4SAndreas Gohr * @link https://stackoverflow.com/a/6018049/172068 38036c0b2b4SAndreas Gohr */ 38136c0b2b4SAndreas Gohr protected function cmdDeletedFiles() 38236c0b2b4SAndreas Gohr { 3838b06c9ddSAndreas Gohr if (!is_dir('.git')) throw new CliException('This extension seems not to be managed by git'); 38436c0b2b4SAndreas Gohr 3858b06c9ddSAndreas Gohr $output = $this->git('log', '--no-renames', '--pretty=format:', '--name-only', '--diff-filter=D'); 38636c0b2b4SAndreas Gohr $output = array_map('trim', $output); 38736c0b2b4SAndreas Gohr $output = array_filter($output); 38836c0b2b4SAndreas Gohr $output = array_unique($output); 38936c0b2b4SAndreas Gohr $output = array_filter($output, function ($item) { 39036c0b2b4SAndreas Gohr return !file_exists($item); 39136c0b2b4SAndreas Gohr }); 39236c0b2b4SAndreas Gohr sort($output); 39336c0b2b4SAndreas Gohr 39436c0b2b4SAndreas Gohr if (!count($output)) { 39536c0b2b4SAndreas Gohr $this->info('No deleted files found'); 39636c0b2b4SAndreas Gohr return 0; 39736c0b2b4SAndreas Gohr } 39836c0b2b4SAndreas Gohr 39936c0b2b4SAndreas Gohr $content = "# This is a list of files that were present in previous releases\n" . 40036c0b2b4SAndreas Gohr "# but were removed later. They should not exist in your installation.\n" . 40136c0b2b4SAndreas Gohr join("\n", $output) . "\n"; 40236c0b2b4SAndreas Gohr 40336c0b2b4SAndreas Gohr file_put_contents('deleted.files', $content); 40436c0b2b4SAndreas Gohr $this->success('written deleted.files'); 40536c0b2b4SAndreas Gohr return 0; 40636c0b2b4SAndreas Gohr } 4078b06c9ddSAndreas Gohr 4088b06c9ddSAndreas Gohr /** 409c5c85a97SAndreas Gohr * Remove files that shouldn't be here anymore 4108b06c9ddSAndreas Gohr */ 4111a23d1dbSAndreas Gohr protected function cmdRmObsolete() 4128b06c9ddSAndreas Gohr { 413c5c85a97SAndreas Gohr $this->deleteFile('_test/general.test.php'); 414c5c85a97SAndreas Gohr $this->deleteFile('.travis.yml'); 41553bec4caSAndreas Gohr $this->deleteFile('.github/workflows/phpTestLinux.yml'); 4168b06c9ddSAndreas Gohr 417c5c85a97SAndreas Gohr return 0; 4188b06c9ddSAndreas Gohr } 4198b06c9ddSAndreas Gohr 4201a23d1dbSAndreas Gohr /** 4211a23d1dbSAndreas Gohr * Download a remote icon 4221a23d1dbSAndreas Gohr * 4231a23d1dbSAndreas Gohr * @param string $ident 4241a23d1dbSAndreas Gohr * @param string $save 42592738407SAndreas Gohr * @param bool $keep 4261a23d1dbSAndreas Gohr * @return int 4271a23d1dbSAndreas Gohr * @throws Exception 4281a23d1dbSAndreas Gohr */ 42992738407SAndreas Gohr protected function cmdDownloadSVG($ident, $save = '', $keep = false) 4301a23d1dbSAndreas Gohr { 4311a23d1dbSAndreas Gohr $svg = new SVGIcon($this); 43292738407SAndreas Gohr $svg->keepNamespace($keep); 4331a23d1dbSAndreas Gohr return (int)$svg->downloadRemoteIcon($ident, $save); 4341a23d1dbSAndreas Gohr } 4351a23d1dbSAndreas Gohr 4361a23d1dbSAndreas Gohr /** 4378f82d673SAndreas Gohr * @param string[] $files 43892738407SAndreas Gohr * @param bool $keep 4391a23d1dbSAndreas Gohr * @return int 4401a23d1dbSAndreas Gohr * @throws Exception 4411a23d1dbSAndreas Gohr */ 4428f82d673SAndreas Gohr protected function cmdCleanSVG($files, $keep = false) 4431a23d1dbSAndreas Gohr { 4441a23d1dbSAndreas Gohr $svg = new SVGIcon($this); 44592738407SAndreas Gohr $svg->keepNamespace($keep); 4468f82d673SAndreas Gohr 4478f82d673SAndreas Gohr $ok = true; 4488f82d673SAndreas Gohr foreach ($files as $file) { 4498f82d673SAndreas Gohr $ok = $ok && $svg->cleanSVGFile($file); 4508f82d673SAndreas Gohr } 4518f82d673SAndreas Gohr return (int)$ok; 4521a23d1dbSAndreas Gohr } 4531a23d1dbSAndreas Gohr 4545586e97bSAndreas Gohr /** 4555586e97bSAndreas Gohr * @return int 4565586e97bSAndreas Gohr */ 4575586e97bSAndreas Gohr protected function cmdCleanLang() 4585586e97bSAndreas Gohr { 4595586e97bSAndreas Gohr $lp = new LangProcessor($this); 4605586e97bSAndreas Gohr 4615586e97bSAndreas Gohr $files = glob('./lang/*/lang.php'); 4625b2e8f12SAndreas Gohr foreach ($files as $file) { 4635b2e8f12SAndreas Gohr $lp->processLangFile($file); 4645b2e8f12SAndreas Gohr } 4655b2e8f12SAndreas Gohr 4665b2e8f12SAndreas Gohr $files = glob('./lang/*/settings.php'); 4675586e97bSAndreas Gohr foreach ($files as $file) { 468f4f76afdSAndreas Gohr $lp->processSettingsFile($file); 4695586e97bSAndreas Gohr } 4705586e97bSAndreas Gohr 4715586e97bSAndreas Gohr return 0; 4725586e97bSAndreas Gohr } 4735586e97bSAndreas Gohr 474ec0a687bSAndreas Gohr /** 47557732a2dSAndreas Gohr * Run the unit tests for this extension 47657732a2dSAndreas Gohr * 47757732a2dSAndreas Gohr * @param string $filter Optional filter string for phpunit 47857732a2dSAndreas Gohr * @param string[] $args Additional arguments to pass to phpunit (files) 479ec0a687bSAndreas Gohr * @return int 480ec0a687bSAndreas Gohr */ 48157732a2dSAndreas Gohr protected function cmdTest($filter = '', $args = []) 482ec0a687bSAndreas Gohr { 483ec0a687bSAndreas Gohr $dir = fullpath(getcwd()); 484ec0a687bSAndreas Gohr [$base, $type] = $this->getTypedNameFromDir($dir); 485ec0a687bSAndreas Gohr 486ec0a687bSAndreas Gohr if ($this->colors->isEnabled()) { 487ec0a687bSAndreas Gohr $colors = 'always'; 488ec0a687bSAndreas Gohr } else { 489ec0a687bSAndreas Gohr $colors = 'never'; 490ec0a687bSAndreas Gohr } 491ec0a687bSAndreas Gohr 49257732a2dSAndreas Gohr $bin = fullpath(__DIR__ . '/../../../_test/vendor/bin/phpunit');; 49357732a2dSAndreas Gohr if (!file_exists($bin)) { 49457732a2dSAndreas Gohr $this->error('Testing framework not found. Please run "composer install" in the _test/ directory first.'); 49557732a2dSAndreas Gohr return 1; 49657732a2dSAndreas Gohr } 49757732a2dSAndreas Gohr 49857732a2dSAndreas Gohr $runArgs = [ 49957732a2dSAndreas Gohr $bin, 500ec0a687bSAndreas Gohr '--verbose', 501ec0a687bSAndreas Gohr "--colors=$colors", 502ec0a687bSAndreas Gohr '--configuration', fullpath(__DIR__ . '/../../../_test/phpunit.xml'), 503ec0a687bSAndreas Gohr '--group', $type . '_' . $base, 504ec0a687bSAndreas Gohr ]; 50557732a2dSAndreas Gohr if ($filter) { 50657732a2dSAndreas Gohr $runArgs[] = '--filter'; 50757732a2dSAndreas Gohr $runArgs[] = $filter; 50857732a2dSAndreas Gohr } 50957732a2dSAndreas Gohr 51057732a2dSAndreas Gohr $runArgs = array_merge($runArgs, $args); 51157732a2dSAndreas Gohr $cmd = join(' ', array_map('escapeshellarg', $runArgs)); 512ec0a687bSAndreas Gohr $this->info("Running $cmd"); 513ec0a687bSAndreas Gohr 514ec0a687bSAndreas Gohr $result = 0; 515ec0a687bSAndreas Gohr passthru($cmd, $result); 516ec0a687bSAndreas Gohr return $result; 517ec0a687bSAndreas Gohr } 518ec0a687bSAndreas Gohr 519ec0a687bSAndreas Gohr /** 520ec0a687bSAndreas Gohr * @return int 521ec0a687bSAndreas Gohr */ 522ec0a687bSAndreas Gohr protected function cmdCheck($files = []) 523ec0a687bSAndreas Gohr { 524ec0a687bSAndreas Gohr $dir = fullpath(getcwd()); 525ec0a687bSAndreas Gohr 526ec0a687bSAndreas Gohr $args = [ 527ec0a687bSAndreas Gohr fullpath(__DIR__ . '/../../../_test/vendor/bin/phpcs'), 528ec0a687bSAndreas Gohr '--standard=' . fullpath(__DIR__ . '/../../../_test/phpcs.xml'), 529ec0a687bSAndreas Gohr ($this->colors->isEnabled()) ? '--colors' : '--no-colors', 530ec0a687bSAndreas Gohr '--', 531ec0a687bSAndreas Gohr ]; 532ec0a687bSAndreas Gohr 533ec0a687bSAndreas Gohr if ($files) { 534ec0a687bSAndreas Gohr $args = array_merge($args, $files); 535ec0a687bSAndreas Gohr } else { 536ec0a687bSAndreas Gohr $args[] = fullpath($dir); 537ec0a687bSAndreas Gohr } 538ec0a687bSAndreas Gohr 539ec0a687bSAndreas Gohr $cmd = join(' ', array_map('escapeshellarg', $args)); 540ec0a687bSAndreas Gohr $this->info("Running $cmd"); 541ec0a687bSAndreas Gohr 542ec0a687bSAndreas Gohr $result = 0; 543ec0a687bSAndreas Gohr passthru($cmd, $result); 544ec0a687bSAndreas Gohr return $result; 545ec0a687bSAndreas Gohr } 546ec0a687bSAndreas Gohr 547ec0a687bSAndreas Gohr /** 548ec0a687bSAndreas Gohr * @return int 549ec0a687bSAndreas Gohr */ 550ec0a687bSAndreas Gohr protected function cmdFix($files = []) 551ec0a687bSAndreas Gohr { 552ec0a687bSAndreas Gohr $dir = fullpath(getcwd()); 553ec0a687bSAndreas Gohr 554ec0a687bSAndreas Gohr // first run rector to refactor outdated code 555ec0a687bSAndreas Gohr $args = [ 556ec0a687bSAndreas Gohr fullpath(__DIR__ . '/../../../_test/vendor/bin/rector'), 557ec0a687bSAndreas Gohr ($this->colors->isEnabled()) ? '--ansi' : '--no-ansi', 558ec0a687bSAndreas Gohr '--config=' . fullpath(__DIR__ . '/../../../_test/rector.php'), 559ec0a687bSAndreas Gohr '--no-diffs', 560ec0a687bSAndreas Gohr 'process', 561ec0a687bSAndreas Gohr ]; 562ec0a687bSAndreas Gohr 563ec0a687bSAndreas Gohr if ($files) { 564ec0a687bSAndreas Gohr $args = array_merge($args, $files); 565ec0a687bSAndreas Gohr } else { 566ec0a687bSAndreas Gohr $args[] = fullpath($dir); 567ec0a687bSAndreas Gohr } 568ec0a687bSAndreas Gohr 569ec0a687bSAndreas Gohr $cmd = join(' ', array_map('escapeshellarg', $args)); 570ec0a687bSAndreas Gohr $this->info("Running $cmd"); 571ec0a687bSAndreas Gohr 572ec0a687bSAndreas Gohr $result = 0; 573ec0a687bSAndreas Gohr passthru($cmd, $result); 574ec0a687bSAndreas Gohr if ($result !== 0) return $result; 575ec0a687bSAndreas Gohr 576ec0a687bSAndreas Gohr // now run phpcbf to clean up code style 577ec0a687bSAndreas Gohr $args = [ 578ec0a687bSAndreas Gohr fullpath(__DIR__ . '/../../../_test/vendor/bin/phpcbf'), 579ec0a687bSAndreas Gohr '--standard=' . fullpath(__DIR__ . '/../../../_test/phpcs.xml'), 580ec0a687bSAndreas Gohr ($this->colors->isEnabled()) ? '--colors' : '--no-colors', 581ec0a687bSAndreas Gohr '--', 582ec0a687bSAndreas Gohr ]; 583ec0a687bSAndreas Gohr 584ec0a687bSAndreas Gohr if ($files) { 585ec0a687bSAndreas Gohr $args = array_merge($args, $files); 586ec0a687bSAndreas Gohr } else { 587ec0a687bSAndreas Gohr $args[] = fullpath($dir); 588ec0a687bSAndreas Gohr } 589ec0a687bSAndreas Gohr 590ec0a687bSAndreas Gohr $cmd = join(' ', array_map('escapeshellarg', $args)); 591ec0a687bSAndreas Gohr $this->info("Running $cmd"); 592ec0a687bSAndreas Gohr 593ec0a687bSAndreas Gohr $result = 0; 594ec0a687bSAndreas Gohr passthru($cmd, $result); 595ec0a687bSAndreas Gohr return $result; 596ec0a687bSAndreas Gohr } 597ec0a687bSAndreas Gohr 598c5c85a97SAndreas Gohr //endregion 59936c0b2b4SAndreas Gohr} 600