Skip to content

Commit 65d4cb3

Browse files
azure-sdkchunyu3
andauthored
Sync eng/common directory with azure-sdk-tools for PR 5691 (Azure#20411)
* typespec renaming * add back scripts for cadl * support .cadl and .tsp * rename * add newline at the end of file --------- Co-authored-by: FAREAST\chunyu <[email protected]>
1 parent c461f81 commit 65d4cb3

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/TypeSpec-Project-Scripts.md
2+
3+
[CmdletBinding()]
4+
param (
5+
[Parameter(Position=0)]
6+
[ValidateNotNullOrEmpty()]
7+
[string] $ProjectDirectory,
8+
[Parameter(Position=1)]
9+
[string] $typespecAdditionalOptions ## addtional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2
10+
)
11+
12+
$ErrorActionPreference = "Stop"
13+
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
14+
. $PSScriptRoot/common.ps1
15+
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
16+
17+
function NpmInstallForProject([string]$workingDirectory) {
18+
Push-Location $workingDirectory
19+
try {
20+
$currentDur = Resolve-Path "."
21+
Write-Host "Generating from $currentDur"
22+
23+
if (Test-Path "package.json") {
24+
Remove-Item -Path "package.json" -Force
25+
}
26+
27+
if (Test-Path ".npmrc") {
28+
Remove-Item -Path ".npmrc" -Force
29+
}
30+
31+
if (Test-Path "node_modules") {
32+
Remove-Item -Path "node_modules" -Force -Recurse
33+
}
34+
35+
if (Test-Path "package-lock.json") {
36+
Remove-Item -Path "package-lock.json" -Force
37+
}
38+
39+
#default to root/eng/emitter-package.json but you can override by writing
40+
#Get-${Language}-EmitterPackageJsonPath in your Language-Settings.ps1
41+
$replacementPackageJson = "$PSScriptRoot/../../emitter-package.json"
42+
if (Test-Path "Function:$GetEmitterPackageJsonPathFn") {
43+
$replacementPackageJson = &$GetEmitterPackageJsonPathFn
44+
}
45+
46+
Write-Host("Copying package.json from $replacementPackageJson")
47+
Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force
48+
npm install --no-lock-file
49+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
50+
}
51+
finally {
52+
Pop-Location
53+
}
54+
}
55+
56+
$resolvedProjectDirectory = Resolve-Path $ProjectDirectory
57+
$emitterName = &$GetEmitterNameFn
58+
$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml"
59+
60+
Write-Host "Reading configuration from $typespecConfigurationFile"
61+
$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml
62+
63+
$specSubDirectory = $configuration["directory"]
64+
$innerFolder = Split-Path $specSubDirectory -Leaf
65+
66+
$tempFolder = "$ProjectDirectory/TempTypeSpecFiles"
67+
$npmWorkingDir = Resolve-Path $tempFolder/$innerFolder
68+
$mainTypeSpecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$npmWorkingDir/client.*" } Else { Resolve-Path "$npmWorkingDir/main.*"}
69+
70+
try {
71+
Push-Location $npmWorkingDir
72+
NpmInstallForProject $npmWorkingDir
73+
74+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
75+
76+
if (Test-Path "Function:$GetEmitterAdditionalOptionsFn") {
77+
$emitterAdditionalOptions = &$GetEmitterAdditionalOptionsFn $resolvedProjectDirectory
78+
if ($emitterAdditionalOptions.Length -gt 0) {
79+
$emitterAdditionalOptions = " $emitterAdditionalOptions"
80+
}
81+
}
82+
$typespecCompileCommand = "npx tsp compile $mainTypeSpecFile --emit $emitterName$emitterAdditionalOptions"
83+
if ($typespecAdditionalOptions) {
84+
$options = $typespecAdditionalOptions.Split(";");
85+
foreach ($option in $options) {
86+
$typespecCompileCommand += " --option $emitterName.$option"
87+
}
88+
}
89+
Write-Host($typespecCompileCommand)
90+
Invoke-Expression $typespecCompileCommand
91+
92+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
93+
}
94+
finally {
95+
Pop-Location
96+
}
97+
98+
$shouldCleanUp = $configuration["cleanup"] ?? $true
99+
if ($shouldCleanUp) {
100+
Remove-Item $tempFolder -Recurse -Force
101+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/TypeSpec-Project-Scripts.md
2+
3+
[CmdletBinding()]
4+
param (
5+
[Parameter(Position=0)]
6+
[ValidateNotNullOrEmpty()]
7+
[string] $ProjectDirectory
8+
)
9+
10+
$ErrorActionPreference = "Stop"
11+
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
12+
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
13+
$sparseCheckoutFile = ".git/info/sparse-checkout"
14+
15+
function AddSparseCheckoutPath([string]$subDirectory) {
16+
if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) {
17+
Write-Output $subDirectory >> .git/info/sparse-checkout
18+
}
19+
}
20+
21+
function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) {
22+
$source = "$specCloneRoot/$mainSpecDir"
23+
Copy-Item -Path $source -Destination $dest -Recurse -Force
24+
Write-Host "Copying spec from $source to $dest"
25+
26+
foreach ($additionalDir in $specAdditionalSubDirectories) {
27+
$source = "$specCloneRoot/$additionalDir"
28+
Write-Host "Copying spec from $source to $dest"
29+
Copy-Item -Path $source -Destination $dest -Recurse -Force
30+
}
31+
}
32+
33+
function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) {
34+
AddSparseCheckoutPath $mainSpecDir
35+
foreach ($subDir in $specAdditionalSubDirectories) {
36+
AddSparseCheckoutPath $subDir
37+
}
38+
}
39+
40+
function GetGitRemoteValue([string]$repo) {
41+
Push-Location $ProjectDirectory
42+
$result = ""
43+
try {
44+
$gitRemotes = (git remote -v)
45+
foreach ($remote in $gitRemotes) {
46+
if ($remote.StartsWith("origin")) {
47+
if ($remote -match 'https://github.com/\S+') {
48+
$result = "https://github.com/$repo.git"
49+
break
50+
} elseif ($remote -match "[email protected]:\S+"){
51+
$result = "[email protected]:$repo.git"
52+
break
53+
} else {
54+
throw "Unknown git remote format found: $remote"
55+
}
56+
}
57+
}
58+
}
59+
finally {
60+
Pop-Location
61+
}
62+
63+
return $result
64+
}
65+
66+
function InitializeSparseGitClone([string]$repo) {
67+
git clone --no-checkout --filter=tree:0 $repo .
68+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
69+
git sparse-checkout init
70+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
71+
Remove-Item $sparseCheckoutFile -Force
72+
}
73+
74+
function GetSpecCloneDir([string]$projectName) {
75+
Push-Location $ProjectDirectory
76+
try {
77+
$root = git rev-parse --show-toplevel
78+
}
79+
finally {
80+
Pop-Location
81+
}
82+
83+
$sparseSpecCloneDir = "$root/../sparse-spec/$projectName"
84+
New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null
85+
$createResult = Resolve-Path $sparseSpecCloneDir
86+
return $createResult
87+
}
88+
89+
$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml"
90+
Write-Host "Reading configuration from $typespecConfigurationFile"
91+
$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml
92+
93+
$pieces = $typespecConfigurationFile.Path.Replace("\","/").Split("/")
94+
$projectName = $pieces[$pieces.Count - 2]
95+
96+
$specSubDirectory = $configuration["directory"]
97+
98+
if ( $configuration["repo"] -and $configuration["commit"]) {
99+
$specCloneDir = GetSpecCloneDir $projectName
100+
$gitRemoteValue = GetGitRemoteValue $configuration["repo"]
101+
102+
Write-Host "Setting up sparse clone for $projectName at $specCloneDir"
103+
104+
Push-Location $specCloneDir.Path
105+
try {
106+
if (!(Test-Path ".git")) {
107+
InitializeSparseGitClone $gitRemoteValue
108+
UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"]
109+
}
110+
git checkout $configuration["commit"]
111+
if ($LASTEXITCODE) { exit $LASTEXITCODE }
112+
}
113+
finally {
114+
Pop-Location
115+
}
116+
} elseif ( $configuration["spec-root-dir"] ) {
117+
$specCloneDir = $configuration["spec-root-dir"]
118+
}
119+
120+
121+
$tempTypeSpecDir = "$ProjectDirectory/TempTypeSpecFiles"
122+
New-Item $tempTypeSpecDir -Type Directory -Force | Out-Null
123+
CopySpecToProjectIfNeeded `
124+
-specCloneRoot $specCloneDir `
125+
-mainSpecDir $specSubDirectory `
126+
-dest $tempTypeSpecDir `
127+
-specAdditionalSubDirectories $configuration["additionalDirectories"]

0 commit comments

Comments
 (0)