1 package com.atlassian.sdk.accept;
2
3 import org.apache.commons.io.FileUtils;
4 import org.apache.commons.io.IOUtils;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.FileOutputStream;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Locale;
15 import java.util.Enumeration;
16 import java.util.zip.ZipFile;
17 import java.util.zip.ZipEntry;
18
19 import junit.framework.Assert;
20
21 public class SdkHelper
22 {
23 public static File setupSdk(File baseDir) throws IOException
24 {
25 File sdkZip = new File(System.getProperty("sdk.zip"));
26
27 baseDir.mkdirs();
28 unzip(sdkZip, baseDir);
29 return new File(baseDir, sdkZip.getName().substring(0, sdkZip.getName().length() - ".zip".length()));
30 }
31
32 public static void runSdkScript(File sdkHome, File baseDir, String scriptName, String... args)
33 throws IOException, InterruptedException
34 {
35 String extension = isWindows() ? ".bat" : "";
36 File bin = new File(sdkHome, "bin");
37
38 ExecRunner runner = new ExecRunner();
39 File command = new File(bin, scriptName + extension);
40
41 if (!isWindows())
42 {
43 runner.run(baseDir, Arrays.asList(
44 "/bin/chmod",
45 "755",
46 sdkHome.getAbsolutePath() + "/apache-maven/bin/mvn",
47 command.getAbsolutePath()), Collections.<String, String>emptyMap());
48 }
49 List<String> cmdlist = new ArrayList<String>(Arrays.asList(args));
50 cmdlist.add(0, command.getAbsolutePath());
51 cmdlist.add("-s");
52 cmdlist.add(file(sdkHome, "apache-maven", "conf", "settings.xml").getPath());
53
54 Assert.assertEquals(0, runner.run(baseDir, cmdlist, new HashMap<String, String>()
55 {{
56 put("MAVEN_OPTS", "-Xmx256m");
57 put("JAVA_HOME", System.getProperty("java.home"));
58 put("PATH", System.getenv("PATH"));
59 }}));
60 }
61
62 public static boolean isWindows()
63 {
64 String myos = System.getProperty("os.name");
65 return (myos.toLowerCase(Locale.ENGLISH).indexOf("windows") > -1);
66 }
67
68 private static void unzip(File zipfile, File baseDir) throws IOException
69 {
70 ZipFile zip = new ZipFile(zipfile);
71 for (Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries(); entries.hasMoreElements();)
72 {
73 ZipEntry entry = entries.nextElement();
74 File target = new File(baseDir, entry.getName());
75 if (entry.isDirectory())
76 {
77 target.mkdirs();
78 }
79 else
80 {
81 FileOutputStream fout = new FileOutputStream(target);
82 IOUtils.copy(zip.getInputStream(entry), fout);
83 IOUtils.closeQuietly(fout);
84 }
85 }
86 }
87
88 public static File file(File parent, String... kids)
89 {
90 File cur = parent;
91 for (String kid : kids)
92 {
93 cur = new File(cur, kid);
94 }
95 return cur;
96 }
97
98 public static File createPlugin(String productId, File baseDir, File sdkHome, String prefix) throws IOException, InterruptedException
99 {
100 final String artifactId = prefix + "-" + productId + "-plugin";
101 final File appDir = new File(baseDir, artifactId);
102 FileUtils.deleteDirectory(appDir);
103
104 runSdkScript(sdkHome, baseDir, "atlas-create-" + productId + "-plugin",
105 "-a", artifactId,
106 "-g", "com.example",
107 "-p", "com.example.foo",
108 "-v", "1.0-SNAPSHOT",
109 "--non-interactive");
110
111 Assert.assertTrue(appDir.exists());
112 return appDir;
113 }
114 }