-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
FindAppConfigFile.cs
150 lines (129 loc) · 4.74 KB
/
FindAppConfigFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
#nullable disable
namespace Microsoft.Build.Tasks
{
/// <summary>
/// Finds the app.config file, if any, in the provided lists.
/// For compat reasons, it has to follow a particular arbitrary algorithm.
/// It also adds the TargetPath metadata.
/// </summary>
public class FindAppConfigFile : TaskExtension
{
// The list to search through
private ITaskItem[] _primaryList;
private ITaskItem[] _secondaryList;
// The target path metadata value to add to the found item
// The item found, if any
// What we're looking for
private const string appConfigFile = "app.config";
/// <summary>
/// The primary list to search through
/// </summary>
[Required]
public ITaskItem[] PrimaryList
{
get
{
ErrorUtilities.VerifyThrowArgumentNull(_primaryList, nameof(PrimaryList));
return _primaryList;
}
set => _primaryList = value;
}
/// <summary>
/// The secondary list to search through
/// </summary>
[Required]
public ITaskItem[] SecondaryList
{
get
{
ErrorUtilities.VerifyThrowArgumentNull(_secondaryList, nameof(SecondaryList));
return _secondaryList;
}
set => _secondaryList = value;
}
/// <summary>
/// The value to add as TargetPath metadata
/// </summary>
[Required]
public string TargetPath { get; set; }
/// <summary>
/// The first matching item found in the list, if any
/// </summary>
[Output]
public ITaskItem AppConfigFile { get; set; }
/// <summary>
/// Find the app config
/// </summary>
public override bool Execute()
{
// Look at the whole item spec first -- ie,
// we want to prefer app.config files that are directly in the project folder.
if (ConsultLists(true))
{
return true;
}
// If that fails, fall back to app.config files anywhere in the project cone.
if (ConsultLists(false))
{
return true;
}
// Not found
return true;
}
private bool ConsultLists(bool matchWholeItemSpec)
{
// Look at primary list first, then secondary list
// We walk backwards on the list to find the last match (for historical reasons)
for (int i = PrimaryList.Length - 1; i >= 0; i--)
{
if (IsMatchingItem(PrimaryList[i], matchWholeItemSpec))
{
return true;
}
}
for (int i = SecondaryList.Length - 1; i >= 0; i--)
{
if (IsMatchingItem(SecondaryList[i], matchWholeItemSpec))
{
return true;
}
}
return false;
}
/// <summary>
/// Examines the item to see if it matches what we are looking for.
/// If it does, returns true.
/// </summary>
private bool IsMatchingItem(ITaskItem item, bool matchWholeItemSpec)
{
try
{
string filename = (matchWholeItemSpec ? item.ItemSpec : Path.GetFileName(item.ItemSpec));
if (String.Equals(filename, appConfigFile, StringComparison.OrdinalIgnoreCase))
{
AppConfigFile = item;
// Originally the app.config was found in such a way that it's "OriginalItemSpec"
// metadata was cleared out. Although it doesn't really matter, for compatibility,
// we'll clear it out here.
AppConfigFile.SetMetadata("OriginalItemSpec", item.ItemSpec);
AppConfigFile.SetMetadata(ItemMetadataNames.targetPath, TargetPath);
Log.LogMessageFromResources(MessageImportance.Low, "FindInList.Found", AppConfigFile.ItemSpec);
return true;
}
}
catch (ArgumentException ex)
{
// Just log this: presumably this item spec is not intended to be
// a file path
Log.LogMessageFromResources(MessageImportance.Low, "FindInList.InvalidPath", item.ItemSpec, ex.Message);
}
return false;
}
}
}