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 IEnumerator <ITaskItem> sourceEnum, outputEnum;
79 sourceEnum = (IEnumerator <ITaskItem>) sources.GetEnumerator ();
80 outputEnum = (IEnumerator <ITaskItem>) outputResources.GetEnumerator ();
81 while (sourceEnum.MoveNext ()) {
82 outputEnum.MoveNext ();
83 string sourceFile = sourceEnum.Current.ItemSpec;
84 string outputFile = outputEnum.Current.ItemSpec;
85 if (outputFile == String.Empty) {
86 Log.LogErrorFromException (new Exception ("Filename of output can not be empty."));
89 if (CompileResourceFile (sourceFile, outputFile) == false) {
90 Log.LogErrorFromException (new Exception ("Error during compiling resource file."));
93 temporaryFilesWritten.Add (outputEnum.Current);
97 filesWritten = temporaryFilesWritten.ToArray ();
102 private IResourceReader GetReader (Stream stream, string name)
104 string format = Path.GetExtension (name);
105 switch (format.ToLower ()) {
107 return new PoResourceReader (stream);
110 return new TxtResourceReader (stream);
112 return new ResourceReader (stream);
114 return new System.Resources.ResXResourceReader (stream);
116 throw new Exception ("Unknown format in file " + name);
120 private IResourceWriter GetWriter (Stream stream, string name)
122 string format = Path.GetExtension (name);
123 switch (format.ToLower ()) {
125 return new PoResourceWriter (stream);
128 return new TxtResourceWriter (stream);
130 return new ResourceWriter (stream);
132 return new System.Resources.ResXResourceWriter (stream);
134 throw new Exception ("Unknown format in file " + name);
138 private bool CompileResourceFile (string sname, string dname )
140 FileStream source, dest;
141 IResourceReader reader;
142 IResourceWriter writer;
145 source = new FileStream (sname, FileMode.Open, FileAccess.Read);
147 reader = GetReader (source, sname);
149 dest = new FileStream (dname, FileMode.Create, FileAccess.Write);
150 writer = GetWriter (dest, dname);
153 foreach (DictionaryEntry e in reader) {
155 object val = e.Value;
157 writer.AddResource ((string)e.Key, (string)e.Value);
159 writer.AddResource ((string)e.Key, e.Value);
164 } catch (Exception e) {
165 Log.LogErrorFromException (e);
172 public ITaskItem[] FilesWritten {
179 public bool NeverLockTypeAssemblies {
181 return neverLockTypeAssemblies;
184 neverLockTypeAssemblies = value;
189 public ITaskItem[] OutputResources {
191 return outputResources;
194 outputResources = value;
198 public bool PublicClass {
199 get { return publicClass; }
200 set { publicClass = value; }
203 public ITaskItem[] References {
213 public ITaskItem[] Sources {
222 public ITaskItem StateFile {
232 public string StronglyTypedClassName {
234 return stronglyTypedClassName;
237 stronglyTypedClassName = value;
242 public string StronglyTypedFileName {
244 return stronglyTypedFilename;
247 stronglyTypedFilename = value;
251 public string StronglyTypedLanguage {
253 return stronglyTypedLanguage;
256 stronglyTypedLanguage = value;
260 public string StronglyTypedNamespace {
262 return stronglyTypedNamespace;
265 stronglyTypedNamespace = value;
269 public bool UseSourcePath {
271 return useSourcePath;
274 useSourcePath = value;