Skip to content

Commit fc6f06b

Browse files
luli0401ArdentLogicianangularsengojanpaolo
authored
Add UnitsNet.NumberExtensions nuget (#742)
* Add number extensions back to Units.NET as separate nuget UnitsNet.NumberExtensions * Change the build process, include the UnitsNet.NumberExtensions project and add the version bump scripts. * Create UnitsNet.NumberExtensions.Tests project. * Sync the Extension project version number with the main library, add a test project for extension methods, and improve the Extension project decription. Co-authored-by: Anthony Langlois <[email protected]> Co-authored-by: Andreas Gullberg Larsen <[email protected]> Co-authored-by: Jan Paolo Go <[email protected]>
1 parent 73ada82 commit fc6f06b

File tree

214 files changed

+14630
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+14630
-6
lines changed

Build/build-functions.psm1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function Start-Build([boolean] $IncludeWindowsRuntimeComponent = $false) {
6060
function Start-Tests {
6161
$projectPaths = @(
6262
"UnitsNet.Tests\UnitsNet.Tests.csproj",
63+
"UnitsNet.NumberExtensions.Tests\UnitsNet.NumberExtensions.Tests.csproj",
6364
"UnitsNet.Serialization.JsonNet.Tests\UnitsNet.Serialization.JsonNet.Tests.csproj",
6465
"UnitsNet.Serialization.JsonNet.CompatibilityTests\UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj"
6566
)
@@ -97,7 +98,8 @@ function Start-Tests {
9798
function Start-PackNugets {
9899
$projectPaths = @(
99100
"UnitsNet\UnitsNet.csproj",
100-
"UnitsNet.Serialization.JsonNet\UnitsNet.Serialization.JsonNet.csproj"
101+
"UnitsNet.Serialization.JsonNet\UnitsNet.Serialization.JsonNet.csproj",
102+
"UnitsNet.NumberExtensions\UnitsNet.NumberExtensions.csproj"
101103
)
102104

103105
write-host -foreground blue "Pack nugets...`n---"

Build/bump-version-UnitsNet-major.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@echo off
22
SET scriptdir=%~dp0
33
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.ps1 -bump major
4+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump major
45
if %errorlevel% neq 0 exit /b %errorlevel%

Build/bump-version-UnitsNet-minor.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@echo off
22
SET scriptdir=%~dp0
33
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.ps1 -bump minor
4+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump minor
45
if %errorlevel% neq 0 exit /b %errorlevel%

Build/bump-version-UnitsNet-patch.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@echo off
22
SET scriptdir=%~dp0
33
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.ps1 -bump patch
4+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump patch
45
if %errorlevel% neq 0 exit /b %errorlevel%
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@echo off
22
SET scriptdir=%~dp0
33
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.ps1 -bump suffix
4+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump suffix
45
if %errorlevel% neq 0 exit /b %errorlevel%
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
SET scriptdir=%~dp0
3+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump major
4+
if %errorlevel% neq 0 exit /b %errorlevel%
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
SET scriptdir=%~dp0
3+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump minor
4+
if %errorlevel% neq 0 exit /b %errorlevel%
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
SET scriptdir=%~dp0
3+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump patch
4+
if %errorlevel% neq 0 exit /b %errorlevel%
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
SET scriptdir=%~dp0
3+
call powershell -NoProfile %scriptdir%\set-version-UnitsNet.NumberExtensions.ps1 -bump suffix
4+
if %errorlevel% neq 0 exit /b %errorlevel%
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<# .SYNOPSIS
2+
Updates the version of all UnitsNet.Extensions projects.
3+
.DESCRIPTION
4+
Updates the <Version> property of the .csproj project files.
5+
.PARAMETER set
6+
Set new version
7+
.PARAMETER bump
8+
Bump major, minor, patch or semver suffix number. Only one can be specified at a time, and bumping one part will reset all the lesser parts.
9+
.EXAMPLE
10+
Set new version.
11+
-v 2.3.4-beta3: 1.0.0 => 2.3.4-beta3
12+
.EXAMPLE
13+
Bump the major, minor, patch or suffix part of the version.
14+
-b major: 1.2.3-alpha1 => 2.0.0
15+
-b minor: 1.2.3-alpha1 => 1.3.0
16+
-b patch: 1.2.3-alpha1 => 1.2.4
17+
-b suffix: 1.2.3-alpha => 1.2.3-alpha2
18+
-b suffix: 1.2.3-alpha2 => 1.2.3-alpha3
19+
-b suffix: 1.2.3-beta2 => 1.2.3-beta3
20+
-b suffix: 1.2.3-rc2 => 1.2.3-rc3
21+
22+
.NOTES
23+
Author: Lu Li
24+
Date: Jan 15, 2020
25+
Based on original work by Luis Rocha from: http://www.luisrocha.net/2009/11/setting-assembly-version-with-windows.html
26+
#>
27+
[CmdletBinding()]
28+
Param(
29+
[Parameter(Mandatory=$true, Position=0, ParameterSetName="set", HelpMessage="Set version string")]
30+
[Alias("version")]
31+
[string]$setVersion,
32+
33+
[Parameter(Mandatory=$true, Position=0, ParameterSetName="bump", HelpMessage="Bump one or more version parts")]
34+
[Alias("bump")]
35+
[ValidateSet('major','minor','patch','suffix')]
36+
[string]$bumpVersion
37+
)
38+
39+
function Help {
40+
"Sets the AssemblyVersion and AssemblyFileVersion of AssemblyInfo.cs files`n"
41+
".\SetVersion.ps1 [VersionNumber]`n"
42+
" [VersionNumber] The version number to set, for example: 1.1.9301.0"
43+
" If not provided, a version number will be generated.`n"
44+
}
45+
46+
# Import functions: Get-NewProjectVersion, Set-ProjectVersion, Invoke-CommitAndTagVersion
47+
Import-Module "$PSScriptRoot\set-version.psm1"
48+
49+
$root = Resolve-Path "$PSScriptRoot\.."
50+
$paramSet = $PsCmdlet.ParameterSetName
51+
$projFile = "$root\UnitsNet.NumberExtensions\UnitsNet.NumberExtensions.csproj"
52+
$versionFiles = @($projFile)
53+
$projectName = "UnitsNet.NumberExtensions"
54+
55+
# Use UnitsNet.Common.props version as base if bumping major/minor/patch
56+
$newVersion = Get-NewProjectVersion $projFile $paramSet $setVersion $bumpVersion
57+
58+
Set-ProjectVersion $projFile $newVersion
59+
Invoke-CommitAndTagVersion $projectName $versionFiles $newVersion
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using CodeGen.JsonTypes;
3+
4+
namespace CodeGen.Generators.UnitsNetGen
5+
{
6+
internal class NumberExtensionsGenerator : GeneratorBase
7+
{
8+
private readonly Unit[] _units;
9+
private readonly string _quantityName;
10+
11+
public NumberExtensionsGenerator(Quantity quantity)
12+
{
13+
if (quantity is null)
14+
throw new ArgumentNullException(nameof(quantity));
15+
16+
_units = quantity.Units;
17+
_quantityName = quantity.Name;
18+
}
19+
20+
public override string Generate()
21+
{
22+
Writer.WL(GeneratedFileHeader);
23+
24+
Writer.WL(
25+
$@"
26+
using System;
27+
28+
namespace UnitsNet.NumberExtensions.NumberTo{_quantityName}
29+
{{
30+
/// <summary>
31+
/// A number to {_quantityName} Extensions
32+
/// </summary>
33+
public static class NumberTo{_quantityName}Extensions
34+
{{");
35+
36+
foreach (var unit in _units)
37+
{
38+
Writer.WL(2, $@"
39+
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(UnitsNet.QuantityValue)"" />");
40+
41+
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
42+
43+
Writer.WL(2, $@"public static {_quantityName} {unit.PluralName}<T>(this T value) =>
44+
{_quantityName}.From{unit.PluralName}(Convert.ToDouble(value));
45+
");
46+
}
47+
48+
Writer.WL(1, @"}
49+
}");
50+
return Writer.ToString();
51+
}
52+
53+
private string GetObsoleteAttributeOrNull(string obsoleteText) =>
54+
string.IsNullOrWhiteSpace(obsoleteText) ?
55+
null :
56+
$"[System.Obsolete({obsoleteText})]";
57+
}
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using CodeGen.JsonTypes;
3+
4+
namespace CodeGen.Generators.UnitsNetGen
5+
{
6+
internal class NumberExtensionsTestClassGenerator : GeneratorBase
7+
{
8+
private readonly Unit[] _units;
9+
private readonly string _quantityName;
10+
11+
public NumberExtensionsTestClassGenerator(Quantity quantity)
12+
{
13+
if (quantity is null)
14+
throw new ArgumentNullException(nameof(quantity));
15+
16+
_units = quantity.Units;
17+
_quantityName = quantity.Name;
18+
}
19+
20+
21+
public override string Generate()
22+
{
23+
Writer.WL(GeneratedFileHeader);
24+
25+
Writer.WL(
26+
$@"
27+
using UnitsNet.NumberExtensions.NumberTo{_quantityName};
28+
using Xunit;
29+
30+
namespace UnitsNet.Tests
31+
{{
32+
public class NumberTo{_quantityName}ExtensionsTests
33+
{{");
34+
35+
foreach (var unit in _units)
36+
{
37+
Writer.WL(2, $@"
38+
[Fact]");
39+
40+
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
41+
42+
Writer.WL(2, $@"public void NumberTo{unit.PluralName}Test() =>
43+
Assert.Equal({_quantityName}.From{unit.PluralName}(2), 2.{unit.PluralName}());
44+
");
45+
}
46+
47+
Writer.WL(1, @"}
48+
}");
49+
return Writer.ToString();
50+
}
51+
52+
private string GetObsoleteAttributeOrNull(string obsoleteText) =>
53+
string.IsNullOrWhiteSpace(obsoleteText) ?
54+
null :
55+
$"[System.Obsolete({obsoleteText})]";
56+
}
57+
}

CodeGen/Generators/UnitsNetGenerator.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System.IO;
@@ -37,11 +37,15 @@ internal static class UnitsNetGenerator
3737
public static void Generate(string rootDir, Quantity[] quantities)
3838
{
3939
var outputDir = $"{rootDir}/UnitsNet/GeneratedCode";
40+
var extensionsOutputDir = $"{rootDir}/UnitsNet.NumberExtensions/GeneratedCode";
41+
var extensionsTestOutputDir = $"{rootDir}/UnitsNet.NumberExtensions.Tests/GeneratedCode";
4042
var testProjectDir = $"{rootDir}/UnitsNet.Tests";
4143

4244
// Ensure output directories exist
4345
Directory.CreateDirectory($"{outputDir}/Quantities");
4446
Directory.CreateDirectory($"{outputDir}/Units");
47+
Directory.CreateDirectory($"{extensionsOutputDir}");
48+
Directory.CreateDirectory($"{extensionsTestOutputDir}");
4549
Directory.CreateDirectory($"{testProjectDir}/GeneratedCode");
4650
Directory.CreateDirectory($"{testProjectDir}/GeneratedCode/TestsBase");
4751
Directory.CreateDirectory($"{testProjectDir}/GeneratedCode/QuantityTests");
@@ -51,6 +55,8 @@ public static void Generate(string rootDir, Quantity[] quantities)
5155
var sb = new StringBuilder($"{quantity.Name}:".PadRight(AlignPad));
5256
GenerateQuantity(sb, quantity, $"{outputDir}/Quantities/{quantity.Name}.g.cs");
5357
GenerateUnitType(sb, quantity, $"{outputDir}/Units/{quantity.Name}Unit.g.cs");
58+
GenerateNumberToExtensions(sb, quantity, $"{extensionsOutputDir}/NumberTo{quantity.Name}Extensions.g.cs");
59+
GenerateNumberToExtensionsTestClass(sb, quantity, $"{extensionsTestOutputDir}/NumberTo{quantity.Name}ExtensionsTest.g.cs");
5460

5561
// Example: CustomCode/Quantities/LengthTests inherits GeneratedCode/TestsBase/LengthTestsBase
5662
// This way when new units are added to the quantity JSON definition, we auto-generate the new
@@ -96,6 +102,20 @@ private static void GenerateQuantity(StringBuilder sb, Quantity quantity, string
96102
sb.Append("quantity(OK) ");
97103
}
98104

105+
private static void GenerateNumberToExtensions(StringBuilder sb, Quantity quantity, string filePath)
106+
{
107+
var content = new NumberExtensionsGenerator(quantity).Generate();
108+
File.WriteAllText(filePath, content, Encoding.UTF8);
109+
sb.Append("number extensions(OK) ");
110+
}
111+
112+
private static void GenerateNumberToExtensionsTestClass(StringBuilder sb, Quantity quantity, string filePath)
113+
{
114+
var content = new NumberExtensionsTestClassGenerator(quantity).Generate();
115+
File.WriteAllText(filePath, content, Encoding.UTF8);
116+
sb.Append("number extensions tests(OK) ");
117+
}
118+
99119
private static void GenerateUnitType(StringBuilder sb, Quantity quantity, string filePath)
100120
{
101121
var content = new UnitTypeGenerator(quantity).Generate();

UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToAccelerationExtensionsTest.g.cs

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)