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("-Dallow.google.tracking=false");
51          cmdlist.add(0, command.getAbsolutePath());
52          cmdlist.add("-s");
53          cmdlist.add(file(sdkHome, "apache-maven", "conf", "settings.xml").getPath());
54  
55          Assert.assertEquals(0, runner.run(baseDir, cmdlist, new HashMap<String, String>()
56          {{
57              put("MAVEN_OPTS", "-Xmx256m");
58              put("JAVA_HOME", System.getProperty("java.home"));
59              put("PATH", System.getenv("PATH"));
60          }}));
61      }
62  
63      public static boolean isWindows()
64      {
65          String myos = System.getProperty("os.name");
66          return (myos.toLowerCase(Locale.ENGLISH).indexOf("windows") > -1);
67      }
68  
69      private static void unzip(File zipfile, File baseDir) throws IOException
70      {
71          ZipFile zip = new ZipFile(zipfile);
72          for (Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries(); entries.hasMoreElements();)
73          {
74              ZipEntry entry = entries.nextElement();
75              File target = new File(baseDir, entry.getName());
76              if (entry.isDirectory())
77              {
78                  target.mkdirs();
79              }
80              else
81              {
82                  FileOutputStream fout = new FileOutputStream(target);
83                  IOUtils.copy(zip.getInputStream(entry), fout);
84                  IOUtils.closeQuietly(fout);
85              }
86          }
87      }
88  
89      public static File file(File parent, String... kids)
90      {
91          File cur = parent;
92          for (String kid : kids)
93          {
94              cur = new File(cur, kid);
95          }
96          return cur;
97      }
98  
99      public static File createPlugin(String productId, File baseDir, File sdkHome, String prefix) throws IOException, InterruptedException
100     {
101         final String artifactId = prefix + "-" + productId + "-plugin";
102         final File appDir = new File(baseDir, artifactId);
103         FileUtils.deleteDirectory(appDir);
104 
105         runSdkScript(sdkHome, baseDir, "atlas-create-" + productId + "-plugin",
106                 "-a", artifactId,
107                 "-g", "com.example",
108                 "-p", "com.example.foo",
109                 "-v", "1.0-SNAPSHOT",
110                 "--non-interactive");
111 
112         Assert.assertTrue(appDir.exists());
113         return appDir;
114     }
115 }