Skip to content

Commit 7093b4b

Browse files
committed
continue work on replacing local gcd.sh with a remote version
1 parent 7385fb5 commit 7093b4b

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.nio.file.Paths;
4343
import java.nio.file.SimpleFileVisitor;
4444
import java.nio.file.attribute.BasicFileAttributes;
45+
import java.util.Locale;
4546
import java.util.zip.ZipEntry;
4647
import java.util.zip.ZipInputStream;
4748

@@ -117,7 +118,7 @@ public LocalGcdHelper(String projectId) {
117118

118119
public void start() throws IOException, InterruptedException {
119120
sendQuitRequest();
120-
gcdPath = Files.createTempDirectory(GCD_VERSION);
121+
gcdPath = Files.createTempDirectory("gcd");
121122
File gcdFolder = gcdPath.toFile();
122123
gcdFolder.deleteOnExit();
123124

@@ -126,6 +127,7 @@ public void start() throws IOException, InterruptedException {
126127
ReadableByteChannel rbc = Channels.newChannel(GCD_URL.openStream());
127128
FileOutputStream fos = new FileOutputStream(gcdZipFile);
128129
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
130+
fos.close();
129131
}
130132
try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(gcdZipFile))) {
131133
ZipEntry entry = zipIn.getNextEntry();
@@ -140,27 +142,41 @@ public void start() throws IOException, InterruptedException {
140142
entry = zipIn.getNextEntry();
141143
}
142144
}
143-
144145
File datasetFolder = new File(gcdFolder, GCD_VERSION + '/' + projectId);
145146
deleteRecurse(datasetFolder.toPath());
146147

147-
// TODO: if System.getProperty("os.name").startsWith("Windows") use cmd.exe /c and gcd.cmd
148-
Process temp = new ProcessBuilder()
148+
ProcessBuilder processBuilder = new ProcessBuilder()
149149
.redirectErrorStream(true)
150-
.directory(new File(gcdFolder, GCD_VERSION))
151-
.redirectOutput(new File("/dev/null"))
152-
.command("bash", "gcd.sh", "create", "-d", projectId, projectId)
153-
.start();
150+
.directory(new File(gcdFolder, GCD_VERSION));
151+
if (isWindows()) {
152+
processBuilder.command("cmd", "/C", "gcd.cmd", "create", "-p", projectId, projectId);
153+
processBuilder.redirectOutput(new File("NULL:"));
154+
} else {
155+
processBuilder.redirectOutput(new File("/dev/null"));
156+
processBuilder.command("bash", "gcd.sh", "create", "-p", projectId, projectId);
157+
}
158+
159+
Process temp = processBuilder.start();
154160
temp.waitFor();
155161

156-
temp = new ProcessBuilder()
162+
processBuilder = new ProcessBuilder()
157163
.directory(new File(gcdFolder, GCD_VERSION))
158-
.redirectErrorStream(true)
159-
.command("bash", "gcd.sh", "start", "--testing", "--allow_remote_shutdown", projectId)
160-
.start();
164+
.redirectErrorStream(true);
165+
if (isWindows()) {
166+
processBuilder.command("cmd", "/C", "gcd.cmd", "start", "--testing",
167+
"--allow_remote_shutdown", projectId);
168+
} else {
169+
processBuilder.command("bash", "gcd.sh", "start", "--testing", "--allow_remote_shutdown",
170+
projectId);
171+
}
172+
temp = processBuilder.start();
161173
processReader = ProcessStreamReader.start(temp, "Dev App Server is now running");
162174
}
163175

176+
private static boolean isWindows() {
177+
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).indexOf("windows") > -1;
178+
}
179+
164180
private static void extractFile(ZipInputStream zipIn, File filePath) throws IOException {
165181
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
166182
byte[] bytesIn = new byte[1024];

0 commit comments

Comments
 (0)