Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CP-1794 Add completions.dart support #151

Merged
merged 5 commits into from
May 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ alias ddev='pub run dart_dev'

#### Bash Completion

Symlink or copy the file `tool/ddev-completion.sh` into
`/etc/bash_completion.d/` (or wherever your completion scripts live, if you
have installed Bash through Homebrew on a Mac, for instance, this will be
`/usr/local/etc/bash_completion.d/`).
Bash command completion is available and easy to use. You'll want to install
dart_dev globally: `pub global activate dart_dev`.

Add the following to your `.bashrc`:

```
eval "$(pub global run dart_dev bash-completion)"
```

If you are using Bash installed through Homebrew, you'll also need to install
the completion machinery with `brew install bash-completion`. Then make sure
Expand All @@ -131,7 +135,7 @@ autoload -U compinit
compinit
autoload -U bashcompinit
bashcompinit
source <path/to/ddev-completion.sh>
eval "$(pub global run dart_dev bash-completion)"
```

#### Configuration
Expand Down
62 changes: 62 additions & 0 deletions lib/bash-completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# Installation:
#
# Via shell config file ~/.bashrc (or ~/.zshrc)
#
# Append the contents to config file
# 'source' the file in the config file
#
# You may also have a directory on your system that is configured
# for completion files, such as:
#
# /usr/local/etc/bash_completion.d/
#

###-begin-ddev-completion-###

if type complete &>/dev/null; then
__ddev_completion() {
local si="$IFS"
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
ddev completion -- "${COMP_WORDS[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
complete -F __ddev_completion ddev
elif type compdef &>/dev/null; then
__ddev_completion() {
si=$IFS
compadd -- $(COMP_CWORD=$((CURRENT-1)) \
COMP_LINE=$BUFFER \
COMP_POINT=0 \
ddev completion -- "${words[@]}" \
2>/dev/null)
IFS=$si
}
compdef __ddev_completion ddev
elif type compctl &>/dev/null; then
__ddev_completion() {
local cword line point words si
read -Ac words
read -cn cword
let cword-=1
read -l line
read -ln point
si="$IFS"
IFS=$'\n' reply=($(COMP_CWORD="$cword" \
COMP_LINE="$line" \
COMP_POINT="$point" \
ddev completion -- "${words[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
compctl -K __ddev_completion ddev
fi

###-end-ddev-completion-###

## Generated 2016-05-17 19:21:47.711257Z
## By /Volumes/Workspace/dart_dev/.pub/bin/completion/shell_completion_generator.dart.snapshot

6 changes: 4 additions & 2 deletions lib/src/dart_dev_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'dart:async';
import 'dart:io';

import 'package:args/args.dart';

import 'package:completion/completion.dart';
import 'package:dart_dev/util.dart'
show
TaskProcess,
Expand All @@ -30,6 +30,7 @@ import 'package:dart_dev/src/tasks/cli.dart';
import 'package:dart_dev/src/tasks/config.dart';

import 'package:dart_dev/src/tasks/analyze/cli.dart';
import 'package:dart_dev/src/tasks/bash_completion/cli.dart';
import 'package:dart_dev/src/tasks/copy_license/cli.dart';
import 'package:dart_dev/src/tasks/coverage/cli.dart';
import 'package:dart_dev/src/tasks/docs/cli.dart';
Expand All @@ -55,6 +56,7 @@ String _topLevelUsage = _parser.usage;

dev(List<String> args) async {
registerTask(new AnalyzeCli(), config.analyze);
registerTask(new BashCompletionCli(), config.bashCompletion);
registerTask(new CopyLicenseCli(), config.copyLicense);
registerTask(new CoverageCli(), config.coverage);
registerTask(new DocsCli(), config.docs);
Expand Down Expand Up @@ -101,7 +103,7 @@ Future _run(List<String> args) async {

ArgResults env;
try {
env = _parser.parse(args);
env = tryArgsCompletion(args, _parser);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does this just try add support for parsing incomplete args? Like if someone were to accidentally run ddev form - it would run ddev format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't read through the full source, but it seems like it basically just adds a new command that provides completions. The completion script is then almost entirely generic in that it just runs a special version of ddev itself to get the completions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok I see 👍

} on FormatException catch (e) {
reporter.error('${e.message}\n', shout: true);
reporter.log(_generateUsage(), shout: true);
Expand Down
43 changes: 43 additions & 0 deletions lib/src/tasks/bash_completion/api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2015 Workiva 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.

import 'dart:async';
import 'dart:convert';

import 'package:resource/resource.dart' as resource;

import 'package:dart_dev/src/tasks/task.dart';

BashCompletionTask bashCompletion() {
Completer done = new Completer();
BashCompletionTask task = new BashCompletionTask(done.future);

var script = new resource.Resource('package:dart_dev/bash-completion.sh');
script.readAsString(encoding: UTF8).then((content) {
task
..completionScript = content
..successful = true;
done.complete();
}).catchError((error) {
done.complete();
});

return task;
}

class BashCompletionTask extends Task {
final Future done;
String completionScript;
BashCompletionTask(Future this.done);
}
41 changes: 41 additions & 0 deletions lib/src/tasks/bash_completion/cli.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2015 Workiva 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.

import 'dart:async';

import 'package:args/args.dart';

import 'package:dart_dev/src/tasks/bash_completion/api.dart';
import 'package:dart_dev/src/tasks/cli.dart';
import 'package:dart_dev/util.dart' show reporter;

class BashCompletionCli extends TaskCli {
final ArgParser argParser = new ArgParser();

final String command = 'bash-completion';

Future<CliResult> run(ArgResults parsedArgs, {bool color: true}) async {
// This is kind of a hack to suppress output coloring, which messes us up since
// we're outputting shell code (the control sequences are syntax errors).
reporter.color = false;

BashCompletionTask task = bashCompletion();
await task.done;
if (task.successful) {
return new CliResult.success(task.completionScript);
} else {
return new CliResult.fail('No completion script found.');
}
}
}
17 changes: 17 additions & 0 deletions lib/src/tasks/bash_completion/config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 Workiva 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.

import 'package:dart_dev/src/tasks/config.dart';

class BashCompletionConfig extends TaskConfig {}
2 changes: 2 additions & 0 deletions lib/src/tasks/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
library dart_dev.src.tasks.config;

import 'package:dart_dev/src/tasks/analyze/config.dart';
import 'package:dart_dev/src/tasks/bash_completion/config.dart';
import 'package:dart_dev/src/tasks/copy_license/config.dart';
import 'package:dart_dev/src/tasks/coverage/config.dart';
import 'package:dart_dev/src/tasks/docs/config.dart';
Expand All @@ -27,6 +28,7 @@ Config config = new Config();

class Config {
AnalyzeConfig analyze = new AnalyzeConfig();
BashCompletionConfig bashCompletion = new BashCompletionConfig();
CopyLicenseConfig copyLicense = new CopyLicenseConfig();
CoverageConfig coverage = new CoverageConfig();
DocsConfig docs = new DocsConfig();
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ homepage: https://github.com/Workiva/dart_dev
dependencies:
ansicolor: "^0.0.9"
args: "^0.13.0"
completion: "^0.1.5"
coverage: "^0.7.3"
dart_style: ">=0.1.8 <0.3.0"
dartdoc: ">=0.4.0 <0.9.0"
path: "^1.3.6"
resource: "^1.1.0"
test: "^0.12.0"
uuid: "^0.5.0"
yaml: "^2.1.0"
Expand Down
43 changes: 0 additions & 43 deletions tool/ddev-completion.sh

This file was deleted.

8 changes: 8 additions & 0 deletions tool/generate_completions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

set -e

# We only support completions when the command is run through
# the `ddev` alias detailed in the README.

pub run completion:shell_completion_generator ddev> lib/bash-completion.sh