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 ] $TypeSpecProjectDirectory , # A directory of `tspconfig.yaml` or a remoteUrl of `tspconfig.yaml`
8
+ [Parameter (Position = 1 )]
9
+ [string ] $CommitHash ,
10
+ [Parameter (Position = 2 )]
11
+ [string ] $RepoUrl
12
+ )
13
+
14
+ . $PSScriptRoot / common.ps1
15
+ . $PSScriptRoot / Helpers/ PSModule- Helpers.ps1
16
+ Install-ModuleIfNotInstalled " powershell-yaml" " 0.4.1" | Import-Module
17
+
18
+ function CreateUpdate-TspLocation ([System.Object ]$tspConfig , [string ]$TypeSpecProjectDirectory , [string ]$CommitHash , [string ]$repo , [string ]$repoRoot ) {
19
+ $serviceDir = " "
20
+ $additionalDirs = @ ()
21
+
22
+ # Parse tspcofig.yaml to get service-dir, additionalDirectories, and package-dir
23
+ if ($tspConfig [" parameters" ] -and $tspConfig [" parameters" ][" service-dir" ]) {
24
+ $serviceDir = $tspConfig [" parameters" ][" service-dir" ][" default" ];
25
+ }
26
+ else {
27
+ Write-Error " Missing service-dir in parameters section of tspconfig.yaml."
28
+ exit 1
29
+ }
30
+ if ($tspConfig [" parameters" ][" dependencies" ] -and $tspConfig [" parameters" ][" dependencies" ][" additionalDirectories" ]) {
31
+ $additionalDirs = $tspConfig [" parameters" ][" dependencies" ][" additionalDirectories" ];
32
+ }
33
+
34
+ $packageDir = Get-PackageDir $tspConfig
35
+
36
+ # Create service-dir if not exist
37
+ $serviceDir = Join-Path $repoRoot $serviceDir
38
+ if (! (Test-Path - Path $serviceDir )) {
39
+ New-Item - Path $serviceDir - ItemType Directory
40
+ Write-Host " created service folder $serviceDir "
41
+ }
42
+
43
+ # Create package-dir if not exist
44
+ $packageDir = Join-Path $serviceDir $packageDir
45
+ if (! (Test-Path - Path $packageDir )) {
46
+ New-Item - Path $packageDir - ItemType Directory
47
+ Write-Host " created package folder $packageDir "
48
+ }
49
+
50
+ # Load tsp-location.yaml if exist
51
+ $tspLocationYamlPath = Join-Path $packageDir " tsp-location.yaml"
52
+ $tspLocationYaml = @ {}
53
+ if (Test-Path - Path $tspLocationYamlPath ) {
54
+ $tspLocationYaml = Get-Content - Path $tspLocationYamlPath - Raw | ConvertFrom-Yaml
55
+ }
56
+ else {
57
+ Write-Host " creating tsp-location.yaml in $packageDir "
58
+ }
59
+
60
+ # Update tsp-location.yaml
61
+ $tspLocationYaml [" commit" ] = $CommitHash
62
+ $tspLocationYaml [" repo" ] = $repo
63
+ $tspLocationYaml [" directory" ] = $TypeSpecProjectDirectory
64
+ $tspLocationYaml [" additionalDirectories" ] = $additionalDirs
65
+ $tspLocationYaml | ConvertTo-Yaml | Out-File $tspLocationYamlPath
66
+ Write-Host " updated tsp-location.yaml in $packageDir "
67
+ return $packageDir
68
+ }
69
+
70
+ function Get-PackageDir ([System.Object ]$tspConfig ) {
71
+ $emitterName = " "
72
+ if (Test-Path " Function:$GetEmitterNameFn " ) {
73
+ $emitterName = & $GetEmitterNameFn
74
+ }
75
+ else {
76
+ Write-Error " Missing $GetEmitterNameFn function in {$Language } SDK repo script."
77
+ exit 1
78
+ }
79
+ $packageDir = " "
80
+ if ($tspConfig [" options" ] -and $tspConfig [" options" ][" $emitterName " ] -and $tspConfig [" options" ][" $emitterName " ][" package-dir" ]) {
81
+ $packageDir = $tspConfig [" options" ][" $emitterName " ][" package-dir" ]
82
+ }
83
+ else {
84
+ Write-Error " Missing package-dir in $emitterName options of tspconfig.yaml."
85
+ exit 1
86
+ }
87
+ return $packageDir
88
+ }
89
+
90
+ $repo = " "
91
+ if ($RepoUrl ) {
92
+ if ($RepoUrl -match " ^https://github.com/(?<repo>[^/]*/azure-rest-api-specs(-pr)?).*" ) {
93
+ $repo = $Matches [" repo" ]
94
+ }
95
+ else {
96
+ Write-Host " Parameter 'RepoUrl' has incorrect value: $RepoUrl . It should be similar like 'https://github.com/Azure/azure-rest-api-specs'"
97
+ exit 1
98
+ }
99
+ }
100
+
101
+ $repoRootPath = (Join-Path $PSScriptRoot .. .. ..)
102
+ $repoRootPath = Resolve-Path $repoRootPath
103
+ $repoRootPath = $repoRootPath -replace " \\" , " /"
104
+ $tspConfigPath = Join-Path $repoRootPath ' tspconfig.yaml'
105
+ # example url of tspconfig.yaml: https://github.com/Azure/azure-rest-api-specs-pr/blob/724ccc4d7ef7655c0b4d5c5ac4a5513f19bbef35/specification/containerservice/Fleet.Management/tspconfig.yaml
106
+ if ($TypeSpecProjectDirectory -match ' ^https://github.com/(?<repo>Azure/azure-rest-api-specs(-pr)?)/blob/(?<commit>[0-9a-f]{40})/(?<path>.*)/tspconfig.yaml$' ) {
107
+ try {
108
+ $TypeSpecProjectDirectory = $TypeSpecProjectDirectory -replace " github.com" , " raw.githubusercontent.com"
109
+ $TypeSpecProjectDirectory = $TypeSpecProjectDirectory -replace " /blob/" , " /"
110
+ Invoke-WebRequest $TypeSpecProjectDirectory - OutFile $tspConfigPath - MaximumRetryCount 3
111
+ }
112
+ catch {
113
+ Write-Host " Failed to download '$TypeSpecProjectDirectory '"
114
+ Write-Error $_.Exception.Message
115
+ return
116
+ }
117
+ $repo = $Matches [" repo" ]
118
+ $TypeSpecProjectDirectory = $Matches [" path" ]
119
+ $CommitHash = $Matches [" commit" ]
120
+ # TODO support the branch name in url then get the commithash from branch name
121
+ } else {
122
+ $tspConfigPath = Join-Path $TypeSpecProjectDirectory " tspconfig.yaml"
123
+ if (! (Test-Path $tspConfigPath )) {
124
+ Write-Error " Failed to find tspconfig.yaml in '$TypeSpecProjectDirectory '"
125
+ exit 1
126
+ }
127
+ }
128
+ $tspConfigYaml = Get-Content $tspConfigPath - Raw | ConvertFrom-Yaml
129
+ # call CreateUpdate-TspLocation function
130
+ $sdkProjectFolder = CreateUpdate- TspLocation $tspConfigYaml $TypeSpecProjectDirectory $CommitHash $repo $repoRootPath
131
+
132
+ # call TypeSpec-Project-Sync.ps1
133
+ & " $PSScriptRoot /TypeSpec-Project-Sync.ps1" $sdkProjectFolder
134
+ # call TypeSpec-Project-Generate.ps1
135
+ & " $PSScriptRoot /TypeSpec-Project-Generate.ps1" $sdkProjectFolder
0 commit comments