Skip to content

Commit 8d1dcea

Browse files
authored
initial version of Azure Powershell swagger ci (#14308)
* initial version of Azure Powershell swagger ci * Remove printenv, which was for debug
1 parent 93908bb commit 8d1dcea

7 files changed

+191
-0
lines changed

swagger_to_sdk_config.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json",
3+
"generateOptions": {
4+
"generateScript": {
5+
"path": "pwsh ./tools/SwaggerCI/psci.ps1",
6+
"stderr": {
7+
"showInComment": "^\\[AUTOREST\\]",
8+
"scriptError": "^\\[ERROR\\]",
9+
"scriptWarning": "^\\[WARNING\\]"
10+
}
11+
}
12+
},
13+
"initOptions": {
14+
"initScript": {
15+
"path": "sh ./tools/SwaggerCI/init.sh",
16+
"stderr": {
17+
"scriptWarning": false
18+
}
19+
}
20+
}
21+
}

tools/SwaggerCI/gitignoreconf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin
2+
obj
3+
.vs
4+
tools
5+
test/*-TestResults.xml

tools/SwaggerCI/init.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash -i
2+
3+
# This script is created to set up the env to generate Azure PowerShell modules based on swagger.
4+
5+
# Installed required packages
6+
sudo apt-get update \
7+
&& sudo apt-get install -y curl \
8+
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash \
9+
&& export NVM_DIR="$HOME/.nvm" \
10+
&& [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" \
11+
&& [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" \
12+
&& nvm install 14.15.5 \
13+
&& npm config set unsafe-perm true \
14+
&& npm install -g autorest \
15+
&& npm install -g dotnet-sdk-2.1
16+
17+
# Write $PATH and some other envs to a file for later usage
18+
19+
echo "{\"envs\":{\"PATH\":\"$PATH\",\"DOTNET_SKIP_FIRST_TIME_EXPERIENCE\":1}}" > $2

tools/SwaggerCI/psci.ps1

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
param(
2+
[Parameter(Position=0, Mandatory=$true)]
3+
[string]
4+
$InputFile,
5+
6+
[Parameter(Position=1, Mandatory=$true)]
7+
[string]
8+
$OutputFile
9+
)
10+
11+
Import-Module (Join-Path $PSScriptRoot 'psci.psm1')
12+
13+
Invoke-SwaggerCI -ConfigFilePath $InputFile -ResultFilePath $OutputFile

tools/SwaggerCI/psci.psm1

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function Invoke-SwaggerCI {
2+
param(
3+
[string] $ConfigFilePath = "./generateInput.json",
4+
[string] $ResultFilePath = "./generateOutput.json"
5+
)
6+
7+
# Get the readme.md files need to be run from config file
8+
$config = Get-Content $ConfigFilePath | ConvertFrom-Json
9+
10+
$packages = @()
11+
$downloadPrefix = $config.installInstructionInput.downloadUrlPrefix
12+
foreach ($rd in $config.relatedReadmeMdFiles) {
13+
try {
14+
$rdFolder = Join-Path $config.specFolder (Split-Path $rd -Parent)
15+
$rdPath = Join-Path $rdFolder "readme.md"
16+
$psRdPath = Join-Path $rdFolder "readme.powershell.md"
17+
$modulePath = $rd.split("/")[1]
18+
19+
#create the a folder for this RP
20+
$moduleFolder = Join-Path (Join-Path (pwd).Path swaggerci) $modulePath
21+
New-Item -Path $moduleFolder -ItemType Directory -Force
22+
23+
#populate read.md.template
24+
$rdContent = Get-Content ./tools/SwaggerCI/readme.md.template
25+
$rdContent = $rdContent.replace('$(readme.md)', $rdPath)
26+
$rdContent = $rdContent.replace('$(readme.powershell.md)', $psRdPath)
27+
$rdContent | Out-File -Path (Join-Path $moduleFolder "readme.md")
28+
29+
#generate code
30+
autorest (Join-Path $moduleFolder "readme.md") --version:3.0.6371
31+
#Build the module
32+
. (Join-Path $moduleFolder "build-module.ps1")
33+
#Override the generated .gitignore file
34+
cp ./tools/SwaggerCI/gitignoreconf (Join-Path $moduleFolder ".gitignore")
35+
#Package
36+
. (Join-Path $moduleFolder "pack-module.ps1")
37+
38+
$moduleName = (Get-ChildItem -Path $moduleFolder -Recurse -Filter "*.nupkg").Name.Split('.')[1]
39+
40+
#Generate result
41+
$downloadUrl = $config.installInstructionInput.downloadUrlPrefix + "Az.$moduleName/Az.$moduleName.0.1.0.nupkg"
42+
$downloadCmd = "curl -L $downloadUrl -o Az.$moduleName.0.1.0.nupkg"
43+
$package = @{
44+
packageName = "Az.$moduleName"
45+
path = @("swaggerci/$modulePath")
46+
readmeMd = @($rd)
47+
artifacts = @("swaggerci/$modulePath/bin/Az.$moduleName.0.1.0.nupkg")
48+
installInstructions = @{full = "Please download the package through the curl command '$downloadCmd', and then you could have a try locally."}
49+
result = "succeeded"
50+
}
51+
$packages += $package
52+
} catch {
53+
$package = @{
54+
packageName = "Az.$moduleName"
55+
path = @("swaggerci/$modulePath")
56+
readmeMd = @($rd)
57+
result = "warning"
58+
}
59+
$packages += $package
60+
}
61+
}
62+
63+
$result = @{
64+
packages = $packages
65+
}
66+
67+
$result | ConvertTo-Json -Depth 5 | Out-File -Path $ResultFilePath
68+
}
69+
70+
Export-ModuleMember -Function Invoke-SwaggerCI
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Azure PowerShell AutoRest Configuration
2+
3+
> Values
4+
``` yaml
5+
azure: true
6+
powershell: true
7+
license-header: MICROSOFT_MIT_NO_VERSION
8+
branch: master
9+
repo: https://github.com/Azure/azure-rest-api-specs/blob/$(branch)
10+
metadata:
11+
authors: Microsoft Corporation
12+
owners: Microsoft Corporation
13+
description: 'Microsoft Azure PowerShell: $(service-name) cmdlets'
14+
copyright: Microsoft Corporation. All rights reserved.
15+
tags: Azure ResourceManager ARM PSModule $(service-name)
16+
companyName: Microsoft Corporation
17+
requireLicenseAcceptance: true
18+
licenseUri: https://aka.ms/azps-license
19+
projectUri: https://github.com/Azure/azure-powershell
20+
```
21+
22+
> Names
23+
``` yaml
24+
prefix: Az
25+
subject-prefix: $(service-name)
26+
module-name: $(prefix).$(service-name)
27+
namespace: Microsoft.Azure.PowerShell.Cmdlets.$(service-name)
28+
```
29+
30+
> Folders
31+
``` yaml
32+
clear-output-folder: true
33+
output-folder: .
34+
```
35+
36+
> Directives
37+
``` yaml
38+
directive:
39+
- where:
40+
subject: Operation
41+
hide: true
42+
- where:
43+
verb: Set
44+
remove: true
45+
- where:
46+
variant: ^Create$|^CreateViaIdentity$|^CreateViaIdentityExpanded$|^Update$|^UpdateViaIdentity$
47+
remove: true
48+
- where:
49+
parameter-name: SubscriptionId
50+
set:
51+
default:
52+
script: '(Get-AzContext).Subscription.Id'
53+
```

tools/SwaggerCI/readme.md.template

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### AutoRest Configuration
2+
> see https://aka.ms/autorest
3+
4+
``` yaml
5+
require:
6+
- $(this-folder)/../../tools/SwaggerCI/readme.azure.noprofile.md
7+
- $(this-folder)/../../$(readme.md)
8+
try-require:
9+
- $(this-folder)/../../$(readme.powershell.md)
10+
```

0 commit comments

Comments
 (0)