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.
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
35 namespace Microsoft.Build.Tasks {
36 public abstract class CreateManifestResourceName : TaskExtension {
38 ITaskItem[] manifestResourceNames;
39 ITaskItem[] resourceFiles;
42 protected CreateManifestResourceName ()
46 public override bool Execute ()
48 if (resourceFiles.Length == 0)
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;
56 Stream binaryStream = null;
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);
69 binaryStream = new FileStream (dependentUponFileName, FileMode.Open,
73 manifestResourceNames [i] = new TaskItem (item);
74 manifestResourceNames [i].ItemSpec = CreateManifestName (
75 filename, item.GetMetadata ("TargetPath"), rootNamespace,
76 dependentUponFileName, binaryStream);
78 if (binaryStream != null)
79 binaryStream.Close ();
87 public static string MakeValidEverettIdentifier (string name)
89 throw new NotImplementedException ();
93 internal static string GetResourceIdFromFileName (string fileName, string rootNamespace)
95 string culture = null;
96 if (String.Compare (Path.GetExtension (fileName), ".resx", true) == 0) {
97 fileName = Path.ChangeExtension (fileName, null);
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;
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 ('\\', '.');
115 if (!String.IsNullOrEmpty (rootNamespace))
116 rname = rootNamespace + "." + rname;
120 return Path.Combine (culture, rname);
123 protected abstract string CreateManifestName (string fileName,
125 string rootNamespace,
126 string dependentUponFileName,
127 Stream binaryStream);
129 protected abstract bool IsSourceFile (string fileName);
132 public ITaskItem[] ManifestResourceNames {
133 get { return manifestResourceNames; }
137 public ITaskItem[] ResourceFiles {
138 get { return resourceFiles; }
139 set { resourceFiles = value; }
142 public string RootNamespace {
143 get { return rootNamespace; }
144 set { rootNamespace = value; }