// Load libraries in ".arcconfig". Libraries here must load.
arcanist_load_libraries(
- $working_copy->getConfig('phutil_libraries'),
+ $working_copy->getConfig('load'),
$must_load = true,
- $lib_source = 'the "phutil_libraries" setting in ".arcconfig"',
+ $lib_source = 'the "load" setting in ".arcconfig"',
$working_copy,
$config_trace_mode);
}
'ArcanistRepositoryAPI' => 'repository/api/ArcanistRepositoryAPI.php',
'ArcanistScriptAndRegexLinter' => 'lint/linter/ArcanistScriptAndRegexLinter.php',
'ArcanistSetConfigWorkflow' => 'workflow/ArcanistSetConfigWorkflow.php',
+ 'ArcanistSettings' => 'configuration/ArcanistSettings.php',
'ArcanistShellCompleteWorkflow' => 'workflow/ArcanistShellCompleteWorkflow.php',
'ArcanistSingleLintEngine' => 'lint/engine/ArcanistSingleLintEngine.php',
'ArcanistSpellingDefaultData' => 'lint/linter/ArcanistSpellingDefaultData.php',
--- /dev/null
+<?php
+
+/*
+ * Copyright 2012 Facebook, Inc.
+ *
+ * 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.
+ */
+
+/**
+ * @group config
+ */
+final class ArcanistSettings {
+
+ private function getOptions() {
+ return array(
+ 'default' => array(
+ 'type' => 'string',
+ 'help' =>
+ 'The URI of a Phabricator install to connect to by default, if '.
+ 'arc is run in a project without a Phabricator URI or run outside '.
+ 'of a project.',
+ 'example' => '"http://phabricator.example.com/"',
+ ),
+ 'base' => array(
+ 'type' => 'string',
+ 'help' =>
+ 'Base commit ruleset to invoke when determining the start of a '.
+ 'commit range. See "Arcanist User Guide: Commit Ranges" for '.
+ 'details.',
+ 'example' => '"arc:amended, arc:prompt"',
+ ),
+ 'load' => array(
+ 'type' => 'list',
+ 'legacy' => 'phutil_libraries',
+ 'help' =>
+ 'A list of paths to phutil libraries that should be loaded at '.
+ 'startup. This can be used to make classes available, like lint or '.
+ 'unit test engines.',
+ 'example' => '["/var/arc/customlib/src"]',
+ ),
+ 'lint.engine' => array(
+ 'type' => 'string',
+ 'legacy' => 'lint_engine',
+ 'help' =>
+ 'The name of a default lint engine to use, if no lint engine is '.
+ 'specified by the current project.',
+ 'example' => '"ExampleLintEngine"',
+ ),
+ 'unit.engine' => array(
+ 'type' => 'string',
+ 'legacy' => 'unit_engine',
+ 'help' =>
+ 'The name of a default unit test engine to use, if no unit test '.
+ 'engine is specified by the current project.',
+ 'example' => '"ExampleUnitTestEngine"',
+ ),
+ 'arc.land.onto.default' => array(
+ 'type' => 'string',
+ 'help' =>
+ 'The name of the default branch to land changes onto when '.
+ '`arc land` is run.',
+ 'example' => '"develop"',
+ ),
+ 'history.immutable' => array(
+ 'type' => 'bool',
+ 'legacy' => 'immutable_history',
+ 'help' =>
+ 'If true, arc will never change repository history (e.g., through '.
+ 'amending or rebasing). Defaults to true in Mercurial and false in '.
+ 'Git. This setting has no effect in Subversion.',
+ 'example' => 'false',
+ ),
+ 'editor' => array(
+ 'type' => 'string',
+ 'help' =>
+ "Command to use to invoke an interactive editor, like 'nano' or ".
+ "'vim'. This setting overrides the EDITOR environmental variable.",
+ 'example' => '"nano"',
+ ),
+ );
+ }
+
+ private function getOption($key) {
+ return idx($this->getOptions(), $key, array());
+ }
+
+ public function getAllKeys() {
+ return array_keys($this->getOptions());
+ }
+
+ public function getHelp($key) {
+ return idx($this->getOption($key), 'help');
+ }
+
+ public function getExample($key) {
+ return idx($this->getOption($key), 'example');
+ }
+
+ public function getType($key) {
+ return idx($this->getOption($key), 'type', 'wild');
+ }
+
+ public function getLegacyName($key) {
+ return idx($this->getOption($key), 'legacy');
+ }
+
+ public function willWriteValue($key, $value) {
+ $type = $this->getType($key);
+ switch ($type) {
+ case 'bool':
+ if (strtolower($value) === 'false' ||
+ strtolower($value) === 'no' ||
+ strtolower($value) === 'off' ||
+ $value === '' ||
+ $value === '0' ||
+ $value === 0 ||
+ $value === false) {
+ $value = false;
+ } else if (strtolower($value) === 'true' ||
+ strtolower($value) === 'yes' ||
+ strtolower($value) === 'on' ||
+ $value === '1' ||
+ $value === 1 ||
+ $value === true) {
+ $value = true;
+ } else {
+ throw new ArcanistUsageException(
+ "Type of setting '{$key}' must be boolean, like 'true' or ".
+ "'false'.");
+ }
+ break;
+ case 'list':
+ if (is_array($value)) {
+ break;
+ }
+
+ if (is_string($value)) {
+ $list = json_decode($value, true);
+ if (is_array($list)) {
+ $value = $list;
+ break;
+ }
+ }
+
+ $list_example = '["apple", "banana", "cherry"]';
+ throw new ArcanistUsageException(
+ "Type of setting '{$key}' must be list. You can specify a list ".
+ "in JSON, like: {$list_example}");
+
+ case 'string':
+ if (!is_scalar($value)) {
+ throw new ArcanistUsageException(
+ "Type of setting '{$key}' must be string.");
+ }
+ $value = (string)$value;
+ break;
+ case 'wild':
+ break;
+ }
+
+ return $value;
+ }
+
+ public function willReadValue($key, $value) {
+ $type = $this->getType($key);
+ switch ($type) {
+ case 'string':
+ if (!is_string($value)) {
+ throw new ArcanistUsageException(
+ "Type of setting '{$key}' must be string.");
+ }
+ break;
+ case 'bool':
+ if ($value !== true && $value !== false) {
+ throw new ArcanistUsageException(
+ "Type of setting '{$key}' must be boolean.");
+ }
+ break;
+ case 'list':
+ if (!is_array($value)) {
+ throw new ArcanistUsageException(
+ "Type of setting '{$key}' must be list.");
+ }
+ break;
+ case 'wild':
+ break;
+ }
+
+ return $value;
+ }
+
+ public function formatConfigValueForDisplay($key, $value) {
+ if ($value === false) {
+ return 'false';
+ }
+
+ if ($value === true) {
+ return 'true';
+ }
+
+ if ($value === null) {
+ return 'null';
+ }
+
+ if (is_string($value)) {
+ return '"'.$value.'"';
+ }
+
+ if (is_array($value)) {
+ // TODO: Both json_encode() and PhutilJSON do a bad job with one-liners.
+ // PhutilJSON splits them across a bunch of lines, while json_encode()
+ // escapes all kinds of stuff like "/". It would be nice if PhutilJSON
+ // had a mode for pretty one-liners.
+ $value = json_encode($value);
+
+ // json_encode() unnecessarily escapes "/" to prevent "</script>" stuff,
+ // optimistically unescape it for display to improve readability.
+ $value = preg_replace('@(?<!\\\\)\\\\/@', '/', $value);
+ return $value;
+ }
+
+ return $value;
+ }
+
+
+}
$repository_api = $this->getRepositoryAPI();
$working_copy = $this->getWorkingCopy();
- $project_config = $working_copy->getConfigFromAnySource('immutable_history');
- if ($project_config !== null) {
- return $project_config;
+ $config = $working_copy->getConfigFromAnySource('history.immutable');
+ if ($config !== null) {
+ return $config;
}
return $repository_api->isHistoryDefaultImmutable();
return $this->repositoryEncoding;
}
- protected static function formatConfigValueForDisplay($value) {
- if (is_array($value)) {
- // TODO: Both json_encode() and PhutilJSON do a bad job with one-liners.
- // PhutilJSON splits them across a bunch of lines, while json_encode()
- // escapes all kinds of stuff like "/". It would be nice if PhutilJSON
- // had a mode for pretty one-liners.
- $value = json_encode($value);
- return $value;
- }
- return $value;
- }
-
protected function newInteractiveEditor($text) {
$editor = new PhutilInteractiveEditor($text);
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
Supports: cli
- Reads an arc configuration option. With no arugment, reads all
+ Reads an arc configuration option. With no argument, reads all
options.
EOTEXT
);
public function run() {
$argv = $this->getArgument('argv');
+ $settings = new ArcanistSettings();
+
$configs = array(
- 'system' => self::readSystemArcConfig(),
- 'global' => self::readGlobalArcConfig(),
- 'local' => $this->readLocalArcConfig(),
+ 'system' => self::readSystemArcConfig(),
+ 'global' => self::readGlobalArcConfig(),
+ 'project' => $this->getWorkingCopy()->getProjectConfig(),
+ 'local' => $this->readLocalArcConfig(),
);
if ($argv) {
$keys = $argv;
}
foreach ($keys as $key) {
+ echo "{$key}\n";
foreach ($configs as $name => $config) {
- if ($name == 'global' || isset($config[$key])) {
- $val = self::formatConfigValueForDisplay(idx($config, $key));
- echo "({$name}) {$key} = {$val}\n";
+ $val = idx($config, $key);
+ if ($val === null) {
+ continue;
}
+ $val = $settings->formatConfigValueForDisplay($key, $val);
+ printf("% 10.10s: %s\n", $name, $val);
}
+ echo "\n";
}
return 0;
$key = $argv[0];
$val = $argv[1];
+ $settings = new ArcanistSettings();
+
$old = null;
if (array_key_exists($key, $config)) {
$old = $config[$key];
self::writeGlobalArcConfig($config);
}
+ $old = $settings->formatConfigValueForDisplay($key, $old);
+
if ($old === null) {
echo "Deleted key '{$key}' from {$which} config.\n";
} else {
- echo "Deleted key '{$key}' from {$which} config (was '{$old}').\n";
+ echo "Deleted key '{$key}' from {$which} config (was {$old}).\n";
}
} else {
- $val = $this->parse($key, $val);
+ $val = $settings->willWriteValue($key, $val);
$config[$key] = $val;
if ($is_local) {
self::writeGlobalArcConfig($config);
}
- $val = self::formatConfigValueForDisplay($val);
- $old = self::formatConfigValueForDisplay($old);
+ $val = $settings->formatConfigValueForDisplay($key, $val);
+ $old = $settings->formatConfigValueForDisplay($key, $old);
if ($old === null) {
- echo "Set key '{$key}' = '{$val}' in {$which} config.\n";
+ echo "Set key '{$key}' = {$val} in {$which} config.\n";
} else {
- echo "Set key '{$key}' = '{$val}' in {$which} config (was '{$old}').\n";
+ echo "Set key '{$key}' = {$val} in {$which} config (was {$old}).\n";
}
}
}
private function show() {
- $keys = array(
- 'default' => array(
- 'help' =>
- 'The URI of a Phabricator install to connect to by default, if '.
- 'arc is run in a project without a Phabricator URI or run outside '.
- 'of a project.',
- 'example' => 'http://phabricator.example.com/',
- ),
- 'load' => array(
- 'help' =>
- 'A list of paths to phutil libraries that should be loaded at '.
- 'startup. This can be used to make classes available, like lint or '.
- 'unit test engines.',
- 'example' => '["/var/arc/customlib/src"]',
- ),
- 'lint.engine' => array(
- 'help' =>
- 'The name of a default lint engine to use, if no lint engine is '.
- 'specified by the current project.',
- 'example' => 'ExampleLintEngine',
- ),
- 'unit.engine' => array(
- 'help' =>
- 'The name of a default unit test engine to use, if no unit test '.
- 'engine is specified by the current project.',
- 'example' => 'ExampleUnitTestEngine',
- ),
- 'arc.land.onto.default' => array(
- 'help' =>
- 'The name of the default branch to land changes onto when '.
- '`arc land` is run.',
- 'example' => 'develop',
- ),
- );
-
$config = self::readGlobalArcConfig();
- foreach ($keys as $key => $spec) {
- $type = $this->getType($key);
+ $settings = new ArcanistSettings();
+
+ $keys = $settings->getAllKeys();
+ sort($keys);
+ foreach ($keys as $key) {
+ $type = $settings->getType($key);
+ $example = $settings->getExample($key);
+ $help = $settings->getHelp($key);
$value = idx($config, $key);
- $value = self::formatConfigValueForDisplay($value);
+ $value = $settings->formatConfigValueForDisplay($key, $value);
echo phutil_console_format("**__%s__** (%s)\n\n", $key, $type);
- echo phutil_console_format(" Example: %s\n", $spec['example']);
+ if ($example !== null) {
+ echo phutil_console_format(" Example: %s\n", $example);
+ }
if (strlen($value)) {
echo phutil_console_format(" Global Setting: %s\n", $value);
}
echo "\n";
- echo phutil_console_wrap($spec['help'], 4);
+ echo phutil_console_wrap($help, 4);
echo "\n\n\n";
}
return 0;
}
- private function getType($key) {
- static $types = array(
- 'load' => 'list',
- );
-
- return idx($types, $key, 'string');
- }
-
- private function parse($key, $val) {
- $type = $this->getType($key);
-
- switch ($type) {
- case 'string':
- return $val;
- case 'list':
- $val = json_decode($val, true);
- if (!is_array($val)) {
- $example = '["apple", "banana", "cherry"]';
- throw new ArcanistUsageException(
- "Value for key '{$key}' must be specified as a JSON-encoded ".
- "list. Example: {$example}");
- }
- return $val;
- default:
- throw new Exception("Unknown config key type '{$type}'!");
- }
- }
-
}
/* -( Config )------------------------------------------------------------- */
+ public function getProjectConfig() {
+ return $this->projectConfig;
+ }
/**
* Read a configuration directive from project configuration. This reads ONLY
* @task config
*/
public function getConfig($key, $default = null) {
- return idx($this->projectConfig, $key, $default);
+ $settings = new ArcanistSettings();
+
+ $pval = idx($this->projectConfig, $key);
+
+ // Test for older names in the per-project config only, since
+ // they've only been used there.
+ if ($pval === null) {
+ $legacy = $settings->getLegacyName($key);
+ if ($legacy) {
+ $pval = $this->getConfig($legacy);
+ }
+ }
+
+ if ($pval === null) {
+ $pval = $default;
+ } else {
+ $pval = $settings->willReadValue($key, $pval);
+ }
+
+ return $pval;
}
* @task config
*/
public function getConfigFromAnySource($key, $default = null) {
+ $settings = new ArcanistSettings();
// try local config first
$pval = $this->getLocalConfig($key);
$pval = $this->getConfig($key);
}
- // Test for older names in the per-project config only, since
- // they've only been used there
- static $deprecated_names = array(
- 'lint.engine' => 'lint_engine',
- 'unit.engine' => 'unit_engine',
- );
- if ($pval === null && isset($deprecated_names[$key])) {
- $pval = $this->getConfig($deprecated_names[$key]);
- }
-
- // lastly, try global (i.e. user-level) config
+ // now try global (i.e. user-level) config
if ($pval === null) {
$global_config = ArcanistBaseWorkflow::readGlobalArcConfig();
$pval = idx($global_config, $key);
}
+ // Finally, try system-level config.
if ($pval === null) {
$system_config = ArcanistBaseWorkflow::readSystemArcConfig();
$pval = idx($system_config, $key);
if ($pval === null) {
$pval = $default;
+ } else {
+ $pval = $settings->willReadValue($key, $pval);
}
return $pval;