2011-01-09 15:22:25 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-31 12:07:05 -08:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-01-09 15:22:25 -08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-02-19 11:36:08 -08:00
|
|
|
/**
|
|
|
|
* Interfaces with basic information about the working copy.
|
|
|
|
*
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
*
|
|
|
|
* @task config
|
|
|
|
*
|
2011-02-19 11:36:08 -08:00
|
|
|
* @group workingcopy
|
|
|
|
*/
|
2012-01-31 12:07:05 -08:00
|
|
|
final class ArcanistWorkingCopyIdentity {
|
2011-01-09 15:22:25 -08:00
|
|
|
|
2012-06-13 16:02:29 -07:00
|
|
|
protected $localConfig;
|
2011-01-09 15:22:25 -08:00
|
|
|
protected $projectConfig;
|
|
|
|
protected $projectRoot;
|
|
|
|
|
|
|
|
public static function newFromPath($path) {
|
|
|
|
$project_id = null;
|
|
|
|
$project_root = null;
|
|
|
|
$config = array();
|
|
|
|
foreach (Filesystem::walkToRoot($path) as $dir) {
|
|
|
|
$config_file = $dir.'/.arcconfig';
|
|
|
|
if (!Filesystem::pathExists($config_file)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$proj_raw = Filesystem::readFile($config_file);
|
2011-02-24 16:34:27 -08:00
|
|
|
$config = self::parseRawConfigFile($proj_raw, $config_file);
|
2011-01-09 15:22:25 -08:00
|
|
|
$project_root = $dir;
|
|
|
|
break;
|
|
|
|
}
|
Allow 'arc' to run without '.arcconfig'
Summary:
This is mostly an onboarding thing, but also allows "arc upload", "arc download", and "arc paste" to work anywhere on the system.
- Try to read the Phabricator install URI from arc global config if we can't find ".arcconfig".
- Build a WorkingCopy anyway if we can't find ".arcconfig", as long as we can find ".svn", ".git", or ".hg".
- Make all the workflows handle "no project ID" at least somewhat gracefully.
Test Plan:
- Ran "arc diff" in .arcconfig-less Mercurial, Git, and Subversion working copies.
- Ran "arc upload" and "arc download" from my desktop.
- Ran "arc paste" from somewhere random.
- Cleared my config and hit the error, got useful instructions.
Reviewers: btrahan, csilvers
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2424
2012-05-07 15:24:58 -07:00
|
|
|
|
|
|
|
if (!$project_root) {
|
|
|
|
foreach (Filesystem::walkToRoot($path) as $dir) {
|
|
|
|
$try = array(
|
|
|
|
$dir.'/.svn',
|
|
|
|
$dir.'/.hg',
|
|
|
|
$dir.'/.git',
|
|
|
|
);
|
|
|
|
foreach ($try as $trydir) {
|
|
|
|
if (Filesystem::pathExists($trydir)) {
|
|
|
|
$project_root = $dir;
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:22:25 -08:00
|
|
|
return new ArcanistWorkingCopyIdentity($project_root, $config);
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:34:27 -08:00
|
|
|
public static function newFromRootAndConfigFile(
|
|
|
|
$root,
|
|
|
|
$config_raw,
|
|
|
|
$from_where) {
|
|
|
|
|
|
|
|
$config = self::parseRawConfigFile($config_raw, $from_where);
|
2011-02-15 14:57:24 -08:00
|
|
|
return new ArcanistWorkingCopyIdentity($root, $config);
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:34:27 -08:00
|
|
|
private static function parseRawConfigFile($raw_config, $from_where) {
|
2011-02-15 14:57:24 -08:00
|
|
|
$proj = json_decode($raw_config, true);
|
|
|
|
if (!is_array($proj)) {
|
|
|
|
throw new Exception(
|
2011-02-24 16:34:27 -08:00
|
|
|
"Unable to parse '.arcconfig' file '{$from_where}'. The file contents ".
|
|
|
|
"should be valid JSON.\n\n".
|
|
|
|
"FILE CONTENTS\n".
|
|
|
|
substr($raw_config, 0, 2048));
|
2011-02-15 14:57:24 -08:00
|
|
|
}
|
|
|
|
$required_keys = array(
|
|
|
|
'project_id',
|
|
|
|
);
|
|
|
|
foreach ($required_keys as $key) {
|
|
|
|
if (!array_key_exists($key, $proj)) {
|
|
|
|
throw new Exception(
|
2011-02-24 16:34:27 -08:00
|
|
|
"Required key '{$key}' is missing from '.arcconfig' file ".
|
|
|
|
"'{$from_where}'.");
|
2011-02-15 14:57:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $proj;
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:22:25 -08:00
|
|
|
protected function __construct($root, array $config) {
|
|
|
|
$this->projectRoot = $root;
|
|
|
|
$this->projectConfig = $config;
|
2012-06-13 16:02:29 -07:00
|
|
|
$this->localConfig = array();
|
|
|
|
|
|
|
|
$vc_dirs = array(
|
|
|
|
'.git',
|
|
|
|
'.hg',
|
|
|
|
'.svn',
|
|
|
|
);
|
2012-06-25 17:13:29 -07:00
|
|
|
$found_meta_dir = false;
|
2012-06-13 16:02:29 -07:00
|
|
|
foreach ($vc_dirs as $dir) {
|
2012-06-25 17:13:29 -07:00
|
|
|
$meta_path = Filesystem::resolvePath(
|
|
|
|
$dir,
|
2012-06-13 16:02:29 -07:00
|
|
|
$this->projectRoot);
|
2012-06-25 17:13:29 -07:00
|
|
|
if (Filesystem::pathExists($meta_path)) {
|
|
|
|
$found_meta_dir = true;
|
|
|
|
$local_path = Filesystem::resolvePath(
|
|
|
|
'arc/config',
|
|
|
|
$meta_path);
|
|
|
|
if (Filesystem::pathExists($local_path)) {
|
|
|
|
$file = Filesystem::readFile($local_path);
|
|
|
|
if ($file) {
|
|
|
|
$this->localConfig = json_decode($file, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$found_meta_dir) {
|
|
|
|
// Try for a single higher-level .svn directory as used by svn 1.7+
|
|
|
|
foreach (Filesystem::walkToRoot($this->projectRoot) as $parent_path) {
|
|
|
|
$local_path = Filesystem::resolvePath(
|
|
|
|
'.svn/arc/config',
|
|
|
|
$parent_path);
|
|
|
|
if (Filesystem::pathExists($local_path)) {
|
|
|
|
$file = Filesystem::readFile($local_path);
|
|
|
|
if ($file) {
|
|
|
|
$this->localConfig = json_decode($file, true);
|
|
|
|
}
|
2012-06-13 16:02:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 15:22:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getProjectID() {
|
|
|
|
return $this->getConfig('project_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProjectRoot() {
|
|
|
|
return $this->projectRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConduitURI() {
|
|
|
|
return $this->getConfig('conduit_uri');
|
|
|
|
}
|
|
|
|
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
|
|
|
|
/* -( Config )------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a configuration directive from project configuration. This reads ONLY
|
|
|
|
* permanent project configuration (i.e., ".arcconfig"), not other
|
|
|
|
* configuration sources. See @{method:getConfigFromAnySource} to read from
|
|
|
|
* user configuration.
|
|
|
|
*
|
|
|
|
* @param key Key to read.
|
|
|
|
* @param wild Default value if key is not found.
|
|
|
|
* @return wild Value, or default value if not found.
|
|
|
|
*
|
|
|
|
* @task config
|
|
|
|
*/
|
|
|
|
public function getConfig($key, $default = null) {
|
|
|
|
return idx($this->projectConfig, $key, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-13 16:02:29 -07:00
|
|
|
/**
|
|
|
|
* Read a configuration directive from local configuration. This
|
|
|
|
* reads ONLY the per-working copy configuration,
|
|
|
|
* i.e. .(git|hg|svn)/arc/config, and not other configuration
|
|
|
|
* sources. See @{method:getConfigFromAnySource} to read from any
|
|
|
|
* config source or @{method:getConfig} to read permanent
|
|
|
|
* project-level config.
|
|
|
|
*
|
|
|
|
* @task config
|
|
|
|
*/
|
|
|
|
public function getLocalConfig($key, $default=null) {
|
|
|
|
return idx($this->localConfig, $key, $default);
|
|
|
|
}
|
|
|
|
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
/**
|
|
|
|
* Read a configuration directive from any available configuration source.
|
|
|
|
* In contrast to @{method:getConfig}, this will look for the directive in
|
2012-06-13 16:02:29 -07:00
|
|
|
* local and user configuration in addition to project configuration.
|
|
|
|
* The precedence is local > project > user
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
*
|
|
|
|
* @param key Key to read.
|
|
|
|
* @param wild Default value if key is not found.
|
|
|
|
* @return wild Value, or default value if not found.
|
|
|
|
*
|
|
|
|
* @task config
|
|
|
|
*/
|
|
|
|
public function getConfigFromAnySource($key, $default = null) {
|
|
|
|
|
2012-06-13 16:02:29 -07:00
|
|
|
// try local config first
|
|
|
|
$pval = $this->getLocalConfig($key);
|
|
|
|
|
|
|
|
// then per-project config
|
|
|
|
if ($pval === null) {
|
|
|
|
$pval = $this->getConfig($key);
|
|
|
|
}
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
|
2012-06-13 16:02:29 -07:00
|
|
|
// Test for older names in the per-project config only, since
|
|
|
|
// they've only been used there
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
static $deprecated_names = array(
|
|
|
|
'lint.engine' => 'lint_engine',
|
|
|
|
'unit.engine' => 'unit_engine',
|
|
|
|
);
|
2012-06-13 16:02:29 -07:00
|
|
|
if ($pval === null && isset($deprecated_names[$key])) {
|
Allow global config to load libraries and set test engines
Summary:
Khan Academy is looking into lint configuration, but doesn't use ".arcconfig" because they have a large number of repositories. Making configuration more flexible generally gives us more options for onboarding installs.
- Currently, only project config (".arcconfig") can load libraries. Allow user config ("~/.arcrc") to load libraries as well.
- Currently, only project config can set lint/unit engines. Allow user config to set default lint/unit engines.
- Add some type checking to "arc set-config".
- Add "arc set-config --show".
Test Plan:
- **load**
- Ran `arc set-config load xxx`, got error about format.
- Ran `arc set-config load ["apple"]`, got warning on running 'arc' commands (no such library) but was able to run 'arc set-config' again to clear it.
- Ran `arc set-config load ["/path/to/a/lib/src/"]`, worked.
- Ran `arc list --trace`, verified my library loaded in addition to `.arcconfig` libraries.
- Ran `arc list --load-phutil-library=xxx --trace`, verified only that library loaded.
- Ran `arc list --trace --load-phutil-library=apple --trace`, got hard error about bad library.
- Set `.arcconfig` to point at a bad library, verified hard error.
- **lint.engine** / **unit.engine**
- Removed lint engine from `.arcconfig`, ran "arc lint", got a run with specified engine.
- Removed unit engine from `.arcconfig`, ran "arc unit", got a run with specified engine.
- **--show**
- Ran `arc set-config --show`.
- **misc**
- Ran `arc get-config`.
Reviewers: csilvers, btrahan, vrana
Reviewed By: csilvers
CC: aran
Differential Revision: https://secure.phabricator.com/D2618
2012-05-31 11:41:39 -07:00
|
|
|
$pval = $this->getConfig($deprecated_names[$key]);
|
|
|
|
}
|
|
|
|
|
2012-06-13 16:02:29 -07:00
|
|
|
// lastly, try global (i.e. user-level) config
|
|
|
|
if ($pval === null) {
|
|
|
|
$global_config = ArcanistBaseWorkflow::readGlobalArcConfig();
|
2012-06-25 15:14:11 -07:00
|
|
|
$pval = idx($global_config, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($pval === null) {
|
|
|
|
$system_config = ArcanistBaseWorkflow::readSystemArcConfig();
|
|
|
|
$pval = idx($system_config, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($pval === null) {
|
|
|
|
$pval = $default;
|
2012-06-13 16:02:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pval;
|
|
|
|
|
2011-01-09 15:22:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|