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