Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistLiberateWorkflow.php
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | if (count($argv) > 1) { | ||||
$paths[] = Filesystem::resolvePath(dirname($init)); | $paths[] = Filesystem::resolvePath(dirname($init)); | ||||
} | } | ||||
} else { | } else { | ||||
$paths = array( | $paths = array( | ||||
Filesystem::resolvePath(head($argv)), | Filesystem::resolvePath(head($argv)), | ||||
); | ); | ||||
} | } | ||||
$any_errors = false; | |||||
foreach ($paths as $path) { | foreach ($paths as $path) { | ||||
$log->writeStatus( | $log->writeStatus( | ||||
pht('WORK'), | pht('WORK'), | ||||
pht( | pht( | ||||
'Updating library: %s', | 'Updating library: %s', | ||||
Filesystem::readablePath($path).DIRECTORY_SEPARATOR)); | Filesystem::readablePath($path).DIRECTORY_SEPARATOR)); | ||||
$this->liberatePath($path); | $exit_code = $this->liberatePath($path); | ||||
if ($exit_code !== 0) { | |||||
$any_errors = true; | |||||
$log->writeError( | |||||
pht('ERROR'), | |||||
pht('Failed to update library: %s', $path)); | |||||
} | |||||
} | } | ||||
if (!$any_errors) { | |||||
$log->writeSuccess( | $log->writeSuccess( | ||||
pht('DONE'), | pht('DONE'), | ||||
pht('Updated %s librarie(s).', phutil_count($paths))); | pht('Updated %s librarie(s).', phutil_count($paths))); | ||||
} | |||||
return 0; | return 0; | ||||
} | } | ||||
/** | |||||
* @return int The exit code of running the rebuild-map.php script, which | |||||
* will be 0 to indicate success or non-zero for failure. | |||||
*/ | |||||
private function liberatePath($path) { | private function liberatePath($path) { | ||||
if (!Filesystem::pathExists($path.'/__phutil_library_init__.php')) { | if (!Filesystem::pathExists($path.'/__phutil_library_init__.php')) { | ||||
echo tsprintf( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht( | pht( | ||||
'No library currently exists at the path "%s"...', | 'No library currently exists at the path "%s"...', | ||||
$path)); | $path)); | ||||
$this->liberateCreateDirectory($path); | $this->liberateCreateDirectory($path); | ||||
$this->liberateCreateLibrary($path); | return $this->liberateCreateLibrary($path); | ||||
return; | |||||
} | } | ||||
$version = $this->getLibraryFormatVersion($path); | $version = $this->getLibraryFormatVersion($path); | ||||
switch ($version) { | switch ($version) { | ||||
case 1: | case 1: | ||||
throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
pht( | pht( | ||||
'This very old library is no longer supported.')); | 'This very old library is no longer supported.')); | ||||
case 2: | case 2: | ||||
return $this->liberateVersion2($path); | return $this->liberateVersion2($path); | ||||
default: | default: | ||||
throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
pht("Unknown library version '%s'!", $version)); | pht("Unknown library version '%s'!", $version)); | ||||
} | } | ||||
echo tsprintf("%s\n", pht('Done.')); | |||||
} | } | ||||
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; | ||||
} | } | ||||
$map = Filesystem::readFile($map_file); | $map = Filesystem::readFile($map_file); | ||||
$matches = null; | $matches = null; | ||||
if (preg_match('/@phutil-library-version (\d+)/', $map, $matches)) { | if (preg_match('/@phutil-library-version (\d+)/', $map, $matches)) { | ||||
return (int)$matches[1]; | return (int)$matches[1]; | ||||
} | } | ||||
return 1; | return 1; | ||||
} | } | ||||
/** | |||||
* @return int The exit code of running the rebuild-map.php script, which | |||||
* will be 0 to indicate success or non-zero for failure. | |||||
*/ | |||||
private function liberateVersion2($path) { | private function liberateVersion2($path) { | ||||
$bin = $this->getScriptPath('support/lib/rebuild-map.php'); | $bin = $this->getScriptPath('support/lib/rebuild-map.php'); | ||||
$argv = array(); | $argv = array(); | ||||
if ($this->getArgument('clean')) { | if ($this->getArgument('clean')) { | ||||
$argv[] = '--drop-cache'; | $argv[] = '--drop-cache'; | ||||
} | } | ||||
Show All 25 Lines | private function liberateCreateDirectory($path) { | ||||
$this->getPrompt('arc.liberate.create') | $this->getPrompt('arc.liberate.create') | ||||
->setQuery($query) | ->setQuery($query) | ||||
->execute(); | ->execute(); | ||||
execx('mkdir -p %R', $path); | execx('mkdir -p %R', $path); | ||||
} | } | ||||
/** | |||||
* @return int The exit code of running the rebuild-map.php script, which | |||||
* will be 0 to indicate success or non-zero for failure. | |||||
*/ | |||||
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 0; | ||||
} | } | ||||
echo pht("Creating new libphutil library in '%s'.", $path)."\n"; | echo pht("Creating new libphutil library in '%s'.", $path)."\n"; | ||||
do { | do { | ||||
echo pht('Choose a name for the new library.')."\n"; | echo pht('Choose a name for the new library.')."\n"; | ||||
$name = phutil_console_prompt( | $name = phutil_console_prompt( | ||||
pht('What do you want to name this library?')); | pht('What do you want to name this library?')); | ||||
Show All 12 Lines | $template = | ||||
"<?php\n\n". | "<?php\n\n". | ||||
"phutil_register_library('{$name}', __FILE__);\n"; | "phutil_register_library('{$name}', __FILE__);\n"; | ||||
echo pht( | echo pht( | ||||
"Writing '%s' to '%s'...\n", | "Writing '%s' to '%s'...\n", | ||||
'__phutil_library_init__.php', | '__phutil_library_init__.php', | ||||
$path); | $path); | ||||
Filesystem::writeFile($init_path, $template); | Filesystem::writeFile($init_path, $template); | ||||
$this->liberateVersion2($path); | return $this->liberateVersion2($path); | ||||
} | } | ||||
private function getScriptPath($script) { | private function getScriptPath($script) { | ||||
$root = dirname(phutil_get_library_root('arcanist')); | $root = dirname(phutil_get_library_root('arcanist')); | ||||
return $root.'/'.$script; | return $root.'/'.$script; | ||||
} | } | ||||
} | } |