-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Cadl-Project-Generate.ps1
93 lines (75 loc) · 3.09 KB
/
Cadl-Project-Generate.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Cadl-Project-Scripts.md
[CmdletBinding()]
param (
[Parameter(Position=0)]
[ValidateNotNullOrEmpty()]
[string] $ProjectDirectory
)
$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
$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml"
Write-Host "Reading configuration from $cadlConfigurationFile"
$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml
$specSubDirectory = $configuration["directory"]
$innerFolder = Split-Path $specSubDirectory -Leaf
$tempFolder = "$ProjectDirectory/TempCadlFiles"
$npmWorkingDir = Resolve-Path $tempFolder/$innerFolder
$mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npmWorkingDir/client.cadl" } Else { Resolve-Path "$npmWorkingDir/main.cadl"}
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"
}
}
$cadlCompileCommand = "npx cadl compile $mainCadlFile --emit $emitterName$emitterAdditionalOptions"
Write-Host($cadlCompileCommand)
Invoke-Expression $cadlCompileCommand
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}
finally {
Pop-Location
}
$shouldCleanUp = $configuration["cleanup"] ?? $true
if ($shouldCleanUp) {
Remove-Item $tempFolder -Recurse -Force
}