~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to samples/Mono/Mono.Build.Tasks/AddMonoAssemblySearchPaths.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// SharpDevelop samples
 
2
// Copyright (c) 2006, AlphaSierraPapa
 
3
// All rights reserved.
 
4
//
 
5
// Redistribution and use in source and binary forms, with or without modification, are
 
6
// permitted provided that the following conditions are met:
 
7
//
 
8
// - Redistributions of source code must retain the above copyright notice, this list
 
9
//   of conditions and the following disclaimer.
 
10
//
 
11
// - Redistributions in binary form must reproduce the above copyright notice, this list
 
12
//   of conditions and the following disclaimer in the documentation and/or other materials
 
13
//   provided with the distribution.
 
14
//
 
15
// - Neither the name of the SharpDevelop team nor the names of its contributors may be used to
 
16
//   endorse or promote products derived from this software without specific prior written
 
17
//   permission.
 
18
//
 
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
 
20
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 
21
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
22
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
23
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
24
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 
25
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
26
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
using System;
 
29
using System.Collections.Generic;
 
30
using Microsoft.Build.Framework;
 
31
using Microsoft.Build.Utilities;
 
32
 
 
33
namespace Mono.Build.Tasks
 
34
{
 
35
        /// <summary>
 
36
        /// Adds GAC folders to the assembly search path for any GAC references.
 
37
        /// </summary>
 
38
        public class AddMonoAssemblySearchPaths : Task
 
39
        {
 
40
                /// <summary>
 
41
                /// String used in the AssemblySearchPath property to specify where
 
42
                /// Mono GAC folder items should be added.
 
43
                /// </summary>
 
44
                public const string MonoGacSearchPathIndicator = "{MonoGAC}";
 
45
                
 
46
                string[] paths;
 
47
                ITaskItem[] assemblies;
 
48
                
 
49
                public AddMonoAssemblySearchPaths()
 
50
                {
 
51
                }
 
52
                
 
53
                /// <summary>
 
54
                /// Gets or sets the Mono assembly search paths.
 
55
                /// </summary>
 
56
                [Required]
 
57
                [Output]
 
58
                public string[] Paths {
 
59
                        get { return paths; }
 
60
                        set { paths = value; }
 
61
                }
 
62
                
 
63
                /// <summary>
 
64
                /// These are the assembly references in the project being built.  This 
 
65
                /// set of items is also passed to the ResolveAssemblyReference task.
 
66
                /// </summary>
 
67
                [Required]
 
68
                public ITaskItem[] Assemblies {
 
69
                        get { return assemblies; }
 
70
                        set { assemblies = value; }
 
71
                }
 
72
                
 
73
                /// <summary>
 
74
                /// Replaces the {MonoGAC} entry in the AssemblySearchPaths.
 
75
                /// </summary>
 
76
                public override bool Execute()
 
77
                {
 
78
                        List<string> updatedSearchPaths = new List<string>();
 
79
                        List<string> monoGacSearchPaths = new List<string>();
 
80
                        
 
81
                        if (assemblies != null) {
 
82
                                foreach (ITaskItem item in assemblies) {
 
83
                                        string monoGacFolder = GetMonoGacFolder(item);
 
84
                                        if (monoGacFolder != null) {
 
85
                                                monoGacSearchPaths.Add(monoGacFolder);
 
86
                                        }
 
87
                                }
 
88
                        }
 
89
                        
 
90
                        // Add Mono GAC search paths to existing search paths.
 
91
                        foreach (string path in paths) {
 
92
                                if (path.Equals(MonoGacSearchPathIndicator, StringComparison.InvariantCultureIgnoreCase)) {     
 
93
                                        updatedSearchPaths.AddRange(monoGacSearchPaths);
 
94
                                } else {
 
95
                                        updatedSearchPaths.Add(path);
 
96
                                }
 
97
                        }
 
98
                        paths = new string[updatedSearchPaths.Count];
 
99
                        updatedSearchPaths.CopyTo(paths);
 
100
                        
 
101
                        return true;
 
102
                }
 
103
                
 
104
                /// <summary>
 
105
                /// Gets the corresponding Mono GAC folder for the task item.
 
106
                /// </summary>
 
107
                /// <remarks>
 
108
                /// Basic logic is:
 
109
                /// 
 
110
                /// 1) If the Gac reference has a full specified assembly name 
 
111
                ///    (e.g. name, version, culture, public key token) then just generate
 
112
                ///    the expected Gac folder.
 
113
                /// 2) If the Gac reference is a short name, then look in Mono's gac for
 
114
                ///    latest version (i.e. highest version number) of the assembly and 
 
115
                ///    adds it folder.
 
116
                /// 
 
117
                /// Extra possiblities:
 
118
                /// 
 
119
                /// 1) Verify the assembly actually exists and take action accordingly.
 
120
                /// 2) Allow partial assembly names (i.e short + version and nothing else).
 
121
                /// 3) Check the hint path actually resolves to an assembly otherwise add
 
122
                ///    a possible GAC folder.
 
123
                /// </remarks>
 
124
                /// <returns><see langword="null"/> if no GAC folder can be found
 
125
                /// for the task item.</returns>
 
126
                string GetMonoGacFolder(ITaskItem item)
 
127
                {
 
128
                        MonoAssemblyName assemblyName = MonoGlobalAssemblyCache.FindAssemblyName(item.ItemSpec);
 
129
                        if (assemblyName != null) {
 
130
                                return assemblyName.Directory;
 
131
                        }
 
132
                        return null;
 
133
                }
 
134
        }
 
135
}