Skip to content

Separate WindowsRuntimeComponent into its own source code and code generators #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 13, 2019
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 21 additions & 8 deletions Build/build-functions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ if ($msbuild) {
}

function Remove-ArtifactsDir {
write-host -foreground blue "Clean up...`n"
rm $artifactsDir -Recurse -ErrorAction Ignore
write-host -foreground blue "Clean up...END`n"
if (Test-Path $artifactsDir) {
write-host -foreground blue "Clean up...`n"
rm $artifactsDir -Recurse -Force -ErrorAction Stop
write-host -foreground blue "Clean up...END`n"
}
}

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

if (-not $IncludeWindowsRuntimeComponent) {
write-host -foreground yellow "Skipping WindowsRuntimeComponent code regen."
} else {
& "$root\UnitsNet.WindowsRuntimeComponent\Scripts\GenerateUnits.ps1"
if ($lastexitcode -ne 0) { exit 1 }
}

write-host -foreground blue "Generate code...END`n"
}

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

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

if ($skipUWP -eq $true)
if (-not $IncludeWindowsRuntimeComponent)
{
write-host -foreground yellow "Skipping WindowsRuntimeComponent build by user-specified flag."
write-host -foreground yellow "Skipping WindowsRuntimeComponent build."
}
else
{
Expand Down Expand Up @@ -98,8 +107,12 @@ function Start-PackNugets {
if ($lastexitcode -ne 0) { exit 1 }
}

write-host -foreground yellow "WindowsRuntimeComponent project not yet supported by dotnet CLI, using nuget.exe instead"
& $nuget pack "$root\UnitsNet.WindowsRuntimeComponent\UnitsNet.WindowsRuntimeComponent.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
if (-not $IncludeWindowsRuntimeComponent) {
write-host -foreground yellow "Skipping WindowsRuntimeComponent nuget pack."
} else {
write-host -foreground yellow "WindowsRuntimeComponent project not yet supported by dotnet CLI, using nuget.exe instead"
& $nuget pack "$root\UnitsNet.WindowsRuntimeComponent\UnitsNet.WindowsRuntimeComponent.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
}

write-host -foreground blue "Pack nugets...END`n"
}
Expand Down
4 changes: 2 additions & 2 deletions Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#>
[CmdletBinding()]
Param(
[switch] $skipUWP
[switch] $IncludeWindowsRuntimeComponent
)

remove-module build-functions -ErrorAction SilentlyContinue
Expand All @@ -29,7 +29,7 @@ import-module $PSScriptRoot\build-functions.psm1
try {
Remove-ArtifactsDir
Update-GeneratedCode
Start-Build $skipUWP
Start-Build -IncludeWindowsRuntimeComponent $IncludeWindowsRuntimeComponent
Start-Tests
Start-PackNugets
Compress-ArtifactsAsZip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal static PropertyInfo GetProperty(this Type type, string name)
#endif
}

// Ambiguous method conflict with GetMethods() name WindowsRuntimeComponent, so use GetDeclaredMethods() instead
internal static IEnumerable<MethodInfo> GetDeclaredMethods(this Type someType)
{
Type t = someType;
Expand Down
2 changes: 0 additions & 2 deletions UnitsNet.Tests/BaseDimensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ public void CheckBaseDimensionMultiplicationWithForceEqualsMassTimesAcceleration
Assert.True(calculatedDimensions == Force.BaseDimensions);
}

#if !WINDOWS_UWP
[Fact]
public void EqualityWorksAsExpectedWithOperatorOverloads()
{
Expand Down Expand Up @@ -692,7 +691,6 @@ public void CheckBaseDimensionMultiplicationWithForceEqualsMassTimesAcceleration
var calculatedDimensions = mass.Dimensions * acceleration.Dimensions;
Assert.True(calculatedDimensions == Force.BaseDimensions);
}
#endif

[Fact]
public void CheckToStringUsingMolarEntropy()
Expand Down
47 changes: 47 additions & 0 deletions UnitsNet.WindowsRuntimeComponent/AmbiguousUnitParseException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using UnitsNet.Units;

namespace UnitsNet
{
/// <summary>
/// Unable to parse because more than one unit of the given quantity type had this exact unit abbreviation.
/// Example: Length.Parse("1 pt") will throw <see cref="AmbiguousUnitParseException" />, because both
/// <see cref="LengthUnit.DtpPoint" /> and
/// <see cref="LengthUnit.PrinterPoint" /> have "pt" as their abbreviation.
/// </summary>
internal class AmbiguousUnitParseException : UnitsNetException
{
/// <inheritdoc />
public AmbiguousUnitParseException(string message) : base(message)
{
HResult = 2;
}

/// <inheritdoc />
public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException)
{
HResult = 2;
}
}
}
23 changes: 23 additions & 0 deletions UnitsNet.WindowsRuntimeComponent/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
[assembly: CLSCompliant(true)]
206 changes: 206 additions & 0 deletions UnitsNet.WindowsRuntimeComponent/BaseDimensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Text;
using System.Linq;

namespace UnitsNet
{
/// <summary>
/// Represents the base dimensions of a quantity.
/// </summary>
public sealed class BaseDimensions
{
/// <inheritdoc />
public BaseDimensions(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
{
Length = length;
Mass = mass;
Time = time;
Current = current;
Temperature = temperature;
Amount = amount;
LuminousIntensity = luminousIntensity;
}

/// <summary>
/// Checks if the dimensions represent a base quantity.
/// </summary>
/// <returns>True if the dimensions represent a base quantity, otherwise false.</returns>
public bool IsBaseQuantity()
{
var dimensionsArray = new[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity};
bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1;
return onlyOneEqualsOne;
}

/// <summary>
/// Checks if the dimensions represent a derived quantity.
/// </summary>
/// <returns>True if the dimensions represent a derived quantity, otherwise false.</returns>
public bool IsDerivedQuantity()
{
return !IsBaseQuantity() && !IsDimensionless();
}

/// <summary>
/// Checks if this base dimensions object represents a dimensionless quantity.
/// </summary>
/// <returns>True if this object represents a dimensionless quantity, otherwise false.</returns>
public bool IsDimensionless()
{
return Equals(this, Dimensionless);
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if(!(obj is BaseDimensions))
return false;

var other = (BaseDimensions)obj;

return Length == other.Length &&
Mass == other.Mass &&
Time == other.Time &&
Current == other.Current &&
Temperature == other.Temperature &&
Amount == other.Amount &&
LuminousIntensity == other.LuminousIntensity;
}

/// <inheritdoc />
public override int GetHashCode()
{
return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode();
}

/// <summary>
/// Get resulting dimensions after multiplying two dimensions, by performing addition of each dimension.
/// </summary>
/// <param name="right">Other dimensions.</param>
/// <returns>Resulting dimensions.</returns>
public BaseDimensions Multiply(BaseDimensions right)
{
if(right is null)
throw new ArgumentNullException(nameof(right));

return new BaseDimensions(
Length + right.Length,
Mass + right.Mass,
Time + right.Time,
Current + right.Current,
Temperature + right.Temperature,
Amount + right.Amount,
LuminousIntensity + right.LuminousIntensity);
}

/// <summary>
/// Get resulting dimensions after dividing two dimensions, by performing subtraction of each dimension.
/// </summary>
/// <param name="right">Other dimensions.</param>
/// <returns>Resulting dimensions.</returns>
public BaseDimensions Divide(BaseDimensions right)
{
if(right is null)
throw new ArgumentNullException(nameof(right));

return new BaseDimensions(
Length - right.Length,
Mass - right.Mass,
Time - right.Time,
Current - right.Current,
Temperature - right.Temperature,
Amount - right.Amount,
LuminousIntensity - right.LuminousIntensity);
}

/// <inheritdoc />
public override string ToString()
{
var sb = new StringBuilder();

AppendDimensionString(sb, "Length", Length);
AppendDimensionString(sb, "Mass", Mass);
AppendDimensionString(sb, "Time", Time);
AppendDimensionString(sb, "Current", Current);
AppendDimensionString(sb, "Temperature", Temperature);
AppendDimensionString(sb, "Amount", Amount);
AppendDimensionString(sb, "LuminousIntensity", LuminousIntensity);

return sb.ToString();
}

private static void AppendDimensionString(StringBuilder sb, string name, int value)
{
var absoluteValue = Math.Abs(value);

if(absoluteValue > 0)
{
sb.AppendFormat("[{0}]", name);

if(absoluteValue > 1)
sb.AppendFormat("^{0}", value);
}
}

/// <summary>
/// Gets the length dimensions (L).
/// </summary>
public int Length { get; }

/// <summary>
/// Gets the mass dimensions (M).
/// </summary>
public int Mass{ get; }

/// <summary>
/// Gets the time dimensions (T).
/// </summary>
public int Time{ get; }

/// <summary>
/// Gets the electric current dimensions (I).
/// </summary>
public int Current{ get; }

/// <summary>
/// Gets the temperature dimensions (Θ).
/// </summary>
public int Temperature{ get; }

/// <summary>
/// Gets the amount of substance dimensions (N).
/// </summary>
public int Amount{ get; }

/// <summary>
/// Gets the luminous intensity dimensions (J).
/// </summary>
public int LuminousIntensity{ get; }

/// <summary>
/// Represents a dimensionless (unitless) quantity.
/// </summary>
public static BaseDimensions Dimensionless { get; } = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
}
}
Loading