|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +UnitsNet is a .NET library that provides strongly-typed physical units and quantities, enabling safe and intuitive unit conversions in code. The library uses code generation from JSON definitions to create type-safe APIs for over 130 physical quantities. |
| 8 | + |
| 9 | +## Key Commands |
| 10 | + |
| 11 | +### Build and Test |
| 12 | +- **Build project**: `build.bat` or `dotnet build UnitsNet.slnx` |
| 13 | +- **Build all targets** (including nanoFramework): `build-all-targets.bat` |
| 14 | +- **Run tests**: `test.bat` or `dotnet test UnitsNet.slnx` |
| 15 | +- **Run single test**: `dotnet test UnitsNet.Tests --filter "FullyQualifiedName~TestClassName.TestMethodName"` |
| 16 | +- **Clean artifacts**: `clean.bat` |
| 17 | + |
| 18 | +### Code Generation |
| 19 | +- **Generate code from JSON definitions**: `generate-code.bat` or `dotnet run --project CodeGen` |
| 20 | + - Always run this after modifying any JSON files in `Common/UnitDefinitions/` |
| 21 | + - The generator reads 131 JSON definition files and creates C# code |
| 22 | + |
| 23 | +### Development Workflow |
| 24 | +1. Modify unit definitions in `Common/UnitDefinitions/*.json` |
| 25 | +2. Run `generate-code.bat` to regenerate C# code |
| 26 | +3. Run `build.bat` to compile and test |
| 27 | +4. Use `test.bat` for isolated test runs |
| 28 | + |
| 29 | +## Code Architecture |
| 30 | + |
| 31 | +### Project Structure |
| 32 | +- **UnitsNet/**: Main library with quantity types and units |
| 33 | + - `GeneratedCode/`: Auto-generated from JSON definitions (do not edit manually) |
| 34 | + - `CustomCode/`: Hand-written code extending generated types |
| 35 | +- **UnitsNet.Tests/**: Comprehensive test suite |
| 36 | +- **CodeGen/**: Code generation tool that creates C# from JSON definitions |
| 37 | +- **Common/UnitDefinitions/**: 131 JSON files defining physical quantities |
| 38 | +- **UnitsNet.NumberExtensions/**: Extension methods for numeric types |
| 39 | +- **UnitsNet.Serialization.*/**: JSON.NET and System.Text.Json serialization support |
| 40 | +- **UnitsNet.NanoFramework/**: Support for embedded .NET nanoFramework |
| 41 | + |
| 42 | +### Code Generation Process |
| 43 | +The project uses a sophisticated code generation system: |
| 44 | +1. JSON definitions in `Common/UnitDefinitions/` describe units, conversions, and localizations |
| 45 | +2. `CodeGen` project processes these to generate: |
| 46 | + - Quantity types (e.g., `Length`, `Mass`) |
| 47 | + - Unit enums (e.g., `LengthUnit`, `MassUnit`) |
| 48 | + - Conversion logic and unit abbreviations |
| 49 | +3. Generated code goes to `*/GeneratedCode/` folders |
| 50 | +4. Custom code in `*/CustomCode/` extends generated types |
| 51 | + |
| 52 | +### Key Classes and Patterns |
| 53 | +- **IQuantity**: Base interface for all quantity types |
| 54 | +- **Quantity**: Static class for dynamic quantity operations |
| 55 | +- **UnitConverter**: Handles conversions between units |
| 56 | +- **QuantityParser/UnitParser**: Parse strings to quantities/units |
| 57 | +- **UnitsNetSetup**: Configuration singleton |
| 58 | + |
| 59 | +### Adding or Modifying Units |
| 60 | +1. Edit or create JSON file in `Common/UnitDefinitions/` |
| 61 | +2. Follow conversion function guidelines: |
| 62 | + - Use multiplication for `FromUnitToBaseFunc` |
| 63 | + - Use division for `FromBaseToUnitFunc` |
| 64 | + - Prefer scientific notation (1e3, 1e-5) |
| 65 | +3. Run `generate-code.bat` |
| 66 | +4. Add tests if needed |
| 67 | + |
| 68 | +## Important Conventions |
| 69 | + |
| 70 | +### Coding Standards |
| 71 | +- Follow `.editorconfig` specifications |
| 72 | +- Use ReSharper settings in `UnitsNet.sln.DotSettings` |
| 73 | +- Treat warnings as errors (except obsolete warnings) |
| 74 | +- Add file headers to new files |
| 75 | + |
| 76 | +### Unit Definition Rules |
| 77 | +- Base units are chosen for each quantity (e.g., meter for Length) |
| 78 | +- All conversions go through the base unit |
| 79 | +- Use superscript in abbreviations: cm², m³ |
| 80 | +- Compound units format: N·m (dot), km/h (slash) |
| 81 | + |
| 82 | +### Testing |
| 83 | +- Test class naming: `<Type>Tests` |
| 84 | +- Test method naming: `<method>_<condition>_<result>` |
| 85 | +- Tests accept error margin of 1E-5 for most units |
| 86 | + |
| 87 | +## Special Considerations |
| 88 | + |
| 89 | +### .NET nanoFramework Support |
| 90 | +- Separate projects for nanoFramework compatibility |
| 91 | +- Use `build-all-targets.bat` to include nanoFramework builds |
| 92 | +- Limited feature set compared to full .NET |
| 93 | + |
| 94 | +### Performance |
| 95 | +- Conversion functions are compiled to delegates for performance |
| 96 | +- All conversions go through base units (potential for small errors) |
| 97 | +- Precision goal is 1E-5 for most units |
| 98 | + |
| 99 | +### Localization |
| 100 | +- Unit abbreviations support multiple cultures |
| 101 | +- JSON definitions include translations for various languages |
| 102 | +- Default culture: Thread.CurrentCulture, fallback to en-US |
| 103 | + |
| 104 | +## Common Tasks |
| 105 | + |
| 106 | +### Find specific quantity or unit implementation |
| 107 | +- Quantity types: `UnitsNet/GeneratedCode/Quantities/*.g.cs` |
| 108 | +- Unit enums: `UnitsNet/GeneratedCode/Units/*.g.cs` |
| 109 | +- Custom extensions: `UnitsNet/CustomCode/Quantities/*.extra.cs` |
| 110 | +- Unit definitions: `Common/UnitDefinitions/*.json` |
| 111 | + |
| 112 | +### Debug code generation |
| 113 | +- Generator entry: `CodeGen/Program.cs` |
| 114 | +- Generator logic: `CodeGen/Generators/` |
| 115 | +- Enable verbose logging: Check Serilog configuration in Program.cs |
| 116 | + |
| 117 | +### Run performance benchmarks |
| 118 | +- Execute: `dotnet run -c Release --project UnitsNet.Benchmark` |
| 119 | +- Results saved to `Artifacts/` folder |
0 commit comments