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