Speed up bash completion loading by packaging it as a file in the ZIP distribution #296

This commit is contained in:
Peter Palaga
2021-01-07 00:30:19 +01:00
parent 445c893d82
commit aae395e8b2
4 changed files with 18 additions and 7 deletions

View File

@@ -15,15 +15,27 @@
*/ */
package org.mvndaemon.mvnd.client; package org.mvndaemon.mvnd.client;
import org.mvndaemon.mvnd.common.IoUtils; import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class Completion { public class Completion {
public static String getCompletion(String shell) { public static String getCompletion(String shell, DaemonParameters daemonParameters) {
if (!"bash".equals(shell)) { if (!"bash".equals(shell)) {
throw new IllegalArgumentException("Unexpected --completion value: '" + shell + "'; expected: 'bash'"); throw new IllegalArgumentException("Unexpected --completion value: '" + shell + "'; expected: 'bash'");
} }
return IoUtils.readResource(Completion.class.getClassLoader(), "mvnd-bash-completion.bash"); final Path bashCompletionPath = daemonParameters.mvndHome().resolve("bin/mvnd-bash-completion.bash");
if (!Files.isRegularFile(bashCompletionPath)) {
throw new IllegalStateException("Bash completion file does not exist: " + bashCompletionPath);
}
try {
return new String(Files.readAllBytes(bashCompletionPath), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException("Could not read " + bashCompletionPath, e);
}
} }
} }

View File

@@ -136,7 +136,7 @@ public class DefaultClient implements Client {
final List<String> args = new ArrayList<>(argv); final List<String> args = new ArrayList<>(argv);
final String completionShell = Environment.COMPLETION.removeCommandLineOption(args); final String completionShell = Environment.COMPLETION.removeCommandLineOption(args);
if (completionShell != null) { if (completionShell != null) {
output.accept(Message.log(Completion.getCompletion(completionShell))); output.accept(Message.log(Completion.getCompletion(completionShell, parameters)));
return DefaultResult.success(argv); return DefaultResult.success(argv);
} }

View File

@@ -29,7 +29,7 @@ import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Environment.OptionOrigin; import org.mvndaemon.mvnd.common.Environment.OptionOrigin;
import org.mvndaemon.mvnd.common.IoUtils; import org.mvndaemon.mvnd.common.IoUtils;
public class CompletionGenerator { public class CompletionGeneratorTest {
@Test @Test
void generate() throws IOException { void generate() throws IOException {
@@ -70,8 +70,7 @@ public class CompletionGenerator {
final Path baseDir = Paths.get(System.getProperty("project.basedir", ".")); final Path baseDir = Paths.get(System.getProperty("project.basedir", "."));
final byte[] bytes = template.getBytes(StandardCharsets.UTF_8); final byte[] bytes = template.getBytes(StandardCharsets.UTF_8);
Files.write(baseDir.resolve("src/main/resources/mvnd-bash-completion.bash"), bytes); Files.write(baseDir.resolve("../dist/src/main/distro/bin/mvnd-bash-completion.bash"), bytes);
Files.write(baseDir.resolve("target/classes/mvnd-bash-completion.bash"), bytes);
} }