Skip to content

Commit df3948f

Browse files
authored
Users/emarq/utilsscripts (#7)
* gitignore added out directory * Add .sln file to .gitignore to exclude Visual Studio solution files * Add script to update ModuleVersion in PowerShell module manifests * Enhance project structure: update .gitignore to exclude package directories, add set-nuspecfile.ps1 for NuGet package generation, and improve update-versionnumber.ps1 with detailed logging. * updated the unit-test helper to check in the new directory.
1 parent dc85ddd commit df3948f

5 files changed

Lines changed: 175 additions & 6 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ ScaffoldingReadMe.txt
3636
*.nupkg
3737
# NuGet Symbol Packages
3838
*.snupkg
39+
# ignore sln file generated by vscode.
40+
*.sln
41+
[pP]ackages/
42+
[pP]owerShell/[mM]odules/
43+
3944

4045
# Others
4146
~$*

Microsoft.AzureStack.Util.ConvertNetwork.csproj

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,46 @@
77
<Version Condition="'$(Version)'==''">1.0.0</Version>
88
<Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
99
<BinariesBuildDirectory>$(MSBuildProjectDirectory)\out</BinariesBuildDirectory>
10-
<Package>$(MSBuildProjectDirectory)\out\packages</Package>
11-
<PesterVersion>5.2.0</PesterVersion>
12-
<!-- Set the custom NuGet packages directory -->
13-
<RestorePackagesPath>$(MSBuildProjectDirectory)\out\packages</RestorePackagesPath>
10+
<PesterVersion>5.2.0</PesterVersion> <!--Used for Unit-Testing only -->
11+
<RestorePackagesPath>$(MSBuildProjectDirectory)\packages</RestorePackagesPath>
12+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
13+
<PackageTags>AzureStack, Utility, Network, Subneting</PackageTags>
14+
<RepositoryUrl>https://github.com/microsoft/$(ProjectName)</RepositoryUrl>
15+
<Description>Microsoft.AzureStack.Util.ConvertNetwork is a PowerShell module that provides utilities for working with IPv4 networks. It includes functions to calculate subnet masks, CIDR values, broadcast addresses, and more. This project is designed to simplify network-related operations for developers and IT professionals.</Description>
1416
</PropertyGroup>
1517

1618
<ItemGroup>
19+
<!-- NuGet Import -->
1720
<PackageReference Include="Pester" Version="$(PesterVersion)" />
1821
</ItemGroup>
1922

23+
<Target Name="LogNuGetPackages" AfterTargets="Restore">
24+
<ItemGroup>
25+
<!-- Collect all restored NuGet packages -->
26+
<RestoredPackages Include="$(RestorePackagesPath)\**\*.nupkg" />
27+
</ItemGroup>
28+
<Message Text="[LogNuGetPackages] Restored NuGet Package: %(RestoredPackages.Identity)" Importance="high" />
29+
</Target>
30+
31+
<Target Name="CustomClean" BeforeTargets="Clean">
32+
<Message Text="Cleaning up the project...$(BinariesBuildDirectory)" Importance="high" Condition="Exists('$(BinariesBuildDirectory)')" />
33+
<Message Text="Cleaning up the project...$(RestorePackagesPath)" Importance="high" Condition="Exists('$(RestorePackagesPath)')" />
34+
<Message Text="Cleaning up the project...$(MSBuildProjectDirectory)\PowerShell" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\PowerShell')" />
35+
<Message Text="Cleaning up the project...$(MSBuildProjectDirectory)\temp" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\temp')" />
36+
<Message Text="Cleaning up the project...$(MSBuildProjectDirectory)\obj" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\obj')" />
37+
<Message Text="Cleaning up the project...$(MSBuildProjectDirectory)\bin" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\bin')" />
38+
39+
<RemoveDir Directories="$(BinariesBuildDirectory)" Condition="Exists('$(BinariesBuildDirectory)')" />
40+
<RemoveDir Directories="$(RestorePackagesPath)" Condition="Exists('$(RestorePackagesPath)')" />
41+
<RemoveDir Directories="$(MSBuildProjectDirectory)\PowerShell" Condition="Exists('$(MSBuildProjectDirectory)\PowerShell')" />
42+
<RemoveDir Directories="$(MSBuildProjectDirectory)\temp" Condition="Exists('$(MSBuildProjectDirectory)\temp')" />
43+
<RemoveDir Directories="$(MSBuildProjectDirectory)\obj" Condition="Exists('$(MSBuildProjectDirectory)\obj')" />
44+
<RemoveDir Directories="$(MSBuildProjectDirectory)\bin" Condition="Exists('$(MSBuildProjectDirectory)\bin')" />
45+
</Target>
46+
2047
<Target Name="PlaceFiles" AfterTargets="Build">
48+
<Message Text="[PlaceFiles] Copying files to $(BinariesBuildDirectory)\$(ProjectName)\Microsoft.AzureStack.Util.ConvertNetwork.psd1" Importance="high" />
49+
<Message Text="[PlaceFiles] Copying files to $(BinariesBuildDirectory)\$(ProjectName)\Microsoft.AzureStack.Util.ConvertNetwork.psm1" Importance="high" />
2150
<Copy SourceFiles="$(MSBuildProjectDirectory)\src\Microsoft.AzureStack.Util.ConvertNetwork.psd1" DestinationFolder="$(BinariesBuildDirectory)\$(ProjectName)" />
2251
<Copy SourceFiles="$(MSBuildProjectDirectory)\src\Microsoft.AzureStack.Util.ConvertNetwork.psm1" DestinationFolder="$(BinariesBuildDirectory)\$(ProjectName)" />
2352
</Target>
@@ -27,7 +56,20 @@
2756
<ItemGroup>
2857
<PesterFiles Include="$(RestorePackagesPath)\pester\$(PesterVersion)\tools\**\*" Exclude="*.nupkg;*.nuspec" />
2958
</ItemGroup>
30-
<Copy SourceFiles="@(PesterFiles)" DestinationFolder="$(BinariesBuildDirectory)\PowerShell\Modules\Pester\$(PesterVersion)\%(RecursiveDir)" />
59+
<Copy SourceFiles="@(PesterFiles)" DestinationFolder="$(MSBuildProjectDirectory)\PowerShell\Modules\Pester\$(PesterVersion)\%(RecursiveDir)" />
60+
<Message Text="[CopyPester] Copying Pester files to $(MSBuildProjectDirectory)\PowerShell\Modules\Pester\$(PesterVersion)" Importance="high" Condition="Exists('$(RestorePackagesPath)\pester')" />
61+
</Target>
62+
63+
<Target Name="UpdateVersionNumber" AfterTargets="PlaceFiles">
64+
<Message Text="[UpdateVersionNumber] Running update-versionnumber.ps1" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\util\update-versionnumber.ps1')" />
65+
<Exec Command="pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command &quot;&amp; { &amp; '$(MSBuildProjectDirectory)\util\update-versionnumber.ps1' -Directory '$(BinariesBuildDirectory)\$(ProjectName)' -Version '$(Version)' }&quot;" />
66+
<Message Text="[UpdateVersionNumber] Finished running update-versionnumber.ps1" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\util\update-versionnumber.ps1')" />
67+
</Target>
68+
69+
<Target Name="NuSpecJob" AfterTargets="UpdateVersionNumber">
70+
<Message Text="[NuSpecJob] Running set-nuspecfile.ps1" Importance="high" Condition="Exists('$(MSBuildProjectDirectory)\util\set-nuspecfile.ps1')" />
71+
<Exec Command="pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command &quot;&amp; { &amp; '$(MSBuildProjectDirectory)\util\set-nuspecfile.ps1' -DirectoryPath '$(BinariesBuildDirectory)\$(ProjectName)' -PackageName '$(ProjectName)' -Version '$(Version)' -RepoUrl '$(RepositoryUrl)' -Description '$(Description)'}&quot;" />
72+
<Message Text="[NuSpecJob] Generating NuGet spec file" Importance="high" Condition="Exists('$(BinariesBuildDirectory)\$(ProjectName)\$(ProjectName).nuspec')" />
3173
</Target>
3274

3375
</Project>

tests/setupTest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$parentPath = (Get-Item $PSScriptRoot).Parent.FullName
44

55
Remove-Module Pester -Force -ErrorAction SilentlyContinue
6-
Import-Module (Join-Path -Path $parentPath -ChildPath 'out\PowerShell\Modules\Pester') -Force
6+
Import-Module (Join-Path -Path $parentPath -ChildPath 'PowerShell\Modules\Pester') -Force
77
New-Item -Path (Join-Path -Path $parentPath -ChildPath 'out\Tests') -ItemType Directory -Force | Out-Null
88

99
$config = New-PesterConfiguration

util/set-nuspecfile.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
param (
2+
[Parameter(Mandatory = $true)]
3+
[string]$DirectoryPath, # Root directory path where the package is located
4+
5+
[Parameter(Mandatory = $true)]
6+
[string]$PackageName, # Name of the NuGet package
7+
8+
[Parameter(Mandatory = $true)]
9+
[string]$Version, # Version of the NuGet package (e.g., 1.0.0)
10+
11+
[Parameter(Mandatory = $true)]
12+
[string]$RepoUrl,
13+
14+
[Parameter(Mandatory = $true)]
15+
[string]$Description # Description of the NuGet package
16+
)
17+
18+
# Define the output directory for the nuspec file
19+
$NuspecFilePath = Join-Path -Path $DirectoryPath -ChildPath "$PackageName.nuspec"
20+
21+
# Generate the .nuspec content
22+
$NuspecContent = @"
23+
<?xml version="1.0" encoding="utf-8"?>
24+
<package >
25+
<metadata>
26+
<id>$PackageName</id>
27+
<version>$Version</version>
28+
<authors>Microsoft</authors>
29+
<owners>Microsoft</owners>
30+
<license type="expression">MIT</license>
31+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
32+
<projectUrl>$RepoUrl</projectUrl>
33+
<description>$Description</description>
34+
<tags>AzureStack, Utility, Network, Subneting</tags>
35+
<copyright>Copyright © $(Get-Date -Format yyyy)</copyright>
36+
<repository url="$RepoUrl" type="git" commit="$(git rev-parse HEAD)" />
37+
</metadata>
38+
<files>
39+
<file src="$($DirectoryPath)\*" target="content" exclude="**\*.nupkg;**\*.nuspec" />
40+
</files>
41+
</package>
42+
"@
43+
44+
# Write the .nuspec file
45+
Write-Host "$($MyInvocation.MyCommand.Name) - Generating .nuspec file at: $NuspecFilePath"
46+
$NuspecContent | Set-Content -Path $NuspecFilePath -Encoding UTF8
47+
48+
Write-Host "$($MyInvocation.MyCommand.Name) - NuSpec file generated successfully!"

util/update-versionnumber.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<#
2+
.SYNOPSIS
3+
This script updates the `ModuleVersion` field in PowerShell module manifest files (*.psd1).
4+
5+
.DESCRIPTION
6+
The script performs a recursive search for PowerShell module manifest files (*.psd1) in the specified directory.
7+
It updates the `ModuleVersion` field in each file with the provided version number.
8+
9+
.PARAMETER Version
10+
The new version number to set in the `ModuleVersion` field. Must conform to the [version] type.
11+
12+
.PARAMETER Directory
13+
The directory to search for *.psd1 files. The search is recursive.
14+
15+
.EXAMPLE
16+
.\update-versionnumber.ps1 -Version "1.2.3" -Directory "C:\Modules"
17+
18+
This command updates the `ModuleVersion` field in all *.psd1 files under "C:\Modules" to "1.2.3".
19+
20+
.EXAMPLE
21+
.\update-versionnumber.ps1 -Version "2.0.0" -Directory "."
22+
23+
This command updates the `ModuleVersion` field in all *.psd1 files in the current directory and its subdirectories to "2.0.0".
24+
25+
.NOTES
26+
- Ensure you have write permissions for the files being updated.
27+
- Use semantic versioning for the version number (e.g., "1.0.0").
28+
#>
29+
30+
param (
31+
[Parameter(Mandatory = $true)]
32+
[version]$Version, # Enforces the [version] type for the parameter
33+
34+
[Parameter(Mandatory = $true)]
35+
[string]$Directory
36+
)
37+
38+
# Validate the directory
39+
if (-not (Test-Path -Path $Directory)) {
40+
Write-Error "$($MyInvocation.MyCommand.Name) - The specified directory does not exist: $Directory"
41+
exit 1
42+
}
43+
44+
# Perform a recursive search for *.psd1 files
45+
$psd1Files = Get-ChildItem -Path $Directory -Recurse -Filter "*.psd1"
46+
47+
if ($psd1Files.Count -eq 0) {
48+
Write-Host "$($MyInvocation.MyCommand.Name) -No *.psd1 files found in the specified directory: $Directory"
49+
exit 0
50+
}
51+
52+
# Update the ModuleVersion in each *.psd1 file
53+
foreach ($file in $psd1Files) {
54+
try {
55+
Write-Host "$($MyInvocation.MyCommand.Name) - Processing file: $($file.FullName)" -ForegroundColor Yellow
56+
57+
# Read the file content as an array of lines
58+
$fileContent = Get-Content -Path $file.FullName
59+
60+
# Update the ModuleVersion line
61+
$updatedContent = $fileContent -replace "ModuleVersion\s*=\s*[`"|\'](.*?)[`"|\']", "ModuleVersion = '$Version'"
62+
63+
# Write the updated content back to the file
64+
$updatedContent | Set-Content -Path $file.FullName -Encoding UTF8
65+
66+
Write-Host "$($MyInvocation.MyCommand.Name) - Updated ModuleVersion in file: $($file.FullName)" -ForegroundColor Green
67+
Write-Host "$($MyInvocation.MyCommand.Name) - New ModuleVersion: $Version" -ForegroundColor Green
68+
}
69+
catch {
70+
Write-Error "$($MyInvocation.MyCommand.Name) - Failed to update file: $($file.FullName). Error: $_"
71+
}
72+
}
73+
74+
Write-Host "$($MyInvocation.MyCommand.Name) - ModuleVersion update completed." -ForegroundColor Cyan

0 commit comments

Comments
 (0)