8000 Sync eng/common directory with azure-sdk-tools for PR 5231 (#28502) · Azure/azure-sdk-for-python@e00bfa2 · GitHub
[go: up one dir, main page]

Skip to content

Commit e00bfa2

Browse files
azure-sdkm-nash
andauthored
Sync eng/common directory with azure-sdk-tools for PR 5231 (#28502)
* move scripts to common * add reference to common script * address feedback * couple tweaks to relative path updates * update copy paste issue with emitter path * updates to make scripts more generic --------- Co-authored-by: m-nash <prognash@gmail.com>
1 parent 59a5837 commit e00bfa2

File tree

3 files changed

+219
-0
lines changed

3 files changed

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

eng/common/scripts/common.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ $GetDocsMsTocChildrenForManagementPackagesFn = "Get-${Language}-DocsMsTocChildre
5757
$UpdateDocsMsTocFn = "Get-${Language}-UpdatedDocsMsToc"
5858
$GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme"
5959
$GetRepositoryLinkFn = "Get-${Language}-RepositoryLink"
60+
$GetEmitterAdditionalOptionsFn = "Get-${Language}-EmitterAdditionalOptions"
61+
$GetEmitterNameFn = "Get-${Language}-EmitterName"
62+
$GetEmitterPackageJsonPathFn = "Get-${Language}-EmitterPackageJsonPath"

0 commit comments

Comments
 (0)
0