mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-08 07:52:39 +01:00
Basic 'shell-complete' workflow.
Summary: Adds data-driven shell completion help to arcanist. Test Plan: ran various commands in git and svn working copies, output seemed reasonable Differential Revision: 201754 Reviewed By: adonohue Reviewers: mroch, adonohue Commenters: crackerjack CC: epriestley, adonohue, achao Revert Plan: OK
This commit is contained in:
parent
026a322dd2
commit
4818892841
11 changed files with 236 additions and 0 deletions
24
resources/shell/bash-completion
Normal file
24
resources/shell/bash-completion
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
_arc()
|
||||||
|
{
|
||||||
|
|
||||||
|
CUR="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
COMPREPLY=()
|
||||||
|
OPTS=$(arc shell-complete --current ${COMP_CWORD} -- ${COMP_WORDS[@]})
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
return $?
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$OPTS" = "FILE" ]; then
|
||||||
|
COMPREPLY=( $(compgen -f -- ${CUR}) )
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$OPTS" = "ARGUMENT" ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
COMPREPLY=( $(compgen -W "${OPTS}" -- ${CUR}) )
|
||||||
|
|
||||||
|
}
|
||||||
|
complete -F _arc -o filenames arc
|
|
@ -51,6 +51,7 @@ phutil_register_library_map(array(
|
||||||
'ArcanistPhutilTestCase' => 'unit/engine/phutil/testcase',
|
'ArcanistPhutilTestCase' => 'unit/engine/phutil/testcase',
|
||||||
'ArcanistPhutilTestTerminatedException' => 'unit/engine/phutil/testcase/exception',
|
'ArcanistPhutilTestTerminatedException' => 'unit/engine/phutil/testcase/exception',
|
||||||
'ArcanistRepositoryAPI' => 'repository/api/base',
|
'ArcanistRepositoryAPI' => 'repository/api/base',
|
||||||
|
'ArcanistShellCompleteWorkflow' => 'workflow/shell-complete',
|
||||||
'ArcanistSubversionAPI' => 'repository/api/subversion',
|
'ArcanistSubversionAPI' => 'repository/api/subversion',
|
||||||
'ArcanistSvnHookPreCommitWorkflow' => 'workflow/svn-hook-pre-commit',
|
'ArcanistSvnHookPreCommitWorkflow' => 'workflow/svn-hook-pre-commit',
|
||||||
'ArcanistTextLinter' => 'lint/linter/text',
|
'ArcanistTextLinter' => 'lint/linter/text',
|
||||||
|
@ -92,6 +93,7 @@ phutil_register_library_map(array(
|
||||||
'ArcanistPEP8Linter' => 'ArcanistLinter',
|
'ArcanistPEP8Linter' => 'ArcanistLinter',
|
||||||
'ArcanistPatchWorkflow' => 'ArcanistBaseWorkflow',
|
'ArcanistPatchWorkflow' => 'ArcanistBaseWorkflow',
|
||||||
'ArcanistPhutilModuleLinter' => 'ArcanistLinter',
|
'ArcanistPhutilModuleLinter' => 'ArcanistLinter',
|
||||||
|
'ArcanistShellCompleteWorkflow' => 'ArcanistBaseWorkflow',
|
||||||
'ArcanistSubversionAPI' => 'ArcanistRepositoryAPI',
|
'ArcanistSubversionAPI' => 'ArcanistRepositoryAPI',
|
||||||
'ArcanistSvnHookPreCommitWorkflow' => 'ArcanistBaseWorkflow',
|
'ArcanistSvnHookPreCommitWorkflow' => 'ArcanistBaseWorkflow',
|
||||||
'ArcanistTextLinter' => 'ArcanistLinter',
|
'ArcanistTextLinter' => 'ArcanistLinter',
|
||||||
|
|
|
@ -62,6 +62,14 @@ class ArcanistConfiguration {
|
||||||
foreach ($symbols as $symbol) {
|
foreach ($symbols as $symbol) {
|
||||||
$class = $symbol['name'];
|
$class = $symbol['name'];
|
||||||
$name = preg_replace('/^Arcanist(\w+)Workflow$/', '\1', $class);
|
$name = preg_replace('/^Arcanist(\w+)Workflow$/', '\1', $class);
|
||||||
|
$name[0] = strtolower($name[0]);
|
||||||
|
$name = preg_replace_callback(
|
||||||
|
'/[A-Z]/',
|
||||||
|
array(
|
||||||
|
'ArcanistConfiguration',
|
||||||
|
'replaceClassnameUppers',
|
||||||
|
),
|
||||||
|
$name);
|
||||||
$name = strtolower($name);
|
$name = strtolower($name);
|
||||||
$workflows[$name] = newv($class, array());
|
$workflows[$name] = newv($class, array());
|
||||||
}
|
}
|
||||||
|
@ -85,4 +93,8 @@ class ArcanistConfiguration {
|
||||||
return strtoupper($m[1]);
|
return strtoupper($m[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function replaceClassnameUppers($m) {
|
||||||
|
return '-'.strtolower($m[0]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,4 +124,9 @@ EOTEXT
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getSupportedRevisionControlSystems() {
|
||||||
|
return array('git');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -543,4 +543,16 @@ class ArcanistBaseWorkflow {
|
||||||
return ltrim(strtoupper($revision_id), 'D');
|
return ltrim(strtoupper($revision_id), 'D');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function shouldShellComplete() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getShellCompletions(array $argv) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getSupportedRevisionControlSystems() {
|
||||||
|
return array('any');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -248,4 +248,8 @@ EOTEXT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getSupportedRevisionControlSystems() {
|
||||||
|
return array('svn');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,10 @@ EOTEXT
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function shouldShellComplete() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function run() {
|
public function run() {
|
||||||
$working_copy = $this->getWorkingCopy();
|
$working_copy = $this->getWorkingCopy();
|
||||||
if (!$working_copy->getProjectID()) {
|
if (!$working_copy->getProjectID()) {
|
||||||
|
|
|
@ -40,6 +40,7 @@ EOTEXT
|
||||||
return array(
|
return array(
|
||||||
'revision' => array(
|
'revision' => array(
|
||||||
'param' => 'revision_id',
|
'param' => 'revision_id',
|
||||||
|
'paramtype' => 'complete',
|
||||||
'help' =>
|
'help' =>
|
||||||
"Apply changes from a Differential revision, using the most recent ".
|
"Apply changes from a Differential revision, using the most recent ".
|
||||||
"diff that has been attached to it.",
|
"diff that has been attached to it.",
|
||||||
|
@ -54,11 +55,13 @@ EOTEXT
|
||||||
),
|
),
|
||||||
'arcbundle' => array(
|
'arcbundle' => array(
|
||||||
'param' => 'bundlefile',
|
'param' => 'bundlefile',
|
||||||
|
'paramtype' => 'file',
|
||||||
'help' =>
|
'help' =>
|
||||||
"Apply changes from an arc bundle generated with 'arc export'.",
|
"Apply changes from an arc bundle generated with 'arc export'.",
|
||||||
),
|
),
|
||||||
'patch' => array(
|
'patch' => array(
|
||||||
'param' => 'patchfile',
|
'param' => 'patchfile',
|
||||||
|
'paramtype' => 'file',
|
||||||
'help' =>
|
'help' =>
|
||||||
"Apply changes from a git patchfile or unified patchfile.",
|
"Apply changes from a git patchfile or unified patchfile.",
|
||||||
),
|
),
|
||||||
|
@ -323,4 +326,9 @@ EOTEXT
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getShellCompletions(array $argv) {
|
||||||
|
// TODO: Pull open diffs from 'arc list'?
|
||||||
|
return array('ARGUMENT');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
144
src/workflow/shell-complete/ArcanistShellCompleteWorkflow.php
Normal file
144
src/workflow/shell-complete/ArcanistShellCompleteWorkflow.php
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2011 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ArcanistShellCompleteWorkflow extends ArcanistBaseWorkflow {
|
||||||
|
|
||||||
|
public function getCommandHelp() {
|
||||||
|
return phutil_console_format(<<<EOTEXT
|
||||||
|
**shell-complete** __--current__ __N__ -- [__argv__]
|
||||||
|
Supports: bash, etc.
|
||||||
|
Implements shell completion. To use shell completion, source the
|
||||||
|
appropriate script from 'resources/shell/' in your .shellrc.
|
||||||
|
|
||||||
|
EOTEXT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getArguments() {
|
||||||
|
return array(
|
||||||
|
'current' => array(
|
||||||
|
'help' => 'Current term in the argument list being completed.',
|
||||||
|
'param' => 'cursor_position',
|
||||||
|
),
|
||||||
|
'*' => 'argv',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function shouldShellComplete() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run() {
|
||||||
|
|
||||||
|
$pos = $this->getArgument('current');
|
||||||
|
$argv = $this->getArgument('argv', array());
|
||||||
|
$argc = count($argv);
|
||||||
|
if ($pos === null) {
|
||||||
|
$pos = $argc - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine which revision control system the working copy uses, so we
|
||||||
|
// can filter out commands and flags which aren't supported. If we can't
|
||||||
|
// figure it out, just return all flags/commands.
|
||||||
|
$vcs = null;
|
||||||
|
|
||||||
|
// We have to build our own because if we requiresWorkingCopy() we'll throw
|
||||||
|
// if we aren't in a .arcconfig directory. We probably still can't do much,
|
||||||
|
// but commands can raise more detailed errors.
|
||||||
|
$working_copy = ArcanistWorkingCopyIdentity::newFromPath($_SERVER['PWD']);
|
||||||
|
if ($working_copy->getProjectRoot()) {
|
||||||
|
$repository_api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
|
||||||
|
$working_copy);
|
||||||
|
$vcs = $repository_api->getSourceControlSystemName();
|
||||||
|
}
|
||||||
|
|
||||||
|
$arc_config = $this->getArcanistConfiguration();
|
||||||
|
|
||||||
|
if ($pos == 1) {
|
||||||
|
$workflows = $arc_config->buildAllWorkflows();
|
||||||
|
|
||||||
|
$complete = array();
|
||||||
|
foreach ($workflows as $name => $workflow) {
|
||||||
|
if (!$workflow->shouldShellComplete()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$supported = $workflow->getSupportedRevisionControlSystems();
|
||||||
|
|
||||||
|
$ok = (in_array('any', $supported)) ||
|
||||||
|
(in_array($vcs, $supported));
|
||||||
|
if (!$ok) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$complete[] = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo implode(' ', $complete)."\n";
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
$workflow = $arc_config->buildWorkflow($argv[1]);
|
||||||
|
if (!$workflow) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
$arguments = $workflow->getArguments();
|
||||||
|
|
||||||
|
$prev = idx($argv, $pos - 1, null);
|
||||||
|
if (!strncmp($prev, '--', 2)) {
|
||||||
|
$prev = substr($prev, 2);
|
||||||
|
} else {
|
||||||
|
$prev = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($prev !== null &&
|
||||||
|
isset($arguments[$prev]) &&
|
||||||
|
isset($arguments[$prev]['param'])) {
|
||||||
|
|
||||||
|
$type = idx($arguments[$prev], 'paramtype');
|
||||||
|
switch ($type) {
|
||||||
|
case 'file':
|
||||||
|
echo "FILE\n";
|
||||||
|
break;
|
||||||
|
case 'complete':
|
||||||
|
echo implode(' ', $workflow->getShellCompletions($argv))."\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo "ARGUMENT\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
$output = array();
|
||||||
|
foreach ($arguments as $argument => $spec) {
|
||||||
|
if ($argument == '*') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($vcs &&
|
||||||
|
isset($spec['supports']) &&
|
||||||
|
!in_array($vcs, $spec['supports'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$output[] = '--'.$argument;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo implode(' ', $output)."\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
src/workflow/shell-complete/__init__.php
Normal file
17
src/workflow/shell-complete/__init__.php
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is automatically generated. Lint this module to rebuild it.
|
||||||
|
* @generated
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
phutil_require_module('arcanist', 'repository/api/base');
|
||||||
|
phutil_require_module('arcanist', 'workflow/base');
|
||||||
|
phutil_require_module('arcanist', 'workingcopyidentity');
|
||||||
|
|
||||||
|
phutil_require_module('phutil', 'console');
|
||||||
|
phutil_require_module('phutil', 'utils');
|
||||||
|
|
||||||
|
|
||||||
|
phutil_require_source('ArcanistShellCompleteWorkflow.php');
|
|
@ -33,6 +33,10 @@ EOTEXT
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function shouldShellComplete() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function run() {
|
public function run() {
|
||||||
|
|
||||||
$svnargs = $this->getArgument('svnargs');
|
$svnargs = $this->getArgument('svnargs');
|
||||||
|
|
Loading…
Reference in a new issue