File: Save-Package-Properties.ps1

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (182 lines) | stat: -rw-r--r-- 5,879 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<#
.SYNOPSIS
Saves package properties from source into JSON files

.DESCRIPTION
Saves package properties in source of a given service directory to JSON files.
JSON files are named in the form <package name>.json or <artifact name>.json if
an artifact name property is available in the package properties.

Can optionally add a dev version property which can be used logic for daily
builds.

In cases of collisions where track 2 packages (IsNewSdk = true) have the same
filename as track 1 packages (e.g. same artifact name or package name), the
track 2 package properties will be written.

.PARAMETER serviceDirectory
Service directory in which to search for packages.

.PARAMETER prDiff
A file path leading to a file generated from Generate-PR-Diff.json. This parameter takes precedence over serviceDirectory, do not provide both.

.PARAMETER outDirectory
Output location (generally a package artifact directory in DevOps) for JSON
files

.PARAMETER addDevVersion
Reads the version out of the source and adds a DevVersion property to the
package properties JSON file. If the package properties JSON file already
exists, read the Version property from the existing package properties JSON file
and set that as the Version property for the new output. This has the effect of
"adding" a DevVersion property to the file which could be different from the
Verison property in that file.
#>

[CmdletBinding()]
Param (
  [string] $serviceDirectory,
  [Parameter(Mandatory = $True)]
  [string] $outDirectory,
  [string] $prDiff,
  [switch] $addDevVersion
)

. (Join-Path $PSScriptRoot common.ps1)

function SetOutput($outputPath, $incomingPackageSpec)
{

  # If there is an exsiting package info json file read that and set that as output object which gets properties updated here.
  if (Test-Path $outputPath)
  {
    Write-Host "Found existing package info json."
    $outputObject = ConvertFrom-Json (Get-Content $outputPath -Raw)
  }
  else
  {
    $outputObject = $incomingPackageSpec
  }


  if ($addDevVersion)
  {
    # Use the "Version" property which was provided by the incoming package spec
    # as the DevVersion. This may be overridden later.
    $outputObject.DevVersion = $incomingPackageSpec.Version
  }

  # Set file paths to relative paths
  $outputObject.DirectoryPath = GetRelativePath $outputObject.DirectoryPath
  $outputObject.ReadMePath = GetRelativePath $outputObject.ReadMePath
  $outputObject.ChangeLogPath = GetRelativePath $outputObject.ChangeLogPath

  Set-Content `
    -Path $outputPath `
    -Value (ConvertTo-Json -InputObject $outputObject -Depth 100)
}

function GetRelativePath($path)
{
  # If the path is empty return an empty string
  if (!$path)
  {
    return ''
  }

  # If the path is already relative return the path. Calling `GetRelativePath`
  # on a relative path converts the relative path to an absolute path based on
  # the current working directory which can result in unexpected outputs.
  if (![IO.Path]::IsPathRooted($path))
  {
    return $path
  }

  $relativeTo = Resolve-Path $PSScriptRoot/../../../
  # Replace "\" with "/" so the path is valid across other platforms and tools
  $relativePath = [IO.Path]::GetRelativePath($relativeTo, $path) -replace "\\", '/'
  return $relativePath
}

$exportedPaths = @{}

$allPackageProperties = @()

if ($prDiff)
{
  Write-Host "Getting package properties for PR diff file: $prDiff"
  $allPackageProperties = Get-PrPkgProperties $prDiff

  if (!$allPackageProperties)
  {
    Write-Host "No packages found matching PR diff file $prDiff"
    Write-Host "Setting NoPackagesChanged variable to true"
    Write-Host "##vso[task.setvariable variable=NoPackagesChanged]true"
    exit 0
  }
}
else
{
  Write-Host "Getting package properties for service directory: $serviceDirectory"
  $allPackageProperties = Get-AllPkgProperties $serviceDirectory

  if (!$allPackageProperties)
  {
    Write-Error "Package properties are not available for service directory $serviceDirectory"
    exit 1
  }
}

if (-not (Test-Path -Path $outDirectory))
{
  New-Item -ItemType Directory -Force -Path $outDirectory | Out-Null
}

foreach ($pkg in $allPackageProperties)
{
  if ($pkg.Name)
  {
    Write-Host ""
    Write-Host "Package Name: $($pkg.Name)"
    Write-Host "Package Version: $($pkg.Version)"
    Write-Host "Package SDK Type: $($pkg.SdkType)"
    Write-Host "Artifact Name: $($pkg.ArtifactName)"
    if (-not [System.String]::IsNullOrEmpty($pkg.Group)) {
      Write-Host "GroupId: $($pkg.Group)"
    }
    Write-Host "Release date: $($pkg.ReleaseStatus)"
    $configFilePrefix = $pkg.Name

    # Any languages (like JS) which need to override the the packageInfo file name to be something
    # other than the name just need to declare this function in their Language-Settings.ps1 return
    # the desired string from there.
    if (Test-Path "Function:Get-PackageInfoNameOverride") {
      $configFilePrefix = Get-PackageInfoNameOverride $pkg
    }

    $outputPath = Join-Path -Path $outDirectory "$configFilePrefix.json"
    Write-Host "Output path of json file: $outputPath"

    $outDir = Split-Path $outputPath -parent
    if (-not (Test-Path -path $outDir))
    {
      Write-Host "Creating directory $($outDir) for json property file"
      New-Item -ItemType Directory -Path $outDir | Out-Null
    }

    # If package properties for a track 2 (IsNewSdk = true) package has
    # already been written, skip writing to that same path.
    if ($exportedPaths.ContainsKey($outputPath) -and $exportedPaths[$outputPath].IsNewSdk -eq $true)
    {
      Write-Host "Track 2 package info with file name $($outputPath) already exported. Skipping export."
      continue
    }

    $exportedPaths[$outputPath] = $pkg
    SetOutput $outputPath $pkg
  }
}

$fileNames = (Get-ChildItem -Path $outDirectory).Name
Write-Host "`nFiles written to $outDirectory`:"
Write-Host "  $($fileNames -join "`n  ")"