diff --git a/eng/scripts/automation/Invoke-GenerateAndBuild.ps1 b/eng/scripts/automation/Invoke-GenerateAndBuild.ps1 index 440ad2fa865a4..e40414a2adaae 100644 --- a/eng/scripts/automation/Invoke-GenerateAndBuild.ps1 +++ b/eng/scripts/automation/Invoke-GenerateAndBuild.ps1 @@ -17,7 +17,6 @@ $readmeFile = $inputJson.relatedReadmeMdFile $readmeFile = $readmeFile -replace "\\", "/" $commitid = $inputJson.headSha $repoHttpsUrl = $inputJson.repoHttpsUrl -$serviceType = $inputJson.serviceType $downloadUrlPrefix = $inputJson.installInstructionInput.downloadUrlPrefix $autorestConfig = $inputJson.autorestConfig @@ -69,53 +68,50 @@ if ($readmeFile) { Invoke-GenerateAndBuildSDK -readmeAbsolutePath $readme -sdkRootPath $sdkPath -autorestConfigYaml "$autorestConfigYaml" -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages } +# generate sdk from typespec file if ($relatedTypeSpecProjectFolder) { - $typespecFolder = Resolve-Path (Join-Path $swaggerDir $relatedTypeSpecProjectFolder) - $newPackageOutput = "newPackageOutput.json" + foreach ($typespecRelativeFolder in $relatedTypeSpecProjectFolder) { + $typespecFolder = Resolve-Path (Join-Path $swaggerDir $typespecRelativeFolder) + $processScript = Resolve-Path (Join-Path $sdkPath "eng/common/scripts" "TypeSpec-Project-Process.ps1") + $sdkProjectFolders = Get-ChildItem -Path (Join-Path $sdkPath "sdk") -Depth 1 -Directory | Select-Object -ExpandProperty FullName - $tspConfigYaml = Get-Content -Path (Join-Path "$typespecFolder" "tspconfig.yaml") -Raw - - Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module - $yml = ConvertFrom-YAML $tspConfigYaml - $service = "" - $namespace = "" - if ($yml) { - if ($yml["parameters"] -And $yml["parameters"]["service-directory-name"]) { - $service = $yml["parameters"]["service-directory-name"]["default"]; - } - if ($yml["options"] -And $yml["options"]["@azure-tools/typespec-csharp"] -And $yml["options"]["@azure-tools/typespec-csharp"]["namespace"]) { - $namespace = $yml["options"]["@azure-tools/typespec-csharp"]["namespace"] + # Invoke Process script. SkipSyncAndGenerate only when it's not a new SDK project + $sdkProjectFolder = & $processScript $typespecFolder $commitid $repoHttpsUrl -SkipSyncAndGenerate + if ($LASTEXITCODE) { + # If Process script call fails, then return with failure to CI and don't need to call GeneratePackage + $generatedSDKPackages.Add(@{ + result = "failed"; + path=@(""); + }) + } else { + $relativeSdkPath = Resolve-Path $sdkProjectFolder -Relative + $specRepoRoot = $swaggerDir -replace "/specification", "" + if ($sdkProjectFolders -contains $sdkProjectFolder) { + # Existed SDK project case, needs to generate code + GeneratePackage ` + -projectFolder $sdkProjectFolder ` + -sdkRootPath $sdkPath ` + -path $relativeSdkPath ` + -downloadUrlPrefix $downloadUrlPrefix ` + -serviceType "data-plane" ` + -generatedSDKPackages $generatedSDKPackages ` + -specRepoRoot $specRepoRoot + } else { + # New SDK project case, code is already generated by emitter. So, skip code generation + GeneratePackage ` + -projectFolder $sdkProjectFolder ` + -sdkRootPath $sdkPath ` + -path $relativeSdkPath ` + -downloadUrlPrefix $downloadUrlPrefix ` + -serviceType "data-plane" ` + -skipGenerate ` + -generatedSDKPackages $generatedSDKPackages ` + -specRepoRoot $specRepoRoot + } } } - if (!$service || !$namespace) { - throw "Not provide service name or namespace." - } - $projectFolder = (Join-Path $sdkPath "sdk" $service $namespace) - $specRoot = $swaggerDir - if ((-Not $relatedTypeSpecProjectFolder.Contains("specification")) -And $swaggerDir.Contains("specification")) - { - $relatedTypeSpecProjectFolder = "specification/$relatedTypeSpecProjectFolder" - $specRoot = Split-Path $specRoot - } - New-TypeSpecPackageFolder ` - -service $service ` - -namespace $namespace ` - -sdkPath $sdkPath ` - -relatedTypeSpecProjectFolder $relatedTypeSpecProjectFolder ` - -specRoot $specRoot ` - -outputJsonFile $newpackageoutput - $newPackageOutputJson = Get-Content $newPackageOutput -Raw | ConvertFrom-Json - $relativeSdkPath = $newPackageOutputJson.path - GeneratePackage ` - -projectFolder $projectFolder ` - -sdkRootPath $sdkPath ` - -path $relativeSdkPath ` - -downloadUrlPrefix $downloadUrlPrefix ` - -serviceType "data-plane" ` - -generatedSDKPackages $generatedSDKPackages } $outputJson = [PSCustomObject]@{ packages = $generatedSDKPackages } -$outputJson $outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile \ No newline at end of file