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;
46 ITaskItem[] references;
49 string stronglyTypedClassName;
50 string stronglyTypedFilename;
51 string stronglyTypedLanguage;
52 string stronglyTypedNamespace;
55 public GenerateResource ()
57 useSourcePath = false;
60 public override bool Execute ()
62 ArrayList temporaryFilesWritten = new ArrayList ();
63 if (sources.Length != outputResources.Length) {
64 Log.LogErrorFromException (new Exception ("Sources count is different than OutputResources count."));
68 if (outputResources == null) {
69 foreach (ITaskItem source in sources) {
70 string sourceFile = source.ItemSpec;
71 string outputFile = Path.ChangeExtension (sourceFile, "resources");
72 CompileResourceFile (sourceFile, outputFile);
75 IEnumerator sourceEnum, outputEnum;
76 sourceEnum = sources.GetEnumerator ();
77 outputEnum = outputResources.GetEnumerator ();
78 while (sourceEnum.MoveNext ()) {
79 outputEnum.MoveNext ();
80 string sourceFile = ((ITaskItem) sourceEnum.Current).ItemSpec;
81 string outputFile = ((ITaskItem) outputEnum.Current).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."));
89 temporaryFilesWritten.Add (outputEnum.Current);
93 filesWritten = new ITaskItem [temporaryFilesWritten.Count];
95 foreach (ITaskItem item in temporaryFilesWritten)
96 filesWritten [i++] = item;
101 private IResourceReader GetReader (Stream stream, string name)
103 string format = Path.GetExtension (name);
104 switch (format.ToLower ()) {
106 return new PoResourceReader (stream);
109 return new TxtResourceReader (stream);
111 return new ResourceReader (stream);
113 return new System.Resources.ResXResourceReader (stream);
115 throw new Exception ("Unknown format in file " + name);
119 private IResourceWriter GetWriter (Stream stream, string name)
121 string format = Path.GetExtension (name);
122 switch (format.ToLower ()) {
124 return new PoResourceWriter (stream);
127 return new TxtResourceWriter (stream);
129 return new ResourceWriter (stream);
131 return new System.Resources.ResXResourceWriter (stream);
133 throw new Exception ("Unknown format in file " + name);
137 private bool CompileResourceFile (string sname, string dname )
139 FileStream source, dest;
140 IResourceReader reader;
141 IResourceWriter writer;
144 source = new FileStream (sname, FileMode.Open, FileAccess.Read);
146 reader = GetReader (source, sname);
148 dest = new FileStream (dname, FileMode.Create, FileAccess.Write);
149 writer = GetWriter (dest, dname);
152 foreach (DictionaryEntry e in reader) {
154 object val = e.Value;
156 writer.AddResource ((string)e.Key, (string)e.Value);
158 writer.AddResource ((string)e.Key, e.Value);
163 } catch (Exception e) {
164 Log.LogErrorFromException (e);
171 public ITaskItem[] FilesWritten {
176 filesWritten = value;
180 /*public bool NeverLockTypeAssemblies {
182 return neverLockTypeAssemblies;
185 neverLockTypeAssemblies = value;
190 public ITaskItem[] OutputResources {
192 return outputResources;
195 outputResources = value;
199 public ITaskItem[] References {
209 public ITaskItem[] Sources {
218 public ITaskItem StateFile {
227 public string StronglyTypedClassName {
229 return stronglyTypedClassName;
232 stronglyTypedClassName = value;
236 public string StronglyTypedFilename {
238 return stronglyTypedFilename;
241 stronglyTypedFilename = value;
245 public string StronglyTypedLanguage {
247 return stronglyTypedLanguage;
250 stronglyTypedLanguage = value;
254 public string StronglyTypedNamespace {
256 return stronglyTypedNamespace;
259 stronglyTypedNamespace = value;
263 public bool UseSourcePath {
265 return useSourcePath;
268 useSourcePath = value;