Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistLiberateWorkflow.php
| Show All 28 Lines | return phutil_console_format(<<<EOTEXT | ||||
| files like \__init__.php. | files like \__init__.php. | ||||
| EOTEXT | EOTEXT | ||||
| ); | ); | ||||
| } | } | ||||
| public function getArguments() { | public function getArguments() { | ||||
| return array( | return array( | ||||
| 'all' => array( | 'all' => array( | ||||
| 'help' => | 'help' => pht( | ||||
| 'Drop the module cache before liberating. This will completely '. | 'Drop the module cache before liberating. This will completely '. | ||||
| 'reanalyze the entire library. Thorough, but slow!', | 'reanalyze the entire library. Thorough, but slow!'), | ||||
| ), | ), | ||||
| 'force-update' => array( | 'force-update' => array( | ||||
| 'help' => | 'help' => pht( | ||||
| 'Force the library map to be updated, even in the presence of '. | 'Force the library map to be updated, even in the presence of '. | ||||
| 'lint errors.', | 'lint errors.'), | ||||
| ), | ), | ||||
| 'library-name' => array( | 'library-name' => array( | ||||
| 'param' => 'name', | 'param' => 'name', | ||||
| 'help' => | 'help' => | ||||
| 'Use a flag for library name rather than awaiting user input.', | pht('Use a flag for library name rather than awaiting user input.'), | ||||
| ), | ), | ||||
| 'remap' => array( | 'remap' => array( | ||||
| 'hide' => true, | 'hide' => true, | ||||
| 'help' => | 'help' => pht( | ||||
| 'Internal. Run the remap step of liberation. You do not need to '. | 'Internal. Run the remap step of liberation. You do not need to '. | ||||
| 'run this unless you are debugging the workflow.', | 'run this unless you are debugging the workflow.'), | ||||
| ), | ), | ||||
| 'verify' => array( | 'verify' => array( | ||||
| 'hide' => true, | 'hide' => true, | ||||
| 'help' => | 'help' => pht( | ||||
| 'Internal. Run the verify step of liberation. You do not need to '. | 'Internal. Run the verify step of liberation. You do not need to '. | ||||
| 'run this unless you are debugging the workflow.', | 'run this unless you are debugging the workflow.'), | ||||
| ), | ), | ||||
| 'upgrade' => array( | 'upgrade' => array( | ||||
| 'hide' => true, | 'hide' => true, | ||||
| 'help' => 'Experimental. Upgrade library to v2.', | 'help' => pht('Experimental. Upgrade library to v2.'), | ||||
| ), | ), | ||||
| '*' => 'argv', | '*' => 'argv', | ||||
| ); | ); | ||||
| } | } | ||||
| public function run() { | public function run() { | ||||
| $argv = $this->getArgument('argv'); | $argv = $this->getArgument('argv'); | ||||
| if (count($argv) > 1) { | if (count($argv) > 1) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| "Provide only one path to 'arc liberate'. The path should be a ". | pht( | ||||
| "directory where you want to create or update a libphutil library."); | "Provide only one path to '%s'. The path should be a directory ". | ||||
| "where you want to create or update a libphutil library.", | |||||
| 'arc liberate')); | |||||
| } else if (count($argv) == 0) { | } else if (count($argv) == 0) { | ||||
| $path = getcwd(); | $path = getcwd(); | ||||
| } else { | } else { | ||||
| $path = reset($argv); | $path = reset($argv); | ||||
| } | } | ||||
| $is_remap = $this->getArgument('remap'); | $is_remap = $this->getArgument('remap'); | ||||
| $is_verify = $this->getArgument('verify'); | $is_verify = $this->getArgument('verify'); | ||||
| $path = Filesystem::resolvePath($path); | $path = Filesystem::resolvePath($path); | ||||
| if (Filesystem::pathExists($path) && is_dir($path)) { | if (Filesystem::pathExists($path) && is_dir($path)) { | ||||
| $init = id(new FileFinder($path)) | $init = id(new FileFinder($path)) | ||||
| ->withPath('*/__phutil_library_init__.php') | ->withPath('*/__phutil_library_init__.php') | ||||
| ->find(); | ->find(); | ||||
| } else { | } else { | ||||
| $init = null; | $init = null; | ||||
| } | } | ||||
| if ($init) { | if ($init) { | ||||
| if (count($init) > 1) { | if (count($init) > 1) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| 'Specified directory contains more than one libphutil library. Use '. | pht( | ||||
| 'a more specific path.'); | 'Specified directory contains more than one libphutil library. '. | ||||
| 'Use a more specific path.')); | |||||
| } | } | ||||
| $path = Filesystem::resolvePath(dirname(reset($init)), $path); | $path = Filesystem::resolvePath(dirname(reset($init)), $path); | ||||
| } else { | } else { | ||||
| $found = false; | $found = false; | ||||
| foreach (Filesystem::walkToRoot($path) as $dir) { | foreach (Filesystem::walkToRoot($path) as $dir) { | ||||
| if (Filesystem::pathExists($dir.'/__phutil_library_init__.php')) { | if (Filesystem::pathExists($dir.'/__phutil_library_init__.php')) { | ||||
| $path = $dir; | $path = $dir; | ||||
| $found = true; | $found = true; | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| if (!$found) { | if (!$found) { | ||||
| echo "No library currently exists at that path...\n"; | echo pht("No library currently exists at that path...\n"); | ||||
| $this->liberateCreateDirectory($path); | $this->liberateCreateDirectory($path); | ||||
| $this->liberateCreateLibrary($path); | $this->liberateCreateLibrary($path); | ||||
| return; | return; | ||||
| } | } | ||||
| } | } | ||||
| $version = $this->getLibraryFormatVersion($path); | $version = $this->getLibraryFormatVersion($path); | ||||
| switch ($version) { | switch ($version) { | ||||
| case 1: | case 1: | ||||
| if ($this->getArgument('upgrade')) { | if ($this->getArgument('upgrade')) { | ||||
| return $this->upgradeLibrary($path); | return $this->upgradeLibrary($path); | ||||
| } | } | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| "This library is using libphutil v1, which is no longer supported. ". | pht( | ||||
| "Run 'arc liberate --upgrade' to upgrade to v2."); | "This library is using libphutil v1, which is no ". | ||||
| "longer supported. Run '%s' to upgrade to v2.", | |||||
| 'arc liberate --upgrade')); | |||||
| case 2: | case 2: | ||||
| if ($this->getArgument('upgrade')) { | if ($this->getArgument('upgrade')) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| "Can't upgrade a v2 library!"); | pht("Can't upgrade a v2 library!")); | ||||
| } | } | ||||
| return $this->liberateVersion2($path); | return $this->liberateVersion2($path); | ||||
| default: | default: | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| "Unknown library version '{$version}'!"); | pht("Unknown library version '%s'!", $version)); | ||||
| } | } | ||||
| } | } | ||||
| private function getLibraryFormatVersion($path) { | private function getLibraryFormatVersion($path) { | ||||
| $map_file = $path.'/__phutil_library_map__.php'; | $map_file = $path.'/__phutil_library_map__.php'; | ||||
| if (!Filesystem::pathExists($map_file)) { | if (!Filesystem::pathExists($map_file)) { | ||||
| // Default to library v1. | // Default to library v1. | ||||
| return 1; | return 1; | ||||
| Show All 20 Lines | EOTEXT | ||||
| } | } | ||||
| private function upgradeLibrary($path) { | private function upgradeLibrary($path) { | ||||
| $inits = id(new FileFinder($path)) | $inits = id(new FileFinder($path)) | ||||
| ->withPath('*/__init__.php') | ->withPath('*/__init__.php') | ||||
| ->withType('f') | ->withType('f') | ||||
| ->find(); | ->find(); | ||||
| echo "Removing __init__.php files...\n"; | echo pht('Removing %s files...', '__init__.php')."\n"; | ||||
| foreach ($inits as $init) { | foreach ($inits as $init) { | ||||
| Filesystem::remove($path.'/'.$init); | Filesystem::remove($path.'/'.$init); | ||||
| } | } | ||||
| echo "Upgrading library to v2...\n"; | echo pht('Upgrading library to v2...')."\n"; | ||||
| $this->liberateVersion2($path); | $this->liberateVersion2($path); | ||||
| } | } | ||||
| private function liberateCreateDirectory($path) { | private function liberateCreateDirectory($path) { | ||||
| if (Filesystem::pathExists($path)) { | if (Filesystem::pathExists($path)) { | ||||
| if (!is_dir($path)) { | if (!is_dir($path)) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| 'Provide a directory to create or update a libphutil library in.'); | pht( | ||||
| 'Provide a directory to create or update a libphutil library in.')); | |||||
| } | } | ||||
| return; | return; | ||||
| } | } | ||||
| echo "The directory '{$path}' does not exist."; | echo pht("The directory '%s' does not exist.", $path); | ||||
| if (!phutil_console_confirm('Do you want to create it?')) { | if (!phutil_console_confirm(pht('Do you want to create it?'))) { | ||||
| throw new ArcanistUsageException('Cancelled.'); | throw new ArcanistUsageException(pht('Canceled.')); | ||||
| } | } | ||||
| execx('mkdir -p %s', $path); | execx('mkdir -p %s', $path); | ||||
| } | } | ||||
| private function liberateCreateLibrary($path) { | private function liberateCreateLibrary($path) { | ||||
| $init_path = $path.'/__phutil_library_init__.php'; | $init_path = $path.'/__phutil_library_init__.php'; | ||||
| if (Filesystem::pathExists($init_path)) { | if (Filesystem::pathExists($init_path)) { | ||||
| return; | return; | ||||
| } | } | ||||
| echo "Creating new libphutil library in '{$path}'.\n"; | echo pht("Creating new libphutil library in '%s'.", $path)."\n"; | ||||
| do { | do { | ||||
| $name = $this->getArgument('library-name'); | $name = $this->getArgument('library-name'); | ||||
| if ($name === null) { | if ($name === null) { | ||||
| echo "Choose a name for the new library.\n"; | echo pht('Choose a name for the new library.')."\n"; | ||||
| $name = phutil_console_prompt('What do you want to name this library?'); | $name = phutil_console_prompt( | ||||
| pht('What do you want to name this library?')); | |||||
| } else { | } else { | ||||
| echo "Using library name {$name}.\n"; | echo pht('Using library name %s.', $name)."\n"; | ||||
| } | } | ||||
| if (preg_match('/^[a-z-]+$/', $name)) { | if (preg_match('/^[a-z-]+$/', $name)) { | ||||
| break; | break; | ||||
| } else { | } else { | ||||
| echo "Library name should contain only lowercase letters and ". | echo pht( | ||||
| "hyphens.\n"; | 'Library name should contain only lowercase letters and hyphens.')."\n"; | ||||
| } | } | ||||
| } while (true); | } while (true); | ||||
| $template = | $template = | ||||
| "<?php\n\n". | "<?php\n\n". | ||||
| "phutil_register_library('{$name}', __FILE__);\n"; | "phutil_register_library('{$name}', __FILE__);\n"; | ||||
| echo "Writing '__phutil_library_init__.php' to '{$path}'...\n"; | echo pht( | ||||
| "Writing '%s' to '%s'...\n", | |||||
| '__phutil_library_init__.php', | |||||
| $path); | |||||
| Filesystem::writeFile($init_path, $template); | Filesystem::writeFile($init_path, $template); | ||||
| $this->liberateVersion2($path); | $this->liberateVersion2($path); | ||||
| } | } | ||||
| private function getScriptPath($script) { | private function getScriptPath($script) { | ||||
| $root = dirname(phutil_get_library_root('phutil')); | $root = dirname(phutil_get_library_root('phutil')); | ||||
| return $root.'/'.$script; | return $root.'/'.$script; | ||||
| } | } | ||||
| } | } | ||||