2 // GenerateResource.cs: Task that generates the resources.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // FIXME: code from monoresgen, add authors
8 // (C) 2005 Marek Sieradzki
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Resources;
36 using System.Reflection;
37 using Microsoft.Build.Framework;
38 using Mono.XBuild.Tasks.GenerateResourceInternal;
40 namespace Microsoft.Build.Tasks {
41 public sealed class GenerateResource : TaskExtension {
43 ITaskItem[] filesWritten;
44 bool neverLockTypeAssemblies;
45 ITaskItem[] outputResources;
47 ITaskItem[] references;
50 string stronglyTypedClassName;
51 string stronglyTypedFilename;
52 string stronglyTypedLanguage;
53 string stronglyTypedNamespace;
56 public GenerateResource ()
58 useSourcePath = false;
61 public override bool Execute ()
63 ArrayList temporaryFilesWritten = new ArrayList ();
64 if (sources.Length != outputResources.Length) {
65 Log.LogErrorFromException (new Exception ("Sources count is different than OutputResources count."));
69 if (outputResources == null) {
70 foreach (ITaskItem source in sources) {
71 string sourceFile = source.ItemSpec;
72 string outputFile = Path.ChangeExtension (sourceFile, "resources");
73 CompileResourceFile (sourceFile, outputFile);
76 IEnumerator sourceEnum, outputEnum;
77 sourceEnum = sources.GetEnumerator ();
78 outputEnum = outputResources.GetEnumerator ();
79 while (sourceEnum.MoveNext ()) {
80 outputEnum.MoveNext ();
81 string sourceFile = ((ITaskItem) sourceEnum.Current).ItemSpec;
82 string outputFile = ((ITaskItem) outputEnum.Current).ItemSpec;
83 if (outputFile == String.Empty) {
84 Log.LogErrorFromException (new Exception ("Filename of output can not be empty."));
87 if (CompileResourceFile (sourceFile, outputFile) == false)
88 Log.LogErrorFromException (new Exception ("Error during compiling resource file."));
90 temporaryFilesWritten.Add (outputEnum.Current);
94 filesWritten = new ITaskItem [temporaryFilesWritten.Count];
96 foreach (ITaskItem item in temporaryFilesWritten)
97 filesWritten [i++] = item;
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;