Skip to content

Commit

Permalink
fix CodeNarc issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottmitch committed Jul 27, 2020
1 parent f99bc24 commit 6e8639f
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy
Expand Up @@ -29,6 +29,8 @@
*/
package com.google.protobuf.gradle

import static java.nio.charset.StandardCharsets.US_ASCII

import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableList
import groovy.transform.CompileDynamic
Expand Down Expand Up @@ -64,8 +66,6 @@ import org.gradle.util.ConfigureUtil
import javax.annotation.Nullable
import javax.inject.Inject

import static java.nio.charset.StandardCharsets.US_ASCII

/**
* The task that compiles proto files into Java files.
*/
Expand All @@ -79,7 +79,7 @@ public abstract class GenerateProtoTask extends DefaultTask {
// Extra command line length when added an additional argument on Windows.
// Two quotes and a space.
static final int CMD_ARGUMENT_EXTRA_LENGTH = 3
private static final JAR_SUFFIX = ".jar"
private static final String JAR_SUFFIX = ".jar"

// include dirs are passed to the '-I' option of protoc. They contain protos
// that may be "imported" from the source protos, but will not be compiled.
Expand Down Expand Up @@ -156,6 +156,20 @@ public abstract class GenerateProtoTask extends DefaultTask {
@Internal("Handled as input via getDescriptorSetOptionsForCaching()")
final DescriptorSetOptions descriptorSetOptions = new DescriptorSetOptions()

private static boolean createNewScriptFile(File outputFile) throws IOException {
if (!outputFile.getParentFile().isDirectory() && !outputFile.getParentFile().mkdirs()) {
throw new IOException("unable to make directories for file: " + outputFile.getCanonicalPath())
}
return true
}

private static void finalizeScriptFile(File outputFile) throws IOException {
if (!outputFile.setExecutable(true)) {
outputFile.delete()
throw new IOException("unable to set file as executable: " + outputFile.getCanonicalPath())
}
}

// protoc allows you to prefix comma-delimited options to the path in
// the --*_out flags, e.g.,
// - Without options: --java_out=/path/to/output
Expand Down Expand Up @@ -668,16 +682,16 @@ public abstract class GenerateProtoTask extends DefaultTask {
*/
private String createJarTrampolineScript(String jarAbsolutePath) {
assert jarAbsolutePath.endsWith(JAR_SUFFIX)
final boolean isWindows = isWindows()
boolean isWindows = isWindows()
File scriptExecutableFile = new File("${project.buildDir}/scripts/" +
jarAbsolutePath.substring(0, jarAbsolutePath.length() - JAR_SUFFIX.length()) + "-trampoline." +
jarAbsolutePath[0..(jarAbsolutePath.length() - JAR_SUFFIX.length())] + "-trampoline." +
(isWindows ? "bat" : "sh"))
try {
if (createNewScriptFile(scriptExecutableFile)) {
// Rewrite the trampoline file unconditionally (even if it already exists) in case the dependency or versioning
// changes we don't need to detect the delta (and the file content is cheap to re-generate).
new FileOutputStream(scriptExecutableFile).withCloseable { execOutputStream ->
final String trampoline = isWindows ?
String trampoline = isWindows ?
"@ECHO OFF\r\njava -jar ${jarAbsolutePath} %*\r\n" :
"#!/bin/sh\nexec java -jar ${jarAbsolutePath} \"\$@\"\n"
execOutputStream.write(trampoline.getBytes(US_ASCII))
Expand All @@ -686,22 +700,8 @@ public abstract class GenerateProtoTask extends DefaultTask {
}
logger.info("Resolved artifact jar: ${jarAbsolutePath}. Created trampoline file: ${scriptExecutableFile}")
return scriptExecutableFile.path
} catch (Exception e) {
} catch (IOException e) {
throw new GradleException("Unable to generate trampoline for .jar protoc plugin", e)
}
}

private static boolean createNewScriptFile(File outputFile) throws IOException {
if (!outputFile.getParentFile().isDirectory() && !outputFile.getParentFile().mkdirs()) {
throw new IOException("unable to make directories for file: " + outputFile.getCanonicalPath())
}
return true
}

private static void finalizeScriptFile(File outputFile) throws IOException {
if (!outputFile.setExecutable(true)) {
outputFile.delete()
throw new IOException("unable to set file as executable: " + outputFile.getCanonicalPath())
}
}
}

0 comments on commit 6e8639f

Please sign in to comment.