diff --git a/src/SDKs/Compute/Management.Compute/generate.ps1 b/src/SDKs/Compute/Management.Compute/generate.ps1 new file mode 100644 index 000000000000..fc9486fd896e --- /dev/null +++ b/src/SDKs/Compute/Management.Compute/generate.ps1 @@ -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" \ No newline at end of file diff --git a/tools/generate.cmd b/tools/generate.cmd index e938ce262c07..7328d2f424b5 100644 --- a/tools/generate.cmd +++ b/tools/generate.cmd @@ -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 \ No newline at end of file diff --git a/tools/generateTool.ps1 b/tools/generateTool.ps1 new file mode 100644 index 000000000000..e55f1b001b9f --- /dev/null +++ b/tools/generateTool.ps1 @@ -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)) { + 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 +}