Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial version of Azure Powershell swagger ci #14308

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions swagger_to_sdk_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json",
"generateOptions": {
"generateScript": {
"path": "pwsh ./tools/SwaggerCI/psci.ps1",
"stderr": {
"showInComment": "^\\[AUTOREST\\]",
"scriptError": "^\\[ERROR\\]",
"scriptWarning": "^\\[WARNING\\]"
}
}
},
"initOptions": {
"initScript": {
"path": "sh ./tools/SwaggerCI/init.sh",
"stderr": {
"scriptWarning": false
}
}
}
}
5 changes: 5 additions & 0 deletions tools/SwaggerCI/gitignoreconf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin
dolauli marked this conversation as resolved.
Show resolved Hide resolved
obj
.vs
tools
test/*-TestResults.xml
19 changes: 19 additions & 0 deletions tools/SwaggerCI/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash -i

# This script is created to set up the env to generate Azure PowerShell modules based on swagger.

# Installed required packages
sudo apt-get update \
dolauli marked this conversation as resolved.
Show resolved Hide resolved
&& sudo apt-get install -y curl \
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash \
&& export NVM_DIR="$HOME/.nvm" \
&& [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" \
&& [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" \
&& nvm install 14.15.5 \
&& npm config set unsafe-perm true \
&& npm install -g autorest \
&& npm install -g dotnet-sdk-2.1

# Write $PATH and some other envs to a file for later usage

echo "{\"envs\":{\"PATH\":\"$PATH\",\"DOTNET_SKIP_FIRST_TIME_EXPERIENCE\":1}}" > $2
13 changes: 13 additions & 0 deletions tools/SwaggerCI/psci.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
param(
[Parameter(Position=0, Mandatory=$true)]
[string]
$InputFile,

[Parameter(Position=1, Mandatory=$true)]
[string]
$OutputFile
)

Import-Module (Join-Path $PSScriptRoot 'psci.psm1')

Invoke-SwaggerCI -ConfigFilePath $InputFile -ResultFilePath $OutputFile
70 changes: 70 additions & 0 deletions tools/SwaggerCI/psci.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function Invoke-SwaggerCI {
param(
[string] $ConfigFilePath = "./generateInput.json",
[string] $ResultFilePath = "./generateOutput.json"
)

# Get the readme.md files need to be run from config file
$config = Get-Content $ConfigFilePath | ConvertFrom-Json

$packages = @()
$downloadPrefix = $config.installInstructionInput.downloadUrlPrefix
foreach ($rd in $config.relatedReadmeMdFiles) {
try {
$rdFolder = Join-Path $config.specFolder (Split-Path $rd -Parent)
$rdPath = Join-Path $rdFolder "readme.md"
$psRdPath = Join-Path $rdFolder "readme.powershell.md"
$modulePath = $rd.split("/")[1]

#create the a folder for this RP
$moduleFolder = Join-Path (Join-Path (pwd).Path swaggerci) $modulePath
New-Item -Path $moduleFolder -ItemType Directory -Force

#populate read.md.template
$rdContent = Get-Content ./tools/SwaggerCI/readme.md.template
$rdContent = $rdContent.replace('$(readme.md)', $rdPath)
$rdContent = $rdContent.replace('$(readme.powershell.md)', $psRdPath)
$rdContent | Out-File -Path (Join-Path $moduleFolder "readme.md")

#generate code
autorest (Join-Path $moduleFolder "readme.md") --version:3.0.6371
#Build the module
. (Join-Path $moduleFolder "build-module.ps1")
#Override the generated .gitignore file
cp ./tools/SwaggerCI/gitignoreconf (Join-Path $moduleFolder ".gitignore")
#Package
. (Join-Path $moduleFolder "pack-module.ps1")

$moduleName = (Get-ChildItem -Path $moduleFolder -Recurse -Filter "*.nupkg").Name.Split('.')[1]

#Generate result
$downloadUrl = $config.installInstructionInput.downloadUrlPrefix + "Az.$moduleName/Az.$moduleName.0.1.0.nupkg"
$downloadCmd = "curl -L $downloadUrl -o Az.$moduleName.0.1.0.nupkg"
$package = @{
packageName = "Az.$moduleName"
path = @("swaggerci/$modulePath")
readmeMd = @($rd)
artifacts = @("swaggerci/$modulePath/bin/Az.$moduleName.0.1.0.nupkg")
installInstructions = @{full = "Please download the package through the curl command '$downloadCmd', and then you could have a try locally."}
result = "succeeded"
}
$packages += $package
} catch {
$package = @{
packageName = "Az.$moduleName"
path = @("swaggerci/$modulePath")
readmeMd = @($rd)
result = "warning"
}
$packages += $package
}
}

$result = @{
packages = $packages
}

$result | ConvertTo-Json -Depth 5 | Out-File -Path $ResultFilePath
}

Export-ModuleMember -Function Invoke-SwaggerCI
53 changes: 53 additions & 0 deletions tools/SwaggerCI/readme.azure.noprofile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Azure PowerShell AutoRest Configuration

> Values
``` yaml
azure: true
powershell: true
license-header: MICROSOFT_MIT_NO_VERSION
branch: master
repo: https://github.com/Azure/azure-rest-api-specs/blob/$(branch)
metadata:
authors: Microsoft Corporation
owners: Microsoft Corporation
description: 'Microsoft Azure PowerShell: $(service-name) cmdlets'
copyright: Microsoft Corporation. All rights reserved.
tags: Azure ResourceManager ARM PSModule $(service-name)
companyName: Microsoft Corporation
requireLicenseAcceptance: true
licenseUri: https://aka.ms/azps-license
projectUri: https://github.com/Azure/azure-powershell
```

> Names
``` yaml
prefix: Az
subject-prefix: $(service-name)
module-name: $(prefix).$(service-name)
namespace: Microsoft.Azure.PowerShell.Cmdlets.$(service-name)
```

> Folders
``` yaml
clear-output-folder: true
output-folder: .
```

> Directives
``` yaml
directive:
- where:
subject: Operation
hide: true
- where:
verb: Set
remove: true
- where:
variant: ^Create$|^CreateViaIdentity$|^CreateViaIdentityExpanded$|^Update$|^UpdateViaIdentity$
remove: true
- where:
parameter-name: SubscriptionId
set:
default:
script: '(Get-AzContext).Subscription.Id'
```
10 changes: 10 additions & 0 deletions tools/SwaggerCI/readme.md.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### AutoRest Configuration
> see https://aka.ms/autorest

``` yaml
require:
- $(this-folder)/../../tools/SwaggerCI/readme.azure.noprofile.md
- $(this-folder)/../../$(readme.md)
try-require:
- $(this-folder)/../../$(readme.powershell.md)
```