* CallTarget.cs: Copy all items from target to TargetOutputs.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / CreateCSharpManifestResourceName.cs
1 //
2 // CreateCSharpManifestResourceName.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.Tasks {
36         public class CreateCSharpManifestResourceName : CreateManifestResourceName {
37                 
38                 public CreateCSharpManifestResourceName ()
39                 {
40                 }
41
42                 protected override bool IsSourceFile (string fileName)
43                 {
44                         return Path.GetExtension (fileName).ToLower () == ".cs";
45                 }
46                 
47                 protected override string CreateManifestName (string fileName,
48                                                               string linkFileName,
49                                                               string rootNamespace,
50                                                               string dependentUponFileName,
51                                                               Stream binaryStream)
52                 {
53                         if (String.IsNullOrEmpty (dependentUponFileName) || binaryStream == null)
54                                 return GetResourceIdFromFileName (fileName, rootNamespace);
55
56                         string ns = null;
57                         string classname = null;
58
59                         using (StreamReader rdr = new StreamReader (binaryStream)) {
60                                 int numopen = 0;
61                                 while (true) {
62                                         string tok = GetNextToken (rdr);
63                                         if (tok == null)
64                                                 break;
65
66                                         if (tok == "@") {
67                                                 //Handle @namespace, @class
68                                                 GetNextToken (rdr);
69                                                 continue;
70                                         }
71
72                                         if (String.Compare (tok, "namespace", false) == 0)
73                                                 ns = GetNextToken (rdr);
74
75                                         if (tok == "{")
76                                                 numopen ++;
77
78                                         if (tok == "}") {
79                                                 numopen --;
80                                                 if (numopen == 0)
81                                                         ns = String.Empty;
82                                         }
83
84                                         if (tok == "class") {
85                                                 classname = GetNextToken (rdr);
86                                                 break;
87                                         }
88                                 }
89
90                                 if (classname == null)
91                                         return GetResourceIdFromFileName (fileName, rootNamespace);
92
93                                 string culture, extn, only_filename;
94                                 if (AssignCulture.TrySplitResourceName (fileName, out only_filename, out culture, out extn))
95                                         extn = "." + culture;
96                                 else
97                                         extn = String.Empty;
98
99                                 if (ns == null)
100                                         return classname + extn;
101                                 else
102                                         return ns + '.' + classname + extn;
103                         }
104                 }
105
106                 // No dependent file
107                 static string GetResourceIdFromFileName (string fileName, string rootNamespace)
108                 {
109                         string culture = null;
110                         if (String.Compare (Path.GetExtension (fileName), ".resx", true) == 0) {
111                                 fileName = Path.ChangeExtension (fileName, null);
112                         } else {
113                                 string only_filename, extn;
114                                 if (AssignCulture.TrySplitResourceName (fileName, out only_filename, out culture, out extn)) {
115                                         //remove the culture from fileName
116                                         //foo.it.bmp -> foo.bmp
117                                         fileName = only_filename + "." + extn;
118                                 }
119                         }
120
121                         //FIXME: path char!
122                         string rname = fileName.Replace ('/', '.').Replace ('\\', '.');
123
124                         if (!String.IsNullOrEmpty (rootNamespace))
125                                 rname = rootNamespace + "." + rname;
126                         if (culture == null)
127                                 return rname;
128                         else
129                                 //FIXME: Why??!! Tests show that this is required!
130                                 return culture + "\\" + rname;
131                 }
132
133                 /* Special parser for C# files
134                  * Assumes that the file is compilable
135                  * skips comments,
136                  * skips strings "foo",
137                  * skips anything after a # , eg. #region, #if
138                  * Won't handle #if false etc kinda blocks*/
139                 static string GetNextToken (StreamReader sr)
140                 {
141                         StringBuilder sb = new StringBuilder ();
142
143                         while (true) {
144                                 int c = sr.Peek ();
145                                 if (c == -1)
146                                         return null;
147
148                                 if (c == '\r' || c == '\n') {
149                                         sr.ReadLine ();
150                                         if (sb.Length > 0)
151                                                 break;
152
153                                         continue;
154                                 }
155
156                                 if (c == '/') {
157                                         sr.Read ();
158
159                                         if (sr.Peek () == '*') {
160                                                 /* multi-line comment */
161                                                 sr.Read ();
162
163                                                 while (true) {
164                                                         int n = sr.Read ();
165                                                         if (n == -1)
166                                                                 break;
167                                                         if (n != '*')
168                                                                 continue;
169
170                                                         if (sr.Peek () == '/') {
171                                                                 /* End of multi-line comment */
172                                                                 if (sb.Length > 0) {
173                                                                         sr.Read ();
174                                                                         return sb.ToString ();
175                                                                 }
176                                                                 break;
177                                                         }
178                                                 }
179                                         } else if (sr.Peek () == '/') {
180                                                 //Single line comment, skip the rest of the line
181                                                 sr.ReadLine ();
182                                                 continue;
183                                         }
184                                 } else if (c == '"') {
185                                         /* String "foo" */
186                                         sr.Read ();
187                                         while (true) {
188                                                 int n = sr.Peek ();
189                                                 if (n == '\r' || n == '\n' || n == -1)
190                                                         throw new Exception ("String literal not closed");
191
192                                                 if (n == '"') {
193                                                         /* end of string */
194                                                         if (sb.Length > 0) {
195                                                                 sr.Read ();
196                                                                 return sb.ToString ();
197                                                         }
198
199                                                         break;
200                                                 }
201                                                 sr.Read ();
202                                         }
203                                 } else if (c == '#') {
204                                         //skip rest of the line
205                                         sr.ReadLine ();
206                                 } else {
207                                         if (Char.IsLetterOrDigit ((char) c) || c == '_' || c == '.') {
208                                                 sb.Append ((char) c);
209                                         } else {
210                                                 if (sb.Length > 0)
211                                                         break;
212
213                                                 if (c != ' ' && c != '\t') {
214                                                         sr.Read ();
215                                                         return ((char) c).ToString ();
216                                                 }
217                                         }
218                                 }
219
220                                 sr.Read ();
221                         }
222
223                         return sb.ToString ();
224                 }
225
226         }
227 }
228
229 #endif