Skip to content

Commit

Permalink
feat(tool): Add support for configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectlyNormal committed Nov 17, 2024
1 parent 7cfce28 commit 2eed87d
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Handle optional segments in routes for API clients (#77)
- Added configuration file support (#90)

### Fixed

Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ Contractor.FromDefaultConfiguration(configuration => configuration
.SetOutputDirectory(Path.Combine(Directory.GetCurrentDirectory(), "api")));
```

## Configuration file

It's possible to create a configuration file and provide all the same options
via a file instead of command line arguments.

Create a file called `typecontractor.config` in the same or a parent directory
from where you run TypeContractor from. We use [dotnet-config] for parsing the
files (written in [TOML]), so it's possible to inherit from parent directories,
a user-wide configuration or a system-wide configuration. Or have a
repository-specific configuration that can be overridden inside the repo with
`typecontractor.config.user`. You can edit the file manually or install the
dotnet-config tool for viewing and changing configuration similar to how
`git config` works.

### Example configuration file

```toml
[typecontractor]
# backslashes must be escaped
assembly = "bin\\Debug\\net8.0\\MyCompany.SystemName.dll"
output = "api"
strip = "MyCompany"
replace = "MyCompany.Common:CommonFiles" # Options can repeat
replace = "ThirdParty.Http:Http"
root = "~/api"
generate-api-clients = true
build-zod-schemas = true
```

## Run manually

Get an instance of `Contractor` and call `contractor.Build();`
Expand Down Expand Up @@ -230,3 +259,6 @@ template is `TypeContractor/Templates/aurelia.hbs`.
* Improve method for finding AspNetCore framework DLLs
* Possible to provide a manual path, so not a priority
* Work with Hot Reload?

[dotnet-config]: https://dotnetconfig.github.io/dotnet-config/index.html
[TOML]: https://toml.io/en/
6 changes: 6 additions & 0 deletions TypeContractor.Tool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using DotNetConfig;
using System.CommandLine;
using System.CommandLine.Parsing;
using TypeContractor.Logger;
using TypeContractor.Tool;

var config = Config.Build("typecontractor.config");

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 Down Expand Up @@ -54,6 +57,9 @@
}
});

// Apply configuration from file, if any
rootCommand = rootCommand.WithConfigurableDefaults("typecontractor", config);

rootCommand.SetHandler(async (context) =>
{
var assemblyOptionValue = context.ParseResult.GetValueForOption(assemblyOption)!;
Expand Down
1 change: 1 addition & 0 deletions TypeContractor.Tool/TypeContractor.Tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DotNetConfig" Version="1.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 2eed87d

Please sign in to comment.