diff options
Diffstat (limited to 'Prebuild/src/Core/Targets/SharpDevelopTarget.cs')
-rw-r--r-- | Prebuild/src/Core/Targets/SharpDevelopTarget.cs | 437 |
1 files changed, 437 insertions, 0 deletions
diff --git a/Prebuild/src/Core/Targets/SharpDevelopTarget.cs b/Prebuild/src/Core/Targets/SharpDevelopTarget.cs new file mode 100644 index 0000000..c725730 --- /dev/null +++ b/Prebuild/src/Core/Targets/SharpDevelopTarget.cs | |||
@@ -0,0 +1,437 @@ | |||
1 | #region BSD License | ||
2 | /* | ||
3 | Copyright (c) 2004 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without modification, are permitted | ||
6 | provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
9 | and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
11 | and the following disclaimer in the documentation and/or other materials provided with the | ||
12 | distribution. | ||
13 | * The name of the author may not be used to endorse or promote products derived from this software | ||
14 | without specific prior written permission. | ||
15 | |||
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
21 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
22 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
23 | */ | ||
24 | #endregion | ||
25 | |||
26 | #region CVS Information | ||
27 | /* | ||
28 | * $Source$ | ||
29 | * $Author: jendave $ | ||
30 | * $Date: 2007-02-13 21:58:03 +0100 (ti, 13 feb 2007) $ | ||
31 | * $Revision: 205 $ | ||
32 | */ | ||
33 | #endregion | ||
34 | |||
35 | using System; | ||
36 | using System.Collections; | ||
37 | using System.Collections.Specialized; | ||
38 | using System.IO; | ||
39 | using System.Text.RegularExpressions; | ||
40 | using System.Reflection; | ||
41 | |||
42 | using Prebuild.Core.Attributes; | ||
43 | using Prebuild.Core.Interfaces; | ||
44 | using Prebuild.Core.Nodes; | ||
45 | using Prebuild.Core.Utilities; | ||
46 | |||
47 | namespace Prebuild.Core.Targets | ||
48 | { | ||
49 | /// <summary> | ||
50 | /// | ||
51 | /// </summary> | ||
52 | [Target("sharpdev")] | ||
53 | public class SharpDevelopTarget : ITarget | ||
54 | { | ||
55 | #region Fields | ||
56 | |||
57 | private Kernel m_Kernel; | ||
58 | |||
59 | #endregion | ||
60 | |||
61 | #region Private Methods | ||
62 | |||
63 | private static string PrependPath(string path) | ||
64 | { | ||
65 | string tmpPath = Helper.NormalizePath(path, '/'); | ||
66 | Regex regex = new Regex(@"(\w):/(\w+)"); | ||
67 | Match match = regex.Match(tmpPath); | ||
68 | if(match.Success || tmpPath[0] == '.' || tmpPath[0] == '/') | ||
69 | { | ||
70 | tmpPath = Helper.NormalizePath(tmpPath); | ||
71 | } | ||
72 | else | ||
73 | { | ||
74 | tmpPath = Helper.NormalizePath("./" + tmpPath); | ||
75 | } | ||
76 | |||
77 | return tmpPath; | ||
78 | } | ||
79 | |||
80 | private static string BuildReference(SolutionNode solution, ReferenceNode refr) | ||
81 | { | ||
82 | string ret = "<Reference type=\""; | ||
83 | if(solution.ProjectsTable.ContainsKey(refr.Name)) | ||
84 | { | ||
85 | ret += "Project\" refto=\"" + refr.Name; | ||
86 | ret += "\" localcopy=\"" + refr.LocalCopy.ToString() + "\" />"; | ||
87 | } | ||
88 | else | ||
89 | { | ||
90 | ProjectNode project = (ProjectNode)refr.Parent; | ||
91 | string fileRef = FindFileReference(refr.Name, project); | ||
92 | |||
93 | if(refr.Path != null || fileRef != null) | ||
94 | { | ||
95 | ret += "Assembly\" refto=\""; | ||
96 | |||
97 | string finalPath = (refr.Path != null) ? Helper.MakeFilePath(refr.Path, refr.Name, "dll") : fileRef; | ||
98 | |||
99 | ret += finalPath; | ||
100 | ret += "\" localcopy=\"" + refr.LocalCopy.ToString() + "\" />"; | ||
101 | return ret; | ||
102 | } | ||
103 | |||
104 | ret += "Gac\" refto=\""; | ||
105 | try | ||
106 | { | ||
107 | //Assembly assem = Assembly.Load(refr.Name); | ||
108 | ret += refr.Name;// assem.FullName; | ||
109 | } | ||
110 | catch (System.NullReferenceException e) | ||
111 | { | ||
112 | e.ToString(); | ||
113 | ret += refr.Name; | ||
114 | } | ||
115 | ret += "\" localcopy=\"" + refr.LocalCopy.ToString() + "\" />"; | ||
116 | } | ||
117 | |||
118 | return ret; | ||
119 | } | ||
120 | |||
121 | private static string FindFileReference(string refName, ProjectNode project) | ||
122 | { | ||
123 | foreach(ReferencePathNode refPath in project.ReferencePaths) | ||
124 | { | ||
125 | string fullPath = Helper.MakeFilePath(refPath.Path, refName, "dll"); | ||
126 | |||
127 | if(File.Exists(fullPath)) | ||
128 | { | ||
129 | return fullPath; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | return null; | ||
134 | } | ||
135 | |||
136 | /// <summary> | ||
137 | /// Gets the XML doc file. | ||
138 | /// </summary> | ||
139 | /// <param name="project">The project.</param> | ||
140 | /// <param name="conf">The conf.</param> | ||
141 | /// <returns></returns> | ||
142 | public static string GenerateXmlDocFile(ProjectNode project, ConfigurationNode conf) | ||
143 | { | ||
144 | if( conf == null ) | ||
145 | { | ||
146 | throw new ArgumentNullException("conf"); | ||
147 | } | ||
148 | if( project == null ) | ||
149 | { | ||
150 | throw new ArgumentNullException("project"); | ||
151 | } | ||
152 | string docFile = (string)conf.Options["XmlDocFile"]; | ||
153 | if(docFile != null && docFile.Length == 0)//default to assembly name if not specified | ||
154 | { | ||
155 | return "False"; | ||
156 | } | ||
157 | return "True"; | ||
158 | } | ||
159 | |||
160 | private void WriteProject(SolutionNode solution, ProjectNode project) | ||
161 | { | ||
162 | string csComp = "Csc"; | ||
163 | string netRuntime = "MsNet"; | ||
164 | if(project.Runtime == ClrRuntime.Mono) | ||
165 | { | ||
166 | csComp = "Mcs"; | ||
167 | netRuntime = "Mono"; | ||
168 | } | ||
169 | |||
170 | string projFile = Helper.MakeFilePath(project.FullPath, project.Name, "prjx"); | ||
171 | StreamWriter ss = new StreamWriter(projFile); | ||
172 | |||
173 | m_Kernel.CurrentWorkingDirectory.Push(); | ||
174 | Helper.SetCurrentDir(Path.GetDirectoryName(projFile)); | ||
175 | |||
176 | using(ss) | ||
177 | { | ||
178 | ss.WriteLine( | ||
179 | "<Project name=\"{0}\" standardNamespace=\"{1}\" description=\"\" newfilesearch=\"None\" enableviewstate=\"True\" version=\"1.1\" projecttype=\"C#\">", | ||
180 | project.Name, | ||
181 | project.RootNamespace | ||
182 | ); | ||
183 | |||
184 | ss.WriteLine(" <Contents>"); | ||
185 | foreach(string file in project.Files) | ||
186 | { | ||
187 | string buildAction = "Compile"; | ||
188 | switch(project.Files.GetBuildAction(file)) | ||
189 | { | ||
190 | case BuildAction.None: | ||
191 | buildAction = "Nothing"; | ||
192 | break; | ||
193 | |||
194 | case BuildAction.Content: | ||
195 | buildAction = "Exclude"; | ||
196 | break; | ||
197 | |||
198 | case BuildAction.EmbeddedResource: | ||
199 | buildAction = "EmbedAsResource"; | ||
200 | break; | ||
201 | |||
202 | default: | ||
203 | buildAction = "Compile"; | ||
204 | break; | ||
205 | } | ||
206 | |||
207 | // Sort of a hack, we try and resolve the path and make it relative, if we can. | ||
208 | string filePath = PrependPath(file); | ||
209 | ss.WriteLine(" <File name=\"{0}\" subtype=\"Code\" buildaction=\"{1}\" dependson=\"\" data=\"\" />", filePath, buildAction); | ||
210 | } | ||
211 | ss.WriteLine(" </Contents>"); | ||
212 | |||
213 | ss.WriteLine(" <References>"); | ||
214 | foreach(ReferenceNode refr in project.References) | ||
215 | { | ||
216 | ss.WriteLine(" {0}", BuildReference(solution, refr)); | ||
217 | } | ||
218 | ss.WriteLine(" </References>"); | ||
219 | |||
220 | ss.Write(" <DeploymentInformation"); | ||
221 | ss.Write(" target=\"\""); | ||
222 | ss.Write(" script=\"\""); | ||
223 | ss.Write(" strategy=\"File\""); | ||
224 | ss.WriteLine(" />"); | ||
225 | |||
226 | int count = 0; | ||
227 | |||
228 | ss.WriteLine(" <Configurations active=\"{0}\">", solution.ActiveConfig); | ||
229 | |||
230 | foreach(ConfigurationNode conf in project.Configurations) | ||
231 | { | ||
232 | ss.Write(" <Configuration"); | ||
233 | ss.Write(" runwithwarnings=\"True\""); | ||
234 | ss.Write(" name=\"{0}\"", conf.Name); | ||
235 | ss.WriteLine(">"); | ||
236 | ss.Write(" <CodeGeneration"); | ||
237 | ss.Write(" runtime=\"{0}\"", netRuntime); | ||
238 | ss.Write(" compiler=\"{0}\"", csComp); | ||
239 | ss.Write(" compilerversion=\"\""); | ||
240 | ss.Write(" warninglevel=\"{0}\"", conf.Options["WarningLevel"]); | ||
241 | ss.Write(" nowarn=\"{0}\"", conf.Options["SuppressWarnings"]); | ||
242 | ss.Write(" includedebuginformation=\"{0}\"", conf.Options["DebugInformation"]); | ||
243 | ss.Write(" optimize=\"{0}\"", conf.Options["OptimizeCode"]); | ||
244 | ss.Write(" unsafecodeallowed=\"{0}\"", conf.Options["AllowUnsafe"]); | ||
245 | ss.Write(" generateoverflowchecks=\"{0}\"", conf.Options["CheckUnderflowOverflow"]); | ||
246 | ss.Write(" mainclass=\"{0}\"", project.StartupObject); | ||
247 | ss.Write(" target=\"{0}\"", project.Type); | ||
248 | ss.Write(" definesymbols=\"{0}\"", conf.Options["CompilerDefines"]); | ||
249 | ss.Write(" generatexmldocumentation=\"{0}\"", GenerateXmlDocFile(project, conf)); | ||
250 | ss.Write(" win32Icon=\"{0}\"", Helper.NormalizePath(".\\" + project.AppIcon)); | ||
251 | ss.Write(" noconfig=\"{0}\"", "False"); | ||
252 | ss.Write(" nostdlib=\"{0}\"", conf.Options["NoStdLib"]); | ||
253 | ss.WriteLine(" />"); | ||
254 | |||
255 | ss.Write(" <Execution"); | ||
256 | ss.Write(" commandlineparameters=\"\""); | ||
257 | ss.Write(" consolepause=\"True\""); | ||
258 | ss.WriteLine(" />"); | ||
259 | |||
260 | ss.Write(" <Output"); | ||
261 | ss.Write(" directory=\".\\{0}\"", Helper.NormalizePath(conf.Options["OutputPath"].ToString())); | ||
262 | ss.Write(" assembly=\"{0}\"", project.AssemblyName); | ||
263 | ss.Write(" executeScript=\"{0}\"", conf.Options["RunScript"]); | ||
264 | if (conf.Options["PreBuildEvent"] != null && conf.Options["PreBuildEvent"].ToString().Length != 0) | ||
265 | { | ||
266 | ss.Write(" executeBeforeBuild=\"{0}\"", Helper.NormalizePath(conf.Options["PreBuildEvent"].ToString())); | ||
267 | } | ||
268 | else | ||
269 | { | ||
270 | ss.Write(" executeBeforeBuild=\"{0}\"", conf.Options["PreBuildEvent"]); | ||
271 | } | ||
272 | if (conf.Options["PostBuildEvent"] != null && conf.Options["PostBuildEvent"].ToString().Length != 0) | ||
273 | { | ||
274 | ss.Write(" executeAfterBuild=\"{0}\"", Helper.NormalizePath(conf.Options["PostBuildEvent"].ToString())); | ||
275 | } | ||
276 | else | ||
277 | { | ||
278 | ss.Write(" executeAfterBuild=\"{0}\"", conf.Options["PostBuildEvent"]); | ||
279 | } | ||
280 | ss.Write(" executeBeforeBuildArguments=\"{0}\"", conf.Options["PreBuildEventArgs"]); | ||
281 | ss.Write(" executeAfterBuildArguments=\"{0}\"", conf.Options["PreBuildEventArgs"]); | ||
282 | ss.WriteLine(" />"); | ||
283 | ss.WriteLine(" </Configuration>"); | ||
284 | |||
285 | count++; | ||
286 | } | ||
287 | ss.WriteLine(" </Configurations>"); | ||
288 | ss.WriteLine("</Project>"); | ||
289 | } | ||
290 | |||
291 | m_Kernel.CurrentWorkingDirectory.Pop(); | ||
292 | } | ||
293 | |||
294 | private void WriteCombine(SolutionNode solution) | ||
295 | { | ||
296 | m_Kernel.Log.Write("Creating SharpDevelop combine and project files"); | ||
297 | foreach(ProjectNode project in solution.Projects) | ||
298 | { | ||
299 | if(m_Kernel.AllowProject(project.FilterGroups)) | ||
300 | { | ||
301 | m_Kernel.Log.Write("...Creating project: {0}", project.Name); | ||
302 | WriteProject(solution, project); | ||
303 | } | ||
304 | } | ||
305 | |||
306 | m_Kernel.Log.Write(""); | ||
307 | string combFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "cmbx"); | ||
308 | StreamWriter ss = new StreamWriter(combFile); | ||
309 | |||
310 | m_Kernel.CurrentWorkingDirectory.Push(); | ||
311 | Helper.SetCurrentDir(Path.GetDirectoryName(combFile)); | ||
312 | |||
313 | using(ss) | ||
314 | { | ||
315 | ss.WriteLine("<Combine fileversion=\"1.0\" name=\"{0}\" description=\"\">", solution.Name); | ||
316 | |||
317 | int count = 0; | ||
318 | foreach(ProjectNode project in solution.Projects) | ||
319 | { | ||
320 | if(count == 0) | ||
321 | ss.WriteLine(" <StartMode startupentry=\"{0}\" single=\"True\">", project.Name); | ||
322 | |||
323 | ss.WriteLine(" <Execute entry=\"{0}\" type=\"None\" />", project.Name); | ||
324 | count++; | ||
325 | } | ||
326 | ss.WriteLine(" </StartMode>"); | ||
327 | |||
328 | ss.WriteLine(" <Entries>"); | ||
329 | foreach(ProjectNode project in solution.Projects) | ||
330 | { | ||
331 | string path = Helper.MakePathRelativeTo(solution.FullPath, project.FullPath); | ||
332 | ss.WriteLine(" <Entry filename=\"{0}\" />", | ||
333 | Helper.MakeFilePath(path, project.Name, "prjx")); | ||
334 | } | ||
335 | ss.WriteLine(" </Entries>"); | ||
336 | |||
337 | count = 0; | ||
338 | foreach(ConfigurationNode conf in solution.Configurations) | ||
339 | { | ||
340 | if(count == 0) | ||
341 | { | ||
342 | ss.WriteLine(" <Configurations active=\"{0}\">", conf.Name); | ||
343 | } | ||
344 | |||
345 | ss.WriteLine(" <Configuration name=\"{0}\">", conf.Name); | ||
346 | foreach(ProjectNode project in solution.Projects) | ||
347 | { | ||
348 | ss.WriteLine(" <Entry name=\"{0}\" configurationname=\"{1}\" build=\"True\" />", project.Name, conf.Name); | ||
349 | } | ||
350 | ss.WriteLine(" </Configuration>"); | ||
351 | |||
352 | count++; | ||
353 | } | ||
354 | ss.WriteLine(" </Configurations>"); | ||
355 | ss.WriteLine("</Combine>"); | ||
356 | } | ||
357 | |||
358 | m_Kernel.CurrentWorkingDirectory.Pop(); | ||
359 | } | ||
360 | |||
361 | private void CleanProject(ProjectNode project) | ||
362 | { | ||
363 | m_Kernel.Log.Write("...Cleaning project: {0}", project.Name); | ||
364 | string projectFile = Helper.MakeFilePath(project.FullPath, project.Name, "prjx"); | ||
365 | Helper.DeleteIfExists(projectFile); | ||
366 | } | ||
367 | |||
368 | private void CleanSolution(SolutionNode solution) | ||
369 | { | ||
370 | m_Kernel.Log.Write("Cleaning SharpDevelop combine and project files for", solution.Name); | ||
371 | |||
372 | string slnFile = Helper.MakeFilePath(solution.FullPath, solution.Name, "cmbx"); | ||
373 | Helper.DeleteIfExists(slnFile); | ||
374 | |||
375 | foreach(ProjectNode project in solution.Projects) | ||
376 | { | ||
377 | CleanProject(project); | ||
378 | } | ||
379 | |||
380 | m_Kernel.Log.Write(""); | ||
381 | } | ||
382 | |||
383 | #endregion | ||
384 | |||
385 | #region ITarget Members | ||
386 | |||
387 | /// <summary> | ||
388 | /// Writes the specified kern. | ||
389 | /// </summary> | ||
390 | /// <param name="kern">The kern.</param> | ||
391 | public void Write(Kernel kern) | ||
392 | { | ||
393 | if( kern == null ) | ||
394 | { | ||
395 | throw new ArgumentNullException("kern"); | ||
396 | } | ||
397 | m_Kernel = kern; | ||
398 | foreach(SolutionNode solution in kern.Solutions) | ||
399 | { | ||
400 | WriteCombine(solution); | ||
401 | } | ||
402 | m_Kernel = null; | ||
403 | } | ||
404 | |||
405 | /// <summary> | ||
406 | /// Cleans the specified kern. | ||
407 | /// </summary> | ||
408 | /// <param name="kern">The kern.</param> | ||
409 | public virtual void Clean(Kernel kern) | ||
410 | { | ||
411 | if( kern == null ) | ||
412 | { | ||
413 | throw new ArgumentNullException("kern"); | ||
414 | } | ||
415 | m_Kernel = kern; | ||
416 | foreach(SolutionNode sol in kern.Solutions) | ||
417 | { | ||
418 | CleanSolution(sol); | ||
419 | } | ||
420 | m_Kernel = null; | ||
421 | } | ||
422 | |||
423 | /// <summary> | ||
424 | /// Gets the name. | ||
425 | /// </summary> | ||
426 | /// <value>The name.</value> | ||
427 | public string Name | ||
428 | { | ||
429 | get | ||
430 | { | ||
431 | return "sharpdev"; | ||
432 | } | ||
433 | } | ||
434 | |||
435 | #endregion | ||
436 | } | ||
437 | } | ||