-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make compatible with Gradle Config Cache (#92)
* bring build.gradle up to date * update gradle to 8.1.1 * introduce CommandExecutor in order to provide different implementations for maven and gradle: - keep using ProcessBuilder for Maven - Use ProviderFactory for Gradle * update gradle to 8.7 * catch and handle exec exception --------- Co-authored-by: Hanzhen Yi <hzyi@google.com>
- Loading branch information
1 parent
c9b7f29
commit fa581f3
Showing
10 changed files
with
174 additions
and
45 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...try-auth-common/src/main/java/com/google/cloud/artifactregistry/auth/CommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.google.cloud.artifactregistry.auth; | ||
|
||
import java.io.IOException; | ||
|
||
public interface CommandExecutor { | ||
public CommandExecutorResult executeCommand( | ||
String command, | ||
String... args | ||
) throws IOException; | ||
} |
17 changes: 17 additions & 0 deletions
17
...th-common/src/main/java/com/google/cloud/artifactregistry/auth/CommandExecutorResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.google.cloud.artifactregistry.auth; | ||
|
||
public class CommandExecutorResult { | ||
public final int exitCode; | ||
public final String stdOut; | ||
public final String stdErr; | ||
|
||
public CommandExecutorResult( | ||
int exitCode, | ||
String stdOut, | ||
String stdErr | ||
) { | ||
this.exitCode = exitCode; | ||
this.stdOut = stdOut; | ||
this.stdErr = stdErr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../java/com/google/cloud/artifactregistry/gradle/plugin/ProviderFactoryCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.google.cloud.artifactregistry.gradle.plugin; | ||
|
||
import com.google.cloud.artifactregistry.auth.CommandExecutor; | ||
import com.google.cloud.artifactregistry.auth.CommandExecutorResult; | ||
import org.gradle.api.provider.ProviderFactory; | ||
import org.gradle.process.ExecOutput; | ||
import org.gradle.process.internal.ExecException; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ProviderFactoryCommandExecutor implements CommandExecutor { | ||
private final ProviderFactory providerFactory; | ||
|
||
public ProviderFactoryCommandExecutor(ProviderFactory providerFactory) { | ||
this.providerFactory = providerFactory; | ||
} | ||
|
||
@Override | ||
public CommandExecutorResult executeCommand(String command, String... args) throws IOException { | ||
List<String> argList = new ArrayList<>(); | ||
argList.add(command); | ||
argList.addAll(Arrays.asList(args)); | ||
|
||
ExecOutput execOutput; | ||
try { | ||
execOutput = providerFactory.exec(execSpec -> { | ||
execSpec.commandLine(argList); | ||
}); | ||
} catch (ExecException e) { | ||
// Downstream cannot handle Gradle specific exceptions | ||
throw new IOException(e); | ||
} | ||
|
||
int exitCode = execOutput.getResult().get().getExitValue(); | ||
String stdOut = execOutput.getStandardOutput().getAsText().get(); | ||
String stdErr = execOutput.getStandardError().getAsText().get(); | ||
|
||
return new CommandExecutorResult( | ||
exitCode, | ||
stdOut, | ||
stdErr | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../src/main/java/com/google/cloud/artifactregistry/wagon/ProcessBuilderCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.google.cloud.artifactregistry.wagon; | ||
|
||
import com.google.api.client.util.GenericData; | ||
import com.google.auth.oauth2.AccessToken; | ||
import com.google.cloud.artifactregistry.auth.CommandExecutor; | ||
import com.google.cloud.artifactregistry.auth.CommandExecutorResult; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ProcessBuilderCommandExecutor implements CommandExecutor { | ||
@Override | ||
public CommandExecutorResult executeCommand(String command, String... args) throws IOException { | ||
List<String> argList = new ArrayList<>(); | ||
argList.add(command); | ||
argList.addAll(Arrays.asList(args)); | ||
|
||
ProcessBuilder processBuilder = new ProcessBuilder(); | ||
processBuilder.command(argList); | ||
Process process = processBuilder.start(); | ||
|
||
try { | ||
int exitCode = process.waitFor(); | ||
String stdOut = readStreamToString(process.getInputStream()); | ||
String stdErr = readStreamToString(process.getErrorStream()); | ||
|
||
return new CommandExecutorResult(exitCode, stdOut, stdErr); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
// Reads a stream to a string, this code is basically copied from 'copyReaderToBuilder' from | ||
// com.google.io.CharStreams in the Guava library. | ||
private static String readStreamToString(InputStream input) throws IOException { | ||
InputStreamReader reader = new InputStreamReader(input); | ||
StringBuilder output = new StringBuilder(); | ||
char[] buf = new char[0x800]; | ||
int nRead; | ||
while ((nRead = reader.read(buf)) != -1) { | ||
output.append(buf, 0, nRead); | ||
} | ||
return output.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters