diff --git a/CHANGELOG.md b/CHANGELOG.md index e533a9f..a8c4b65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +### Added + +- Tool exits with code 0 when everything is okay and 1 when something has gone wrong + ### Fixed - Log an error and continue when we fail to convert a file diff --git a/TypeContractor.Tool/Generator.cs b/TypeContractor.Tool/Generator.cs index bc762a8..09fe2ec 100644 --- a/TypeContractor.Tool/Generator.cs +++ b/TypeContractor.Tool/Generator.cs @@ -27,8 +27,10 @@ public Generator(string assemblyPath, string output, CleanMethod cleanMethod, st _logger = logger; } - public Task Execute() + public Task Execute() { + var returnCode = 0; + MetadataLoadContext context; try { @@ -37,7 +39,7 @@ public Task Execute() catch (FileNotFoundException ex) { _logger.LogError(ex, ex.Message); - return Task.CompletedTask; + return Task.FromResult(1); } var assembly = context.LoadFromAssemblyPath(_assemblyPath); @@ -47,7 +49,7 @@ public Task Execute() if (!controllers.Any()) { _logger.LogError("Unable to find any controllers."); - return Task.CompletedTask; + return Task.FromResult(1); } var typesToLoad = new Dictionary>(); @@ -83,7 +85,7 @@ public Task Execute() if (!typesToLoad.Any()) { _logger.LogWarning("Unable to find any types to convert that matches the expected format."); - return Task.CompletedTask; + return Task.FromResult(1); } var contractor = GenerateContractor(typesToLoad); @@ -99,10 +101,10 @@ public Task Execute() } _logger.LogMessage("Writing types."); - contractor.Build(context, _cleanMethod == CleanMethod.Smart); + returnCode = contractor.Build(context, _cleanMethod == CleanMethod.Smart); _logger.LogMessage("Finished generating types."); - return Task.CompletedTask; + return Task.FromResult(returnCode); } private Contractor GenerateContractor(Dictionary> typesToLoad) diff --git a/TypeContractor.Tool/Program.cs b/TypeContractor.Tool/Program.cs index a355aa7..613c592 100644 --- a/TypeContractor.Tool/Program.cs +++ b/TypeContractor.Tool/Program.cs @@ -1,6 +1,8 @@ using System.CommandLine; using TypeContractor.Tool; +int returnCode = 0; + var rootCommand = new RootCommand("Tool for generating TypeScript definitions from C# code"); var assemblyOption = new Option("--assembly", "Path to the assembly to start with. Will be relative to the current directory"); var outputOption = new Option("--output", "Output path to write to. Will be relative to the current directory"); @@ -26,7 +28,9 @@ { var logger = new Logger(logLevel); var generator = new Generator(assemblyOption, output, clean, replacements, strip, customMaps, packsPath, logger); - await generator.Execute(); + returnCode = await generator.Execute(); }, assemblyOption, outputOption, cleanOption, replaceOptions, stripOptions, mapOptions, packsOptions, logLevelOptions); await rootCommand.InvokeAsync(args); + +return returnCode;