Skip to content

Commit 21eca5f

Browse files
committed
Merge remote-tracking branch 'origin/master' into conversions
# Conflicts: # UnitsNet/GeneratedCode/Quantities/ElectricConductivity.g.cs # UnitsNet/GeneratedCode/Quantities/Length.g.cs # UnitsNet/GeneratedCode/Quantities/Molarity.g.cs # UnitsNet/GeneratedCode/Quantities/Power.g.cs # UnitsNet/GeneratedCode/Quantities/Pressure.g.cs # UnitsNet/GeneratedCode/Quantities/SpecificEnergy.g.cs # UnitsNet/GeneratedCode/Quantities/Speed.g.cs # UnitsNet/GeneratedCode/Quantities/ThermalResistance.g.cs
2 parents e64fddc + ee058dc commit 21eca5f

File tree

845 files changed

+71280
-3873
lines changed

Some content is hidden

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

845 files changed

+71280
-3873
lines changed

.editorconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ root = true
88
[*]
99
indent_style = space
1010
trim_trailing_whitespace = true
11-
trim_trailing_white_space_on_save = true
1211
# (Please don't specify an indent_size here; that has too many unintended consequences.)
1312

1413
# Code files
@@ -66,7 +65,7 @@ dotnet_style_explicit_tuple_names = true:suggestion
6665
# Prefer "var" everywhere
6766
csharp_style_var_for_built_in_types = true:suggestion
6867
csharp_style_var_when_type_is_apparent = true:suggestion
69-
csharp_style_var_elsewhere = true:suggestion
68+
csharp_style_var_elsewhere = false:suggestion
7069

7170
# Prefer method-like constructs to have a block body
7271
csharp_style_expression_bodied_methods = false:none

.github/workflows/codeql-analysis.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ master ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ master ]
20+
schedule:
21+
- cron: '34 11 * * 2'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'csharp' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v2
42+
43+
# .NET 6 is used in CodeGen project.
44+
- uses: actions/setup-dotnet@v1
45+
with:
46+
dotnet-version: '6.0.x'
47+
48+
# Initializes the CodeQL tools for scanning.
49+
- name: Initialize CodeQL
50+
uses: github/codeql-action/init@v1
51+
with:
52+
languages: ${{ matrix.language }}
53+
# If you wish to specify custom queries, you can do so here or in a config file.
54+
# By default, queries listed here will override any specified in a config file.
55+
# Prefix the list here with "+" to use these queries and those in the config file.
56+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
57+
58+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
59+
# If this step fails, then you should remove it and run the build manually (see below)
60+
- name: Autobuild
61+
uses: github/codeql-action/autobuild@v1
62+
63+
# ℹ️ Command-line programs to run using the OS shell.
64+
# 📚 https://git.io/JvXDl
65+
66+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
67+
# and modify them (or add more) to build your code if your project
68+
# uses a compiled language
69+
70+
#- run: |
71+
# make bootstrap
72+
# make release
73+
74+
- name: Perform CodeQL Analysis
75+
uses: github/codeql-action/analyze@v1

Build/build-pack-nano-nugets.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ function Invoke-Build-NanoNugets {
1919
& $nuget pack "$root\UnitsNet.NanoFramework\GeneratedCode\VolumeConcentration\UnitsNet.NanoFramework.VolumeConcentration.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
2020
& $nuget pack "$root\UnitsNet.NanoFramework\GeneratedCode\Power\UnitsNet.NanoFramework.Power.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
2121
& $nuget pack "$root\UnitsNet.NanoFramework\GeneratedCode\Density\UnitsNet.NanoFramework.Density.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
22+
& $nuget pack "$root\UnitsNet.NanoFramework\GeneratedCode\Mass\UnitsNet.NanoFramework.Mass.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
23+
& $nuget pack "$root\UnitsNet.NanoFramework\GeneratedCode\MassConcentration\UnitsNet.NanoFramework.MassConcentration.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
24+
& $nuget pack "$root\UnitsNet.NanoFramework\GeneratedCode\Angle\UnitsNet.NanoFramework.Angle.nuspec" -Verbosity detailed -OutputDirectory "$nugetOutDir"
2225

2326
}
2427

Build/init.ps1

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,59 @@ if (!(Test-Path "$msbuildPath/nanoFramework")) {
2323
Write-Host "Installing .NET nanoFramework VS extension..."
2424

2525
[System.Net.WebClient]$webClient = New-Object System.Net.WebClient
26-
$webClient.UseDefaultCredentials = $true
26+
$webClient.Headers.Add("User-Agent", "request")
27+
$webClient.Headers.Add("Accept", "application/vnd.github.v3+json")
2728

28-
$vsixFeedXml = Join-Path -Path $tempDir -ChildPath "vs-extension-feed.xml"
29-
$webClient.DownloadFile("http://vsixgallery.com/feed/author/nanoframework", $vsixFeedXml)
30-
[xml]$feedDetails = Get-Content $vsixFeedXml
29+
$releaseList = $webClient.DownloadString('https://api.github.com/repos/nanoframework/nf-Visual-Studio-extension/tags')
3130

32-
foreach ($entry in $feedDetails.feed.entry)
31+
if($releaseList -match '"(?<VS2022_version>v2022\.\d+\.\d+\.\d+)')
3332
{
34-
if($entry.id -eq '455f2be5-bb07-451e-b351-a9faf3018dc9')
35-
{
36-
$extensionUrl = $entry.content.src
37-
$extensionVersion = $entry.Vsix.Version
38-
break
39-
}
33+
$vs2022Tag = $Matches.VS2022_version
4034
}
4135

36+
if($releaseList -match '"(?<VS2019_version>v2019\.\d+\.\d+\.\d+)')
37+
{
38+
$vs2019Tag = $Matches.VS2019_version
39+
}
40+
41+
# Find which VS version is installed
42+
$VsWherePath = "${env:PROGRAMFILES(X86)}\Microsoft Visual Studio\Installer\vswhere.exe"
43+
44+
Write-Output "VsWherePath is: $VsWherePath"
45+
46+
$VsInstance = $(&$VSWherePath -latest -property displayName)
47+
48+
Write-Output "Latest VS is: $VsInstance"
49+
50+
# Get extension details according to VS version, starting from VS2022 down to VS2019
51+
if($vsInstance.Contains('2022'))
52+
{
53+
$extensionUrl = "https://github.com/nanoframework/nf-Visual-Studio-extension/releases/download/$vs2022Tag/nanoFramework.Tools.VS2022.Extension.vsix"
54+
$vsixPath = Join-Path $tempDir "nanoFramework.Tools.VS2022.Extension.zip"
55+
$extensionVersion = $vs2022Tag
56+
}
57+
elseif($vsInstance.Contains('2019'))
58+
{
59+
$extensionUrl = "https://github.com/nanoframework/nf-Visual-Studio-extension/releases/download/$vs2019Tag/nanoFramework.Tools.VS2019.Extension.vsix"
60+
$vsixPath = Join-Path $tempDir "nanoFramework.Tools.VS2019.Extension.zip"
61+
$extensionVersion = $vs2019Tag
62+
}
63+
64+
Write-Output "Downloading visx..." -NoNewline
65+
66+
# download VS extension
67+
Write-Debug "Download VSIX file from $extensionUrl to $vsixPath"
68+
$webClient.DownloadFile($extensionUrl, $vsixPath)
69+
70+
$outputPath = "$tempDir\nf-extension"
71+
4272
$vsixPath = Join-Path -Path $tempDir -ChildPath "nf-extension.zip"
4373
$webClient.DownloadFile($extensionUrl,$vsixPath)
44-
Expand-Archive -LiteralPath $vsixPath -DestinationPath $tempDir\nf-extension\ | Write-Host
74+
Expand-Archive -LiteralPath $vsixPath -DestinationPath $outputPath | Write-Host
4575

46-
Copy-Item -Path "$tempDir\nf-extension\`$MSBuild\nanoFramework" -Destination $msbuildPath -Recurse
76+
Copy-Item -Path "$outputPath\`$MSBuild\nanoFramework" -Destination $msbuildPath -Recurse
4777

48-
Write-Host "Installed VS extension v$extensionVersion"
78+
Write-Host "Installed VS extension $extensionVersion"
4979
}
5080

5181
# Cleanup

CodeGen/CodeGen.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
14-
<PackageReference Include="NuGet.Protocol" Version="5.9.1" />
15-
<PackageReference Include="Serilog" Version="2.10.0" />
16-
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
14+
<PackageReference Include="NuGet.Protocol" Version="6.2.1" />
15+
<PackageReference Include="Serilog" Version="2.11.0" />
16+
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
1717
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.2.0-alpha.19174.3" />
1818
</ItemGroup>
1919

CodeGen/Generators/NanoFrameworkGen/NuspecGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public override string Generate()
2525
<package xmlns=""http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"">
2626
<metadata>
2727
<id>UnitsNet.nanoFramework.{_quantity.Name}</id>
28-
<version>4.121.0</version>
28+
<version>4.141.0</version>
2929
<title>Units.NET {_quantity.Name} - nanoFramework</title>
3030
<authors>Andreas Gullberg Larsen,nanoFramework project contributors</authors>
3131
<owners>UnitsNet</owners>

CodeGen/Generators/NanoFrameworkGen/UnitTypeGenerator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CodeGen.Helpers;
2+
using CodeGen.Helpers.UnitEnumValueAllocation;
23
using CodeGen.JsonTypes;
34

45
namespace CodeGen.Generators.NanoFrameworkGen
@@ -7,11 +8,13 @@ namespace CodeGen.Generators.NanoFrameworkGen
78
internal class UnitTypeGenerator : GeneratorBase
89
{
910
private readonly Quantity _quantity;
11+
private readonly UnitEnumNameToValue _unitEnumNameToValue;
1012
private readonly string _unitEnumName;
1113

12-
public UnitTypeGenerator(Quantity quantity)
14+
public UnitTypeGenerator(Quantity quantity, UnitEnumNameToValue unitEnumNameToValue)
1315
{
1416
_quantity = quantity;
17+
_unitEnumNameToValue = unitEnumNameToValue;
1518
_unitEnumName = $"{quantity.Name}Unit";
1619
}
1720

@@ -48,7 +51,7 @@ public enum {_unitEnumName}
4851

4952
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
5053
Writer.WL($@"
51-
{unit.SingularName},");
54+
{unit.SingularName} = {_unitEnumNameToValue[unit.SingularName]},");
5255
}
5356

5457
Writer.WL($@"

CodeGen/Generators/NanoFrameworkGenerator.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.RegularExpressions;
77
using CodeGen.Generators.NanoFrameworkGen;
88
using CodeGen.Helpers;
9+
using CodeGen.Helpers.UnitEnumValueAllocation;
910
using CodeGen.JsonTypes;
1011
using NuGet.Common;
1112
using Serilog;
@@ -40,7 +41,8 @@ internal static class NanoFrameworkGenerator
4041
/// </summary>
4142
/// <param name="rootDir">The root directory</param>
4243
/// <param name="quantities">The quantities to create</param>
43-
public static void Generate(string rootDir, Quantity[] quantities)
44+
/// <param name="quantityNameToUnitEnumValues"></param>
45+
public static void Generate(string rootDir, Quantity[] quantities, QuantityNameToUnitEnumValues quantityNameToUnitEnumValues)
4446
{
4547
// get latest version of .NET nanoFramework mscorlib
4648
ILogger logger = NullLogger.Instance;
@@ -82,7 +84,9 @@ public static void Generate(string rootDir, Quantity[] quantities)
8284
versions.MscorlibNugetVersion,
8385
versions.MathNugetVersion);
8486

85-
GenerateUnitType(quantity, Path.Combine(outputUnits, $"{quantity.Name}Unit.g.cs"));
87+
UnitEnumNameToValue unitEnumValues = quantityNameToUnitEnumValues[quantity.Name];
88+
89+
GenerateUnitType(quantity, Path.Combine(outputUnits, $"{quantity.Name}Unit.g.cs"), unitEnumValues);
8690
GenerateQuantity(quantity, Path.Combine(outputQuantities, $"{quantity.Name}.g.cs"));
8791
GenerateProject(quantity, Path.Combine(projectPath, $"{quantity.Name}.nfproj"), versions);
8892

@@ -347,9 +351,9 @@ private static void GenerateProperties(string filePath, string version)
347351
Log.Information("✅ AssemblyInfo.cs (nanoFramework)");
348352
}
349353

350-
private static void GenerateUnitType(Quantity quantity, string filePath)
354+
private static void GenerateUnitType(Quantity quantity, string filePath, UnitEnumNameToValue unitEnumValues)
351355
{
352-
var content = new UnitTypeGenerator(quantity).Generate();
356+
var content = new UnitTypeGenerator(quantity, unitEnumValues).Generate();
353357
File.WriteAllText(filePath, content);
354358
}
355359

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
205205
Assert.Equal(""{_quantity.Name}"", quantityInfo.Name);
206206
Assert.Equal(QuantityType.{_quantity.Name}, quantityInfo.QuantityType);
207207
208-
var units = EnumUtils.GetEnumValues<{_unitEnumName}>().Except(new[] {{{_unitEnumName}.Undefined}}).ToArray();
208+
var units = EnumUtils.GetEnumValues<{_unitEnumName}>().Except(new[] {{{_unitEnumName}.Undefined}}).OrderBy(x => x.ToString()).ToArray();
209209
var unitNames = units.Select(x => x.ToString());
210210
211211
// Obsolete members
@@ -281,6 +281,84 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
281281
}}
282282
}}
283283
284+
[Fact]
285+
public void Parse()
286+
{{");
287+
foreach(var unit in _quantity.Units.Where(u => string.IsNullOrEmpty(u.ObsoleteText)))
288+
foreach(var localization in unit.Localization)
289+
foreach(var abbreviation in localization.Abbreviations)
290+
{
291+
Writer.WL($@"
292+
try
293+
{{
294+
var parsed = {_quantity.Name}.Parse(""1 {abbreviation}"", CultureInfo.GetCultureInfo(""{localization.Culture}""));
295+
AssertEx.EqualTolerance(1, parsed.{unit.PluralName}, {unit.PluralName}Tolerance);
296+
Assert.Equal({GetUnitFullName(unit)}, parsed.Unit);
297+
}} catch (AmbiguousUnitParseException) {{ /* Some units have the same abbreviations */ }}
298+
");
299+
}
300+
Writer.WL($@"
301+
}}
302+
303+
[Fact]
304+
public void TryParse()
305+
{{");
306+
foreach(var unit in _quantity.Units.Where(u => string.IsNullOrEmpty(u.ObsoleteText)))
307+
foreach(var localization in unit.Localization)
308+
foreach(var abbreviation in localization.Abbreviations)
309+
{
310+
// Skip units with ambiguous abbreviations, since there is no exception to describe this is why TryParse failed.
311+
if (IsAmbiguousAbbreviation(localization, abbreviation)) continue;
312+
313+
Writer.WL($@"
314+
{{
315+
Assert.True({_quantity.Name}.TryParse(""1 {abbreviation}"", CultureInfo.GetCultureInfo(""{localization.Culture}""), out var parsed));
316+
AssertEx.EqualTolerance(1, parsed.{unit.PluralName}, {unit.PluralName}Tolerance);
317+
Assert.Equal({GetUnitFullName(unit)}, parsed.Unit);
318+
}}
319+
");
320+
}
321+
Writer.WL($@"
322+
}}
323+
324+
[Fact]
325+
public void ParseUnit()
326+
{{");
327+
foreach(var unit in _quantity.Units.Where(u => string.IsNullOrEmpty(u.ObsoleteText)))
328+
foreach(var localization in unit.Localization)
329+
foreach(var abbreviation in localization.Abbreviations)
330+
{
331+
Writer.WL($@"
332+
try
333+
{{
334+
var parsedUnit = {_quantity.Name}.ParseUnit(""{abbreviation}"", CultureInfo.GetCultureInfo(""{localization.Culture}""));
335+
Assert.Equal({GetUnitFullName(unit)}, parsedUnit);
336+
}} catch (AmbiguousUnitParseException) {{ /* Some units have the same abbreviations */ }}
337+
");
338+
}
339+
Writer.WL($@"
340+
}}
341+
342+
[Fact]
343+
public void TryParseUnit()
344+
{{");
345+
foreach(var unit in _quantity.Units.Where(u => string.IsNullOrEmpty(u.ObsoleteText)))
346+
foreach(var localization in unit.Localization)
347+
foreach(var abbreviation in localization.Abbreviations)
348+
{
349+
// Skip units with ambiguous abbreviations, since there is no exception to describe this is why TryParse failed.
350+
if (IsAmbiguousAbbreviation(localization, abbreviation)) continue;
351+
352+
Writer.WL($@"
353+
{{
354+
Assert.True({_quantity.Name}.TryParseUnit(""{abbreviation}"", CultureInfo.GetCultureInfo(""{localization.Culture}""), out var parsedUnit));
355+
Assert.Equal({GetUnitFullName(unit)}, parsedUnit);
356+
}}
357+
");
358+
}
359+
Writer.WL($@"
360+
}}
361+
284362
[Theory]
285363
[MemberData(nameof(UnitTypes))]
286364
public void ToUnit({_unitEnumName} unit)
@@ -766,5 +844,12 @@ public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value)
766844
}}");
767845
return Writer.ToString();
768846
}
847+
848+
private bool IsAmbiguousAbbreviation(Localization localization, string abbreviation)
849+
{
850+
return _quantity.Units.Count(u =>
851+
u.Localization.SingleOrDefault(l => l.Culture == localization.Culture) is { } otherUnitLocalization &&
852+
otherUnitLocalization.Abbreviations.Contains(abbreviation, StringComparer.OrdinalIgnoreCase)) > 1;
853+
}
769854
}
770855
}

0 commit comments

Comments
 (0)