-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
logic.targets
388 lines (321 loc) · 16.3 KB
/
logic.targets
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2015-2017 Denis Kuzmin [ [email protected] ]
Distributed under the MIT License
( see accompanying file LICENSE )
-->
<!--
GetNuTool - github.com/3F/GetNuTool
========================
The lightweight non-binary NuGet Client as a portable & embeddable tool for work with NuGet packages via basic MSBuild.
All versions and documentation here:
https://github.com/3F/GetNuTool/blob/master/README.md
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Default values -->
<PropertyGroup>
<ngconfig Condition="'$(ngconfig)' == ''">packages.config</ngconfig> <!-- Where to look the packages.config of solution-level -->
<ngserver Condition="'$(ngserver)' == ''">https://www.nuget.org/api/v2/package/</ngserver> <!-- Server by default -->
<ngpackages Condition="'$(ngpackages)' == ''"></ngpackages> <!-- Packages by default -->
<ngpath Condition="'$(ngpath)' == ''">packages</ngpath> <!-- Path by default -->
</PropertyGroup>
<!-- Entry points -->
<Target Name="get" BeforeTargets="Build" DependsOnTargets="header">
<PrepareList config="$(ngconfig)" plist="$(ngpackages)" wpath="$(wpath)">
<Output PropertyName="plist" TaskParameter="Result" />
</PrepareList>
<NGDownload plist="$(plist)" url="$(ngserver)" wpath="$(wpath)" defpath="$(ngpath)" debug="$(debug)" />
</Target>
<Target Name="pack" DependsOnTargets="header">
<NGPack dir="$(ngin)" dout="$(ngout)" wpath="$(wpath)" vtool="$(GetNuTool)" debug="$(debug)" />
</Target>
<!-- Tasks settings -->
<PropertyGroup>
<TaskCoreDllPath Condition="Exists('$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll')">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll</TaskCoreDllPath>
<TaskCoreDllPath Condition="'$(TaskCoreDllPath)' == '' and Exists('$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll')">$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll</TaskCoreDllPath>
</PropertyGroup>
<!-- Prepares list for downloader below -->
<UsingTask TaskName="PrepareList" TaskFactory="CodeTaskFactory" AssemblyFile="$(TaskCoreDllPath)">
<ParameterGroup>
<config ParameterType="System.String" Required="true" />
<plist ParameterType="System.String" />
<wpath ParameterType="System.String" />
<Result ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Using Namespace="System" />
<Using Namespace="System.Collections.Generic" />
<Using Namespace="System.IO" />
<Using Namespace="System.Xml.Linq" />
<Code Type="Fragment" Language="cs"><![CDATA[
if(!String.IsNullOrEmpty(plist)) {
Result = plist;
return true;
}
var _err = Console.Error;
Action<string, Queue<string>> h = delegate(string cfg, Queue<string> list)
{
foreach(var pkg in XDocument.Load(cfg).Descendants("package"))
{
var id = pkg.Attribute("id");
var version = pkg.Attribute("version");
var output = pkg.Attribute("output");
if(id == null) {
_err.WriteLine("Some 'id' does not exist in '{0}'", cfg);
return; // throw new ArgumentException();
}
var link = id.Value;
if(version != null) {
link += "/" + version.Value;
}
if(output != null) {
list.Enqueue(link + ":" + output.Value);
continue;
}
list.Enqueue(link);
}
};
var ret = new Queue<string>();
foreach(var cfg in config.Split('|', ';')) {
var lcfg = Path.Combine(wpath, cfg ?? "");
if(File.Exists(lcfg)) {
h(lcfg, ret);
}
else {
_err.WriteLine(".config '{0}' was not found.", lcfg);
}
}
if(ret.Count < 1) {
_err.WriteLine("List of packages is empty. Use .config or /p:ngpackages=\"...\"\n");
}
else {
Result = String.Join(";", ret.ToArray());
}
]]></Code>
</Task>
</UsingTask>
<!-- Downloader -->
<UsingTask TaskName="NGDownload" TaskFactory="CodeTaskFactory" AssemblyFile="$(TaskCoreDllPath)">
<ParameterGroup>
<plist ParameterType="System.String" />
<url ParameterType="System.String" Required="true" />
<wpath ParameterType="System.String" />
<defpath ParameterType="System.String" />
<debug ParameterType="System.Boolean" />
</ParameterGroup>
<Task>
<Reference Include="WindowsBase" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.IO.Packaging" />
<Using Namespace="System.Net" />
<Code Type="Fragment" Language="cs"><![CDATA[
if(plist == null) {
return false;
}
// to ignore from package
var ignore = new string[] { "/_rels/", "/package/", "/[Content_Types].xml" };
Action<string, object> dbg = delegate(string s, object p) {
if(debug) {
Console.WriteLine(s, p);
}
};
Func<string, string> loc = delegate(string p) {
return Path.Combine(wpath, p ?? "");
};
Action<string, string, string> get = delegate(string link, string name, string path)
{
var to = Path.GetFullPath(loc(path ?? name));
if(Directory.Exists(to)) {
Console.WriteLine("`{0}` is already exists: \"{1}\"", name, to);
return;
}
Console.Write("Getting `{0}` ... ", link);
var tmp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
using(var l = new WebClient()) {
try {
l.Headers.Add("User-Agent", "GetNuTool");
l.UseDefaultCredentials = true;
l.DownloadFile(url + link, tmp);
}
catch(Exception ex) {
Console.Error.WriteLine(ex.Message);
return;
}
}
Console.WriteLine("Extracting into \"{0}\"", to);
using(var pkg = ZipPackage.Open(tmp, FileMode.Open, FileAccess.Read))
{
foreach(var part in pkg.GetParts())
{
var uri = Uri.UnescapeDataString(part.Uri.OriginalString);
if(ignore.Any(x => uri.StartsWith(x, StringComparison.Ordinal))) {
continue;
}
var dest = Path.Combine(to, uri.TrimStart('/'));
dbg("- `{0}`", uri);
var dir = Path.GetDirectoryName(dest);
if(!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}
using(Stream src = part.GetStream(FileMode.Open, FileAccess.Read))
using(var target = File.OpenWrite(dest))
{
try {
src.CopyTo(target);
}
catch(FileFormatException ex) { dbg("[x]?crc: {0}", dest); }
}
}
}
File.Delete(tmp);
};
//Format: id/version[:path];id2/version[:path];...
foreach(var pkg in plist.Split(';'))
{
var ident = pkg.Split(':');
var link = ident[0];
var path = (ident.Length > 1) ? ident[1] : null;
var name = link.Replace('/', '.');
if(!String.IsNullOrEmpty(defpath)) {
path = Path.Combine(defpath, path ?? name);
}
get(link, name, path);
}
]]></Code>
</Task>
</UsingTask>
<!-- Packing -->
<UsingTask TaskName="NGPack" TaskFactory="CodeTaskFactory" AssemblyFile="$(TaskCoreDllPath)">
<ParameterGroup>
<dir ParameterType="System.String" Required="true" />
<dout ParameterType="System.String" />
<wpath ParameterType="System.String" />
<vtool ParameterType="System.String" Required="true" />
<debug ParameterType="System.Boolean" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="WindowsBase" />
<Using Namespace="System" />
<Using Namespace="System.Collections.Generic" />
<Using Namespace="System.IO" />
<Using Namespace="System.Linq" />
<Using Namespace="System.IO.Packaging" />
<Using Namespace="System.Xml.Linq" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs"><![CDATA[
const string EXT_NUSPEC = ".nuspec";
const string EXT_NUPKG = ".nupkg";
const string TAG_META = "metadata";
const string DEF_CONTENT_TYPE = "application/octet"; //System.Net.Mime.MediaTypeNames.Application.Octet
const string MANIFEST_URL = "http://schemas.microsoft.com/packaging/2010/07/manifest";
// Tags
const string ID = "id";
const string VER = "version";
Action<string, object> dbg = delegate(string s, object p) {
if(debug) {
Console.WriteLine(s, p);
}
};
var _err = Console.Error;
dir = Path.Combine(wpath, dir);
if(!Directory.Exists(dir)) {
_err.WriteLine("`{0}` was not found.", dir);
return false;
}
dout = Path.Combine(wpath, dout ?? "");
// Get metadata
var nuspec = Directory.GetFiles(dir, "*" + EXT_NUSPEC, SearchOption.TopDirectoryOnly).FirstOrDefault();
if(nuspec == null) {
_err.WriteLine("{0} was not found in `{1}`", EXT_NUSPEC, dir);
return false; // throw new FileNotFoundException();
}
Console.WriteLine("Found {0}: `{1}`", EXT_NUSPEC, nuspec);
var root = XDocument.Load(nuspec).Root.Elements().FirstOrDefault(x => x.Name.LocalName == TAG_META);
if(root == null) {
_err.WriteLine("{0} does not contain {1}.", nuspec, TAG_META);
return false; // throw new FileNotFoundException();
}
var metadata = new Dictionary<string, string>();
foreach(var tag in root.Elements()) {
metadata[tag.Name.LocalName.ToLower()] = tag.Value;
}
// Validate data - rules of nuget core
if(metadata[ID].Length > 100 || !Regex.IsMatch(metadata[ID],
@"^\w+([_.-]\w+)*$",
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture))
{
_err.WriteLine("The format of `{0}` is not correct.", ID);
return false; // throw new FormatException();
}
// Format package
var ignore = new string[] { // to ignore from package
Path.Combine(dir, "_rels"),
Path.Combine(dir, "package"),
Path.Combine(dir, "[Content_Types].xml") };
string pout = String.Format("{0}.{1}{2}", metadata[ID], metadata[VER], EXT_NUPKG);
if(!String.IsNullOrWhiteSpace(dout)) {
if(!Directory.Exists(dout)) {
Directory.CreateDirectory(dout);
}
pout = Path.Combine(dout, pout);
}
Console.WriteLine("Started packing `{0}` ...", pout);
using(var pkg = Package.Open(pout, FileMode.Create))
{
// manifest relationship
Uri manifestUri = new Uri(String.Format("/{0}{1}", metadata[ID], EXT_NUSPEC), UriKind.Relative);
pkg.CreateRelationship(manifestUri, TargetMode.Internal, MANIFEST_URL);
// content
foreach(var file in Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories))
{
if(ignore.Any(x => file.StartsWith(x, StringComparison.Ordinal))) {
continue;
}
string pUri;
if(file.StartsWith(dir, StringComparison.OrdinalIgnoreCase)) {
pUri = file.Substring(dir.Length).TrimStart(Path.DirectorySeparatorChar);
}
else {
pUri = file;
}
dbg("- `{0}`", pUri);
// to protect path without separators
var escaped = String.Join("/", pUri.Split('\\', '/').Select(p => Uri.EscapeDataString(p)));
Uri uri = PackUriHelper.CreatePartUri(new Uri(escaped, UriKind.Relative));
var part = pkg.CreatePart(uri, DEF_CONTENT_TYPE, CompressionOption.Maximum);
using (Stream tstream = part.GetStream())
using(var fs = new FileStream(file, FileMode.Open, FileAccess.Read)) {
fs.CopyTo(tstream);
}
}
// metadata
Func<string, string> getmeta = delegate(string key) {
return (metadata.ContainsKey(key))? metadata[key] : "";
};
var _p = pkg.PackageProperties;
_p.Creator = getmeta("authors");
_p.Description = getmeta("description");
_p.Identifier = metadata[ID];
_p.Version = metadata[VER];
_p.Keywords = getmeta("tags");
_p.Title = getmeta("title");
_p.LastModifiedBy = "GetNuTool v" + vtool;
}
]]></Code>
</Task>
</UsingTask>
<!-- Service settings -->
<Target Name="Build" DependsOnTargets="get" />
<PropertyGroup>
<GetNuTool>1</GetNuTool>
<wpath Condition="'$(wpath)' == ''">$(MSBuildProjectDirectory)</wpath>
</PropertyGroup>
<Target Name="header">
<Message Text="%0D%0AGetNuTool v$(GetNuTool) - github.com/3F%0D%0A=========%0D%0A" Importance="high" />
</Target>
</Project>