Skip to content

Commit

Permalink
Bash completion apache#215
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Dec 10, 2020
1 parent dfdaec8 commit 8712b39
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 33 deletions.
10 changes: 10 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<project.basedir>${project.basedir}</project.basedir>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -124,6 +133,7 @@
--no-fallback
--allow-incomplete-classpath
-H:IncludeResources=org/mvndaemon/mvnd/.*
-H:IncludeResources=mvnd-bash-completion.bash
-H:-ParseRuntimeOptions
-ea
</buildArgs>
Expand Down
29 changes: 29 additions & 0 deletions client/src/main/java/org/mvndaemon/mvnd/client/Completion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 the original author or authors.
*
* 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.
*/
package org.mvndaemon.mvnd.client;

import org.mvndaemon.mvnd.common.IoUtils;

public class Completion {

public static String getCompletion(String shell) {
if (!"bash".equals(shell)) {
throw new IllegalArgumentException("Unexpected --completion value: '" + shell + "'; expected: 'bash'");
}
return IoUtils.readResource(Completion.class.getClassLoader(), "mvnd-bash-completion.bash");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public static void main(String[] argv) throws Exception {
}

// Batch mode
boolean batchMode = Environment.MAVEN_BATCH_MODE.hasCommandLineOption(args);
boolean batchMode = Environment.MAVEN_BATCH_MODE.hasCommandLineOption(args)
|| Environment.COMPLETION.hasCommandLineOption(args);

// System properties
setSystemPropertiesFromCommandLine(args);
Expand Down Expand Up @@ -133,6 +134,12 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
LOGGER.debug("Starting client");

final List<String> args = new ArrayList<>(argv);
final String completionShell = Environment.COMPLETION.removeCommandLineOption(args);
if (completionShell != null) {
output.accept(Message.log(Completion.getCompletion(completionShell)));
return DefaultResult.success(argv);
}

boolean version = Environment.MAVEN_VERSION.hasCommandLineOption(args);
boolean showVersion = Environment.MAVEN_SHOW_VERSION.hasCommandLineOption(args);
boolean debug = Environment.MAVEN_DEBUG.hasCommandLineOption(args);
Expand Down
286 changes: 286 additions & 0 deletions client/src/main/resources/mvnd-bash-completion.bash

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 the original author or authors.
*
* 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.
*/
package org.mvndaemon.mvnd.client;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Environment.OptionOrigin;
import org.mvndaemon.mvnd.common.IoUtils;

public class CompletionGenerator {

@Test
void generate() throws IOException {

String template = IoUtils.readResource(Completion.class.getClassLoader(),
"completion-templates/mvnd-bash-completion.bash");

final String shortOpts = Stream.of(Environment.values())
.filter(env -> !env.isInternal())
.flatMap(env -> env.getOptionMap().entrySet().stream())
.filter(optEntry -> optEntry.getValue() == OptionOrigin.mvnd)
.map(Map.Entry::getKey)
.filter(opt -> !opt.startsWith("--"))
.sorted()
.collect(Collectors.joining("|"));

final String longOpts = Stream.of(Environment.values())
.filter(env -> !env.isInternal())
.flatMap(env -> env.getOptionMap().entrySet().stream())
.filter(optEntry -> optEntry.getValue() == OptionOrigin.mvnd)
.map(Map.Entry::getKey)
.filter(opt -> opt.startsWith("--"))
.sorted()
.collect(Collectors.joining("|"));

final String props = Stream.of(Environment.values())
.filter(env -> !env.isInternal())
.map(Environment::getProperty)
.filter(Objects::nonNull)
.sorted()
.map(prop -> "-D" + prop)
.collect(Collectors.joining("|"));

template = template.replace("%mvnd_opts%", shortOpts);
template = template.replace("%mvnd_long_opts%", longOpts);
template = template.replace("%mvnd_properties%", props);

final Path baseDir = Paths.get(System.getProperty("project.basedir", "."));

final byte[] bytes = template.getBytes(StandardCharsets.UTF_8);
Files.write(baseDir.resolve("src/main/resources/mvnd-bash-completion.bash"), bytes);
Files.write(baseDir.resolve("target/classes/mvnd-bash-completion.bash"), bytes);

}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#
# Copyright 2019 the original author or authors.
#
# 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.
#

# Adapted from https://github.com/juven/maven-bash-completion/blob/master/bash_completion.bash by Juven Xu and others
# under Apache License Version 2.0

function_exists()
{
declare -F $1 > /dev/null
Expand Down Expand Up @@ -118,16 +137,19 @@ __pom_hierarchy()
done
}

_mvn()
_mvnd()
{
local cur prev
COMPREPLY=()
POM_HIERARCHY=()
__pom_hierarchy
_get_comp_words_by_ref -n : cur prev

local opts="-am|-amd|-B|-C|-c|-cpu|-D|-e|-emp|-ep|-f|-fae|-ff|-fn|-gs|-h|-l|-N|-npr|-npu|-nsu|-o|-P|-pl|-q|-rf|-s|-T|-t|-U|-up|-V|-v|-X"
local long_opts="--also-make|--also-make-dependents|--batch-mode|--strict-checksums|--lax-checksums|--check-plugin-updates|--define|--errors|--encrypt-master-password|--encrypt-password|--file|--fail-at-end|--fail-fast|--fail-never|--global-settings|--help|--log-file|--non-recursive|--no-plugin-registry|--no-plugin-updates|--no-snapshot-updates|--offline|--activate-profiles|--projects|--quiet|--resume-from|--settings|--threads|--toolchains|--update-snapshots|--update-plugins|--show-version|--version|--debug"
local mvnd_opts="%mvnd_opts%"
local mvnd_long_opts="%mvnd_long_opts%"
local mvnd_properties="%mvnd_properties%"
local opts="-am|-amd|-B|-C|-c|-cpu|-D|-e|-emp|-ep|-f|-fae|-ff|-fn|-gs|-h|-l|-N|-npr|-npu|-nsu|-o|-P|-pl|-q|-rf|-s|-T|-t|-U|-up|-V|-v|-X|${mvnd_opts}"
local long_opts="--also-make|--also-make-dependents|--batch-mode|--strict-checksums|--lax-checksums|--check-plugin-updates|--define|--errors|--encrypt-master-password|--encrypt-password|--file|--fail-at-end|--fail-fast|--fail-never|--global-settings|--help|--log-file|--non-recursive|--no-plugin-registry|--no-plugin-updates|--no-snapshot-updates|--offline|--activate-profiles|--projects|--quiet|--resume-from|--settings|--threads|--toolchains|--update-snapshots|--update-plugins|--show-version|--version|--debug|${mvnd_long_opts}"

local common_clean_lifecycle="pre-clean|clean|post-clean"
local common_default_lifecycle="validate|initialize|generate-sources|process-sources|generate-resources|process-resources|compile|process-classes|generate-test-sources|process-test-sources|generate-test-resources|process-test-resources|test-compile|process-test-classes|test|prepare-package|package|pre-integration-test|integration-test|post-integration-test|verify|install|deploy"
Expand Down Expand Up @@ -197,7 +219,7 @@ _mvn()
## some plugin (like jboss-as) has '-' which is not allowed in shell var name, to use '_' then replace
local common_plugins=`compgen -v | grep "^plugin_goals_.*" | sed 's/plugin_goals_//g' | tr '_' '-' | tr '\n' '|'`

local options="-Dmaven.test.skip=true|-DskipTests|-DskipITs|-Dtest|-Dit.test|-DfailIfNoTests|-Dmaven.surefire.debug|-DenableCiProfile|-Dpmd.skip=true|-Dcheckstyle.skip=true|-Dtycho.mode=maven|-Dmaven.javadoc.skip=true|-Dgwt.compiler.skip|-Dcobertura.skip=true|-Dfindbugs.skip=true||-DperformRelease=true|-Dgpg.skip=true|-DforkCount"
local options="-Dmaven.test.skip=true|-DskipTests|-DskipITs|-Dtest|-Dit.test|-DfailIfNoTests|-Dmaven.surefire.debug|-DenableCiProfile|-Dpmd.skip=true|-Dcheckstyle.skip=true|-Dtycho.mode=maven|-Dmaven.javadoc.skip=true|-Dgwt.compiler.skip|-Dcobertura.skip=true|-Dfindbugs.skip=true||-DperformRelease=true|-Dgpg.skip=true|-DforkCount|${mvnd_properties}"

local profile_settings=`[ -e ~/.m2/settings.xml ] && grep -e "<profile>" -A 1 ~/.m2/settings.xml | grep -e "<id>.*</id>" | sed 's/.*<id>//' | sed 's/<\/id>.*//g' | tr '\n' '|' `

Expand Down Expand Up @@ -257,6 +279,8 @@ _mvn()
__ltrim_colon_completions "$cur"
}

complete -o default -F _mvn -o nospace mvn
complete -o default -F _mvn -o nospace mvnDebug
complete -o default -F _mvn -o nospace mvnw
#complete -o default -F _mvn -o nospace mvn
#complete -o default -F _mvn -o nospace mvnDebug
#complete -o default -F _mvn -o nospace mvnw

complete -o default -F _mvnd -o nospace mvnd
Loading

0 comments on commit 8712b39

Please sign in to comment.