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

Bash completion #255

Merged
merged 3 commits into from Dec 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/pom.xml
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
@@ -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");
}

}
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.

@@ -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);

}

}