Repository navigation
Generate code from the unit definitions as part of the build - #1742
Open
tmilnthorp wants to merge 4 commits into
Open
tmilnthorp wants to merge 4 commits into
tmilnthorp wants to merge 4 commits into
Conversation
Building the solution compiled CodeGen but never ran it, so the generated code only updated when someone ran generate-code.bat/.sh. CI ran CodeGen in a separate step, so local builds and CI could differ. Projects with generated code now opt in with GenerateUnitsNetCode=true. Before they build, Directory.Build.targets runs CodeGen through a target in CodeGen.csproj: - CodeGen runs at most once per build, shared by all projects and target frameworks, and only when the unit definitions or CodeGen changed. - Files that CodeGen adds or removes are picked up in the same build. - It is skipped in IDE design-time builds, or with SkipUnitsNetCodeGen=true. CodeGen now only writes files whose contents changed, so regenerating doesn't touch unchanged files. CI no longer runs CodeGen separately; build.ps1 instead fails on CI if the build changed any generated files, meaning the committed generated code was out of date. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Building the solution runs CodeGen when the unit definitions changed, so generate-code.bat and generate-code.sh are no longer needed. The docs now say to build instead, and mention `dotnet run --project CodeGen` for regenerating without building. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Building a project or solution that doesn't include CodeGen, such as a single test project or UnitsNet.Modular.slnx, failed because CodeGen was never restored. Restore it first when its assets file is missing. Packing or testing with --no-build ran CodeGen, which tried to build it with NoBuild=true. Nothing is compiled then, so skip generating code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1742 +/- ##
======================================
Coverage 98% 98%
======================================
Files 515 515
Lines 24092 24092
======================================
Hits 23692 23692
Misses 400 400
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Rather than each project with generated code opting in and running CodeGen before it compiles, CodeGen now runs itself after it builds, and UnitsNet has a build-order only reference to it. Every other project with generated code references UnitsNet, so they all build after the code is regenerated, and the GenerateUnitsNetCode opt-ins are removed. Restore follows the reference, and building without --no-build doesn't build references, so the explicit restore and NoBuild check are no longer needed. Projects with a GeneratedCode folder still pick up files that CodeGen added or removed in the same build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Collaborator
Author
|
Related: #1745 proposes building on this so applications can generate their own quantities with the same CodeGen, as part of their own build, while still using the 🤖 Generated with Claude Code |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Building the solution compiled CodeGen but never ran it. After editing a JSON file in
Common/UnitDefinitions, you had to remembergenerate-code.bat(or.sh), otherwise the build silently used stale generated code. CI ran CodeGen in a separate step, so local builds and CI could produce different results.Change
AfterTargets="Build"inCodeGen.csproj).UnitsNethas a build-order only reference to CodeGen (ReferenceOutputAssembly="false"), and every other project with generated code referencesUnitsNet, so they all build after the code is regenerated. No per-project opt-in is needed.GeneratedCodefolder pick up files that CodeGen added or removed in the same build, such as a new quantity (Directory.Build.targets).-p:SkipUnitsNetCodeGen=true.UnitsNet.Modular.slnxworks without CodeGen in the solution.--no-builddoesn't build references, so pack and test with--no-builddon't run it.build.ps1fails on CI if the build changed any generated files, meaning the committed generated code was out of date.generate-code.batandgenerate-code.share removed, and the docs say to build instead.dotnet run --project CodeGenstill works for regenerating without building.Contributor workflow becomes: edit the JSON, then
dotnet build(or build in the IDE).Testing
Length.jsonand building regenerates the code; reverting and building restores it.UnitsNet.dllin the first build.pack --no-build,test --no-build, and the Modular restore +build --no-restore.Not included
The header of generated files still says "generated by \generate-code.bat". It comes from the generator template, so changing it rewrites every generated file. I'd rather do that in a separate PR to keep this one reviewable.
🤖 Generated with Claude Code
https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG