4b71e08ab7a55ae9970a2c6db48c2d9188a98deb
[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 afile = null;
58                                 //FIXME: Hack!
59                                 string normalized_root = Path.GetFullPath (rootFolder);
60
61                                 // cur dir should already be set to
62                                 // the project dir
63                                 file = Path.GetFullPath (file);
64
65                                 if (file.StartsWith (normalized_root)) {
66                                         afile = Path.GetFullPath (file).Substring (
67                                                         normalized_root.Length);
68                                         // skip over "root/"
69                                         if (afile [0] == '\\' ||
70                                                 afile [0] == '/')
71                                                 afile = afile.Substring (1);
72
73                                 } else {
74                                         afile = Path.GetFileName (file);
75                                 }
76
77                                 assignedFiles [i] = new TaskItem (files [i]);
78                                 assignedFiles [i].SetMetadata ("TargetPath", afile);
79                         }
80
81                         return true;
82                 }
83                 
84                 [Output]
85                 public ITaskItem[] AssignedFiles {
86                         get { return assignedFiles; }
87                 }
88                 
89                 public ITaskItem[] Files {
90                         get { return files; }
91                         set { files = value; }
92                 }
93                 
94                 [Required]
95                 public string RootFolder {
96                         get { return rootFolder; }
97                         set { rootFolder = value; }
98                 }
99         }
100 }
101
102 #endif