-
Notifications
You must be signed in to change notification settings - Fork 180
/
TypeSpec-Project-Generate.ps1
121 lines (98 loc) · 4.12 KB
/
TypeSpec-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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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,
[string] $TypespecAdditionalOptions = $null, ## additional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2
[switch] $SaveInputs = $false ## saves the temporary files during execution, default false
)
$ErrorActionPreference = "Stop"
. $PSScriptRoot/Helpers/PSModule-Helpers.ps1
. $PSScriptRoot/Helpers/CommandInvocation-Helpers.ps1
. $PSScriptRoot/common.ps1
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.7" | Import-Module
function NpmInstallForProject([string]$workingDirectory) {
Push-Location $workingDirectory
try {
$currentDur = Resolve-Path "."
Write-Host "Generating from $currentDur"
if (Test-Path "package.json") {
Write-Host "Removing existing package.json"
Remove-Item -Path "package.json" -Force
}
if (Test-Path ".npmrc") {
Write-Host "Removing existing .nprc"
Remove-Item -Path ".npmrc" -Force
}
if (Test-Path "node_modules") {
Write-Host "Removing existing node_modules"
Remove-Item -Path "node_modules" -Force -Recurse
}
if (Test-Path "package-lock.json") {
Write-Host "Removing existing package-lock.json"
Remove-Item -Path "package-lock.json" -Force
}
$replacementPackageJson = Join-Path $PSScriptRoot "../../emitter-package.json"
Write-Host("Copying package.json from $replacementPackageJson")
Copy-Item -Path $replacementPackageJson -Destination "package.json" -Force
$emitterPackageLock = Join-Path $PSScriptRoot "../../emitter-package-lock.json"
$usingLockFile = Test-Path $emitterPackageLock
if ($usingLockFile) {
Write-Host("Copying package-lock.json from $emitterPackageLock")
Copy-Item -Path $emitterPackageLock -Destination "package-lock.json" -Force
}
if ($usingLockFile) {
Invoke-LoggedCommand "npm ci"
}
else {
Invoke-LoggedCommand "npm install"
}
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"
}
}
if ($SaveInputs) {
$typespecCompileCommand += " --option $emitterName.save-inputs=true"
}
Write-Host($typespecCompileCommand)
Invoke-Expression $typespecCompileCommand
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}
finally {
Pop-Location
}
$shouldCleanUp = !$SaveInputs
if ($shouldCleanUp) {
Remove-Item $tempFolder -Recurse -Force
}
exit 0