Skip to content

Commit

Permalink
Merge pull request #19 from I-RzR-I/feature/AddNewMethodAndFixSomeAccess
Browse files Browse the repository at this point in the history
Add new extension and fix method modifier.
  • Loading branch information
I-RzR-I authored Jan 16, 2024
2 parents e85dfad + 470f0b0 commit 0732058
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 9 deletions.
267 changes: 267 additions & 0 deletions build/pack-repo.ps1
Original file line number Diff line number Diff line change
@@ -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; }
7 changes: 6 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@
-> Fix wrong modification.<br />

### **v.1.0.12.1447**
-> Add IDataReader extensions to convert object in specific type.<br />
-> Add IDataReader extensions to convert object in specific type.<br />

### **v.1.0.13.8399**
-> Add excel column name generator `GetExcelColumnName`.<br />
-> Adjust method modifier for `GetDuplicates`.<br />
-> Fix tests.
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public static bool HasDuplicates<T, TProp>(this IEnumerable<T> list, Func<T, TPr
/// <param name="list">Source list</param>
/// <returns></returns>
/// <remarks></remarks>
private static IEnumerable<string> GetDuplicates(IEnumerable<string> list)
public static IEnumerable<string> GetDuplicates(this IEnumerable<string> list)
{
var duplicates = list
.GroupBy(x => x)
Expand Down
42 changes: 42 additions & 0 deletions src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// </summary>
// ***********************************************************************

using System;
using System.Globalization;
using DomainCommonExtensions.CommonExtensions;

namespace DomainCommonExtensions.DataTypeExtensions
Expand Down Expand Up @@ -123,5 +125,45 @@ public static bool IsLessZero(this int? value)
{
return (value ?? 0) < 0;
}

///-------------------------------------------------------------------------------------------------
/// <summary>Gets excel column name.</summary>
/// <param name="columnIndex">Zero-based index of the column.</param>
/// <returns>The excel column name.</returns>
///=================================================================================================
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");
}
}
}
6 changes: 3 additions & 3 deletions src/DomainCommonExtensions/DomainCommonExtensions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!--<TargetFramework>netstandard2.0</TargetFramework>-->
<TargetFrameworks>net40;net45;netstandard2.0;netstandard2.1</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Authors>RzR</Authors>
Expand Down Expand Up @@ -82,7 +82,7 @@
<UserProperties BuildVersion_StartDate="2022/8/22" />
</VisualStudio>
</ProjectExtensions>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<!--<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="PowerShell -NoProfile -ExecutionPolicy unrestricted -file $(SolutionDir)../build/pack.ps1 $(ConfigurationName)" />
</Target>
</Target>-->
</Project>
6 changes: 3 additions & 3 deletions src/shared/GeneralAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
[assembly: AssemblyVersion("1.0.13.8399")]
[assembly: AssemblyFileVersion("1.0.13.8399")]
[assembly: AssemblyInformationalVersion("1.0.13.8399")]
2 changes: 1 addition & 1 deletion src/tests/DataTypeTests/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0732058

Please sign in to comment.