This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClariusLabs.tasks
265 lines (242 loc) · 10.6 KB
/
ClariusLabs.tasks
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<Project ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
============================================================
LogItem
Logs all of the input item properties.
============================================================
-->
<UsingTask TaskName="Log"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Linq"/>
<Code Type="Fragment"
Language="cs">
<![CDATA[
foreach (var item in this.Items)
{
Log.LogMessage("ItemSpec={0}", item.ItemSpec);
foreach (var name in item.MetadataNames.OfType<string>())
{
Log.LogMessage("{0}={1}", name, item.GetMetadata(name));
}
}
]]>
</Code>
</Task>
</UsingTask>
<!--
============================================================
RegexTransform
Transforms the input Items parameter by evaluating the
regular expression in their Find metadata and
replacing with their ReplaceWith metadata. Optional, the
options for the regular expression evaluation can be specified.
Example input item:
<RegexTransform Include="$(BuildRoot)Src\GlobalAssemblyInfo.cs">
<Find>AssemblyFileVersion\(".*?"\)</Find>
<ReplaceWith>AssemblyFileVersion("$(FileVersion)")</ReplaceWith>
<Options>Multiline | IgnorePatternWhitespace</Options>
</RegexTransform>
Invoking the target:
<RegexTransform Items="@(RegexTransform)" />
============================================================
-->
<UsingTask TaskName="RegexTransform"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Microsoft.Build.Framework" />
<Code Type="Fragment"
Language="cs">
<![CDATA[
foreach(var item in Items)
{
string fileName = item.GetMetadata("FullPath");
string find = item.GetMetadata("Find");
string replaceWith = item.GetMetadata("ReplaceWith");
string optionsValue = item.GetMetadata("Options") ?? "";
var options = string.IsNullOrWhiteSpace(optionsValue) ?
RegexOptions.None : (RegexOptions)Enum.Parse(typeof(RegexOptions), optionsValue.Replace('|', ','));
if(!File.Exists(fileName))
{
Log.LogError("Could not find file: {0}", fileName);
return false;
}
string content = File.ReadAllText(fileName);
File.WriteAllText(
fileName,
Regex.Replace(
content,
find,
replaceWith,
options
)
);
}
]]>
</Code>
</Task>
</UsingTask>
<!--
============================================================
ReadAssemblyVersion
Reads the given File input parameter, which should contain
an [AssemblyVersion] attribute, and parses the version
components into the following output parameters:
* Major
* Minor
* Revision
* Build
============================================================
-->
<UsingTask TaskName="ReadAssemblyVersion"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<File Required="true" />
<Major Output="true" ParameterType="System.Int32" />
<Minor Output="true" ParameterType="System.Int32" />
<Revision Output="true" ParameterType="System.Int32" />
<Build Output="true" ParameterType="System.Int32" />
</ParameterGroup>
<Task>
<Using Namespace="System.Text.RegularExpressions"/>
<Code Type="Fragment"
Language="cs">
<![CDATA[
if (!System.IO.File.Exists(this.File))
{
Log.LogError(null, null, null, null, 0, 0, 0, 0, String.Format("Could not find file: {0}", this.File), new object[0]);
return false;
}
var contents = System.IO.File.ReadAllText(this.File);
var reg = new Regex(@"AssemblyVersion\(""(?<major>\d+)\.(?<minor>\d+)(\.(?<build>\d+))?(\.(?<revision>\d+))?""\)");
var match = reg.Match(contents);
this.Major = int.Parse(match.Groups["major"].Value);
this.Minor = int.Parse(match.Groups["minor"].Value);
if (match.Groups["build"].Success)
this.Build = int.Parse(match.Groups["build"].Value);
if (match.Groups["revision"].Success)
this.Revision = int.Parse(match.Groups["revision"].Value);
]]>
</Code>
</Task>
</UsingTask>
<!--
============================================================
ParseFrameworkName
Parses the input FrameworkName which is the simplified folder
name format used by NuGet, into an output item property
named FrameworkSpec with the following metadata:
* Name: the value of the input FrameworkName
* Moniker: the full target framework moniker, i.e. ".NETFramework,Version=v4.0,Profile=Client"
* Identifier: i.e. ".NETFramework", "Silverlight", etc.
* Version: i.e. "v4.0", "v4.5"
* Profile: i.e. "Client"
Read more at http://blogs.msdn.com/b/visualstudio/archive/2010/05/19/visual-studio-managed-multi-targeting-part-1-concepts-target-framework-moniker-target-framework.aspx
============================================================
-->
<UsingTask TaskName="ParseFrameworkName"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<NuGetExe Required="true" />
<FrameworkName Required="true" />
<FrameworkSpec ParameterType="Microsoft.Build.Framework.ITaskItem" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Reflection" />
<Using Namespace="System.Runtime.Versioning" />
<Using Namespace="System.Collections.Generic" />
<Code Type="Fragment"
Language="cs">
<![CDATA[
Log.LogMessage("Parsing framework name " + this.FrameworkName);
var asm = Assembly.LoadFrom(this.NuGetExe);
var utility = asm.GetType("NuGet.VersionUtility", true);
var flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod;
var name = (FrameworkName)utility.InvokeMember("ParseFrameworkName", flags, null, null, new object[] { this.FrameworkName });
this.FrameworkSpec = new TaskItem(this.FrameworkName, new Dictionary<string, string>
{
{ "Name", this.FrameworkName },
{ "Moniker", (string)utility.InvokeMember("GetFrameworkString", flags, null, null, new object[] { name }) },
{ "Identifier", name.Identifier },
{ "Version", name.Version.ToString() },
{ "Profile", name.Profile }
});
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="DownloadString"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Url Required="true" />
<Content Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment"
Language="cs">
<![CDATA[
this.Content = new System.Net.WebClient().DownloadString(this.Url);
]]>
</Code>
</Task>
</UsingTask>
<!--
============================================================
AssignNuGetSpec
Augments the input .nuspec item from Spec input by adding
following metadata to it:
* Id
* Version
============================================================
-->
<UsingTask TaskName="AssignNuGetSpec"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true"/>
<Result ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true"/>
</ParameterGroup>
<Task>
<Reference Include="System.Xml"/>
<Reference Include="System.Xml.Linq"/>
<Using Namespace="System.Collections.Generic"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Linq"/>
<Using Namespace="System.Xml"/>
<Using Namespace="System.Xml.Linq"/>
<Code Type="Fragment"
Language="cs">
<![CDATA[
var output = new List<ITaskItem>();
foreach (var item in this.Items)
{
var spec = XDocument.Load(item.GetMetadata("FullPath"));
var metadata = spec.Root.Elements().First();
var result = new TaskItem(item.ItemSpec, new Dictionary<string, string>
{
{ "Id", metadata.Elements().First(x => x.Name.LocalName == "id").Value },
{ "Version", metadata.Elements().First(x => x.Name.LocalName == "version").Value },
});
item.CopyMetadataTo(result);
output.Add(result);
}
this.Result = output.ToArray();
]]>
</Code>
</Task>
</UsingTask>
</Project>