View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import java.io.File;
23  import java.io.FileFilter;
24  import java.io.IOException;
25  import java.util.Arrays;
26  import java.util.HashSet;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import org.apache.commons.io.FileUtils;
31  
32  public class XCodeTest
33  {
34    protected void ensureCleanProjectDirectoryAndFilterPom(final File projectDirectory) throws Exception
35    {
36      File source = new File(new File(".").getAbsoluteFile(), "src/test/projects");
37  
38      MacFileUtil.deleteDirectory(projectDirectory);
39      FileUtils.copyDirectory(source, projectDirectory);
40      filterPoms(projectDirectory);
41    }
42  
43    private static void filterPoms(File projectDirectory) throws IOException
44    {
45      Set<File> pomFiles = new HashSet<File>();
46      getPomFiles(projectDirectory, pomFiles);
47  
48      for (File pomFile : pomFiles) {
49        String pom = FileUtils.readFileToString(pomFile);
50        pom = pom.replaceAll("\\$\\{xcode.maven.plugin.version\\}", getMavenXcodePluginVersion());
51        FileUtils.writeStringToFile(pomFile, pom, "UTF-8");
52      }
53    }
54  
55    private static void getPomFiles(File root, final Set<File> pomFiles)
56    {
57      if (root.isFile())
58        return;
59  
60      pomFiles.addAll(Arrays.asList(root.listFiles(new FileFilter() {
61  
62        @Override
63        public boolean accept(File f)
64        {
65          // here we have the implicit assumtion that a pom file is named "pom.xml".
66          // In case we introduce any test with a diffent name for a pom file we have
67          // to revisit that.
68          return f.isFile() && f.getName().equals("pom.xml");
69        }
70  
71      })));
72  
73      for (File f : Arrays.asList(root.listFiles(new FileFilter() {
74  
75        @Override
76        public boolean accept(File f)
77        {
78          return f.isDirectory();
79        }
80      })))
81        getPomFiles(f, pomFiles);
82    }
83  
84    private static String getMavenXcodePluginVersion() throws IOException
85    {
86      Properties properties = new Properties();
87      properties.load(XCodeManagerTest.class.getResourceAsStream("/misc/project.properties"));
88      final String xcodePluginVersion = properties.getProperty("xcode-plugin-version");
89  
90      if (xcodePluginVersion.equals("${project.version}"))
91        throw new IllegalStateException(
92              "Variable ${project.version} was not replaced. May be running \"mvn clean install\" beforehand might solve this issue.");
93      return xcodePluginVersion;
94    }
95  
96  }