From 8d1dcea0de56b5d9a949f909ddf4b46137c2aa5f Mon Sep 17 00:00:00 2001 From: Xiaogang Date: Thu, 25 Feb 2021 12:08:16 +0800 Subject: [PATCH] initial version of Azure Powershell swagger ci (#14308) * initial version of Azure Powershell swagger ci * Remove printenv, which was for debug --- swagger_to_sdk_config.json | 21 +++++++ tools/SwaggerCI/gitignoreconf | 5 ++ tools/SwaggerCI/init.sh | 19 ++++++ tools/SwaggerCI/psci.ps1 | 13 +++++ tools/SwaggerCI/psci.psm1 | 70 +++++++++++++++++++++++ tools/SwaggerCI/readme.azure.noprofile.md | 53 +++++++++++++++++ tools/SwaggerCI/readme.md.template | 10 ++++ 7 files changed, 191 insertions(+) create mode 100644 swagger_to_sdk_config.json create mode 100644 tools/SwaggerCI/gitignoreconf create mode 100644 tools/SwaggerCI/init.sh create mode 100644 tools/SwaggerCI/psci.ps1 create mode 100644 tools/SwaggerCI/psci.psm1 create mode 100644 tools/SwaggerCI/readme.azure.noprofile.md create mode 100644 tools/SwaggerCI/readme.md.template diff --git a/swagger_to_sdk_config.json b/swagger_to_sdk_config.json new file mode 100644 index 000000000000..9bb07d505d9c --- /dev/null +++ b/swagger_to_sdk_config.json @@ -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 + } + } + } + } \ No newline at end of file diff --git a/tools/SwaggerCI/gitignoreconf b/tools/SwaggerCI/gitignoreconf new file mode 100644 index 000000000000..7998f37e1e47 --- /dev/null +++ b/tools/SwaggerCI/gitignoreconf @@ -0,0 +1,5 @@ +bin +obj +.vs +tools +test/*-TestResults.xml \ No newline at end of file diff --git a/tools/SwaggerCI/init.sh b/tools/SwaggerCI/init.sh new file mode 100644 index 000000000000..14f0edd4d5aa --- /dev/null +++ b/tools/SwaggerCI/init.sh @@ -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 \ + && 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 diff --git a/tools/SwaggerCI/psci.ps1 b/tools/SwaggerCI/psci.ps1 new file mode 100644 index 000000000000..0fa037062390 --- /dev/null +++ b/tools/SwaggerCI/psci.ps1 @@ -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 diff --git a/tools/SwaggerCI/psci.psm1 b/tools/SwaggerCI/psci.psm1 new file mode 100644 index 000000000000..55e15f4b1778 --- /dev/null +++ b/tools/SwaggerCI/psci.psm1 @@ -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 \ No newline at end of file diff --git a/tools/SwaggerCI/readme.azure.noprofile.md b/tools/SwaggerCI/readme.azure.noprofile.md new file mode 100644 index 000000000000..8048f8a48009 --- /dev/null +++ b/tools/SwaggerCI/readme.azure.noprofile.md @@ -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' +``` \ No newline at end of file diff --git a/tools/SwaggerCI/readme.md.template b/tools/SwaggerCI/readme.md.template new file mode 100644 index 000000000000..fd51de1a529b --- /dev/null +++ b/tools/SwaggerCI/readme.md.template @@ -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) +``` \ No newline at end of file