[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / CreateManifestResourceName.cs
1 //
2 // CreateManifestResourceName.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 Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34
35 namespace Microsoft.Build.Tasks {
36         public abstract class CreateManifestResourceName : TaskExtension {
37         
38                 ITaskItem[]     manifestResourceNames;
39                 ITaskItem[]     resourceFiles;
40                 string          rootNamespace;
41                 
42                 protected CreateManifestResourceName ()
43                 {
44                 }
45                 
46                 public override bool Execute ()
47                 {
48                         if (resourceFiles.Length == 0)
49                                 return true;
50
51                         manifestResourceNames = new ITaskItem [resourceFiles.Length];
52                         for (int i = 0; i < resourceFiles.Length; i ++) {
53                                 ITaskItem item = resourceFiles [i];
54                                 string filename = item.ItemSpec;
55
56                                 Stream binaryStream = null;
57                                 try {
58                                         string dependentUponFileName = item.GetMetadata ("DependentUpon");
59                                         if (!String.IsNullOrEmpty (dependentUponFileName)) {
60                                                 dependentUponFileName = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (
61                                                                         filename), dependentUponFileName));
62                                                 if (!File.Exists (dependentUponFileName)) {
63                                                         Log.LogError ("Unable to create resource name for '{0}'," +
64                                                                         "as dependent file {1} was not found.",
65                                                                         filename, dependentUponFileName);
66                                                         return false;
67                                                 }
68
69                                                 binaryStream = new FileStream (dependentUponFileName, FileMode.Open,
70                                                                 FileAccess.Read);
71                                         }
72
73                                         manifestResourceNames [i] = new TaskItem (item);
74                                         manifestResourceNames [i].ItemSpec = CreateManifestName (
75                                                                 filename, item.GetMetadata ("TargetPath"), rootNamespace,
76                                                                 dependentUponFileName, binaryStream);
77                                 } finally {
78                                         if (binaryStream != null)
79                                                 binaryStream.Close ();
80                                 }
81                         }
82
83                         return true;
84                 }
85                 
86                 [MonoTODO]
87                 public static string MakeValidEverettIdentifier (string name)
88                 {
89                         throw new NotImplementedException ();
90                 }
91                 
92                 // No dependent file
93                 internal static string GetResourceIdFromFileName (string fileName, string rootNamespace)
94                 {
95                         string culture = null;
96                         if (String.Compare (Path.GetExtension (fileName), ".resx", true) == 0) {
97                                 fileName = Path.ChangeExtension (fileName, null);
98                         } else {
99                                 string only_filename, extn;
100                                 if (AssignCulture.TrySplitResourceName (fileName, out only_filename, out culture, out extn)) {
101                                         //remove the culture from fileName
102                                         //foo.it.bmp -> foo.bmp
103                                         fileName = only_filename + "." + extn;
104                                 } else {
105                                         culture = null;
106                                 }
107                         }
108
109                         // spaces in folder name are changed to _, those in filename remain
110                         string dirname = Path.GetDirectoryName (fileName) ?? String.Empty;
111                         dirname = dirname.Replace (' ', '_');
112                         fileName = Path.Combine (dirname, Path.GetFileName (fileName));
113                         string rname = fileName.Replace ('/', '.').Replace ('\\', '.');
114
115                         if (!String.IsNullOrEmpty (rootNamespace))
116                                 rname = rootNamespace + "." + rname;
117                         if (culture == null)
118                                 return rname;
119                         else
120                                 return Path.Combine (culture, rname);
121                 }
122
123                 protected abstract string CreateManifestName (string fileName,
124                                                               string linkFileName,
125                                                               string rootNamespace,
126                                                               string dependentUponFileName,
127                                                               Stream binaryStream);
128                 
129                 protected abstract bool IsSourceFile (string fileName);
130                 
131                 [Output]
132                 public ITaskItem[] ManifestResourceNames {
133                         get { return manifestResourceNames; }
134                 }
135                 
136                 [Required]
137                 public ITaskItem[] ResourceFiles {
138                         get { return resourceFiles; }
139                         set { resourceFiles = value; }
140                 }
141                 
142                 public string RootNamespace {
143                         get { return rootNamespace; }
144                         set { rootNamespace = value; }
145                 }
146         }
147 }
148
149 #endif