In .:
[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                 bool            publicClass;
47                 ITaskItem[]     references;
48                 ITaskItem[]     sources;
49                 ITaskItem       stateFile;
50                 string          stronglyTypedClassName;
51                 string          stronglyTypedFilename;
52                 string          stronglyTypedLanguage;
53                 string          stronglyTypedNamespace;
54                 bool            useSourcePath;
55                 
56                 public GenerateResource ()
57                 {
58                         useSourcePath = false;
59                 }
60
61                 public override bool Execute ()
62                 {
63                         ArrayList temporaryFilesWritten = new ArrayList ();
64                         if (sources.Length != outputResources.Length) {
65                                 Log.LogErrorFromException (new Exception ("Sources count is different than OutputResources count."));
66                                 return false;
67                         }
68                         
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);
74                                 }
75                         } else {
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."));
85                                                 return false;
86                                         }
87                                         if (CompileResourceFile (sourceFile, outputFile) == false)
88                                                 Log.LogErrorFromException (new Exception ("Error during compiling resource file."));
89                                                 return false;
90                                         temporaryFilesWritten.Add (outputEnum.Current);
91                                 }
92                         }
93                         
94                         filesWritten = new ITaskItem [temporaryFilesWritten.Count];
95                         int i = 0;
96                         foreach (ITaskItem item in temporaryFilesWritten)
97                                 filesWritten [i++] = item;
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