Merge pull request #2971 from BrzVlad/feature-cross-binprot
[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
31 using System;
32 using System.IO;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
35
36 namespace Microsoft.Build.Tasks {
37         public class AssignTargetPath : TaskExtension {
38         
39                 ITaskItem[]     assignedFiles;
40                 ITaskItem[]     files;
41                 string          rootFolder;
42         
43                 public AssignTargetPath ()
44                 {
45                 }
46                 
47                 public override bool Execute ()
48                 {
49                         if (files == null || files.Length == 0)
50                                 //nothing to do
51                                 return true;
52
53                         assignedFiles = new ITaskItem [files.Length];
54                         for (int i = 0; i < files.Length; i ++) {
55                                 string file = files [i].ItemSpec;
56                                 string link = files [i].GetMetadata ("Link");
57                                 string afile = null;
58
59                                 if (String.IsNullOrEmpty (link)) {
60                                         //FIXME: Hack!
61                                         string normalized_root = Path.GetFullPath (rootFolder);
62
63                                         // cur dir should already be set to
64                                         // the project dir
65                                         file = Path.GetFullPath (file);
66
67                                         if (file.StartsWith (normalized_root)) {
68                                                 afile = Path.GetFullPath (file).Substring (
69                                                                 normalized_root.Length);
70                                                 // skip over "root/"
71                                                 if (afile [0] == '\\' ||
72                                                         afile [0] == '/')
73                                                         afile = afile.Substring (1);
74
75                                         } else {
76                                                 afile = Path.GetFileName (file);
77                                         }
78                                 } else {
79                                         afile = link;
80                                 }
81
82                                 assignedFiles [i] = new TaskItem (files [i]);
83                                 assignedFiles [i].SetMetadata ("TargetPath", afile);
84                         }
85
86                         return true;
87                 }
88                 
89                 [Output]
90                 public ITaskItem[] AssignedFiles {
91                         get { return assignedFiles; }
92                 }
93                 
94                 public ITaskItem[] Files {
95                         get { return files; }
96                         set { files = value; }
97                 }
98                 
99                 [Required]
100                 public string RootFolder {
101                         get { return rootFolder; }
102                         set { rootFolder = value; }
103                 }
104         }
105 }
106