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.PrintStream;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Context object for Xcode build to hold relevant data: * projectName * Set of configurations * Set
33   * of SDKs * List of buildActions projectRootDirectory codeSignIdentity output stream xcode options
34   * xcode settings
35   */
36  public class XCodeContext implements IXCodeContext
37  {
38    enum SourceCodeLocation
39    {
40      ORIGINAL, WORKING_COPY
41    };
42  
43    private final static String ls = System.getProperty("line.separator");
44  
45    private final List<String> buildActions;
46  
47    private final File projectRootDirectory;
48  
49    private PrintStream out;
50  
51    private final Options options;
52  
53    private final Settings settings;
54  
55    public XCodeContext(List<String> buildActions,
56          File projectRootDirectory, PrintStream out, Settings settings, Options options)
57    {
58      super();
59  
60      raiseExceptionIfBuildActionsAreInvalid("buildActions", buildActions);
61  
62      if (projectRootDirectory == null || !projectRootDirectory.canRead())
63        throw new IllegalArgumentException("ProjectRootDirectory '" + projectRootDirectory
64              + "' is null or cannot be read.");
65  
66      this.buildActions = Collections.unmodifiableList(new ArrayList<String>(buildActions));
67      this.projectRootDirectory = new File(projectRootDirectory, "");
68      setOut(out);
69  
70      if (settings == null) {
71        Map<String, String> userSettings = new HashMap<String, String>(), managedSettings = new HashMap<String, String>();
72        this.settings = new Settings(userSettings, managedSettings);
73      }
74      else {
75        this.settings = settings;
76      }
77  
78      if (options == null) {
79        Map<String, String> userOptions = new HashMap<String, String>(), managedOptions = new HashMap<String, String>();
80        this.options = new Options(userOptions, managedOptions);
81      }
82      else {
83        this.options = options;
84      }
85    }
86  
87    public String getProjectName()
88    {
89      return options.getAllOptions().get(Options.ManagedOption.PROJECT.getOptionName());
90    }
91  
92    public List<String> getBuildActions()
93    {
94      return buildActions;
95    }
96  
97    public String getCodeSignIdentity()
98    {
99      return settings.getAllSettings().get(Settings.ManagedSetting.CODE_SIGN_IDENTITY.name());
100   }
101 
102   public File getProjectRootDirectory()
103   {
104     return projectRootDirectory;
105   }
106 
107   public PrintStream getOut()
108   {
109     return out;
110   }
111 
112   public final void setOut(PrintStream out)
113   {
114     if (out == null)
115       throw new IllegalArgumentException("PrintStream for log handling is not available.");
116     this.out = out;
117   }
118 
119   /* (non-Javadoc)
120    * @see com.sap.prd.mobile.ios.mios.IXCodeContext#getSDK()
121    */
122   @Override
123   public String getSDK()
124   {
125     return getOptions().getAllOptions().get(Options.ManagedOption.SDK.getOptionName());
126   }
127 
128   @Override
129   public String getConfiguration()
130   {
131     return getOptions().getAllOptions().get(Options.ManagedOption.CONFIGURATION.getOptionName());
132   }
133 
134   public String getProvisioningProfile()
135   {
136     return getSettings().getAllSettings().get(Settings.ManagedSetting.PROVISIONING_PROFILE.name());
137   }
138 
139   public String getTarget()
140   {
141     return getOptions().getAllOptions().get(Options.ManagedOption.TARGET.getOptionName());
142   }
143 
144   public Options getOptions()
145   {
146     return options;
147   }
148 
149   /* (non-Javadoc)
150    * @see com.sap.prd.mobile.ios.mios.IXCodeContext#getSettings()
151    */
152   public Settings getSettings()
153   {
154     return settings;
155   }
156 
157   @Override
158   public String toString()
159   {
160     final StringBuilder sb = new StringBuilder();
161     sb.append(ls).append(super.toString()).append(ls);
162     sb.append("ProjectRootDirectory: ").append(getProjectRootDirectory()).append(ls);
163     sb.append("BuildActions: ").append(buildActions).append(ls).append(ls);
164     sb.append("Options:").append(ls);
165     sb.append(options).append(ls).append(ls);
166     sb.append("Settings:").append(ls);
167     sb.append(settings).append(ls);
168     return sb.toString();
169   }
170 
171   @Override
172   public int hashCode()
173   {
174     final int prime = 31;
175     int result = 1;
176     result = prime * result + ((buildActions == null) ? 0 : buildActions.hashCode());
177     result = prime * result + ((options == null) ? 0 : options.hashCode());
178     result = prime * result + ((projectRootDirectory == null) ? 0 : projectRootDirectory.hashCode());
179     result = prime * result + ((settings == null) ? 0 : settings.hashCode());
180     return result;
181   }
182 
183   @Override
184   public boolean equals(Object obj)
185   {
186     if (this == obj) return true;
187     if (obj == null) return false;
188     if (getClass() != obj.getClass()) return false;
189     XCodeContext other = (XCodeContext) obj;
190     if (buildActions == null) {
191       if (other.buildActions != null) return false;
192     }
193     else if (!buildActions.equals(other.buildActions)) return false;
194     if (options == null) {
195       if (other.options != null) return false;
196     }
197     else if (!options.equals(other.options)) return false;
198     if (projectRootDirectory == null) {
199       if (other.projectRootDirectory != null) return false;
200     }
201     else if (!projectRootDirectory.equals(other.projectRootDirectory)) return false;
202     if (settings == null) {
203       if (other.settings != null) return false;
204     }
205     else if (!settings.equals(other.settings)) return false;
206     return true;
207   }
208 
209   private static void raiseExceptionIfBuildActionsAreInvalid(final String key, final Collection<String> buildActions)
210   {
211 
212     for (final String buildAction : buildActions) {
213 
214       if (buildAction == null || buildAction.length() == 0)
215         throw new InvalidBuildActionException("Build action array contained a null element or an empty element.");
216 
217       if (!buildAction.matches("[A-Za-z0-9_]+"))
218         throw new InvalidBuildActionException("Build action array contains an invalid element (" + buildAction + ").");
219     }
220   }
221 
222   static class InvalidBuildActionException extends IllegalArgumentException
223   {
224 
225     private static final long serialVersionUID = 6635006296438188082L;
226 
227     InvalidBuildActionException(String message)
228     {
229       super(message);
230     }
231   }
232 }