[xbuild] Fix bug #671700, resource naming in presence of "Link".
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / AssignTargetPath.cs
1 //
2 // AssignTargetPath.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 //
8 // (C) 2006 Marek Sieradzki
9 // Copyright 2008 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 #if NET_2_0
31
32 using System;
33 using System.IO;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36
37 namespace Microsoft.Build.Tasks {
38         public class AssignTargetPath : TaskExtension {
39         
40                 ITaskItem[]     assignedFiles;
41                 ITaskItem[]     files;
42                 string          rootFolder;
43         
44                 public AssignTargetPath ()
45                 {
46                 }
47                 
48                 public override bool Execute ()
49                 {
50                         if (files == null || files.Length == 0)
51                                 //nothing to do
52                                 return true;
53
54                         assignedFiles = new ITaskItem [files.Length];
55                         for (int i = 0; i < files.Length; i ++) {
56                                 string file = files [i].ItemSpec;
57                                 string link = files [i].GetMetadata ("Link");
58                                 string afile = null;
59
60                                 if (String.IsNullOrEmpty (link)) {
61                                         //FIXME: Hack!
62                                         string normalized_root = Path.GetFullPath (rootFolder);
63
64                                         // cur dir should already be set to
65                                         // the project dir
66                                         file = Path.GetFullPath (file);
67
68                                         if (file.StartsWith (normalized_root)) {
69                                                 afile = Path.GetFullPath (file).Substring (
70                                                                 normalized_root.Length);
71                                                 // skip over "root/"
72                                                 if (afile [0] == '\\' ||
73                                                         afile [0] == '/')
74                                                         afile = afile.Substring (1);
75
76                                         } else {
77                                                 afile = Path.GetFileName (file);
78                                         }
79                                 } else {
80                                         afile = link;
81                                 }
82
83                                 assignedFiles [i] = new TaskItem (files [i]);
84                                 assignedFiles [i].SetMetadata ("TargetPath", afile);
85                         }
86
87                         return true;
88                 }
89                 
90                 [Output]
91                 public ITaskItem[] AssignedFiles {
92                         get { return assignedFiles; }
93                 }
94                 
95                 public ITaskItem[] Files {
96                         get { return files; }
97                         set { files = value; }
98                 }
99                 
100                 [Required]
101                 public string RootFolder {
102                         get { return rootFolder; }
103                         set { rootFolder = value; }
104                 }
105         }
106 }
107
108 #endif