merge -r 53370:58178
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / GenerateResource.cs
1 //
2 // GenerateResource.cs: Task that generates the resources.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // FIXME: code from monoresgen, add authors
7 //
8 // (C) 2005 Marek Sieradzki
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
28
29 #if NET_2_0
30
31 using System;
32 using System.Text;
33 using System.IO;
34 using System.Collections;
35 using System.Resources;
36 using System.Reflection;
37 using Microsoft.Build.Framework;
38 using Mono.XBuild.Tasks.GenerateResourceInternal;
39
40 namespace Microsoft.Build.Tasks {
41         public sealed class GenerateResource : TaskExtension {
42         
43                 ITaskItem[]     filesWritten;
44                 //bool          neverLockTypeAssemblies;
45                 ITaskItem[]     outputResources;
46                 ITaskItem[]     references;
47                 ITaskItem[]     sources;
48                 ITaskItem       stateFile;
49                 string          stronglyTypedClassName;
50                 string          stronglyTypedFilename;
51                 string          stronglyTypedLanguage;
52                 string          stronglyTypedNamespace;
53                 bool            useSourcePath;
54                 
55                 public GenerateResource ()
56                 {
57                         useSourcePath = false;
58                 }
59
60                 public override bool Execute ()
61                 {
62                         ArrayList temporaryFilesWritten = new ArrayList ();
63                         if (sources.Length != outputResources.Length) {
64                                 Log.LogErrorFromException (new Exception ("Sources count is different than OutputResources count."));
65                                 return false;
66                         }
67                         
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);
73                                 }
74                         } else {
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."));
84                                                 return false;
85                                         }
86                                         if (CompileResourceFile (sourceFile, outputFile) == false)
87                                                 Log.LogErrorFromException (new Exception ("Error during compiling resource file."));
88                                                 return false;
89                                         temporaryFilesWritten.Add (outputEnum.Current);
90                                 }
91                         }
92                         
93                         filesWritten = new ITaskItem [temporaryFilesWritten.Count];
94                         int i = 0;
95                         foreach (ITaskItem item in temporaryFilesWritten)
96                                 filesWritten [i++] = item;
97                         
98                         return true;
99                 }
100                 
101                 private IResourceReader GetReader (Stream stream, string name)
102                 {
103                         string format = Path.GetExtension (name);
104                         switch (format.ToLower ()) {
105                         case ".po":
106                                 return new PoResourceReader (stream);
107                         case ".txt":
108                         case ".text":
109                                 return new TxtResourceReader (stream);
110                         case ".resources":
111                                 return new ResourceReader (stream);
112                         case ".resx":
113                                 return new System.Resources.ResXResourceReader (stream);
114                         default:
115                                 throw new Exception ("Unknown format in file " + name);
116                         }
117                 }
118                 
119                 private IResourceWriter GetWriter (Stream stream, string name)
120                 {
121                         string format = Path.GetExtension (name);
122                         switch (format.ToLower ()) {
123                         case ".po":
124                                 return new PoResourceWriter (stream);
125                         case ".txt":
126                         case ".text":
127                                 return new TxtResourceWriter (stream);
128                         case ".resources":
129                                 return new ResourceWriter (stream);
130                         case ".resx":
131                                 return new System.Resources.ResXResourceWriter (stream);
132                         default:
133                                 throw new Exception ("Unknown format in file " + name);
134                         }
135                 }
136                 
137                 private bool CompileResourceFile (string sname, string dname )
138                 {
139                         FileStream source, dest;
140                         IResourceReader reader;
141                         IResourceWriter writer;
142
143                         try {
144                                 source = new FileStream (sname, FileMode.Open, FileAccess.Read);
145
146                                 reader = GetReader (source, sname);
147
148                                 dest = new FileStream (dname, FileMode.Create, FileAccess.Write);
149                                 writer = GetWriter (dest, dname);
150
151                                 int rescount = 0;
152                                 foreach (DictionaryEntry e in reader) {
153                                         rescount++;
154                                         object val = e.Value;
155                                         if (val is string)
156                                                 writer.AddResource ((string)e.Key, (string)e.Value);
157                                         else
158                                                 writer.AddResource ((string)e.Key, e.Value);
159                                 }
160
161                                 reader.Close ();
162                                 writer.Close ();
163                         } catch (Exception e) {
164                                 Log.LogErrorFromException (e);
165                                 return false;
166                         }
167                         return true;
168                 }
169
170                 [Output]
171                 public ITaskItem[] FilesWritten {
172                         get {
173                                 return filesWritten;
174                         }
175                         set {
176                                 filesWritten = value;
177                         }
178                 }
179
180                 /*public bool NeverLockTypeAssemblies {
181                         get {
182                                 return neverLockTypeAssemblies;
183                         }
184                         set {
185                                 neverLockTypeAssemblies = value;
186                         }
187                 }*/
188
189                 [Output]
190                 public ITaskItem[] OutputResources {
191                         get {
192                                 return outputResources;
193                         }
194                         set {
195                                 outputResources = value;
196                         }
197                 }
198
199                 public ITaskItem[] References {
200                         get {
201                                 return references;
202                         }
203                         set {
204                                 references = value;
205                         }
206                 }
207
208                 [Required]
209                 public ITaskItem[] Sources {
210                         get {
211                                 return sources;
212                         }
213                         set {
214                                 sources = value;
215                         }
216                 }
217
218                 public ITaskItem StateFile {
219                         get {
220                                 return stateFile;
221                         }
222                         set {
223                                 stateFile = value;
224                         }
225                 }
226
227                 public string StronglyTypedClassName {
228                         get {
229                                 return stronglyTypedClassName;
230                         }
231                         set {
232                                 stronglyTypedClassName = value;
233                         }
234                 }
235
236                 public string StronglyTypedFilename {
237                         get {
238                                 return stronglyTypedFilename;
239                         }
240                         set {
241                                 stronglyTypedFilename = value;
242                         }
243                 }
244
245                 public string StronglyTypedLanguage {
246                         get {
247                                 return stronglyTypedLanguage;
248                         }
249                         set {
250                                 stronglyTypedLanguage = value;
251                         }
252                 }
253
254                 public string StronglyTypedNamespace {
255                         get {
256                                 return stronglyTypedNamespace;
257                         }
258                         set {
259                                 stronglyTypedNamespace = value;
260                         }
261                 }
262
263                 public bool UseSourcePath {
264                         get {
265                                 return useSourcePath;
266                         }
267                         set {
268                                 useSourcePath = value;
269                         }
270                 }
271         }
272 }
273
274 #endif