From 470f0b0d22045748762a325ada169ef568999f06 Mon Sep 17 00:00:00 2001 From: RzR Date: Tue, 16 Jan 2024 23:30:53 +0200 Subject: [PATCH] Add new extension and fix method modifier. Add excel column name generator `GetExcelColumnName`. Adjust method modifier for `GetDuplicates` --- build/pack-repo.ps1 | 267 ++++++++++++++++++ docs/CHANGELOG.md | 7 +- .../ArraysExtensions/EnumerableExtensions.cs | 2 +- .../DataTypeExtensions/IntExtensions.cs | 42 +++ .../DomainCommonExtensions.csproj | 6 +- src/shared/GeneralAssemblyInfo.cs | 6 +- src/tests/DataTypeTests/DateTimeTests.cs | 2 +- 7 files changed, 323 insertions(+), 9 deletions(-) create mode 100644 build/pack-repo.ps1 diff --git a/build/pack-repo.ps1 b/build/pack-repo.ps1 new file mode 100644 index 0000000..7544a5f --- /dev/null +++ b/build/pack-repo.ps1 @@ -0,0 +1,267 @@ +Param +( + [Parameter(Mandatory = $false)] + [string]$ownVersion, + [Parameter(Mandatory = $false)] + [bool]$runTest +); + +$assemblyPath = "..\src\shared\GeneralAssemblyInfo.cs"; +$defaultVersion = "1.0.0.0"; +$nugetPath = "../nuget"; +$data = ("..\src\DomainCommonExtensions\DomainCommonExtensions.csproj"); +$testExec = $false; + +<# + .SYNOPSIS + A brief description of the Get-CurrentAssemblyVersion function. + + .DESCRIPTION + Get current assembly version + + .EXAMPLE + PS C:\> Get-CurrentAssemblyVersion + + .NOTES + Additional information about the function. +#> +function Get-CurrentAssemblyVersion +{ + [OutputType([string])] + param () + + $assemblyInfo = (Get-Content $assemblyPath); + $asVersion = ($assemblyInfo -match 'AssemblyVersion\(".*"\)'); + $asVersion = $asVersion -split ('"'); + $asVersion = $asVersion[1]; + + return $asVersion; +} + +<# + .SYNOPSIS + A brief description of the Build-And-Pack-BuildPack function. + + .DESCRIPTION + Build project and pack as package (u pkg) + + .PARAMETER packVersion + Package/Build version + + .PARAMETER currentVersion + A description of the currentVersion parameter. + + .EXAMPLE + PS C:\> Build-And-Pack-BuildPack -packVersion 'Value1' + + .NOTES + Additional information about the function. +#> +function Set-BuildAndPack +{ + [CmdletBinding()] + [OutputType([bool])] + param + ( + [Parameter(Mandatory = $true)] + [string]$packVersion, + [string]$currentVersion + ) + + try + { + Write-Host "Project restore '$($_)'!" -ForegroundColor Green; + dotnet restore $($_); + + Write-Host "Build in Release '$($_)'!" -ForegroundColor Green; + $buildResult = dotnet build $($_) --source https://api.nuget.org/v3/index.json -c Release /p:AssemblyVersion=$packVersion /p:AssemblyFileVersion=$packVersion /p:AssemblyInformationalVersion=$packVersion; + if ($LASTEXITCODE -ne 0) + { + Set-VersionAssembly -packVersion $currentVersion; + Write-Host $buildResult; + + return $false; + } + + Write-Host "Pack in Release '$($_)'!" -ForegroundColor Green; + $packResult = dotnet pack $($_) -p:PackageVersion=$packVersion --no-build -c Release --output $nugetPath; + if ($LASTEXITCODE -ne 0) + { + Set-VersionAssembly -packVersion $currentVersion; + Write-Host $buildResult; + + return $false; + } + + return $true; + } + catch + { + Write-Host -foregroundcolor Red "An error occurred: $_" + + return $false; + } +} + +<# + .SYNOPSIS + A brief description of the Get-TimeStamp function. + + .DESCRIPTION + Get time stamp version + + .EXAMPLE + PS C:\> Get-TimeStamp + + .NOTES + Additional information about the function. +#> +function Get-TimeStamp +{ + [CmdletBinding()] + [OutputType([int])] + param () + + $current = [System.DateTime]::Now; + $end = [System.DateTime]::Now.Date; + $diff = (New-TimeSpan -Start $current -End $end).TotalSeconds / 10; + $timeSec = If ($diff -le 0) { $diff * -1 } + Else { $diff }; + + return [int]$timeSec; +} + +<# + .SYNOPSIS + A brief description of the Set-VersionAssembly function. + + .DESCRIPTION + Set current version in assembly file + + .PARAMETER packVersion + A description of the packVersion parameter. + + .EXAMPLE + PS C:\> Set-VersionAssembly -packVersion 'Value1' + + .NOTES + Additional information about the function. +#> +function Set-VersionAssembly +{ + [CmdletBinding()] + [OutputType([void])] + param + ( + [Parameter(Mandatory = $true)] + [string]$packVersion + ) + $NewVersion = 'AssemblyVersion("' + $packVersion + '")'; + $NewFileVersion = 'AssemblyFileVersion("' + $packVersion + '")'; + $NewAssemblyInformationalVersion = 'AssemblyInformationalVersion("' + $packVersion + '")'; + + (Get-Content $assemblyPath -encoding utf8) | + %{ $_ -replace 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', $NewVersion } | + %{ $_ -replace 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', $NewFileVersion } | + %{ $_ -replace 'AssemblyInformationalVersion\("[0-9x]+(\.([0-9x]+|\*)){1,3}"\)', $NewAssemblyInformationalVersion } | + Set-Content $assemblyPath -encoding utf8 +} + +<# + .SYNOPSIS + A brief description of the Exec-TestSolution function. + + .DESCRIPTION + Execute solution test + + .EXAMPLE + PS C:\> Exec-TestSolution + + .NOTES + Additional information about the function. +#> +function Exec-TestSolution +{ + [CmdletBinding()] + [OutputType([bool])] + param () + + # Merge all streams into stdout + $result = dotnet test "..\src\tests\DataTypeTests\DataTypeTests.csproj" *>&1 + + # Evaluate success/failure + if ($LASTEXITCODE -eq 0) + { + return $true; + } + else + { + $errorString = $result -join [System.Environment]::NewLine; + Write-Host -foregroundcolor Red "An error occurred: $errorString"; + + return $false; + } +} + +If ($runTest -eq $true) +{ + Write-Host "Init test solution...`n" -ForegroundColor Green; + $testExec = Exec-TestSolution; +} +Else { $testExec = $true; } + +If ($testExec -eq $true) +{ + Write-Host "Path to pack: '$nugetPath'`n" -ForegroundColor Green; + + $currentVersion = ""; + If ($ownVersion -eq $null -or $ownVersion -eq "") { $currentVersion = Get-CurrentAssemblyVersion; } + Else { $currentVersion = $ownVersion; } + + $directoryInfo = Get-ChildItem $nugetPath | Where-Object { $_.Name -match '[a-z]*.1.0.0.nupkg$' } | Measure-Object; + If ($defaultVersion -eq $currentVersion -and $directoryInfo.count -eq 0) + { + Set-VersionAssembly -packVersion $currentVersion; + + $data | ForEach-Object { + $buildResult = Set-BuildAndPack -packVersion $currentVersion; + If ($buildResult -eq $false -or $buildResult -contains $false) + { + Write-Host "`nBuild/pack failed!!!" -ForegroundColor Red; + + exit; + } + } + + Write-Host "`nPack executed with success with version: $currentVersion!" -ForegroundColor Green; + + exit; + } + Else + { + $finalVersion = ""; + If ($ownVersion -eq $null -or $ownVersion -eq "") + { + $versArray = $currentVersion.Split('.'); + $finalVersion = $versArray[0].ToString() + "." + $versArray[1].ToString() + "." + (([int]$versArray[2]) + 1).ToString() + "." + (Get-TimeStamp).ToString(); + } + Else { $finalVersion = $ownVersion; } + + Set-VersionAssembly -packVersion $finalVersion; + + $data | ForEach-Object { + $buildResult = Set-BuildAndPack -packVersion $finalVersion -currentVersion $currentVersion; + If ($buildResult -eq $false -or $buildResult -contains $false) + { + Write-Host "`nBuild/pack failed!!!" -ForegroundColor Red; + + exit; + } + } + + Write-Host "`nPack executed with success with version: $finalVersion!" -ForegroundColor Green; + + exit; + } +} +Else { exit; } \ No newline at end of file diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5619b16..b0fda92 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -85,4 +85,9 @@ -> Fix wrong modification.
### **v.1.0.12.1447** --> Add IDataReader extensions to convert object in specific type.
\ No newline at end of file +-> Add IDataReader extensions to convert object in specific type.
+ +### **v.1.0.13.8399** +-> Add excel column name generator `GetExcelColumnName`.
+-> Adjust method modifier for `GetDuplicates`.
+-> Fix tests. \ No newline at end of file diff --git a/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs b/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs index b35ddfb..cf85129 100644 --- a/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs +++ b/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs @@ -453,7 +453,7 @@ public static bool HasDuplicates(this IEnumerable list, FuncSource list /// /// - private static IEnumerable GetDuplicates(IEnumerable list) + public static IEnumerable GetDuplicates(this IEnumerable list) { var duplicates = list .GroupBy(x => x) diff --git a/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs index 6c71ce7..17c433e 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs @@ -14,6 +14,8 @@ // // *********************************************************************** +using System; +using System.Globalization; using DomainCommonExtensions.CommonExtensions; namespace DomainCommonExtensions.DataTypeExtensions @@ -123,5 +125,45 @@ public static bool IsLessZero(this int? value) { return (value ?? 0) < 0; } + + ///------------------------------------------------------------------------------------------------- + /// Gets excel column name. + /// Zero-based index of the column. + /// The excel column name. + ///================================================================================================= + public static string GetExcelColumnName(this int columnIndex) + { + // A - Z + if (columnIndex >= 0 && columnIndex <= 25) + { + return ((char)('A' + columnIndex)).ToString(); + } + + // AA - ZZ + if (columnIndex >= 26 && columnIndex <= 701) + { + var firstChar = (char)('A' + (columnIndex / 26) - 1); + var secondChar = (char)('A' + (columnIndex % 26)); + + return string.Format(CultureInfo.InvariantCulture, "{0}{1}", firstChar, secondChar); + } + + // 17576 + // AAA - ZZZ + if (columnIndex >= 702 && columnIndex <= 18277) + { + var fc = (columnIndex - 702) / 676; + var sc = ((columnIndex - 702) % 676) / 26; + var tc = ((columnIndex - 702) % 676) % 26; + + var firstChar = (char)('A' + fc); + var secondChar = (char)('A' + sc); + var thirdChar = (char)('A' + tc); + + return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", firstChar, secondChar, thirdChar); + } + + throw new ArgumentOutOfRangeException($"Column reference ({columnIndex}) is out of range"); + } } } \ No newline at end of file diff --git a/src/DomainCommonExtensions/DomainCommonExtensions.csproj b/src/DomainCommonExtensions/DomainCommonExtensions.csproj index f7db6a9..f685bc5 100644 --- a/src/DomainCommonExtensions/DomainCommonExtensions.csproj +++ b/src/DomainCommonExtensions/DomainCommonExtensions.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + net40;net45;netstandard2.0;netstandard2.1 false RzR @@ -82,7 +82,7 @@ - + \ No newline at end of file diff --git a/src/shared/GeneralAssemblyInfo.cs b/src/shared/GeneralAssemblyInfo.cs index 4f5f74c..4368088 100644 --- a/src/shared/GeneralAssemblyInfo.cs +++ b/src/shared/GeneralAssemblyInfo.cs @@ -47,6 +47,6 @@ [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)] #endif -[assembly: AssemblyVersion("1.0.12.1447")] -[assembly: AssemblyFileVersion("1.0.12.1447")] -[assembly: AssemblyInformationalVersion("1.0.12.x")] \ No newline at end of file +[assembly: AssemblyVersion("1.0.13.8399")] +[assembly: AssemblyFileVersion("1.0.13.8399")] +[assembly: AssemblyInformationalVersion("1.0.13.8399")] diff --git a/src/tests/DataTypeTests/DateTimeTests.cs b/src/tests/DataTypeTests/DateTimeTests.cs index d5c3557..9764433 100644 --- a/src/tests/DataTypeTests/DateTimeTests.cs +++ b/src/tests/DataTypeTests/DateTimeTests.cs @@ -165,7 +165,7 @@ public void CalculateAgeTest() var res = date.CalculateAge(); Assert.IsNotNull(res); - Assert.IsTrue(res.Equals(28)); + Assert.IsTrue(res.Equals(29)); } [TestMethod]