Skip to content

Commit 1593e14

Browse files
committed
[Fixes #40] SplitPackages needs to fail with a summary of all failures
1 parent 2c830d8 commit 1593e14

File tree

2 files changed

+94
-57
lines changed

2 files changed

+94
-57
lines changed

src/SplitPackages/Program.cs

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Program
2525
private CommandOption _csvOption;
2626
private CommandOption _destinationOption;
2727
private CommandOption _whatIf;
28-
private CommandOption _warningsOnly;
29-
private CommandOption _warningsAsErrors;
28+
private CommandOption _quiet;
29+
private CommandOption _ignoreErrors;
3030

3131
public ILogger Logger
3232
{
@@ -47,16 +47,16 @@ public Program(
4747
CommandOption csvOption,
4848
CommandOption destinationOption,
4949
CommandOption whatIfOption,
50-
CommandOption warningsOnly,
51-
CommandOption warningsAsErrors)
50+
CommandOption quiet,
51+
CommandOption ignoreErrors)
5252
{
5353
_app = app;
5454
_sourceOption = sourceOption;
5555
_csvOption = csvOption;
5656
_destinationOption = destinationOption;
5757
_whatIf = whatIfOption;
58-
_warningsOnly = warningsOnly;
59-
_warningsAsErrors = warningsAsErrors;
58+
_quiet = quiet;
59+
_ignoreErrors = ignoreErrors;
6060
}
6161

6262
static int Main(string[] args)
@@ -86,14 +86,14 @@ static int Main(string[] args)
8686
"Performs a dry run of the command with the given arguments without copying the packages",
8787
CommandOptionType.NoValue);
8888

89-
var warningsOnly = app.Option(
89+
var quiet = app.Option(
9090
"--quiet",
9191
"Avoids printing to the output anything other than warnings or errors",
9292
CommandOptionType.NoValue);
9393

94-
var warningsAsErrors = app.Option(
95-
"--warningsaserrors",
96-
"Treats warnigns and errors and stops the execution of the command",
94+
var ignoreErrors = app.Option(
95+
"--ignore-errors",
96+
"Treats errors as warnings and allows the command to continue executing",
9797
CommandOptionType.NoValue);
9898

9999
var program = new Program(
@@ -102,8 +102,8 @@ static int Main(string[] args)
102102
csvOption,
103103
destinationOption,
104104
whatIfOption,
105-
warningsOnly,
106-
warningsAsErrors);
105+
quiet,
106+
ignoreErrors);
107107

108108
app.OnExecute(new Func<int>(program.Execute));
109109

@@ -128,7 +128,17 @@ private int Execute()
128128

129129
EnsureNoExtraSourcePackagesFound(packagesFromSource, expandedPackages);
130130

131-
EmitWarningsForMissingPackages(packagesFromSource, packagesFromCsv);
131+
var errors = new List<string>();
132+
EmitWarningsForMissingPackages(packagesFromSource, packagesFromCsv, errors);
133+
134+
if (errors.Count > 0)
135+
{
136+
WriteErrorMessage(errors);
137+
if (!_ignoreErrors.HasValue())
138+
{
139+
return Error;
140+
}
141+
}
132142

133143
EnsureDestinationDirectories(
134144
arguments.DestinationFolder,
@@ -150,6 +160,24 @@ private int Execute()
150160
}
151161
}
152162

163+
private void WriteErrorMessage(List<string> errors)
164+
{
165+
var header = new [] {
166+
"The list of packages in the source folder is inconsistent with the list of packages in the CSV file:"
167+
};
168+
169+
var message = string.Join($"{Environment.NewLine} ", header.Concat(errors));
170+
171+
if(_ignoreErrors.HasValue())
172+
{
173+
Logger.LogWarning(message);
174+
}
175+
else
176+
{
177+
Logger.LogError(message);
178+
}
179+
}
180+
153181
private void CreateProjectJsonFiles(IEnumerable<PackageItem> packages)
154182
{
155183
var packagesByFolder = packages.GroupBy(p => Path.GetDirectoryName(p.DestinationPath));
@@ -161,7 +189,7 @@ private void CreateProjectJsonFiles(IEnumerable<PackageItem> packages)
161189
var jsonFileBuilder = new ProjectJsonFileBuilder(
162190
Path.Combine(path.Key, "project.json"),
163191
_whatIf.HasValue(),
164-
_warningsAsErrors.HasValue(),
192+
_ignoreErrors.HasValue(),
165193
Logger);
166194

167195
jsonFileBuilder.AddDependencies(dependenciesList);
@@ -173,7 +201,7 @@ private void CreateProjectJsonFiles(IEnumerable<PackageItem> packages)
173201
private ILogger CreateLogger()
174202
{
175203
var loggerFactory = new LoggerFactory();
176-
var logLevel = _warningsOnly.HasValue() ? LogLevel.Warning : LogLevel.Information;
204+
var logLevel = _quiet.HasValue() ? LogLevel.Warning : LogLevel.Information;
177205

178206
loggerFactory.AddConsole(logLevel, includeScopes: false);
179207

@@ -385,15 +413,15 @@ private void EnsureNoExtraSourcePackagesFound(IList<PackageItem> source, IList<P
385413
}
386414
}
387415

388-
private void EmitWarningsForMissingPackages(IList<PackageItem> source, IList<PackageItem> csv)
416+
private void EmitWarningsForMissingPackages(IList<PackageItem> source, IList<PackageItem> csv, IList<string> errors)
389417
{
390418
for (int i = 0; i < source.Count; i++)
391419
{
392420
if (!source[i].FoundInCsv)
393421
{
394422
var msg = $@"No entry in the csv file matched the following package:
395423
{source[i].Name}";
396-
LogWarning(msg);
424+
LogWarning(msg, errors);
397425
}
398426
}
399427

@@ -403,7 +431,7 @@ private void EmitWarningsForMissingPackages(IList<PackageItem> source, IList<Pac
403431
{
404432
var msg = $@"No package found in the source folder for the following csv entry:
405433
{csv[i].Name}";
406-
LogWarning(msg);
434+
LogWarning(msg, errors);
407435
}
408436
}
409437
}
@@ -465,14 +493,14 @@ private void CopyPackages(IList<PackageItem> expandedPackages)
465493
}
466494
}
467495

468-
private void LogWarning(string message)
496+
private void LogWarning(string message, IList<string> errors)
469497
{
470-
if (_warningsAsErrors.HasValue())
498+
if (_ignoreErrors.HasValue())
471499
{
472-
throw new InvalidOperationException(message);
500+
Logger.LogWarning(message);
473501
}
474502

475-
Logger.LogWarning(message);
503+
errors.Add(message);
476504
}
477505

478506
private class PackageItem : IEquatable<PackageItem>
@@ -513,18 +541,18 @@ private class ProjectJsonFileBuilder
513541
{
514542
private readonly string _path;
515543
private readonly bool _whatIf;
516-
private readonly bool _warningsAsErrors;
544+
private readonly bool _ignoreErrors;
517545
private readonly ILogger _logger;
518546

519547
public ProjectJsonFileBuilder(
520548
string path,
521549
bool whatIf,
522-
bool warningsAsErrors,
550+
bool ignoreErrors,
523551
ILogger logger)
524552
{
525553
_path = path;
526554
_whatIf = whatIf;
527-
_warningsAsErrors = warningsAsErrors;
555+
_ignoreErrors = ignoreErrors;
528556
_logger = logger;
529557
}
530558

@@ -580,7 +608,7 @@ private IDictionary<string, string> CreateDependenciesDictionary()
580608
if (dictionary.ContainsKey(dependency.Identity))
581609
{
582610
var message = $"A duplicate dependency exists in {_path}, name = {dependency.Identity}";
583-
if (_warningsAsErrors)
611+
if (_ignoreErrors)
584612
{
585613
throw new InvalidOperationException(message);
586614
}

src/SplitPackages/project.json

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
{
2-
"version": "1.0.1-*",
3-
"description": "Copies Nuget packages form a given source folder into a specific set of folders based on a CSV file.",
4-
"authors": [ "Microsoft" ],
5-
"tags": [ "" ],
6-
"projectUrl": "",
7-
"licenseUrl": "",
8-
"compilationOptions": {
9-
"emitEntryPoint": true
10-
},
11-
"dependencies": {
12-
"Microsoft.Extensions.CommandLineUtils": "1.0.0-*",
13-
"Microsoft.Extensions.Logging": "1.0.0-*",
14-
"Microsoft.Extensions.Logging.Console": "1.0.0-*",
15-
"Newtonsoft.Json": "8.0.2",
16-
"NuGet.Client": "3.4.0-*"
17-
},
18-
"frameworks": {
19-
"dnxcore50": {
20-
"dependencies": {
21-
"NETStandard.Library": "1.0.0-*"
22-
}
23-
},
24-
"net451": {
25-
"frameworkAssemblies": {
26-
"Microsoft.CSharp": "",
27-
"System.Collections": "",
28-
"System.Xml.Linq": "",
29-
"System.Threading": "",
30-
"System.IO.Compression.FileSystem": ""
31-
}
1+
{
2+
"version": "1.0.1-*",
3+
"description": "Copies Nuget packages form a given source folder into a specific set of folders based on a CSV file.",
4+
"authors": [ "Microsoft" ],
5+
"tags": [ "" ],
6+
"projectUrl": "",
7+
"licenseUrl": "",
8+
"compilationOptions": {
9+
"emitEntryPoint": true
10+
},
11+
"dependencies": {
12+
"Microsoft.Extensions.CommandLineUtils": "1.0.0-*",
13+
"Microsoft.Extensions.Logging": "1.0.0-*",
14+
"Microsoft.Extensions.Logging.Console": "1.0.0-*",
15+
"Newtonsoft.Json": "8.0.2",
16+
"NuGet.Client": "3.4.0-*"
17+
},
18+
"frameworks": {
19+
"netcoreapp1.0": {
20+
"imports": [
21+
"dnxcore50",
22+
"portable-net451+win8"
23+
],
24+
"dependencies": {
25+
"System.Runtime.Serialization.Primitives": "4.1.1-*",
26+
"Microsoft.NETCore.App": {
27+
"version": "1.0.0-*",
28+
"type": "platform"
3229
}
30+
}
31+
},
32+
"net451": {
33+
"frameworkAssemblies": {
34+
"Microsoft.CSharp": "",
35+
"System.Collections": "",
36+
"System.Xml.Linq": "",
37+
"System.Threading": "",
38+
"System.IO.Compression.FileSystem": "",
39+
"System.Runtime.Serialization.Primitives": ""
40+
}
3341
}
42+
}
3443
}

0 commit comments

Comments
 (0)