1
// SharpDevelop samples
2
// Copyright (c) 2006, AlphaSierraPapa
3
// All rights reserved.
5
// Redistribution and use in source and binary forms, with or without modification, are
6
// permitted provided that the following conditions are met:
8
// - Redistributions of source code must retain the above copyright notice, this list
9
// of conditions and the following disclaimer.
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.
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
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.
29
using System.Collections.Generic;
30
using Microsoft.Build.Framework;
31
using Microsoft.Build.Utilities;
33
namespace Mono.Build.Tasks
36
/// Adds GAC folders to the assembly search path for any GAC references.
38
public class AddMonoAssemblySearchPaths : Task
41
/// String used in the AssemblySearchPath property to specify where
42
/// Mono GAC folder items should be added.
44
public const string MonoGacSearchPathIndicator = "{MonoGAC}";
47
ITaskItem[] assemblies;
49
public AddMonoAssemblySearchPaths()
54
/// Gets or sets the Mono assembly search paths.
58
public string[] Paths {
60
set { paths = value; }
64
/// These are the assembly references in the project being built. This
65
/// set of items is also passed to the ResolveAssemblyReference task.
68
public ITaskItem[] Assemblies {
69
get { return assemblies; }
70
set { assemblies = value; }
74
/// Replaces the {MonoGAC} entry in the AssemblySearchPaths.
76
public override bool Execute()
78
List<string> updatedSearchPaths = new List<string>();
79
List<string> monoGacSearchPaths = new List<string>();
81
if (assemblies != null) {
82
foreach (ITaskItem item in assemblies) {
83
string monoGacFolder = GetMonoGacFolder(item);
84
if (monoGacFolder != null) {
85
monoGacSearchPaths.Add(monoGacFolder);
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);
95
updatedSearchPaths.Add(path);
98
paths = new string[updatedSearchPaths.Count];
99
updatedSearchPaths.CopyTo(paths);
105
/// Gets the corresponding Mono GAC folder for the task item.
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
117
/// Extra possiblities:
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.
124
/// <returns><see langword="null"/> if no GAC folder can be found
125
/// for the task item.</returns>
126
string GetMonoGacFolder(ITaskItem item)
128
MonoAssemblyName assemblyName = MonoGlobalAssemblyCache.FindAssemblyName(item.ItemSpec);
129
if (assemblyName != null) {
130
return assemblyName.Directory;