Skip to content

Commit

Permalink
feat(tool): Set exit code properly
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectlyNormal committed Dec 5, 2023
1 parent 7c5f3b9 commit 5c3d13a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions TypeContractor.Tool/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public Generator(string assemblyPath, string output, CleanMethod cleanMethod, st
_logger = logger;
}

public Task Execute()
public Task<int> Execute()
{
var returnCode = 0;

MetadataLoadContext context;
try
{
Expand All @@ -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);
Expand All @@ -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<Assembly, HashSet<Type>>();
Expand Down Expand Up @@ -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);
Expand All @@ -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<Assembly, HashSet<Type>> typesToLoad)
Expand Down
6 changes: 5 additions & 1 deletion TypeContractor.Tool/Program.cs
Original file line number Diff line number Diff line change
@@ -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<string>("--assembly", "Path to the assembly to start with. Will be relative to the current directory");
var outputOption = new Option<string>("--output", "Output path to write to. Will be relative to the current directory");
Expand All @@ -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;

0 comments on commit 5c3d13a

Please sign in to comment.