Skip to content

Commit

Permalink
Sync tools folder from main branch to generation branch (#25871)
Browse files Browse the repository at this point in the history
Co-authored-by: azurepowershell <[email protected]>
  • Loading branch information
azure-powershell-bot and azurepowershell authored Aug 20, 2024
1 parent 263135c commit 94ffdec
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines/daily-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
script: |
$command = "`$PSVersionTable `
Get-PSRepository `
./tools/RunVersionController.ps1 -Release 'Daily Build $(today)' `
./tools/RunVersionController.ps1 -Release 'Daily Build $(today)' -ReleaseType $(ReleaseType)`
Exit"
dotnet tool run pwsh -c $command
Expand Down
27 changes: 17 additions & 10 deletions tools/RunVersionController.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ Param(
[string]$GalleryName = "PSGallery",

[Parameter()]
[string]$ArtifactsOutputPath = "$PSScriptRoot/../artifacts/Release/"
[string]$ArtifactsOutputPath = "$PSScriptRoot/../artifacts/Release/",

[Parameter()]
[ValidateSet("STS", "LTS")]
[string]$ReleaseType = "STS"
)

enum PSVersion
Expand Down Expand Up @@ -194,15 +198,18 @@ function Bump-AzVersion
{
Write-Host "Getting local Az information..." -ForegroundColor Yellow
$localAz = Import-PowerShellDataFile -Path "$PSScriptRoot\Az\Az.psd1"

Write-Host "Getting gallery Az information..." -ForegroundColor Yellow
$galleryAz = Find-Module -Name Az -Repository $GalleryName
Write-Host "Getting Az $ReleaseType information from gallery..." -ForegroundColor Yellow
if("LTS" -eq $ReleaseType){
$galleryAz = (Find-Module -Name Az -Repository $GalleryName -AllVersions | Where-Object {([System.Version]($_.Version)).Major%2 -eq 0} | Sort-Object {[System.Version]$_.Version} -Descending)[0]
}else{
$galleryAz = Find-Module -Name Az -Repository $GalleryName
}

$versionBump = [PSVersion]::NONE
$updatedModules = @()
foreach ($localDependency in $localAz.RequiredModules)
{
$galleryDependency = $galleryAz.Dependencies | where { $_.Name -eq $localDependency.ModuleName }
$galleryDependency = $galleryAz.Dependencies | Where-Object { $_.Name -eq $localDependency.ModuleName }
if ($null -eq $galleryDependency)
{
$updatedModules += $localDependency.ModuleName
Expand Down Expand Up @@ -313,7 +320,7 @@ function Update-AzPreview
$Psd1Object = Import-PowerShellDataFile $Psd1FilePath
$moduleName = [System.IO.Path]::GetFileName($Psd1FilePath) -replace ".psd1"
$moduleVersion = $Psd1Object.ModuleVersion.ToString()
if('Az.Accounts' -eq $moduleName)
if('Az.Accounts' -eq $moduleName -and "STS" -eq $ReleaseType)
{
$requiredModulesString += "@{ModuleName = '$moduleName'; ModuleVersion = '$moduleVersion'; }, `n "
}
Expand Down Expand Up @@ -482,8 +489,8 @@ switch ($PSCmdlet.ParameterSetName)
{
"ReleaseSingleModule"
{
Write-Host executing dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll $PSScriptRoot/../artifacts/VersionController/Exceptions $ModuleName
dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll $PSScriptRoot/../artifacts/VersionController/Exceptions $ModuleName
Write-Host executing dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll $PSScriptRoot/../artifacts/VersionController/Exceptions $ModuleName $ReleaseType
dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll $PSScriptRoot/../artifacts/VersionController/Exceptions $ModuleName $ReleaseType
Update-AzPreview
}

Expand Down Expand Up @@ -521,8 +528,8 @@ switch ($PSCmdlet.ParameterSetName)
}
}

Write-Host executing dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll
dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll
Write-Host executing dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll $ReleaseType
dotnet $PSScriptRoot/../artifacts/VersionController/VersionController.Netcore.dll $ReleaseType

$versionBump = Bump-AzVersion
# Each release needs to update AzPreview.psd1 and dotnet csv
Expand Down
21 changes: 12 additions & 9 deletions tools/Tools.Common/Loaders/MetadataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ namespace Tools.Common.Loaders
{
public class MetadataLoader
{
private static string _rootPath = Path.GetFullPath(Path.Combine(Assembly.GetExecutingAssembly().Location, "..", "..", ".."));

public static ModuleMetadata GetModuleMetadata(string moduleName)
{
string rootPath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", ".."));
string modulePsd1Path = Directory.GetFiles(Path.Combine(rootPath, "artifacts"), $"{moduleName}.psd1", SearchOption.AllDirectories)[0];
// bez: notice that this search way always find artifacts/Debug/{moduleName}/psd1 first, which may cause some issues
// to work around this issue, clear Debug folder if we are intended to bump version for Release
string modulePsd1Path = Directory.GetFiles(Path.Combine(_rootPath, "artifacts"), $"{moduleName}.psd1", SearchOption.AllDirectories)[0];
if (modulePsd1Path == null)
{
Console.Error.WriteLine($"Cannot find {moduleName}.psd1 in {Path.Combine(rootPath, "artifacts")}!");
Console.Error.WriteLine($"Cannot find {moduleName}.psd1 in {Path.Combine(_rootPath, "artifacts")}!");
}
return GetModuleMetadata(moduleName, modulePsd1Path);
}
public static ModuleMetadata GetModuleMetadata(string moduleName, string modulePsd1Path)

private static ModuleMetadata GetModuleMetadata(string moduleName, string modulePsd1Path)
{
using (var powershell = PowerShell.Create(RunspaceMode.NewRunspace))
{
Expand All @@ -49,15 +53,14 @@ public static ModuleMetadata GetModuleMetadata(string moduleName, string moduleP
powershell.AddScript("Set-ExecutionPolicy Unrestricted -Scope Process -ErrorAction Ignore");
}
powershell.AddScript("$error.clear()");
powershell.AddScript($"Write-Debug \"current directory: { AppDomain.CurrentDomain.BaseDirectory }\"");
string rootPath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", ".."));
string repoToolsPath = Path.Combine(rootPath, "tools");
powershell.AddScript($"Write-Debug \"current directory: { Assembly.GetExecutingAssembly().Location}\"");
string repoToolsPath = Path.Combine(_rootPath, "tools");
powershell.AddScript($"cd {repoToolsPath}\\ModuleMetadata");
powershell.AddScript($"Import-Module {repoToolsPath}\\ModuleMetadata\\GetModuleMetadata.psm1");
string accountsPsd1Path = Directory.GetFiles(Path.Combine(rootPath, "artifacts"), "Az.Accounts.psd1", SearchOption.AllDirectories)[0];
string accountsPsd1Path = Directory.GetFiles(Path.Combine(_rootPath, "artifacts"), "Az.Accounts.psd1", SearchOption.AllDirectories)[0];
if (accountsPsd1Path == null)
{
Console.Error.WriteLine($"Cannot find Az.Accounts.psd1 in {Path.Combine(rootPath, "artifacts", "Accounts")}!");
Console.Error.WriteLine($"Cannot find Az.Accounts.psd1 in {Path.Combine(_rootPath, "artifacts", "Accounts")}!");
}
powershell.AddScript($"Import-Module {accountsPsd1Path}");
powershell.AddScript($"(Get-ModuleMetadata -Psd1Path {modulePsd1Path} -ModuleName {moduleName}).ToJsonString()");
Expand Down
5 changes: 5 additions & 0 deletions tools/Tools.Common/Models/CmdletMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,17 @@ public override bool Equals(Object obj)
var otherParameterSet = other.ParameterSets.Find(p => string.Equals(p.Name, thisParameterSet.Name, StringComparison.OrdinalIgnoreCase));
if (otherParameterSet == null)
{
// Console.WriteLine($"Parameter set {thisParameterSet.Name} in cmdlet {this.Name} is not found in new module.");
return false;
}

cmdletsEqual &= thisParameterSet.Equals(otherParameterSet);
}

/*if (this.ParameterSets.Count != other.ParameterSets.Count)
{
Console.WriteLine($"The number of parameter sets in cmdlet {this.Name} is unmatched.");
}*/
cmdletsEqual &= this.ParameterSets.Count == other.ParameterSets.Count;
return cmdletsEqual;
}
Expand Down
17 changes: 17 additions & 0 deletions tools/Tools.Common/Models/CommonInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;

namespace VersionController.Netcore.Models
{
public class CommonInfo
{
public static List<string> ExcludedParameters = new List<string>{
"AzureRMContext", "Break", "Debug", "DefaultProfile", "EnableTestCoverage",
"ErrorAction", "ErrorVariable", "HttpPipelineAppend", "HttpPipelinePrepend", "InformationAction",
"InformationVariable", "OutBuffer", "OutVariable", "PipelineVariable", "Proxy",
"ProxyCredential", "ProxyUseDefaultCredentials", "Verbose", "WarningAction", "WarningVariable",
"ProgressAction",
// excluded runtime dynamic parameters
// "EnableTestCoverage", "TestCoverageLocation", "TargetName"
};
}
}
7 changes: 5 additions & 2 deletions tools/Tools.Common/Models/ModuleMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ public override bool Equals(Object obj)
var otherCmdlet = other.Cmdlets.Find(c => thisCmdletNames.ContainsKey(c.Name) || c.AliasList.Find(a => thisCmdletNames.ContainsKey(a)) != null);
if (otherCmdlet == null)
{
// Console.WriteLine($"Cannot find cmdlet {thisCmdletNames} in new version.");
return false;
}

modulesEqual &= thisCmdlet.Equals(otherCmdlet);
}

/*if(this.Cmdlets.Count != other.Cmdlets.Count)
{
Console.WriteLine($"The number of cmdlets is unmatched in old and new module");
}*/
modulesEqual &= this.Cmdlets.Count == other.Cmdlets.Count;
return modulesEqual;
}
Expand Down
21 changes: 18 additions & 3 deletions tools/Tools.Common/Models/ParameterSetMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
// ----------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using VersionController.Netcore.Models;

namespace Tools.Common.Models
{
Expand Down Expand Up @@ -58,13 +62,24 @@ public override bool Equals(Object obj)
var otherParameter = other.Parameters.Find(p => thisParameterNames.ContainsKey(p.ParameterMetadata.Name) || p.ParameterMetadata.AliasList.Find(a => thisParameterNames.ContainsKey(a)) != null);
if (otherParameter == null)
{
// Console.WriteLine($"Parameter {thisParameter.ParameterMetadata.Name} in parameter set {this.Name} is not found in new module.");
return false;
}

paramsSetEqual &= thisParameter.Equals(otherParameter);
}

paramsSetEqual &= this.Parameters.Count == other.Parameters.Count;
var curParameters = this.Parameters.Where(p => !CommonInfo.ExcludedParameters.Contains(p.ParameterMetadata.Name)).ToList();
var otherParameters = other.Parameters.Where(p => !CommonInfo.ExcludedParameters.Contains(p.ParameterMetadata.Name)).ToList();
/*if (curParameters.Count != otherParameters.Count)
{
Console.WriteLine($"The number of parameters in parameter set {this.Name} is not same.");
Console.WriteLine("Parameters in old version: ");
this.Parameters.ForEach(p => Console.Write(p.ParameterMetadata.Name + " "));
Console.WriteLine(Environment.NewLine + "Parameters in new version: ");
other.Parameters.ForEach(p => Console.Write(p.ParameterMetadata.Name + " "));
Console.WriteLine();
}*/
paramsSetEqual &= curParameters.Count == otherParameters.Count;
return paramsSetEqual;
}

Expand Down Expand Up @@ -232,13 +247,13 @@ public override bool Equals(Object obj)
{
return false;
}

var paramsEqual = true;
paramsEqual &= this.Mandatory == other.Mandatory &&
this.Position == other.Position &&
this.ValueFromPipeline == other.ValueFromPipeline &&
this.ValueFromPipelineByPropertyName == other.ValueFromPipelineByPropertyName &&
this.ParameterMetadata.Equals(other.ParameterMetadata);
// if (!paramsEqual) Console.WriteLine($"The attributes of {this.ParameterMetadata.Name} is different in new version");
return paramsEqual;
}

Expand Down
75 changes: 75 additions & 0 deletions tools/VersionController/Models/ModuleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

using Tools.Common.Models;

namespace VersionController.Netcore.Models
{
internal class ModuleHelper
{
/// <summary>
/// Get the version of latest Az.Accounts in LTS status from PSGallery
/// </summary>
/// <returns></returns>
internal static string GetLatestVersionFromPSGallery(string moduleName, ReleaseType releaseType = ReleaseType.STS)
{

string version = null;
string findModuleScript = releaseType == ReleaseType.STS ? $"Find-Module {moduleName} -Repository PSGallery -AllVersions" : "Find-Module Az -Repository PSGallery -AllVersions";
string filterRequiredReleaseTypeScript = releaseType == ReleaseType.STS ? "" : "| Where-Object {([System.Version]($_.Version)).Major%2 -eq 0}";
string sortModuleScript = "| Sort-Object {[System.Version]$_.Version} -Descending";
string getLastModuleVersionScript = releaseType == ReleaseType.STS ?
$"({findModuleScript}{filterRequiredReleaseTypeScript}{sortModuleScript})[0].Version" :
$"(({findModuleScript}{filterRequiredReleaseTypeScript}{sortModuleScript})[0].Dependencies | Where-Object {{$_.Name -eq '{moduleName}'}})[1]";
using (PowerShell powershell = PowerShell.Create())
{
powershell.AddScript(getLastModuleVersionScript);
var cmdletResult = powershell.Invoke();
version = cmdletResult[0]?.ToString();
}
return version;
}

/// <summary>
/// Get version from PSGallery and TestGallery and merge into one list.
/// </summary>
/// <returns>A list of version</returns>
internal static List<AzurePSVersion> GetAllVersionsFromGallery(string moduleName, string psRepository)
{
HashSet<AzurePSVersion> galleryVersion = new HashSet<AzurePSVersion>();
using (PowerShell powershell = PowerShell.Create())
{
powershell.AddScript($"Find-Module -Name {moduleName} -Repository {psRepository} -AllowPrerelease -AllVersions");
var cmdletResult = powershell.Invoke();
foreach (var versionInformation in cmdletResult)
{
if (versionInformation.Properties["Version"]?.Value != null)
{
galleryVersion.Add(new AzurePSVersion(versionInformation.Properties["Version"]?.Value?.ToString()));
}
}
}
return galleryVersion.ToList();
}


/// <summary>
/// Under the same Major version, check if there exist preview version in gallery that has greater version.
/// </summary>
/// <returns>True if exist a version, false otherwise.</returns>
internal static AzurePSVersion GetLatestVersionFromGalleryUnderSameMajorVersion(AzurePSVersion bumpedVersion, List<AzurePSVersion> galleryVersion, bool IsPreview)
{
var maxVersionInGallery = new AzurePSVersion(0, 0, 0);

foreach (var version in galleryVersion)
{
if (version.Major == bumpedVersion.Major && (version.IsPreview == IsPreview) && version > maxVersionInGallery)
{
maxVersionInGallery = version;
}
}
return maxVersionInGallery;
}
}
}
14 changes: 14 additions & 0 deletions tools/VersionController/Models/ReleaseType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VersionController.Netcore.Models
{
public enum ReleaseType
{
STS,
LTS
}
}
9 changes: 2 additions & 7 deletions tools/VersionController/Models/SyntaxChangelogGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ public class SyntaxChangelogGenerator
public AnalysisLogger Logger { get; set; }
public string Name { get; set; }
public string CmdletDiffIssueReportLoggerName { get; set; }
private List<string> _ignoreParameters = new List<string>
{
"AzureRMContext", "Break", "Debug", "DefaultProfile", "EnableTestCoverage",
"ErrorAction", "ErrorVariable", "HttpPipelineAppend", "HttpPipelinePrepend", "InformationAction",
"InformationVariable", "OutBuffer", "OutVariable", "PipelineVariable", "Proxy",
"ProxyCredential", "ProxyUseDefaultCredentials", "Verbose", "WarningAction", "WarningVariable"
};
private List<string> _ignoreParameters = CommonInfo.ExcludedParameters;
private List<CmdletDiffInformation> diffInfo = new List<CmdletDiffInformation>();
public void Analyze(String rootDirectory)
{
var srcDirs = Path.Combine(rootDirectory, @"src\");
var toolsCommonDirs = Path.Combine(rootDirectory, @"tools\Tools.Common");
// bez: Will include psd1 files under test proj
var manifestFiles = Directory.EnumerateFiles(srcDirs, "*.psd1", SearchOption.AllDirectories)
.Where(file =>
!Path.GetDirectoryName(file)
Expand Down
Loading

0 comments on commit 94ffdec

Please sign in to comment.