-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync eng/common directory with azure-sdk-tools for PR 5691 (#34028)
* typespec renaming * add back scripts for cadl * support .cadl and .tsp * rename * add newline at the end of file --------- Co-authored-by: FAREAST\chunyu <chunyu@microsoft.com>
- Loading branch information
Showing
2 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/TypeSpec-Project-Scripts.md | ||
|
||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Position=0)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $ProjectDirectory, | ||
[Parameter(Position=1)] | ||
[string] $typespecAdditionalOptions ## addtional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 | ||
) | ||
|
||
$ErrorActionPreference = "Stop" | ||
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1 | ||
. $PSScriptRoot/common.ps1 | ||
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module | ||
|
||
function NpmInstallForProject([string]$workingDirectory) { | ||
Push-Location $workingDirectory | ||
try { | ||
$currentDur = Resolve-Path "." | ||
Write-Host "Generating from $currentDur" | ||
|
||
if (Test-Path "package.json") { | ||
Remove-Item -Path "package.json" -Force | ||
} | ||
|
||
if (Test-Path ".npmrc") { | ||
Remove-Item -Path ".npmrc" -Force | ||
} | ||
|
||
if (Test-Path "node_modules") { | ||
Remove-Item -Path "node_modules" -Force -Recurse | ||
} | ||
|
||
if (Test-Path "package-lock.json") { | ||
Remove-Item -Path "package-lock.json" -Force | ||
} | ||
|
||
#default to root/eng/emitter-package.json but you can override by writing | ||
#Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1 | ||
$replacementPackageJson = "$PSScriptRoot/../../emitter-package.json" | ||
if (Test-Path "Function:$GetEmitterPackageJsonPathFn") { | ||
$replacementPackageJson = &$GetEmitterPackageJsonPathFn | ||
} | ||
|
||
Write-Host("Copying package.json from $replacementPackageJson") | ||
Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force | ||
npm install --no-lock-file | ||
if ($LASTEXITCODE) { exit $LASTEXITCODE } | ||
} | ||
finally { | ||
Pop-Location | ||
} | ||
} | ||
|
||
$resolvedProjectDirectory = Resolve-Path $ProjectDirectory | ||
$emitterName = &$GetEmitterNameFn | ||
$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml" | ||
|
||
Write-Host "Reading configuration from $typespecConfigurationFile" | ||
$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml | ||
|
||
$specSubDirectory = $configuration["directory"] | ||
$innerFolder = Split-Path $specSubDirectory -Leaf | ||
|
||
$tempFolder = "$ProjectDirectory/TempTypeSpecFiles" | ||
$npmWorkingDir = Resolve-Path $tempFolder/$innerFolder | ||
$mainTypeSpecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$npmWorkingDir/client.*" } Else { Resolve-Path "$npmWorkingDir/main.*"} | ||
|
||
try { | ||
Push-Location $npmWorkingDir | ||
NpmInstallForProject $npmWorkingDir | ||
|
||
if ($LASTEXITCODE) { exit $LASTEXITCODE } | ||
|
||
if (Test-Path "Function:$GetEmitterAdditionalOptionsFn") { | ||
$emitterAdditionalOptions = &$GetEmitterAdditionalOptionsFn $resolvedProjectDirectory | ||
if ($emitterAdditionalOptions.Length -gt 0) { | ||
$emitterAdditionalOptions = " $emitterAdditionalOptions" | ||
} | ||
} | ||
$typespecCompileCommand = "npx tsp compile $mainTypeSpecFile --emit $emitterName$emitterAdditionalOptions" | ||
if ($typespecAdditionalOptions) { | ||
$options = $typespecAdditionalOptions.Split(";"); | ||
foreach ($option in $options) { | ||
$typespecCompileCommand += " --option $emitterName.$option" | ||
} | ||
} | ||
Write-Host($typespecCompileCommand) | ||
Invoke-Expression $typespecCompileCommand | ||
|
||
if ($LASTEXITCODE) { exit $LASTEXITCODE } | ||
} | ||
finally { | ||
Pop-Location | ||
} | ||
|
||
$shouldCleanUp = $configuration["cleanup"] ?? $true | ||
if ($shouldCleanUp) { | ||
Remove-Item $tempFolder -Recurse -Force | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/TypeSpec-Project-Scripts.md | ||
|
||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Position=0)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $ProjectDirectory | ||
) | ||
|
||
$ErrorActionPreference = "Stop" | ||
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1 | ||
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module | ||
$sparseCheckoutFile = ".git/info/sparse-checkout" | ||
|
||
function AddSparseCheckoutPath([string]$subDirectory) { | ||
if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) { | ||
Write-Output $subDirectory >> .git/info/sparse-checkout | ||
} | ||
} | ||
|
||
function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) { | ||
$source = "$specCloneRoot/$mainSpecDir" | ||
Copy-Item -Path $source -Destination $dest -Recurse -Force | ||
Write-Host "Copying spec from $source to $dest" | ||
|
||
foreach ($additionalDir in $specAdditionalSubDirectories) { | ||
$source = "$specCloneRoot/$additionalDir" | ||
Write-Host "Copying spec from $source to $dest" | ||
Copy-Item -Path $source -Destination $dest -Recurse -Force | ||
} | ||
} | ||
|
||
function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) { | ||
AddSparseCheckoutPath $mainSpecDir | ||
foreach ($subDir in $specAdditionalSubDirectories) { | ||
AddSparseCheckoutPath $subDir | ||
} | ||
} | ||
|
||
function GetGitRemoteValue([string]$repo) { | ||
Push-Location $ProjectDirectory | ||
$result = "" | ||
try { | ||
$gitRemotes = (git remote -v) | ||
foreach ($remote in $gitRemotes) { | ||
if ($remote.StartsWith("origin")) { | ||
if ($remote -match 'https://github.com/\S+') { | ||
$result = "https://github.com/$repo.git" | ||
break | ||
} elseif ($remote -match "git@github.com:\S+"){ | ||
$result = "git@github.com:$repo.git" | ||
break | ||
} else { | ||
throw "Unknown git remote format found: $remote" | ||
} | ||
} | ||
} | ||
} | ||
finally { | ||
Pop-Location | ||
} | ||
|
||
return $result | ||
} | ||
|
||
function InitializeSparseGitClone([string]$repo) { | ||
git clone --no-checkout --filter=tree:0 $repo . | ||
if ($LASTEXITCODE) { exit $LASTEXITCODE } | ||
git sparse-checkout init | ||
if ($LASTEXITCODE) { exit $LASTEXITCODE } | ||
Remove-Item $sparseCheckoutFile -Force | ||
} | ||
|
||
function GetSpecCloneDir([string]$projectName) { | ||
Push-Location $ProjectDirectory | ||
try { | ||
$root = git rev-parse --show-toplevel | ||
} | ||
finally { | ||
Pop-Location | ||
} | ||
|
||
$sparseSpecCloneDir = "$root/../sparse-spec/$projectName" | ||
New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null | ||
$createResult = Resolve-Path $sparseSpecCloneDir | ||
return $createResult | ||
} | ||
|
||
$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml" | ||
Write-Host "Reading configuration from $typespecConfigurationFile" | ||
$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml | ||
|
||
$pieces = $typespecConfigurationFile.Path.Replace("\","/").Split("/") | ||
$projectName = $pieces[$pieces.Count - 2] | ||
|
||
$specSubDirectory = $configuration["directory"] | ||
|
||
if ( $configuration["repo"] -and $configuration["commit"]) { | ||
$specCloneDir = GetSpecCloneDir $projectName | ||
$gitRemoteValue = GetGitRemoteValue $configuration["repo"] | ||
|
||
Write-Host "Setting up sparse clone for $projectName at $specCloneDir" | ||
|
||
Push-Location $specCloneDir.Path | ||
try { | ||
if (!(Test-Path ".git")) { | ||
InitializeSparseGitClone $gitRemoteValue | ||
UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"] | ||
} | ||
git checkout $configuration["commit"] | ||
if ($LASTEXITCODE) { exit $LASTEXITCODE } | ||
} | ||
finally { | ||
Pop-Location | ||
} | ||
} elseif ( $configuration["spec-root-dir"] ) { | ||
$specCloneDir = $configuration["spec-root-dir"] | ||
} | ||
|
||
|
||
$tempTypeSpecDir = "$ProjectDirectory/TempTypeSpecFiles" | ||
New-Item $tempTypeSpecDir -Type Directory -Force | Out-Null | ||
CopySpecToProjectIfNeeded ` | ||
-specCloneRoot $specCloneDir ` | ||
-mainSpecDir $specSubDirectory ` | ||
-dest $tempTypeSpecDir ` | ||
-specAdditionalSubDirectories $configuration["additionalDirectories"] |