Skip to content

Commit

Permalink
make wrapper not write to stdout when generating autocompletions
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Mar 13, 2021
1 parent af340b6 commit 2d4a129
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
Empty file modified samples/voodoo
100644 → 100755
Empty file.
Empty file modified samples/voodoo-dev
100644 → 100755
Empty file.
Empty file modified samples/voodoo-release
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions samples/wrapper-dev
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cp -f $DIR/../../wrapper/build/libs/wrapper-0.6.0-local-all.jar $DIR/wrapper/wrapper.jar
java -jar $DIR/wrapper/wrapper.jar $@
cp -f $DIR/../wrapper/build/libs/wrapper-0.6.0-local-all.jar $DIR/wrapper/wrapper-dev.jar
java -jar $DIR/wrapper/wrapper-dev.jar $@
14 changes: 14 additions & 0 deletions voodoo/src/main/kotlin/voodoo/cli/UpdateCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ class UpdateCommand : CliktCommand(
properties.store(it, "generated by: voodoo generateWrapper")
propertiesFile.writeText(it.toString())
}

// download wrapper -> wrapper/wrapper.jar
val wrapperFile = rootDir.resolve("wrapper/wrapper.jar")
wrapperFile.absoluteFile.parentFile.mkdirs()

MavenUtil.downloadArtifact(
GeneratedConstants.MAVEN_URL,
GeneratedConstants.MAVEN_GROUP,
"wrapper",
GeneratedConstants.FULL_VERSION,
outputFile = wrapperFile,
outputDir = wrapperFile.absoluteFile.parentFile,
classifier = GeneratedConstants.MAVEN_SHADOW_CLASSIFIER
)
}
}

Expand Down
59 changes: 37 additions & 22 deletions wrapper/src/main/java/voodoo/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

public class Wrapper {

public static void main(String[] args) throws URISyntaxException {
boolean isAutocomplete = false;
for(String key : System.getenv().keySet()) {
if(key.startsWith("_") && key.endsWith("_COMPLETE")) {
isAutocomplete = true;
break;
}
}
Properties props = new Properties();
File propertiesFile = new File(new File(Wrapper.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile(), "wrapper.properties");
System.out.printf("loading wrapper properties from %s%n", propertiesFile.getPath());
if(!propertiesFile.exists()) {
if (!isAutocomplete) System.out.printf("loading wrapper properties from %s%n", propertiesFile.getPath());
if (!propertiesFile.exists()) {
System.err.println("cannot open file " + propertiesFile.getPath());
System.exit(-1);
}
try {
try(FileReader reader = new FileReader(propertiesFile)) {
try (FileReader reader = new FileReader(propertiesFile)) {
props.load(reader);
}
} catch (IOException e) {
Expand All @@ -34,14 +42,15 @@ public static void main(String[] args) throws URISyntaxException {

String distributionUrl = props.getProperty("distributionUrl", null);

if(distributionUrl == null) {
if (distributionUrl == null) {
System.err.println("missing key 'distributionUrl' in file " + propertiesFile.getPath());
System.exit(-1);
}

String distributionPath = props.getProperty("distributionPath", null);;
String distributionPath = props.getProperty("distributionPath", null);
;

if(distributionPath == null) {
if (distributionPath == null) {
System.err.println("missing key 'distributionPath' in file " + propertiesFile.getPath());
System.exit(-1);
}
Expand All @@ -50,8 +59,8 @@ public static void main(String[] args) throws URISyntaxException {

try {
cleanup(binariesDir);
launch(distributionUrl, binariesDir, args);
} catch(Throwable t) {
launch(distributionUrl, binariesDir, args, isAutocomplete);
} catch (Throwable t) {
t.printStackTrace();
System.err.println("Error: " + t.getLocalizedMessage());
System.exit(-1);
Expand All @@ -67,33 +76,38 @@ private static void cleanup(File binariesDir) {
}


private static void launch(String distributionUrl, File binariesDir, String[] originalArgs) throws Throwable {
private static void launch(
String distributionUrl,
File binariesDir,
String[] originalArgs,
boolean isAutocomplete
) throws Throwable {
String artifact = distributionUrl.substring(distributionUrl.lastIndexOf('/'));
artifact = artifact.substring(0, artifact.lastIndexOf(".jar"));

System.out.printf("Downloading the %s binary...%n", artifact);
if(!isAutocomplete) System.out.printf("Downloading the %s binary...%n", artifact);

// File lastFile = new File(binariesDir, artifact + ".last.jar");

File file;
try {
file = download(distributionUrl, binariesDir, artifact);
if(!file.exists()) {
file = download(distributionUrl, binariesDir, artifact, isAutocomplete);
if (!file.exists()) {
throw new IllegalStateException("downloaded file does not seem to exist");
}
} catch(IOException e) {
} catch (IOException e) {
e.printStackTrace();
System.err.printf("cannot download %s from %s%n", artifact, distributionUrl);
// file = lastFile;
System.exit(-1);
return;
}

if(!file.exists()) {
if (!file.exists()) {
throw new IllegalStateException(String.format("binary %s does not exist", file.getPath()));
}
// Files.copy(file.toPath(), lastFile.toPath());
System.out.printf("Loaded %s%n", file.getPath());
if(!isAutocomplete) System.out.printf("Loaded %s%n", file.getPath());
String java = Paths.get(System.getProperty("java.home"), "bin", "java").toFile().getPath();
File workingDir = new File(System.getProperty("user.dir"));

Expand All @@ -104,7 +118,7 @@ private static void launch(String distributionUrl, File binariesDir, String[] or
// System.out.println(key + ": " + value);
// //TODO: systemPropertyArgs += "-D${key}=${value}"
// }
if(System.getProperty("kotlinx.coroutines.debug") != null) {
if (System.getProperty("kotlinx.coroutines.debug") != null) {
systemPropertyArgs = new String[]{"-Dkotlinx.coroutines.debug"};
} else {
systemPropertyArgs = new String[0];
Expand All @@ -119,9 +133,9 @@ private static void launch(String distributionUrl, File binariesDir, String[] or

String[] args = argsList.toArray(new String[0]);

System.out.printf("Executing %s", argsList.toString());
if(!isAutocomplete) System.out.printf("Executing %s", argsList.toString());
int exitStatus = new ProcessBuilder(args)
.directory(workingDir)
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
Expand All @@ -130,7 +144,7 @@ private static void launch(String distributionUrl, File binariesDir, String[] or
}

public static byte[] createMD5(File file) throws Exception {
InputStream fis = new FileInputStream(file);
InputStream fis = new FileInputStream(file);

byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
Expand Down Expand Up @@ -158,7 +172,8 @@ private static String toHexString(byte[] bytes) {
public static File download(
String distributionUrl,
File outputDir,
String artifact
String artifact,
boolean isAutocomplete
) throws Exception {
File targetFile = new File(outputDir, artifact + ".jar");
File tmpFile = new File(targetFile.getParent(), targetFile.getName() + ".tmp");
Expand All @@ -172,7 +187,7 @@ public static File download(
String fileMd5 = toHexString(createMD5(targetFile));

if (fileMd5.equalsIgnoreCase(md5)) {
System.out.println("cached file matched md5 hash");
if(!isAutocomplete) System.out.println("cached file matched md5 hash");
return targetFile;
}
}
Expand All @@ -188,7 +203,7 @@ public static File download(

{
String fileMd5 = toHexString(createMD5(tmpFile));
if(!fileMd5.equalsIgnoreCase(md5)) {
if (!fileMd5.equalsIgnoreCase(md5)) {
throw new IllegalArgumentException(String.format("%s did not match md5 hash: '%s' fileHash: %s", distributionUrl, md5, fileMd5));
}
}
Expand Down

0 comments on commit 2d4a129

Please sign in to comment.