2 // GenerateResource.cs: Task that generates the resources.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Paolo Molaro (lupus@ximian.com)
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (C) 2005 Marek Sieradzki
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Resources;
38 using System.Reflection;
39 using Microsoft.Build.Framework;
40 using Microsoft.Build.Utilities;
41 using Mono.XBuild.Tasks.GenerateResourceInternal;
43 namespace Microsoft.Build.Tasks {
44 public sealed class GenerateResource : TaskExtension {
46 ITaskItem[] filesWritten;
47 bool neverLockTypeAssemblies;
48 ITaskItem[] outputResources;
50 ITaskItem[] references;
53 string stronglyTypedClassName;
54 string stronglyTypedFilename;
55 string stronglyTypedLanguage;
56 string stronglyTypedNamespace;
59 public GenerateResource ()
61 useSourcePath = false;
64 public override bool Execute ()
66 if (sources.Length == 0)
70 List <ITaskItem> temporaryFilesWritten = new List <ITaskItem> ();
71 if (outputResources == null) {
72 foreach (ITaskItem source in sources) {
73 string sourceFile = source.ItemSpec;
74 string outputFile = Path.ChangeExtension (sourceFile, "resources");
76 result &= CompileResourceFile (sourceFile, outputFile);
78 ITaskItem newItem = new TaskItem (source);
79 source.ItemSpec = outputFile;
81 temporaryFilesWritten.Add (newItem);
84 if (sources.Length != outputResources.Length) {
85 Log.LogError ("Sources count is different than OutputResources count.");
89 for (int i = 0; i < sources.Length; i ++) {
90 if (String.IsNullOrEmpty (outputResources [i].ItemSpec)) {
91 Log.LogError ("Filename of output can not be empty.");
96 result &= CompileResourceFile (sources [i].ItemSpec, outputResources [i].ItemSpec);
97 temporaryFilesWritten.Add (outputResources [i]);
101 filesWritten = temporaryFilesWritten.ToArray ();
107 private IResourceReader GetReader (Stream stream, string name)
109 string format = Path.GetExtension (name);
110 switch (format.ToLower ()) {
112 return new PoResourceReader (stream);
115 return new TxtResourceReader (stream);
117 return new ResourceReader (stream);
119 ResXResourceReader reader = new ResXResourceReader (stream);
121 // set correct basepath to resolve relative paths in file refs
123 reader.BasePath = Path.GetDirectoryName (Path.GetFullPath (name));
127 throw new Exception ("Unknown format in file " + name);
131 private IResourceWriter GetWriter (Stream stream, string name)
133 string format = Path.GetExtension (name);
134 switch (format.ToLower ()) {
136 return new PoResourceWriter (stream);
139 return new TxtResourceWriter (stream);
141 return new ResourceWriter (stream);
143 return new System.Resources.ResXResourceWriter (stream);
145 throw new Exception ("Unknown format in file " + name);
150 private bool CompileResourceFile (string sname, string dname )
152 if (!File.Exists (sname)) {
153 Log.LogError ("Resource file '{0}' not found.", sname);
157 if (File.GetLastWriteTime (sname) <= File.GetLastWriteTime (dname)) {
158 Log.LogMessage (MessageImportance.Low,
159 "Resource file '{0}' is newer than the source file '{1}', skipping.",
164 Resgen resgen = new Resgen ();
165 resgen.BuildEngine = this.BuildEngine;
166 resgen.UseSourcePath = true;
168 resgen.SourceFile = sname;
169 resgen.OutputFile = dname;
171 return resgen.Execute ();
175 public ITaskItem[] FilesWritten {
182 public bool NeverLockTypeAssemblies {
184 return neverLockTypeAssemblies;
187 neverLockTypeAssemblies = value;
192 public ITaskItem[] OutputResources {
194 return outputResources;
197 outputResources = value;
201 public bool PublicClass {
202 get { return publicClass; }
203 set { publicClass = value; }
206 public ITaskItem[] References {
216 public ITaskItem[] Sources {
225 public ITaskItem StateFile {
235 public string StronglyTypedClassName {
237 return stronglyTypedClassName;
240 stronglyTypedClassName = value;
245 public string StronglyTypedFileName {
247 return stronglyTypedFilename;
250 stronglyTypedFilename = value;
254 public string StronglyTypedLanguage {
256 return stronglyTypedLanguage;
259 stronglyTypedLanguage = value;
263 public string StronglyTypedNamespace {
265 return stronglyTypedNamespace;
268 stronglyTypedNamespace = value;
272 public bool UseSourcePath {
274 return useSourcePath;
277 useSourcePath = value;
282 class Resgen : ToolTaskExtension
288 protected internal override void AddCommandLineCommands (
289 CommandLineBuilderExtension commandLine)
292 commandLine.AppendSwitch ("/useSourcePath");
294 commandLine.AppendSwitch (String.Format ("/compile {0}{1}", SourceFile,
295 OutputFile != null ? "," + OutputFile : ""));
298 public override bool Execute ()
300 EnvironmentOverride ["MONO_IOMAP"] = "drive";
301 return base.Execute ();
304 protected override string GenerateFullPathToTool ()
306 return Path.Combine (ToolPath, ToolExe);
309 protected override MessageImportance StandardOutputLoggingImportance {
310 get { return MessageImportance.Low; }
313 protected override string ToolName {
314 get { return Utilities.RunningOnWindows ? "resgen2.bat" : "resgen2"; }
317 public string SourceFile { get; set; }
318 public string OutputFile { get; set; }
320 public bool UseSourcePath { get; set; }