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 Mono.XBuild.Tasks.GenerateResourceInternal;
42 namespace Microsoft.Build.Tasks {
43 public sealed class GenerateResource : TaskExtension {
45 ITaskItem[] filesWritten;
46 bool neverLockTypeAssemblies;
47 ITaskItem[] outputResources;
49 ITaskItem[] references;
52 string stronglyTypedClassName;
53 string stronglyTypedFilename;
54 string stronglyTypedLanguage;
55 string stronglyTypedNamespace;
58 public GenerateResource ()
60 useSourcePath = false;
63 public override bool Execute ()
65 List <ITaskItem> temporaryFilesWritten = new List <ITaskItem> ();
66 if (sources.Length != outputResources.Length) {
67 Log.LogErrorFromException (new Exception ("Sources count is different than OutputResources count."));
71 if (outputResources == null) {
72 foreach (ITaskItem source in sources) {
73 string sourceFile = source.ItemSpec;
74 string outputFile = Path.ChangeExtension (sourceFile, "resources");
75 CompileResourceFile (sourceFile, outputFile);
78 for (int i = 0; i < sources.Length; i ++) {
79 string sourceFile = sources [i].ItemSpec;
80 string outputFile = outputResources [i].ItemSpec;
82 if (outputFile == String.Empty) {
83 Log.LogErrorFromException (new Exception ("Filename of output can not be empty."));
86 if (CompileResourceFile (sourceFile, outputFile) == false) {
87 Log.LogErrorFromException (new Exception ("Error during compiling resource file."));
90 temporaryFilesWritten.Add (outputResources [i]);
94 filesWritten = temporaryFilesWritten.ToArray ();
99 private IResourceReader GetReader (Stream stream, string name)
101 string format = Path.GetExtension (name);
102 switch (format.ToLower ()) {
104 return new PoResourceReader (stream);
107 return new TxtResourceReader (stream);
109 return new ResourceReader (stream);
111 ResXResourceReader reader = new ResXResourceReader (stream);
113 // set correct basepath to resolve relative paths in file refs
115 reader.BasePath = Path.GetDirectoryName (Path.GetFullPath (name));
119 throw new Exception ("Unknown format in file " + name);
123 private IResourceWriter GetWriter (Stream stream, string name)
125 string format = Path.GetExtension (name);
126 switch (format.ToLower ()) {
128 return new PoResourceWriter (stream);
131 return new TxtResourceWriter (stream);
133 return new ResourceWriter (stream);
135 return new System.Resources.ResXResourceWriter (stream);
137 throw new Exception ("Unknown format in file " + name);
141 private bool CompileResourceFile (string sname, string dname )
143 FileStream source, dest;
144 IResourceReader reader;
145 IResourceWriter writer;
147 Log.LogMessage ("Compiling resource file '{0}' into '{1}'", sname, dname);
149 source = new FileStream (sname, FileMode.Open, FileAccess.Read);
151 reader = GetReader (source, sname);
153 dest = new FileStream (dname, FileMode.Create, FileAccess.Write);
154 writer = GetWriter (dest, dname);
157 foreach (DictionaryEntry e in reader) {
159 object val = e.Value;
161 writer.AddResource ((string)e.Key, (string)e.Value);
163 writer.AddResource ((string)e.Key, e.Value);
168 } catch (Exception e) {
169 Log.LogErrorFromException (e);
176 public ITaskItem[] FilesWritten {
183 public bool NeverLockTypeAssemblies {
185 return neverLockTypeAssemblies;
188 neverLockTypeAssemblies = value;
193 public ITaskItem[] OutputResources {
195 return outputResources;
198 outputResources = value;
202 public bool PublicClass {
203 get { return publicClass; }
204 set { publicClass = value; }
207 public ITaskItem[] References {
217 public ITaskItem[] Sources {
226 public ITaskItem StateFile {
236 public string StronglyTypedClassName {
238 return stronglyTypedClassName;
241 stronglyTypedClassName = value;
246 public string StronglyTypedFileName {
248 return stronglyTypedFilename;
251 stronglyTypedFilename = value;
255 public string StronglyTypedLanguage {
257 return stronglyTypedLanguage;
260 stronglyTypedLanguage = value;
264 public string StronglyTypedNamespace {
266 return stronglyTypedNamespace;
269 stronglyTypedNamespace = value;
273 public bool UseSourcePath {
275 return useSourcePath;
278 useSourcePath = value;