Skip to content

Commit c7a3b73

Browse files
committed
scripts: Include all functions globally
Fixes issue of calling script from another working dir, plus it seems like a waste to include the same files over and over. Had to rename functions in Include-GenerateLogarithmicCode.ps1 to avoid naming conflicts.
1 parent b7b7861 commit c7a3b73

5 files changed

+50
-56
lines changed

UnitsNet/Scripts/GenerateUnits.ps1

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function ToCamelCase($str)
77
function GetUnits($unitClass)
88
{
99
$units = @();
10-
10+
1111
foreach ($unit in $unitClass.Units)
1212
{
1313
$units += $unit;
@@ -47,78 +47,78 @@ function GetUnits($unitClass)
4747
"Tebi" { "Ti", "(1024d * 1024 * 1024 * 1024)"; break; }
4848
"Pebi" { "Pi", "(1024d * 1024 * 1024 * 1024 * 1024)"; break; }
4949
"Exbi" { "Ei", "(1024d * 1024 * 1024 * 1024 * 1024 * 1024)"; break; }
50-
}
51-
50+
}
51+
5252
$prefixAbbreviation = $prefixInfo[0];
5353
$prefixFactor = $prefixInfo[1];
5454

55-
$u = New-Object PsObject -Property @{
56-
SingularName=$prefix + $(ToCamelCase $unit.SingularName);
55+
$u = New-Object PsObject -Property @{
56+
SingularName=$prefix + $(ToCamelCase $unit.SingularName);
5757
PluralName=$prefix + $(ToCamelCase $unit.PluralName);
5858
FromUnitToBaseFunc="("+$unit.FromUnitToBaseFunc+") * $prefixFactor";
5959
FromBaseToUnitFunc="("+$unit.FromBaseToUnitFunc+") / $prefixFactor";
60-
Localization=$unit.Localization | % {
60+
Localization=$unit.Localization | % {
6161
$abbrev = $prefixAbbreviation + $_.Abbreviations[0];
6262
if ($_.AbbreviationsWithPrefixes) {
6363
$abbrev = $_.AbbreviationsWithPrefixes[$prefixIndex++];
6464
}
65-
65+
6666
New-Object PsObject -Property @{
6767
Culture=$_.Culture;
6868
Abbreviations= $abbrev;
6969
}};
7070
}
71-
71+
7272
$units += $u;
7373
}
7474
}
75-
75+
7676
foreach ($u in $units) {
7777
# Use decimal for internal calculations if base type is not double, such as for long or int.
7878
if ($unitClass.BaseType -ne "double") {
7979
$u.FromUnitToBaseFunc = $u.FromUnitToBaseFunc -replace "m", "d";
8080
$u.FromBaseToUnitFunc = $u.FromBaseToUnitFunc -replace "d", "m";
8181
}
82-
82+
8383
# Convert to/from double for other base types
8484
if ($unitClass.BaseType -eq "decimal") {
8585
$u.FromUnitToBaseFunc = "Convert.ToDecimal($($u.FromUnitToBaseFunc))";
8686
$u.FromBaseToUnitFunc = "Convert.ToDouble($($u.FromBaseToUnitFunc))";
87-
} else {
87+
} else {
8888
if ($unitClass.BaseType -eq "long") {
8989
$u.FromUnitToBaseFunc = "Convert.ToInt64($($u.FromUnitToBaseFunc))";
9090
$u.FromBaseToUnitFunc = "Convert.ToDouble($($u.FromBaseToUnitFunc))";
9191
}
9292
}
9393
}
94-
94+
9595
return $units | sort SingularName;
9696
}
9797

9898
function GenerateUnitClass($unitClass)
9999
{
100-
Write-Host "Generate unit for: " + $unitClass.Name;
101-
100+
Write-Host "Generate unit for: " + $unitClass.Name;
101+
102102
$outFileName = "$PSScriptRoot/../GeneratedCode/UnitClasses/$($unitClass.Name).g.cs";
103103
GenerateUnitClassSourceCode $unitClass | Out-File -Encoding "UTF8" $outFileName
104104
}
105105

106106
function GenerateUnitTestBaseClass($unitClass)
107107
{
108-
Write-Host "Generate test base for: " + $unitClass.Name;
109-
108+
Write-Host "Generate test base for: " + $unitClass.Name;
109+
110110
$outFileName = "$PSScriptRoot/../../UnitsNet.Tests/GeneratedCode/$($unitClass.Name)TestsBase.g.cs";
111111
GenerateUnitTestBaseClassSourceCode $unitClass | Out-File -Encoding "UTF8" $outFileName
112112
}
113113

114114
function GenerateUnitTestClassIfNotExists($unitClass)
115115
{
116116
$outFileName = "$PSScriptRoot/../../UnitsNet.Tests/CustomCode/$($unitClass.Name)Tests.cs";
117-
if (Test-Path $outFileName)
117+
if (Test-Path $outFileName)
118118
{
119119
return;
120-
}
121-
else
120+
}
121+
else
122122
{
123123
Write-Host "Generate test placeholder for: " + $unitClass.Name;
124124
GenerateUnitTestPlaceholderSourceCode $unitClass | Out-File -Encoding "UTF8" $outFileName
@@ -131,9 +131,9 @@ function GenerateUnitEnum($unitClass)
131131

132132
$outDir = "$PSScriptRoot/../GeneratedCode/Enums";
133133
$outFileName = "$outDir/$($unitClass.Name)Unit.g.cs";
134-
134+
135135
New-Item -ItemType Directory -Force -Path $outDir; # Make sure directory exists
136-
136+
137137
$result = GenerateUnitEnumSourceCode $unitClass;
138138
$result | Out-File -Encoding "UTF8" $outFileName;
139139
}
@@ -146,12 +146,14 @@ function GenerateUnitSystemDefault($unitClasses)
146146
$outFileName = "$outDir/UnitSystem.Default.g.cs";
147147

148148
New-Item -ItemType Directory -Force -Path $outDir; # Make sure directory exists
149-
149+
150150
$result = GenerateUnitSystemDefaultSourceCode $unitClasses;
151151
$result | Out-File -Encoding "UTF8" $outFileName;
152152
}
153153

154154
# Load external generator functions with same name as file
155+
. "$PSScriptRoot/Include-GenerateTemplates.ps1"
156+
. "$PSScriptRoot/Include-GenerateLogarithmicCode.ps1"
155157
. "$PSScriptRoot/Include-GenerateUnitSystemDefaultSourceCode.ps1"
156158
. "$PSScriptRoot/Include-GenerateUnitClassSourceCode.ps1"
157159
. "$PSScriptRoot/Include-GenerateUnitEnumSourceCode.ps1"
@@ -177,15 +179,15 @@ get-childitem -path $templatesDir -filter "*.json" | % {
177179
if (!$unitClass.LogarithmicScalingFactor) {
178180
$unitClass | Add-Member LogarithmicScalingFactor "1"
179181
}
180-
182+
181183
# Expand unit prefixes into units
182184
$unitClass.Units = GetUnits $unitClass;
183-
185+
184186
GenerateUnitClass $unitClass
185187
GenerateUnitEnum $unitClass
186188
GenerateUnitTestBaseClass $unitClass
187189
GenerateUnitTestClassIfNotExists $unitClass
188-
190+
189191
$unitClasses += $unitClass;
190192
}
191193

UnitsNet/Scripts/Include-GenerateLogarithmicCode.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ The data type of the backing field used to store the unit's value.
1414
.PARAMETER scalingFactor
1515
The scaling factor used in logarithmic calculations. In most cases this is equal to 1.
1616
#>
17-
function GenerateUnitClassSourceCode([string]$className, [string]$baseUnitFieldName, [string]$baseType, [int]$scalingFactor)
17+
function GenerateLogarithmicArithmeticOperators([string]$className, [string]$baseUnitFieldName, [string]$baseType, [int]$scalingFactor)
1818
{
1919
# Most logarithmic operators need a simple scaling factor of 10. However, certain units such as voltage ratio need to
2020
# use 20 instead of 10.
2121
$x = 10 * $scalingFactor;
22-
22+
2323
@"
2424
2525
#region Logarithmic Arithmetic Operators
@@ -38,7 +38,7 @@ function GenerateUnitClassSourceCode([string]$className, [string]$baseUnitFieldN
3838
3939
public static $className operator -($className left, $className right)
4040
{
41-
// Logarithmic subtraction
41+
// Logarithmic subtraction
4242
// Formula: $x*log10(10^(x/$x) - 10^(y/$x))
4343
return new $className($x*Math.Log10(Math.Pow(10, left.$baseUnitFieldName/$x) - Math.Pow(10, right.$baseUnitFieldName/$x)));
4444
}
@@ -85,7 +85,7 @@ The plural name of the backing field used to store the unit's value.
8585
.PARAMETER unit
8686
The actual unit type.
8787
#>
88-
function GenerateTestBaseClassSourceCode([string]$className, [string]$baseUnitPluralName, $unit)
88+
function GenerateLogarithmicTestBaseClassSourceCode([string]$className, [string]$baseUnitPluralName, $unit)
8989
{
9090
@"
9191
[Test]
@@ -100,9 +100,9 @@ function GenerateTestBaseClassSourceCode([string]$className, [string]$baseUnitPl
100100
Assert.AreEqual(35, (v/5).$baseUnitPluralName, $($unit.PluralName)Tolerance);
101101
Assert.AreEqual(35, v/$className.From$baseUnitPluralName(5), $($unit.PluralName)Tolerance);
102102
}
103-
103+
104104
protected abstract void AssertLogarithmicAddition();
105-
105+
106106
protected abstract void AssertLogarithmicSubtraction();
107107
"@;
108108
}

UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
. ".\Include-GenerateTemplates.ps1"
2-
31
function GenerateUnitClassSourceCode($unitClass)
42
{
53
$className = $unitClass.Name;
@@ -15,17 +13,17 @@ function GenerateUnitClassSourceCode($unitClass)
1513
@"
1614
// Copyright © 2007 by Initial Force AS. All rights reserved.
1715
// https://github.com/anjdreas/UnitsNet
18-
//
16+
//
1917
// Permission is hereby granted, free of charge, to any person obtaining a copy
2018
// of this software and associated documentation files (the "Software"), to deal
2119
// in the Software without restriction, including without limitation the rights
2220
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2321
// copies of the Software, and to permit persons to whom the Software is
2422
// furnished to do so, subject to the following conditions:
25-
//
23+
//
2624
// The above copyright notice and this permission notice shall be included in
2725
// all copies or substantial portions of the Software.
28-
//
26+
//
2927
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3028
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3129
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -89,7 +87,7 @@ namespace UnitsNet
8987
9088
#endregion
9189
92-
#region Static
90+
#region Static
9391
9492
public static $className Zero
9593
{
@@ -187,10 +185,8 @@ namespace UnitsNet
187185
188186
#endregion
189187
"@; if ($unitClass.Logarithmic -eq $true) {
190-
# Dot into the script to load its functions into the global scope so we can access them.
191-
. .\Include-GenerateLogarithmicCode.ps1;
192188
# Call another script function to generate logarithm-specific arithmetic operator code.
193-
GenerateUnitClassSourceCode -className $className -baseUnitFieldName $baseUnitFieldName -baseType $baseType -scalingFactor $unitClass.LogarithmicScalingFactor
189+
GenerateLogarithmicArithmeticOperators -className $className -baseUnitFieldName $baseUnitFieldName -baseType $baseType -scalingFactor $unitClass.LogarithmicScalingFactor
194190
}
195191
else {@"
196192
@@ -333,11 +329,11 @@ namespace UnitsNet
333329
/// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
334330
/// <exception cref="ArgumentException">
335331
/// Expected string to have one or two pairs of quantity and unit in the format
336-
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
332+
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
337333
/// </exception>
338334
/// <exception cref="AmbiguousUnitParseException">
339335
/// More than one unit is represented by the specified unit abbreviation.
340-
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
336+
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
341337
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
342338
/// </exception>
343339
/// <exception cref="UnitsNetException">

UnitsNet/Scripts/Include-GenerateUnitEnumSourceCode.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
. ".\Include-GenerateTemplates.ps1"
2-
31
function GenerateUnitEnumSourceCode($unitClass) {
42
$className = $unitClass.Name;
53
$units = $unitClass.Units;
64
$unitEnumName = "$($className)Unit";
75
@"
86
// Copyright © 2007 by Initial Force AS. All rights reserved.
97
// https://github.com/anjdreas/UnitsNet
10-
//
8+
//
119
// Permission is hereby granted, free of charge, to any person obtaining a copy
1210
// of this software and associated documentation files (the "Software"), to deal
1311
// in the Software without restriction, including without limitation the rights
1412
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1513
// copies of the Software, and to permit persons to whom the Software is
1614
// furnished to do so, subject to the following conditions:
17-
//
15+
//
1816
// The above copyright notice and this permission notice shall be included in
1917
// all copies or substantial portions of the Software.
20-
//
18+
//
2119
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2220
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2321
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -36,7 +34,7 @@ namespace UnitsNet.Units
3634
$obsoleteAttribute = GetObsoleteAttribute($unit);
3735

3836
if ($unit.XmlDocSummary) {@"
39-
37+
4038
/// <summary>
4139
/// $($unit.XmlDocSummary)
4240
/// </summary>

UnitsNet/Scripts/Include-GenerateUnitTestBaseClassSourceCode.ps1

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ function GenerateUnitTestBaseClassSourceCode($unitClass)
1111
@"
1212
// Copyright © 2007 by Initial Force AS. All rights reserved.
1313
// https://github.com/anjdreas/UnitsNet
14-
//
14+
//
1515
// Permission is hereby granted, free of charge, to any person obtaining a copy
1616
// of this software and associated documentation files (the "Software"), to deal
1717
// in the Software without restriction, including without limitation the rights
1818
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1919
// copies of the Software, and to permit persons to whom the Software is
2020
// furnished to do so, subject to the following conditions:
21-
//
21+
//
2222
// The above copyright notice and this permission notice shall be included in
2323
// all copies or substantial portions of the Software.
24-
//
24+
//
2525
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2626
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2727
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -93,10 +93,8 @@ namespace UnitsNet.Tests
9393
}
9494
9595
"@; if ($unitClass.Logarithmic -eq $true) {
96-
# Dot into the script to load its functions into the global scope so we can access them.
97-
. .\Include-GenerateLogarithmicCode.ps1;
9896
# Call another script function to generate logarithm-specific arithmetic operator test code.
99-
GenerateTestBaseClassSourceCode -className $className -baseUnitPluralName $baseUnitPluralName -unit $unit
97+
GenerateLogarithmicTestBaseClassSourceCode -className $className -baseUnitPluralName $baseUnitPluralName -unit $unit
10098
}
10199
else {@"
102100
[Test]
@@ -151,7 +149,7 @@ namespace UnitsNet.Tests
151149
[Test]
152150
[ExpectedException(typeof(ArgumentNullException))]
153151
public void CompareToThrowsOnNull()
154-
{
152+
{
155153
$className $baseUnitVariableName = $className.From$baseUnitPluralName(1);
156154
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
157155
$baseUnitVariableName.CompareTo(null);
@@ -165,7 +163,7 @@ namespace UnitsNet.Tests
165163
$className b = $className.From$baseUnitPluralName(2);
166164
167165
// ReSharper disable EqualExpressionComparison
168-
Assert.True(a == a);
166+
Assert.True(a == a);
169167
Assert.True(a != b);
170168
171169
Assert.False(a == b);

0 commit comments

Comments
 (0)