Skip to content

Commit f437a11

Browse files
authored
Merge pull request #607 from angularsen/separate-wrc
Separate WindowsRuntimeComponent into its own source code and code generators
2 parents 38a502c + 5a2d402 commit f437a11

File tree

388 files changed

+13253
-1473
lines changed

Some content is hidden

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

388 files changed

+13253
-1473
lines changed

Build/build-functions.psm1

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ if ($msbuild) {
1010
}
1111

1212
function Remove-ArtifactsDir {
13-
write-host -foreground blue "Clean up...`n"
14-
rm $artifactsDir -Recurse -ErrorAction Ignore
15-
write-host -foreground blue "Clean up...END`n"
13+
if (Test-Path $artifactsDir) {
14+
write-host -foreground blue "Clean up...`n"
15+
rm $artifactsDir -Recurse -Force -ErrorAction Stop
16+
write-host -foreground blue "Clean up...END`n"
17+
}
1618
}
1719

1820
function Update-GeneratedCode {
@@ -23,10 +25,17 @@ function Update-GeneratedCode {
2325
& "$root\UnitsNet\Scripts\GenerateUnits.ps1"
2426
if ($lastexitcode -ne 0) { exit 1 }
2527

28+
if (-not $IncludeWindowsRuntimeComponent) {
29+
write-host -foreground yellow "Skipping WindowsRuntimeComponent code regen."
30+
} else {
31+
& "$root\UnitsNet.WindowsRuntimeComponent\Scripts\GenerateUnits.ps1"
32+
if ($lastexitcode -ne 0) { exit 1 }
33+
}
34+
2635
write-host -foreground blue "Generate code...END`n"
2736
}
2837

29-
function Start-Build([boolean] $skipUWP = $false) {
38+
function Start-Build([boolean] $IncludeWindowsRuntimeComponent = $false) {
3039
write-host -foreground blue "Start-Build...`n---"
3140

3241
$fileLoggerArg = "/logger:FileLogger,Microsoft.Build;logfile=$testReportDir\UnitsNet.msbuild.log"
@@ -38,9 +47,9 @@ function Start-Build([boolean] $skipUWP = $false) {
3847
dotnet build --configuration Release "$root\UnitsNet.sln" $fileLoggerArg $appVeyorLoggerArg
3948
if ($lastexitcode -ne 0) { exit 1 }
4049

41-
if ($skipUWP -eq $true)
50+
if (-not $IncludeWindowsRuntimeComponent)
4251
{
43-
write-host -foreground yellow "Skipping WindowsRuntimeComponent build by user-specified flag."
52+
write-host -foreground yellow "Skipping WindowsRuntimeComponent build."
4453
}
4554
else
4655
{
@@ -98,8 +107,12 @@ function Start-PackNugets {
98107
if ($lastexitcode -ne 0) { exit 1 }
99108
}
100109

101-
write-host -foreground yellow "WindowsRuntimeComponent project not yet supported by dotnet CLI, using nuget.exe instead"
102-
& $nuget pack "$root\UnitsNet.WindowsRuntimeComponent\UnitsNet.WindowsRuntimeComponent.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
110+
if (-not $IncludeWindowsRuntimeComponent) {
111+
write-host -foreground yellow "Skipping WindowsRuntimeComponent nuget pack."
112+
} else {
113+
write-host -foreground yellow "WindowsRuntimeComponent project not yet supported by dotnet CLI, using nuget.exe instead"
114+
& $nuget pack "$root\UnitsNet.WindowsRuntimeComponent\UnitsNet.WindowsRuntimeComponent.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
115+
}
103116

104117
write-host -foreground blue "Pack nugets...END`n"
105118
}

Build/build.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#>
2121
[CmdletBinding()]
2222
Param(
23-
[switch] $skipUWP
23+
[switch] $IncludeWindowsRuntimeComponent
2424
)
2525

2626
remove-module build-functions -ErrorAction SilentlyContinue
@@ -29,7 +29,7 @@ import-module $PSScriptRoot\build-functions.psm1
2929
try {
3030
Remove-ArtifactsDir
3131
Update-GeneratedCode
32-
Start-Build $skipUWP
32+
Start-Build -IncludeWindowsRuntimeComponent $IncludeWindowsRuntimeComponent
3333
Start-Tests
3434
Start-PackNugets
3535
Compress-ArtifactsAsZip

UnitsNet.Serialization.JsonNet/Internal/ReflectionHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ internal static PropertyInfo GetProperty(this Type type, string name)
1919
#endif
2020
}
2121

22-
// Ambiguous method conflict with GetMethods() name WindowsRuntimeComponent, so use GetDeclaredMethods() instead
2322
internal static IEnumerable<MethodInfo> GetDeclaredMethods(this Type someType)
2423
{
2524
Type t = someType;

UnitsNet.Tests/BaseDimensionsTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ public void CheckBaseDimensionMultiplicationWithForceEqualsMassTimesAcceleration
410410
Assert.True(calculatedDimensions == Force.BaseDimensions);
411411
}
412412

413-
#if !WINDOWS_UWP
414413
[Fact]
415414
public void EqualityWorksAsExpectedWithOperatorOverloads()
416415
{
@@ -692,7 +691,6 @@ public void CheckBaseDimensionMultiplicationWithForceEqualsMassTimesAcceleration
692691
var calculatedDimensions = mass.Dimensions * acceleration.Dimensions;
693692
Assert.True(calculatedDimensions == Force.BaseDimensions);
694693
}
695-
#endif
696694

697695
[Fact]
698696
public void CheckToStringUsingMolarEntropy()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
using UnitsNet.Units;
24+
25+
namespace UnitsNet
26+
{
27+
/// <summary>
28+
/// Unable to parse because more than one unit of the given quantity type had this exact unit abbreviation.
29+
/// Example: Length.Parse("1 pt") will throw <see cref="AmbiguousUnitParseException" />, because both
30+
/// <see cref="LengthUnit.DtpPoint" /> and
31+
/// <see cref="LengthUnit.PrinterPoint" /> have "pt" as their abbreviation.
32+
/// </summary>
33+
internal class AmbiguousUnitParseException : UnitsNetException
34+
{
35+
/// <inheritdoc />
36+
public AmbiguousUnitParseException(string message) : base(message)
37+
{
38+
HResult = 2;
39+
}
40+
41+
/// <inheritdoc />
42+
public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException)
43+
{
44+
HResult = 2;
45+
}
46+
}
47+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
[assembly: CLSCompliant(true)]
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
using System.Text;
24+
using System.Linq;
25+
26+
namespace UnitsNet
27+
{
28+
/// <summary>
29+
/// Represents the base dimensions of a quantity.
30+
/// </summary>
31+
public sealed class BaseDimensions
32+
{
33+
/// <inheritdoc />
34+
public BaseDimensions(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
35+
{
36+
Length = length;
37+
Mass = mass;
38+
Time = time;
39+
Current = current;
40+
Temperature = temperature;
41+
Amount = amount;
42+
LuminousIntensity = luminousIntensity;
43+
}
44+
45+
/// <summary>
46+
/// Checks if the dimensions represent a base quantity.
47+
/// </summary>
48+
/// <returns>True if the dimensions represent a base quantity, otherwise false.</returns>
49+
public bool IsBaseQuantity()
50+
{
51+
var dimensionsArray = new[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity};
52+
bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1;
53+
return onlyOneEqualsOne;
54+
}
55+
56+
/// <summary>
57+
/// Checks if the dimensions represent a derived quantity.
58+
/// </summary>
59+
/// <returns>True if the dimensions represent a derived quantity, otherwise false.</returns>
60+
public bool IsDerivedQuantity()
61+
{
62+
return !IsBaseQuantity() && !IsDimensionless();
63+
}
64+
65+
/// <summary>
66+
/// Checks if this base dimensions object represents a dimensionless quantity.
67+
/// </summary>
68+
/// <returns>True if this object represents a dimensionless quantity, otherwise false.</returns>
69+
public bool IsDimensionless()
70+
{
71+
return Equals(this, Dimensionless);
72+
}
73+
74+
/// <inheritdoc />
75+
public override bool Equals(object obj)
76+
{
77+
if(!(obj is BaseDimensions))
78+
return false;
79+
80+
var other = (BaseDimensions)obj;
81+
82+
return Length == other.Length &&
83+
Mass == other.Mass &&
84+
Time == other.Time &&
85+
Current == other.Current &&
86+
Temperature == other.Temperature &&
87+
Amount == other.Amount &&
88+
LuminousIntensity == other.LuminousIntensity;
89+
}
90+
91+
/// <inheritdoc />
92+
public override int GetHashCode()
93+
{
94+
return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode();
95+
}
96+
97+
/// <summary>
98+
/// Get resulting dimensions after multiplying two dimensions, by performing addition of each dimension.
99+
/// </summary>
100+
/// <param name="right">Other dimensions.</param>
101+
/// <returns>Resulting dimensions.</returns>
102+
public BaseDimensions Multiply(BaseDimensions right)
103+
{
104+
if(right is null)
105+
throw new ArgumentNullException(nameof(right));
106+
107+
return new BaseDimensions(
108+
Length + right.Length,
109+
Mass + right.Mass,
110+
Time + right.Time,
111+
Current + right.Current,
112+
Temperature + right.Temperature,
113+
Amount + right.Amount,
114+
LuminousIntensity + right.LuminousIntensity);
115+
}
116+
117+
/// <summary>
118+
/// Get resulting dimensions after dividing two dimensions, by performing subtraction of each dimension.
119+
/// </summary>
120+
/// <param name="right">Other dimensions.</param>
121+
/// <returns>Resulting dimensions.</returns>
122+
public BaseDimensions Divide(BaseDimensions right)
123+
{
124+
if(right is null)
125+
throw new ArgumentNullException(nameof(right));
126+
127+
return new BaseDimensions(
128+
Length - right.Length,
129+
Mass - right.Mass,
130+
Time - right.Time,
131+
Current - right.Current,
132+
Temperature - right.Temperature,
133+
Amount - right.Amount,
134+
LuminousIntensity - right.LuminousIntensity);
135+
}
136+
137+
/// <inheritdoc />
138+
public override string ToString()
139+
{
140+
var sb = new StringBuilder();
141+
142+
AppendDimensionString(sb, "Length", Length);
143+
AppendDimensionString(sb, "Mass", Mass);
144+
AppendDimensionString(sb, "Time", Time);
145+
AppendDimensionString(sb, "Current", Current);
146+
AppendDimensionString(sb, "Temperature", Temperature);
147+
AppendDimensionString(sb, "Amount", Amount);
148+
AppendDimensionString(sb, "LuminousIntensity", LuminousIntensity);
149+
150+
return sb.ToString();
151+
}
152+
153+
private static void AppendDimensionString(StringBuilder sb, string name, int value)
154+
{
155+
var absoluteValue = Math.Abs(value);
156+
157+
if(absoluteValue > 0)
158+
{
159+
sb.AppendFormat("[{0}]", name);
160+
161+
if(absoluteValue > 1)
162+
sb.AppendFormat("^{0}", value);
163+
}
164+
}
165+
166+
/// <summary>
167+
/// Gets the length dimensions (L).
168+
/// </summary>
169+
public int Length { get; }
170+
171+
/// <summary>
172+
/// Gets the mass dimensions (M).
173+
/// </summary>
174+
public int Mass{ get; }
175+
176+
/// <summary>
177+
/// Gets the time dimensions (T).
178+
/// </summary>
179+
public int Time{ get; }
180+
181+
/// <summary>
182+
/// Gets the electric current dimensions (I).
183+
/// </summary>
184+
public int Current{ get; }
185+
186+
/// <summary>
187+
/// Gets the temperature dimensions (Θ).
188+
/// </summary>
189+
public int Temperature{ get; }
190+
191+
/// <summary>
192+
/// Gets the amount of substance dimensions (N).
193+
/// </summary>
194+
public int Amount{ get; }
195+
196+
/// <summary>
197+
/// Gets the luminous intensity dimensions (J).
198+
/// </summary>
199+
public int LuminousIntensity{ get; }
200+
201+
/// <summary>
202+
/// Represents a dimensionless (unitless) quantity.
203+
/// </summary>
204+
public static BaseDimensions Dimensionless { get; } = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
205+
}
206+
}

0 commit comments

Comments
 (0)