Skip to content

Commit

Permalink
Remove the Shlex class now it is no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Sep 23, 2024
1 parent c66825b commit c87f557
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package io.github.ascopes.protobufmavenplugin.plugins;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.util.Objects.requireNonNullElse;

import io.github.ascopes.protobufmavenplugin.dependencies.DependencyResolutionDepth;
Expand All @@ -29,14 +26,15 @@
import io.github.ascopes.protobufmavenplugin.utils.Digests;
import io.github.ascopes.protobufmavenplugin.utils.FileUtils;
import io.github.ascopes.protobufmavenplugin.utils.HostSystem;
import io.github.ascopes.protobufmavenplugin.utils.Shlex;
import io.github.ascopes.protobufmavenplugin.utils.SystemPathBinaryResolver;
import java.io.IOException;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -141,6 +139,10 @@ private ResolvedProtocPlugin resolveMavenPlugin(
.build();
}

private String hashPlugin(MavenProtocPlugin plugin) {
return Digests.sha1(plugin.toString());
}

private ArgumentFileBuilder buildArgLine(MavenProtocPlugin plugin)
throws ResolutionException, IOException {
// Expectation: this always has at least one item in it, and the first item is the plugin
Expand Down Expand Up @@ -292,45 +294,45 @@ private Path writePosixScripts(
String id,
Path javaExecutable,
Path scratchDir,
ArgumentFileBuilder argumentFileBuilder
ArgumentFileBuilder argFileBuilder
) throws IOException, ResolutionException {
var sh = pathResolver.resolve("sh").orElseThrow();
var argLineFile = writeArgLineFile(id, UTF_8, scratchDir, argumentFileBuilder);
var content = writeArgLineFile(id, StandardCharsets.UTF_8, scratchDir, argFileBuilder);
var file = scratchDir.resolve("invoke.sh");

try (var writer = Files.newBufferedWriter(file, UTF_8, CREATE_NEW)) {
writer.write("#!");
writer.write(sh.toString());
writer.write("\n");
var sb = new StringBuilder()
.append("#!").append(sh).append('\n')
.append("set -o errexit\n")
.append("set -o posix > /dev/null 2>&1 || :\n");

writer.write("set -o errexit");
writer.write("\n");
quoteShellArg(sb, javaExecutable.toString());
sb.append(' ');
quoteShellArg(sb, "@" + content);
sb.append('\n');

writer.write(Shlex.quoteShellArgs(List.of(javaExecutable.toString(), "@" + argLineFile)));
writer.write("\n");
}
Files.writeString(file, sb, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
FileUtils.makeExecutable(file);

return file;
}

private Path writeWindowsScripts(
String id,
Path javaExecutable,
Path scratchDir,
ArgumentFileBuilder argumentFileBuilder
ArgumentFileBuilder argFileBuilder
) throws IOException {
var argLineFile = writeArgLineFile(id, ISO_8859_1, scratchDir, argumentFileBuilder);
var content = writeArgLineFile(id, StandardCharsets.ISO_8859_1, scratchDir, argFileBuilder);
var file = scratchDir.resolve("invoke.bat");

try (var writer = Files.newBufferedWriter(file, ISO_8859_1, CREATE_NEW)) {
writer.write("@echo off");
writer.write("\r\n");
var sb = new StringBuilder()
.append("@echo off\r\n");

writer.write(Shlex.quoteBatchArgs(List.of(javaExecutable.toString(), "@" + argLineFile)));
writer.write("\r\n");
}
quoteBatchArg(sb, javaExecutable.toString());
sb.append(' ');
quoteBatchArg(sb, "@" + content);
sb.append("\r\n");

Files.writeString(file, sb, StandardCharsets.ISO_8859_1, StandardOpenOption.CREATE_NEW);
return file;
}

Expand All @@ -341,13 +343,69 @@ private Path writeArgLineFile(
ArgumentFileBuilder argumentFileBuilder
) throws IOException {
var file = scratchDir.resolve("args.txt");
try (var writer = Files.newBufferedWriter(file, charset, CREATE_NEW)) {
try (var writer = Files.newBufferedWriter(file, charset, StandardOpenOption.CREATE_NEW)) {
argumentFileBuilder.write(writer);
}
return file;
}

private String hashPlugin(MavenProtocPlugin plugin) {
return Digests.sha1(plugin.toString());
private void quoteShellArg(StringBuilder sb, String arg) {
sb.append('\'');
for (var i = 0; i < arg.length(); ++i) {
var c = arg.charAt(i);
switch (c) {
case '\\':
sb.append("\\\\");
break;
case '\'':
sb.append("'\"'\"'");
break;
case '\n':
sb.append("'$'\\n''");
break;
case '\r':
sb.append("'$'\\r''");
break;
case '\t':
sb.append("'$'\\t''");
break;
default:
sb.append(c);
break;
}
}

sb.append('\'');
}

private void quoteBatchArg(StringBuilder sb, String arg) {
sb.append('"');

for (var i = 0; i < arg.length(); ++i) {
var c = arg.charAt(i);

switch (c) {
case '"':
sb.append("\"\"\"");
break;
case '%':
sb.append("%%");
break;
case '\r':
case '\t':
case '^':
case '&':
case '<':
case '>':
case '|':
sb.append('^').append(c);
break;
default:
sb.append(c);
break;
}
}

sb.append('"');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.github.ascopes.protobufmavenplugin.protoc;

import io.github.ascopes.protobufmavenplugin.utils.HostSystem;
import io.github.ascopes.protobufmavenplugin.utils.Shlex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -63,11 +62,8 @@ public boolean execute(List<String> args) throws IOException {
}

private void reportInvocation(List<String> args) {
var renderedCommandLine = hostSystem.isProbablyWindows()
? Shlex.quoteBatchArgs(args)
: Shlex.quoteShellArgs(args);
log.info("Calling protoc with the following invocation:");
renderedCommandLine.lines()
log.info("Calling protoc with the following arguments:");
args.stream()
.map(" "::concat)
.map(String::stripTrailing)
.forEach(log::info);
Expand Down

This file was deleted.

Loading

0 comments on commit c87f557

Please sign in to comment.