...
1<#
2.SYNOPSIS
3Saves package properties from source into JSON files
4
5.DESCRIPTION
6Saves package properties in source of a given service directory to JSON files.
7JSON files are named in the form <package name>.json or <artifact name>.json if
8an artifact name property is available in the package properties.
9
10Can optionally add a dev version property which can be used logic for daily
11builds.
12
13.PARAMETER serviceDirectory
14Service directory in which to search for packages
15
16.PARAMETER outDirectory
17Output location (generally a package artifact directory in DevOps) for JSON
18files
19
20.PARAMETER addDevVersion
21Reads the version out of the source and adds a DevVersion property to the
22package properties JSON file. If the package properties JSON file already
23exists, read the Version property from the existing package properties JSON file
24and set that as the Version property for the new output. This has the effect of
25"adding" a DevVersion property to the file which could be different from the
26Verison property in that file.
27#>
28
29[CmdletBinding()]
30Param (
31 [Parameter(Mandatory=$True)]
32 [string] $serviceDirectory,
33 [Parameter(Mandatory=$True)]
34 [string] $outDirectory,
35 [switch] $addDevVersion
36)
37
38. (Join-Path $PSScriptRoot common.ps1)
39
40function SetOutput($outputPath, $incomingPackageSpec) {
41
42 # If there is an exsiting package info json file read that and set that as output object which gets properties updated here.
43 if (Test-Path $outputPath)
44 {
45 Write-Host "Found existing package info json."
46 $outputObject = ConvertFrom-Json (Get-Content $outputPath -Raw)
47 }
48 else
49 {
50 $outputObject = $incomingPackageSpec
51 }
52
53
54 if ($addDevVersion)
55 {
56 # Use the "Version" property which was provided by the incoming package spec
57 # as the DevVersion. This may be overridden later.
58 $outputObject.DevVersion = $incomingPackageSpec.Version
59 }
60
61 # Set file paths to relative paths
62 $outputObject.DirectoryPath = GetRelativePath $outputObject.DirectoryPath
63 $outputObject.ReadMePath = GetRelativePath $outputObject.ReadMePath
64 $outputObject.ChangeLogPath = GetRelativePath $outputObject.ChangeLogPath
65
66 Set-Content `
67 -Path $outputPath `
68 -Value (ConvertTo-Json -InputObject $outputObject -Depth 100)
69}
70
71function GetRelativePath($path) {
72 # If the path is empty return an empty string
73 if (!$path) {
74 return ''
75 }
76
77 # If the path is already relative return the path. Calling `GetRelativePath`
78 # on a relative path converts the relative path to an absolute path based on
79 # the current working directory which can result in unexpected outputs.
80 if (![IO.Path]::IsPathRooted($path)) {
81 return $path
82 }
83
84 $relativeTo = Resolve-Path $PSScriptRoot/../../../
85 # Replace "\" with "/" so the path is valid across other platforms and tools
86 $relativePath = [IO.Path]::GetRelativePath($relativeTo, $path) -replace "\\", '/'
87 return $relativePath
88}
89
90$allPackageProperties = Get-AllPkgProperties $serviceDirectory
91if ($allPackageProperties)
92{
93 if (-not (Test-Path -Path $outDirectory))
94 {
95 New-Item -ItemType Directory -Force -Path $outDirectory
96 }
97 foreach($pkg in $allPackageProperties)
98 {
99 Write-Host "Package Name: $($pkg.Name)"
100 Write-Host "Package Version: $($pkg.Version)"
101 Write-Host "Package SDK Type: $($pkg.SdkType)"
102 Write-Host "Artifact Name: $($pkg.ArtifactName)"
103 Write-Host "Release date: $($pkg.ReleaseStatus)"
104 $configFilePrefix = $pkg.Name
105 if ($pkg.ArtifactName)
106 {
107 $configFilePrefix = $pkg.ArtifactName
108 }
109 $outputPath = Join-Path -Path $outDirectory "$configFilePrefix.json"
110 Write-Host "Output path of json file: $outputPath"
111 $outDir = Split-Path $outputPath -parent
112 if (-not (Test-Path -path $outDir))
113 {
114 Write-Host "Creating directory $($outDir) for json property file"
115 New-Item -ItemType Directory -Path $outDir
116 }
117 SetOutput $outputPath $pkg
118 }
119
120 Get-ChildItem -Path $outDirectory
121}
122else
123{
124 Write-Error "Package properties are not available for service directory $($serviceDirectory)"
125 exit 1
126}
View as plain text