Skip to content
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
1 change: 1 addition & 0 deletions src/SDKs/Compute/Management.Compute/generate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File "$(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)\..\..\..\..\tools\generateTool.ps1" -ResourceProvider "compute/resource-manager" -SdkDirectory "$(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)" -PowershellInvoker -AutoRestVersion "latest"
20 changes: 5 additions & 15 deletions tools/generate.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,13 @@ if not "%req_help%" == "" (

:: repo information
set rp="%1"
if not "%2" == "" (set version="%2") else (set version="latest")
if not "%3" == "" (set specsRepoUser="%3") else (set specsRepoUser="Azure")
if not "%2" == "" (set autoRestVersion="%2") else (set autoRestVersion="latest")
if not "%3" == "" (set specsRepoFork="%3") else (set specsRepoFork="Azure")
if not "%4" == "" (set specsRepoBranch="%4") else (set specsRepoBranch="master")
if not "%5" == "" (set specsRepoName="%5") else (set specsRepoName="azure-rest-api-specs")
if not "%6" == "" (set sdksFolder="%6") else (set sdksFolder=%~dp0..\src\SDKS)
set configFile="https://github.com/%specsRepoUser%/%specsRepoName%/blob/%specsRepoBranch%/specification/%rp%/readme.md"

:: installation
if "%7" == "" (call npm i -g autorest)
if not "%6" == "" (set sdkDirectory="%6") else (set sdkDirectory=%~dp0..\src\SDKS)

:: code generation
@echo on
call autorest %configFile% --csharp --csharp-sdks-folder=%sdksFolder% --version=%version% --reflect-api-versions
@echo off

:: metadata
mkdir %~dp0\..\src\SDKs\_metadata >nul 2>&1
call powershell %~dp0\generateMetadata.ps1 %specsRepoUser% %specsRepoBranch% %version% %configFile% > %~dp0\..\src\SDKs\_metadata\%rp:/=_%.txt

endlocal
call powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File "%~dp0\generateTool.ps1" -ResourceProvider %rp% -SpecsRepoFork %specsRepoFork% -SpecsRepoBranch %specsRepoBranch% -SpecsRepoName %specsRepoName% -SdkDirectory %sdkDirectory% -AutoRestVersion %autoRestVersion%
@echo off
121 changes: 121 additions & 0 deletions tools/generateTool.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<#

.SYNOPSIS
Powershell script that generates the C# code for your sdk usin the config file provided

.DESCRIPTION
This script:
- fetches the config file from user/branch provided
- Generates code based off the config file provided
- into the directory path provided

.PARAMETER ResourceProvider
The Resource provider for whom to generate the code; also helps determine path where config file is located in repo

.PARAMETER Version
The AutoRest version to use to generate the code, "latest" is recommended

.PARAMETER SpecsRepoFork
The Rest Spec repo fork which contains the config file

.PARAMETER SpecsRepoBranch
The Branch which contains the config file

.PARAMETER SpecsRepoName
The name of the repo that contains the config file (Can only be either of azure-rest-api-specs or azure-rest-api-specs-pr)

.PARAMETER SdkFolder
The path where to generate the code

.EXAMPLE
A sample command that uses the function or script, optionally followed
by sample output and a description. Repeat this keyword for each example.

.NOTES
Additional information about the function or script.

.LINK
The name of a related topic. Repeat this keyword for each related topic.

This content appears in the Related Links section of the Help topic.

The Link keyword content can also include a Uniform Resource Identifier
(URI) to an online version of the same Help topic. The online version
opens when you use the Online parameter of Get-Help. The URI must begin
with "http" or "https".
#>

Param(
[Parameter(Mandatory = $true)]
[string] $ResourceProvider = "compute/resource-manager",
[string] $SpecsRepoFork = "Azure",
[string] $SpecsRepoName = "azure-rest-api-specs",
[string] $SpecsRepoBranch = "master",
[Parameter(Mandatory = $true)]
[string] $SdkDirectory= "$([System.IO.Path]::GetTempPath())\Compute",
[string] $AutoRestVersion = "latest",
[switch] $PowershellInvoker
)

Write-Host "autorest version received is : $AutoRestVersion"

$errorStream = New-Object -TypeName "System.Text.StringBuilder";
$outputStream = New-Object -TypeName "System.Text.StringBuilder";
$currPath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
$modulePath = "$currPath\SdkBuildTools\psModules\CodeGenerationModules\generateDotNetSdkCode.psm1"
$logFile = "$currPath\..\src\SDKs\_metadata\$($ResourceProvider.Replace("/","_")).txt"
$errorFile = "$currPath\SdkBuildTools\errorlog.txt"

function NotifyError {
param (
[string] $errorMsg
)
Write-Error $errorMsg
$errorMsg | Out-File -FilePath $errorFile
Start-Process "$errorFile"
}

if (-not ($modulePath | Test-Path)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dsgouda just curios why can't we have line 78 to 88 wrapped in a function and that function getting called right before Start-Script on lin 110?
The sole reason I say that is because then in your entire scripts the only execution points are via function, no more free flowing execution of any script.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without loading the module it won't be possible to move the execution of this piece of code in the try..catch..finally block, the catch and finally block need some functions imported from the module itself (eg. Get-OutputStream)

NotifyError "Could not find code generation module at: $modulePath. Please run `msbuild build.proj` to install the module."
Exit -1
}

Import-Module "$modulePath"

if (-not (Get-Module -ListAvailable -Name "$modulePath")) {
NotifyError "Could not find module: $modulePath. Please run `msbuild build.proj` to install the module."
Exit -1
}

function Start-Script {
Write-InfoLog "Importing code generation module" -logToConsole

Install-AutoRest $AutoRestVersion

$configFile="https://github.com/$SpecsRepoFork/$SpecsRepoName/blob/$SpecsRepoBranch/specification/$ResourceProvider/readme.md"
Write-InfoLog "Commencing code generation" -logToConsole
Start-CodeGeneration -SpecsRepoFork $SpecsRepoFork -SpecsRepoBranch $SpecsRepoBranch -SdkDirectory $SdkDirectory -AutoRestVersion $AutoRestVersion -SpecsRepoName $SpecsRepoName

$invokerMessage = ".\tools\generate.ps1 was invoked by"
if($PowershellInvoker) {
$outputStream.Append("$invokerMessage generate.ps1") | Out-Null
}
else {
$outputStream.Append("$invokerMessage generate.cmd") | Out-Null
}

}

try {
Start-Script
}
catch {
Write-ErrorLog $_.ToString() -logToConsole
Write-ErrorLog $_.ToString() -logToFile
}
finally {
Get-OutputStream | Out-File -FilePath $logFile | Out-Null
Get-ErrorStream | Out-File -FilePath $logFile -Append | Out-Null
Clear-OutputStreams
Get-Module -Name "$modulePath" | Remove-Module
}