From a8ddec4f64d90228ca65a398ed470b4df4e363e4 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 30 May 2012 14:18:11 -0700 Subject: [PATCH] Allow "arc liberate" to liberate v2 libraries and upgrade v1 -> v2 Summary: - "arc liberate" now works for v1 or v2 libraries. - "arc liberate --upgrade" converts a v1 to a v2. - We delete __init__.php files but do not move Class.php files, since this is vastly simpler. Authors can do this on their own if they want. We'll do it separately. - v2 has less lint support than v1, but I think we can move first and migrate lint support later. Much of the v1 lint is bad anyway. Test Plan: Upgraded libphutil, arcanist and phabricator to v2 Reviewers: vrana, btrahan, jungejason Reviewed By: vrana CC: aran Maniphest Tasks: T1103 Differential Revision: https://secure.phabricator.com/D2588 --- .../ArcanistPhutilModuleLinter.php | 9 ++- .../liberate/ArcanistLiberateWorkflow.php | 67 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php b/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php index 747f21e7..5539219a 100644 --- a/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php +++ b/src/lint/linter/phutilmodule/ArcanistPhutilModuleLinter.php @@ -108,6 +108,7 @@ final class ArcanistPhutilModuleLinter extends ArcanistLinter { $moduleinfo = array(); $project_root = $this->getEngine()->getWorkingCopy()->getProjectRoot(); + $bootloader = PhutilBootloader::getInstance(); foreach ($paths as $path) { $absolute_path = $project_root.'/'.$path; @@ -119,6 +120,12 @@ final class ArcanistPhutilModuleLinter extends ArcanistLinter { continue; } $library_name = phutil_get_library_name_for_root($library_root); + + $version = $bootloader->getLibraryFormatVersion($library_name); + if ($version != 1) { + continue; + } + if (!is_dir($path)) { $path = dirname($path); } @@ -128,7 +135,7 @@ final class ArcanistPhutilModuleLinter extends ArcanistLinter { if ($path == $library_root) { continue; } - $map = PhutilBootloader::getInstance()->getLibraryMap($library_name); + $map = $bootloader->getLibraryMap($library_name); $module_name = Filesystem::readablePath($path, $library_root); $module_key = $library_name.':'.$module_name; if (empty($modules[$module_key])) { diff --git a/src/workflow/liberate/ArcanistLiberateWorkflow.php b/src/workflow/liberate/ArcanistLiberateWorkflow.php index dea2a113..a709aa53 100644 --- a/src/workflow/liberate/ArcanistLiberateWorkflow.php +++ b/src/workflow/liberate/ArcanistLiberateWorkflow.php @@ -69,6 +69,10 @@ EOTEXT "Internal. Run the verify step of liberation. You do not need to ". "run this unless you are debugging the workflow.", ), + 'upgrade' => array( + 'hide' => true, + 'help' => "Experimental. Upgrade library to v2.", + ), '*' => 'argv', ); } @@ -120,6 +124,69 @@ EOTEXT } } + $version = $this->getLibraryFormatVersion($path); + switch ($version) { + case 1: + if ($this->getArgument('upgrade')) { + return $this->upgradeLibrary($path); + } + return $this->liberateVersion1($path); + case 2: + if ($this->getArgument('upgrade')) { + throw new ArcanistUsageException( + "Can't upgrade a v2 library!"); + } + return $this->liberateVersion2($path); + default: + throw new ArcanistUsageException( + "Unknown library version '{$version}'!"); + } + } + + private function getLibraryFormatVersion($path) { + $map_file = $path.'/__phutil_library_map__.php'; + if (!Filesystem::pathExists($map_file)) { + // Default to library v1. + return 1; + } + + $map = Filesystem::readFile($map_file); + + $matches = null; + if (preg_match('/@phutil-library-version (\d+)/', $map, $matches)) { + return (int)$matches[1]; + } + + return 1; + } + + private function liberateVersion2($path) { + $bin = $this->getScriptPath('scripts/phutil_rebuild_map.php'); + + return phutil_passthru( + '%s %C %s', + $bin, + $this->getArgument('all') ? '--drop-cache' : '', + $path); + } + + private function upgradeLibrary($path) { + $inits = id(new FileFinder($path)) + ->withPath('*/__init__.php') + ->withType('f') + ->find(); + + echo "Removing __init__.php files...\n"; + foreach ($inits as $init) { + Filesystem::remove($path.'/'.$init); + } + + echo "Upgrading library to v2...\n"; + $this->liberateVersion2($path); + } + + private function liberateVersion1($path) { + if ($this->getArgument('remap')) { return $this->liberateRunRemap($path); }