Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/scripts/check-package.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
assert len(packages) == 1, packages
with zipfile.ZipFile(packages[0]) as package:
names = package.namelist()
assert "lib/net10.0/DimensionalQuantities.dll" in names
assert "lib/netstandard2.0/DimensionalQuantities.dll" in names
assert "analyzers/dotnet/cs/DimensionalQuantities.Roslyn.dll" in names
assert not any("Microsoft.CodeAnalysis" in name for name in names)
specification = ET.fromstring(package.read("DimensionalQuantities.nuspec"))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A C# library for dimensional quantities, with generated type aliases, SI unit co

## Usage

Requires .NET 10 and C# 14. The `DimensionalQuantities` NuGet package includes both the runtime library and its source generator.
The runtime library targets .NET Standard 2.0. Consumers need a C# 14 compiler for the generated extension blocks. The `DimensionalQuantities` NuGet package includes both the runtime library and its source generator.

```csharp
Voltage voltage = Voltage.FromVolts(12);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup>
<ProjectReference Include="../DimensionalQuantities/DimensionalQuantities.csproj" />
<ProjectReference Include="../DimensionalQuantities.Roslyn/DimensionalQuantities.Roslyn.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<Analyzer Include="../DimensionalQuantities/bin/$(Configuration)/net10.0/DimensionalQuantities.dll" />
<Analyzer Include="../DimensionalQuantities/bin/$(Configuration)/netstandard2.0/DimensionalQuantities.dll" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="18.9.6" ExcludeAssets="runtime" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void PackagedGeneratorConvertsVoltageInConsumer()
{
string sourceProject = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "DimensionalQuantities", "DimensionalQuantities.csproj"));
FileInfo nupkg = PackLibrary(sourceProject, _scratch);
using PackageRepository repository = PackageRepository.Create(_scratch);
using PackageRepository repository = PackageRepository.Create(_scratch, new Uri("https://api.nuget.org/v3/index.json"));
repository.Package(nupkg, out Package package);

string consumerDirectory = Path.Combine(_scratch, "consumer");
Expand All @@ -39,9 +39,18 @@ public static double ConvertVoltage(double volts)
Voltage voltage = Voltage.FromVolts(volts);
return voltage.AsVolts();
}

public static double MultiplyVoltageCurrent(double volts, double amps)
{
Voltage voltage = Voltage.FromVolts(volts);
Current current = Current.FromAmps(amps);
Power power = voltage * current;
return power.AsWatts();
}
}
""");
ProjectCreator creator = ProjectCreator.Templates.SdkCsproj(path: consumerProject, targetFramework: "net10.0")
ProjectCreator creator = ProjectCreator.Templates.SdkCsproj(path: consumerProject, targetFramework: "netstandard2.0")
.Property("LangVersion", "14.0")
.ItemPackageReference(package)
.Save();

Expand All @@ -51,13 +60,16 @@ public static double ConvertVoltage(double volts)
Assert.True(built, $"Consumer build failed.{Environment.NewLine}{output.GetConsoleLog()}");
}

string assemblyFile = Path.Combine(consumerDirectory, "bin", "Debug", "net10.0", "Consumer.dll");
string assemblyFile = Path.Combine(consumerDirectory, "bin", "Debug", "netstandard2.0", "Consumer.dll");
Assembly assembly = Assembly.LoadFrom(assemblyFile);
Type consumerType = assembly.GetType("ConsumerProject.Consumer") ?? throw new InvalidOperationException("Consumer type was not built.");
MethodInfo convertVoltage = consumerType.GetMethod("ConvertVoltage") ?? throw new InvalidOperationException("Consumer Voltage conversion was not built.");
MethodInfo multiplyVoltageCurrent = consumerType.GetMethod("MultiplyVoltageCurrent") ?? throw new InvalidOperationException("Consumer arithmetic was not built.");
double result = Assert.IsType<double>(convertVoltage.Invoke(null, [12.5]));
double expected = 12.5;
Assert.Equal(expected, result);
double product = Assert.IsType<double>(multiplyVoltageCurrent.Invoke(null, [12.5, 2.0]));
Assert.Multiple(
() => Assert.Equal(12.5, result),
() => Assert.Equal(25.0, product));
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../DimensionalQuantities/DimensionalQuantities.csproj" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.9.0" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="DimensionalQuantities.Roslyn.Tests" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ private static IReadOnlyDictionary<string, Rational> CollectConversions(IReadOnl
throw new InvalidOperationException($"Unit {name} has conflicting SI scales.");
}

conversions.TryAdd(name, scaleExponent);
if (!conversions.ContainsKey(name))
{
conversions.Add(name, scaleExponent);
}
}

return conversions;
Expand Down Expand Up @@ -142,7 +145,7 @@ private static string RenderAccessor(string variableName, ConversionDefinition c
return $"public double {conversion.AccessorName}({parameters}) => {variableName}.AsArbitrarySiComponents(overallScaleExponent: {conversion.ScaleExponentExpression});";
}

private static string FormatExponent(Rational exponent) => double.CreateChecked(exponent).ToString("R", CultureInfo.InvariantCulture) + "d";
private static string FormatExponent(Rational exponent) => ((double)exponent).ToString("R", CultureInfo.InvariantCulture) + "d";

private static Type[] GetExponentTypes(Dimension dimension) =>
[
Expand Down
Loading
Loading