2 // CreateManifestResourceName.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
34 namespace Microsoft.Build.Tasks {
35 public abstract class CreateManifestResourceName : TaskExtension {
37 ITaskItem[] manifestResourceNames;
38 ITaskItem[] resourceFiles;
41 protected CreateManifestResourceName ()
45 public override bool Execute ()
47 if (resourceFiles.Length == 0)
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;
55 Stream binaryStream = null;
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);
68 binaryStream = new FileStream (dependentUponFileName, FileMode.Open,
72 manifestResourceNames [i] = new TaskItem (item);
73 manifestResourceNames [i].ItemSpec = CreateManifestName (
74 filename, item.GetMetadata ("TargetPath"), rootNamespace,
75 dependentUponFileName, binaryStream);
77 if (binaryStream != null)
78 binaryStream.Close ();
86 public static string MakeValidEverettIdentifier (string name)
88 throw new NotImplementedException ();
92 internal static string GetResourceIdFromFileName (string fileName, string rootNamespace)
94 string culture = null;
95 if (String.Compare (Path.GetExtension (fileName), ".resx", true) == 0) {
96 fileName = Path.ChangeExtension (fileName, null);
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;
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 ('\\', '.');
114 if (!String.IsNullOrEmpty (rootNamespace))
115 rname = rootNamespace + "." + rname;
119 return Path.Combine (culture, rname);
122 protected abstract string CreateManifestName (string fileName,
124 string rootNamespaceName,
125 string dependentUponFileName,
126 Stream binaryStream);
128 protected abstract bool IsSourceFile (string fileName);
131 public ITaskItem[] ManifestResourceNames {
132 get { return manifestResourceNames; }
136 public ITaskItem[] ResourceFiles {
137 get { return resourceFiles; }
138 set { resourceFiles = value; }
141 public string RootNamespace {
142 get { return rootNamespace; }
143 set { rootNamespace = value; }