-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNake.csx
More file actions
629 lines (495 loc) · 15.1 KB
/
Nake.csx
File metadata and controls
629 lines (495 loc) · 15.1 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#r "tools\MsBuild\Microsoft.Build.Utilities.v4.0.dll"
#r "tools\MsBuild\Microsoft.Build.Framework.dll"
#r "tools\AssemblyInfoManager\AssemblyInfoManager.dll"
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Linq;
using Microsoft.Build.Utilities;
using AssemblyInfoManager;
using Nake;
using Nake.FS;
using Nake.Log;
using Nake.Env;
using Nake.Run;
public static string ApplicationName = "TestPipe";
public static string BuildConfig = "Release";
public static string BuildPlatform = "Any CPU";
public static string CompanyName = "CharlesBryant.com";
public static string Copyright = "Copyright (c) 2014, " + CompanyName;
public static string Trademark = "Trademark by " + CompanyName;
public static string Divider = "---------------------------------";
public static string MajorMinorVersion = "0.0";
public static string ApplicationVersion = MajorMinorVersion + ".0";
public static string MsbuildExe = @"tools\MSBuild\MSBuild.exe";
public static string NuGetExe = @"tools\nuget.exe";
public static string NuGetToolsPath = ApplicationPath + @"\TestPipe.Build\tools\TestPipe";
public static string SolutionFile = "TestPipe.sln";
public static string SourcePath = "source";
public static string ApplicationPath = SourcePath + @"\application";
public static string TestPath = SourcePath + @"\tests";
public static string DemoPath = SourcePath + @"\demo";
public static string BuildPath = "Build";
public static string DocsPath = BuildPath + @"\Docs";
public static string ReportsPath = BuildPath + @"\Reports";
public static string DistroPath = BuildPath + @"\Distro";
public static string PubPath = BuildPath + @"\Pub";
public static string OutputPath = ReportsPath;
public static string[] DistributeAppPaths = new string[] { ApplicationPath, DemoPath };
public static bool AppVeyor = false;
public static bool DebugBuild;
//Command Line >nake Go "3.0.0"
//Where "3.0.0" is the version of the build
[Task]
public static void Default(string version = "", string configuration = "Release", string platform = "Any CPU", string logPath = "nake.log.txt")
{
Go(version, configuration, platform, logPath);
}
/// <summary>
/// Build and package TestPipe
/// </summary>
[Task]
public static void Go(string version = "", string configuration = "Release", string platform = "Any CPU", string logPath = "nake.log.txt")
{
SetLogging(logPath, true);
PrintHeader("Go");
if (string.IsNullOrWhiteSpace(version))
{
version = GetVersion();
}
Log.Info("Version: " + version);
NuGetRestore();
Build(version, configuration, platform);
Distro();
Package(version);
Publish();
PrintFooter("Go");
}
/// <summary>
/// Builds you solution's sources
/// </summary>
[Task]
public static void Build(string version, string configuration = "Release", string platform = "Any CPU", bool noChm = false)
{
PrintHeader("Build");
if (string.IsNullOrWhiteSpace(SolutionFile))
{
string error = "SolutionsFile can not be a null or white space.";
Log.Error(error);
throw new Exception(error);
}
UpdateAssembly(version);
Log.Info("Configuration: " + configuration);
Log.Info("Platform: " + platform);
if (AppVeyor || noChm)
{
File.WriteAllText(@"{PubPath}\Utility.chm", "NOP");
}
else
{
string command = string.Format(@"{0} /p:Configuration=""{1}"";Platform=""{2}"";ReferencePath=""{3}"" /target:Rebuild /m", SolutionFile, configuration, platform, BuildPath);
Exec(MsbuildExe, command);
}
PrintFooter("Build");
}
/// <summary>
/// Copy build output to distro folder
/// </summary>
[Task]
public static void Distro()
{
PrintHeader("Distro");
ResetDirectory(DistroPath);
try
{
foreach(var directory in DistributeAppPaths)
{
CopyFilesToDistro(directory);
}
}
catch (System.Exception ex)
{
Log.Error(ex);
throw;
}
PrintFooter("Distro");
}
[Task]
public static void Package(string version)
{
PrintHeader("Package");
ResetDirectory(PubPath);
try
{
foreach (string directory in Directory.GetDirectories(DistroPath))
{
Log.Info("Packaging:" + directory);
//Get nuspec file
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
FileInfo file = directoryInfo.EnumerateFiles()
.Where(f => ".nuspec".Contains(f.Extension.ToLower()))
.FirstOrDefault();
if (file == null)
{
continue;
}
string nuspec = file.FullName;
if(string.IsNullOrWhiteSpace(version))
{
version = GetVersion();
}
string projectName = GetProjectName(directory);
string output = PubPath;
Log.Info("Creating Folder: " + output);
FS.MakeDir(output);
PackageProject(nuspec, version, output, directory);
}
}
catch (System.Exception ex)
{
Log.Error(ex);
}
PrintFooter("Package");
}
/// <summary>
/// Publish packages to artifact repository
/// </summary>
[Task]
public static void Publish()
{
PrintHeader("Publish");
PrintFooter("Publish");
}
/// <summary>
/// Generate code coverage report
/// </summary>
[Task]
public static void ReportCoverage(string configuration = "Debug", string outputPath = null)
{
if(string.IsNullOrWhiteSpace(outputPath))
{
outputPath = OutputPath;
}
TestAndCoverage(configuration, outputPath);
string reports = @".\Reports\projectCoverageReport.xml";
string targetdir = @".\Reports\CodeCoverage";
string reporttypes = @"Html,HtmlSummary^";
string filters = @"-EmailService.Test.Core*";
string command = string.Format(@".\packages\ReportGenerator.2.0.0-beta5\ReportGenerator.exe -reports:{0} -targetdir:{1} -reporttypes:{2} -filters:{3}", reports, targetdir, reporttypes, filters);
Cmd(command);
}
/// <summary>
/// Runs unit tests
/// </summary>
[Task]
public static void XunitTest(string configuration = "Debug", string outputPath = null)
{
if (string.IsNullOrWhiteSpace(outputPath))
{
outputPath = OutputPath;
}
Build(configuration, outputPath);
var target = @".\packages\xunit.runners.2.0.0-beta-build2650\tools\xunit.console.x86.exe";
var targetargs = @".\Output\EmailService.Test.Core.dll";
string command = "{target} {targetargs}";
Cmd(command);
}
/// <summary>
/// Runs unit tests and code coverage
/// </summary>
[Task]
public static void TestAndCoverage(string configuration = "Debug", string outputPath = null)
{
if (string.IsNullOrWhiteSpace(outputPath))
{
outputPath = OutputPath;
}
Build(configuration, outputPath);
//string tests = new FileSet { @"{outputPath}\*.Specs.dll" };
//Cmd(@"Packages\NUnit.Runners.2.6.2\tools\nunit-console.exe /framework:net-4.0 /noshadow /nologo {tests}");
var register = "user";
var target = @".\packages\xunit.runners.2.0.0-beta-build2650\tools\xunit.console.x86.exe";
var targetargs = @".\Output\EmailService.Test.Core.dll /noshadow";
var targetdir = @".\Output";
var filter = "+[EmailService.Core]* -[EmailService.Core.Test]*";
var output = @".\Reports\projectCoverageReport.xml";
CommandLineBuilder builder = new CommandLineBuilder();
builder.AppendTextUnquoted(".\\packages\\OpenCover.4.5.2506\\OpenCover.Console.exe");
builder.AppendSwitchIfNotNull(" -register:{register} ", "-target:" + target);
builder.AppendSwitchIfNotNull(" -targetargs:", targetargs);
builder.AppendTextUnquoted(" -targetdir:{targetdir}");
builder.AppendSwitchIfNotNull(" -filter:", filter);
builder.AppendTextUnquoted(" -mergebyhash");
builder.AppendTextUnquoted(" -output:{output}");
Cmd(builder.ToString());
}
/// <summary>
/// Update the assembly file for a project.
/// </summary>
[Task]
public static void UpdateAssembly(string version = null)
{
PrintHeader("UpdateAssembly");
Log.Info("Version: " + version);
try
{
if (string.IsNullOrWhiteSpace(version))
{
version = GetVersion();
}
string[] filePaths = Directory.GetFiles(ApplicationPath, "AssemblyInfo.cs", SearchOption.AllDirectories);
foreach (var path in filePaths)
{
Log.Info("Updating: " + path);
AssemblyInfo assemblyInfo = new AssemblyInfo()
.Attribute("AssemblyVersion", version)
.Attribute("AssemblyFileVersion", version)
.Attribute("AssemblyTitle", ApplicationName)
.Attribute("AssemblyCopyright", Copyright)
.Attribute("AssemblyTrademark", Trademark)
.Reference("System")
.Reference("System.Reflection")
.Path(path);
assemblyInfo.Update();
}
}
catch(Exception ex)
{
Log.Error(ex);
}
PrintFooter("UpdateAssembly");
}
public static void PackageProject(string nuspec, string version, string output, string basePath)
{
Log.Info("Nuspec: " + nuspec);
Log.Info("Version: " + version);
Log.Info("Output: " + output);
Log.Info("Base Path: " + basePath);
Log.Info("NuGetExe: " + NuGetExe);
string packCmd = string.Format("{0} pack \"{1}\" -Version {2} -OutputDirectory \"{3}\" -BasePath \"{4}\" -NoPackageAnalysis -Symbols", NuGetExe, nuspec, version, output, basePath);
Log.Info("Pack Command: " + packCmd);
Cmd(packCmd);
}
public static void NuGetRestore()
{
string cmd = string.Format("{0} restore {1} -source \"http://agpjaxsrvbuild2/pne.nuget/nuget\"", NuGetExe, SolutionFile);
Log.Info("Restore Command: " + cmd);
Cmd(cmd);
}
public static void CopyFilesToDistro(string path)
{
foreach (string directory in Directory.GetDirectories(path))
{
if (directory.EndsWith("Specs"))
{
continue;
}
Log.Info("Copying files to distribution path: " + directory);
string projectName = GetProjectName(directory);
ProcessDistroSourceFiles(directory, projectName);
}
}
public static string GetProjectName(string directory)
{
return directory.Substring(directory.LastIndexOf('\\') + 1);
}
public static string GetVersion()
{
string defaultVersion = ApplicationVersion;
return defaultVersion;
}
public static void ProcessDistroSourceFiles(string directory, string projectName)
{
CopyNuspecFiles(directory, projectName);
CopyLibFiles(directory, projectName);
CopyContentFiles(directory, projectName);
CopyToolsFiles(NuGetToolsPath, projectName);
CopyLicenseFiles(string.Empty, projectName);
}
public static void CopyNuspecFiles(string directory, string projectName)
{
Log.Info("Processing " + projectName + " nuspec files");
string source = directory;
string dest = string.Format(@"{0}\{1}", DistroPath, projectName);
string[] include = new[] { ".nuspec" };
CopyFiles(source, dest, include);
}
public static void CopyLibFiles(string directory, string projectName)
{
Log.Info("Copying " + projectName + " lib files");
string source = string.Format(@"{0}\bin\{1}", directory, BuildConfig);
Log.Info("Lib Source: " + source);
string dest = string.Format(@"{0}\{1}\lib\net40", DistroPath, projectName);
Log.Info("Lib Destination: " + dest);
string[] include = new[] { ".dll", ".exe", ".config", ".xml", ".pdb" };
CopyFiles(source, dest, include);
}
public static void CopyContentFiles(string directory, string projectName)
{
Log.Info("Copying " + projectName + " content files");
string source = string.Format(@"{0}", directory);
string dest = string.Format(@"{0}\{1}\content", DistroPath, projectName);
string[] include = new[] { ".txt", ".xml", ".config", ".htm", ".html", ".js", ".jpg", ".gif", ".png", ".css" };
string[] exclude = new[] { "packages.config" };
CopyFiles(source, dest, include, exclude);
}
public static void CopyToolsFiles(string directory, string projectName)
{
Log.Info("Copying " + projectName + " NuGet tools files");
string source = directory;
string dest = string.Format(@"{0}\{1}\tools", DistroPath, projectName);
string[] include = new[] { ".ps1", ".psm1" };
CopyFiles(source, dest, include);
}
public static void CopyLicenseFiles(string directory, string projectName)
{
Log.Info("Copyng " + projectName + " license files");
string source = directory + @"license.txt";
string dest = string.Format(@"{0}\{1}", DistroPath, projectName);
Log.Info(string.Format("Copying {0} to {1}", source, dest));
FS.Copy(source, dest, true, false);
}
public static void Stage(string version)
{
foreach (string source in Directory.GetDirectories(DistroPath))
{
Log.Info("Processing " + source);
string projectName = GetProjectName(source);
string packageVersion = version;
string dest = string.Format(@"{0}\{1}.{2}", PubPath, projectName, packageVersion);
string[] include = new[] { ".nupkg" };
CopyFiles(source, dest, include);
}
}
public static void CopyFiles(string source, string dest, string[] include = null, string[] exclude = null)
{
if (!System.IO.Directory.Exists(source))
{
return;
}
DirectoryInfo directoryInfo = new DirectoryInfo(source);
IEnumerable<FileInfo> includedFiles = directoryInfo.EnumerateFiles();
if (include != null)
{
includedFiles = includedFiles.Where(f => include.Contains(f.Extension.ToLower()));
}
if (exclude != null)
{
includedFiles = includedFiles.Where(f => !exclude.Contains(f.Name.ToLower()));
}
FileInfo[] files = includedFiles.ToArray();
if (files.Length < 1)
{
return;
}
foreach (FileInfo file in files)
{
Log.Info(string.Format("Copying {0} to {1}", file.FullName, dest));
FS.Copy(file.FullName, dest, true, false);
}
}
public static void DeleteFiles(string[] files)
{
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
}
public static void PrintHeader(string message)
{
PrintMessage(message, "Started");
}
public static void PrintFooter(string message)
{
PrintMessage(message, "Completed");
}
public static void PrintMessage(string message, string type)
{
Log.Info(string.Empty);
Log.Info(Divider);
Log.Info(string.Format("@@{0} : {1}", message, type));
Log.Info(string.Format("@@Timestamp: {0}", DateTime.Now.ToString()));
Log.Info(Divider);
Log.Info(string.Empty);
}
public static void ResetDirectories(string[] directories)
{
CleanDirectories(directories);
CreateDirectories(directories);
}
public static void ResetDirectory(string directory)
{
string[] directories = new string[] { directory };
ResetDirectories(directories);
}
public static void DeleteDirectory(string target)
{
if (!Directory.Exists(target))
{
return;
}
string[] files = Directory.GetFiles(target);
IEnumerable<string> dirs = Directory.GetDirectories(target);
DeleteFiles(files);
foreach (string dir in dirs)
{
Log.Info("Deleting files in " + dir);
string[] dirFiles = Directory.GetFiles(dir);
DeleteFiles(dirFiles);
Log.Info("Deleting directory " + dir);
DeleteDirectory(dir);
}
Directory.Delete(target, false);
}
public static void CleanDirectories(string[] directories)
{
try
{
foreach (var folder in directories.Reverse())
{
Log.Info("Removing Folder: " + folder);
DeleteDirectory(folder);
}
}
catch (System.Exception ex)
{
Log.Error(ex);
}
}
public static void CreateDirectories(string[] directories)
{
foreach (var folder in directories)
{
Log.Info("Creating Folder: " + folder);
FS.MakeDir(folder);
}
}
public static void Alert(string message)
{
string log = string.Format("##########------ {0} ------##########", message);
Log.Info(log);
}
public static void SetLogging(string logPath = "", bool overwrite = false)
{
if(overwrite)
{
File.Delete(logPath);
}
//Print log to file and console.
if (!string.IsNullOrWhiteSpace(logPath))
{
Log.Out = text => {
File.AppendAllText(logPath, text + Environment.NewLine);
Console.WriteLine(text);
};
return;
}
Log.Out = text => {
Console.WriteLine(text);
};
}