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