From 432aabb807e8d8a14e7a84bf1ef58de30959146c Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Mon, 13 Mar 2023 16:52:17 +0800 Subject: [PATCH 1/5] typespec renaming --- ...rate.ps1 => Typespec-Project-Generate.ps1} | 26 +++++++++---------- ...ect-Sync.ps1 => Typespec-Project-Sync.ps1} | 16 ++++++------ 2 files changed, 21 insertions(+), 21 deletions(-) rename eng/common/scripts/{Cadl-Project-Generate.ps1 => Typespec-Project-Generate.ps1} (72%) rename eng/common/scripts/{Cadl-Project-Sync.ps1 => Typespec-Project-Sync.ps1} (88%) diff --git a/eng/common/scripts/Cadl-Project-Generate.ps1 b/eng/common/scripts/Typespec-Project-Generate.ps1 similarity index 72% rename from eng/common/scripts/Cadl-Project-Generate.ps1 rename to eng/common/scripts/Typespec-Project-Generate.ps1 index 3e7ee781b0..43b6beab64 100644 --- a/eng/common/scripts/Cadl-Project-Generate.ps1 +++ b/eng/common/scripts/Typespec-Project-Generate.ps1 @@ -1,4 +1,4 @@ -# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Cadl-Project-Scripts.md +# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Typespec-Project-Scripts.md [CmdletBinding()] param ( @@ -6,7 +6,7 @@ param ( [ValidateNotNullOrEmpty()] [string] $ProjectDirectory, [Parameter(Position=1)] - [string] $CadlAdditionalOptions ## addtional cadl emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 + [string] $TypespecAdditionalOptions ## addtional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 ) $ErrorActionPreference = "Stop" @@ -55,17 +55,17 @@ function NpmInstallForProject([string]$workingDirectory) { $resolvedProjectDirectory = Resolve-Path $ProjectDirectory $emitterName = &$GetEmitterNameFn -$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml" +$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/typespec-location.yaml" -Write-Host "Reading configuration from $cadlConfigurationFile" -$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-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/TempCadlFiles" +$tempFolder = "$ProjectDirectory/TempTypespecFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder -$mainCadlFile = If (Test-Path "$npmWorkingDir/client.cadl") { Resolve-Path "$npmWorkingDir/client.cadl" } Else { Resolve-Path "$npmWorkingDir/main.cadl"} +$mainTypespecFile = If (Test-Path "$npmWorkingDir/client.tsp") { Resolve-Path "$npmWorkingDir/client.tsp" } Else { Resolve-Path "$npmWorkingDir/main.tsp"} try { Push-Location $npmWorkingDir @@ -79,15 +79,15 @@ try { $emitterAdditionalOptions = " $emitterAdditionalOptions" } } - $cadlCompileCommand = "npx cadl compile $mainCadlFile --emit $emitterName$emitterAdditionalOptions" - if ($CadlAdditionalOptions) { - $options = $CadlAdditionalOptions.Split(";"); + $typespecCompileCommand = "npx tsp compile $mainTypespecFile --emit $emitterName$emitterAdditionalOptions" + if ($TypespecAdditionalOptions) { + $options = $TypespecAdditionalOptions.Split(";"); foreach ($option in $options) { - $cadlCompileCommand += " --option $emitterName.$option" + $typespecCompileCommand += " --option $emitterName.$option" } } - Write-Host($cadlCompileCommand) - Invoke-Expression $cadlCompileCommand + Write-Host($typespecCompileCommand) + Invoke-Expression $typespecCompileCommand if ($LASTEXITCODE) { exit $LASTEXITCODE } } diff --git a/eng/common/scripts/Cadl-Project-Sync.ps1 b/eng/common/scripts/Typespec-Project-Sync.ps1 similarity index 88% rename from eng/common/scripts/Cadl-Project-Sync.ps1 rename to eng/common/scripts/Typespec-Project-Sync.ps1 index c6e8cf5ce5..24f68a8c54 100644 --- a/eng/common/scripts/Cadl-Project-Sync.ps1 +++ b/eng/common/scripts/Typespec-Project-Sync.ps1 @@ -1,4 +1,4 @@ -# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Cadl-Project-Scripts.md +# For details see https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/Typespec-Project-Scripts.md [CmdletBinding()] param ( @@ -86,11 +86,11 @@ function GetSpecCloneDir([string]$projectName) { return $createResult } -$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml" -Write-Host "Reading configuration from $cadlConfigurationFile" -$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml +$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/typespec-location.yaml" +Write-Host "Reading configuration from $typespecConfigurationFile" +$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml -$pieces = $cadlConfigurationFile.Path.Replace("\","/").Split("/") +$pieces = $typespecConfigurationFile.Path.Replace("\","/").Split("/") $projectName = $pieces[$pieces.Count - 2] $specSubDirectory = $configuration["directory"] @@ -118,10 +118,10 @@ if ( $configuration["repo"] -and $configuration["commit"]) { } -$tempCadlDir = "$ProjectDirectory/TempCadlFiles" -New-Item $tempCadlDir -Type Directory -Force | Out-Null +$tempTypespecDir = "$ProjectDirectory/TempTypespecFiles" +New-Item $tempTypespecDir -Type Directory -Force | Out-Null CopySpecToProjectIfNeeded ` -specCloneRoot $specCloneDir ` -mainSpecDir $specSubDirectory ` - -dest $tempCadlDir ` + -dest $tempTypespecDir ` -specAdditionalSubDirectories $configuration["additionalDirectories"] From 9ae57dfce9956b5d30c00d3eec1a4565ed7c0678 Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Tue, 14 Mar 2023 09:25:21 +0800 Subject: [PATCH 2/5] add back scripts for cadl --- eng/common/scripts/Cadl-Project-Generate.ps1 | 101 +++++++++++++++ eng/common/scripts/Cadl-Project-Sync.ps1 | 127 +++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 eng/common/scripts/Cadl-Project-Generate.ps1 create mode 100644 eng/common/scripts/Cadl-Project-Sync.ps1 diff --git a/eng/common/scripts/Cadl-Project-Generate.ps1 b/eng/common/scripts/Cadl-Project-Generate.ps1 new file mode 100644 index 0000000000..3e7ee781b0 --- /dev/null +++ b/eng/common/scripts/Cadl-Project-Generate.ps1 @@ -0,0 +1,101 @@ +# 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, + [Parameter(Position=1)] + [string] $CadlAdditionalOptions ## addtional cadl emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 +) + +$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" + if ($CadlAdditionalOptions) { + $options = $CadlAdditionalOptions.Split(";"); + foreach ($option in $options) { + $cadlCompileCommand += " --option $emitterName.$option" + } + } + Write-Host($cadlCompileCommand) + Invoke-Expression $cadlCompileCommand + + if ($LASTEXITCODE) { exit $LASTEXITCODE } +} +finally { + Pop-Location +} + +$shouldCleanUp = $configuration["cleanup"] ?? $true +if ($shouldCleanUp) { + Remove-Item $tempFolder -Recurse -Force +} \ No newline at end of file diff --git a/eng/common/scripts/Cadl-Project-Sync.ps1 b/eng/common/scripts/Cadl-Project-Sync.ps1 new file mode 100644 index 0000000000..c6e8cf5ce5 --- /dev/null +++ b/eng/common/scripts/Cadl-Project-Sync.ps1 @@ -0,0 +1,127 @@ +# 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 +Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module +$sparseCheckoutFile = ".git/info/sparse-checkout" + +function AddSparseCheckoutPath([string]$subDirectory) { + if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) { + Write-Output $subDirectory >> .git/info/sparse-checkout + } +} + +function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) { + $source = "$specCloneRoot/$mainSpecDir" + Copy-Item -Path $source -Destination $dest -Recurse -Force + Write-Host "Copying spec from $source to $dest" + + foreach ($additionalDir in $specAdditionalSubDirectories) { + $source = "$specCloneRoot/$additionalDir" + Write-Host "Copying spec from $source to $dest" + Copy-Item -Path $source -Destination $dest -Recurse -Force + } +} + +function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) { + AddSparseCheckoutPath $mainSpecDir + foreach ($subDir in $specAdditionalSubDirectories) { + AddSparseCheckoutPath $subDir + } +} + +function GetGitRemoteValue([string]$repo) { + Push-Location $ProjectDirectory + $result = "" + try { + $gitRemotes = (git remote -v) + foreach ($remote in $gitRemotes) { + if ($remote.StartsWith("origin")) { + if ($remote -match 'https://github.com/\S+') { + $result = "https://github.com/$repo.git" + break + } elseif ($remote -match "git@github.com:\S+"){ + $result = "git@github.com:$repo.git" + break + } else { + throw "Unknown git remote format found: $remote" + } + } + } + } + finally { + Pop-Location + } + + return $result +} + +function InitializeSparseGitClone([string]$repo) { + git clone --no-checkout --filter=tree:0 $repo . + if ($LASTEXITCODE) { exit $LASTEXITCODE } + git sparse-checkout init + if ($LASTEXITCODE) { exit $LASTEXITCODE } + Remove-Item $sparseCheckoutFile -Force +} + +function GetSpecCloneDir([string]$projectName) { + Push-Location $ProjectDirectory + try { + $root = git rev-parse --show-toplevel + } + finally { + Pop-Location + } + + $sparseSpecCloneDir = "$root/../sparse-spec/$projectName" + New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null + $createResult = Resolve-Path $sparseSpecCloneDir + return $createResult +} + +$cadlConfigurationFile = Resolve-Path "$ProjectDirectory/cadl-location.yaml" +Write-Host "Reading configuration from $cadlConfigurationFile" +$configuration = Get-Content -Path $cadlConfigurationFile -Raw | ConvertFrom-Yaml + +$pieces = $cadlConfigurationFile.Path.Replace("\","/").Split("/") +$projectName = $pieces[$pieces.Count - 2] + +$specSubDirectory = $configuration["directory"] + +if ( $configuration["repo"] -and $configuration["commit"]) { + $specCloneDir = GetSpecCloneDir $projectName + $gitRemoteValue = GetGitRemoteValue $configuration["repo"] + + Write-Host "Setting up sparse clone for $projectName at $specCloneDir" + + Push-Location $specCloneDir.Path + try { + if (!(Test-Path ".git")) { + InitializeSparseGitClone $gitRemoteValue + UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"] + } + git checkout $configuration["commit"] + if ($LASTEXITCODE) { exit $LASTEXITCODE } + } + finally { + Pop-Location + } +} elseif ( $configuration["spec-root-dir"] ) { + $specCloneDir = $configuration["spec-root-dir"] +} + + +$tempCadlDir = "$ProjectDirectory/TempCadlFiles" +New-Item $tempCadlDir -Type Directory -Force | Out-Null +CopySpecToProjectIfNeeded ` + -specCloneRoot $specCloneDir ` + -mainSpecDir $specSubDirectory ` + -dest $tempCadlDir ` + -specAdditionalSubDirectories $configuration["additionalDirectories"] From 26da2c4439280e27bee95d8fddd5ea0222b2b27f Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Tue, 14 Mar 2023 11:15:41 +0800 Subject: [PATCH 3/5] support .cadl and .tsp --- eng/common/scripts/Typespec-Project-Generate.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Typespec-Project-Generate.ps1 b/eng/common/scripts/Typespec-Project-Generate.ps1 index 43b6beab64..3dc9a6ec1b 100644 --- a/eng/common/scripts/Typespec-Project-Generate.ps1 +++ b/eng/common/scripts/Typespec-Project-Generate.ps1 @@ -65,7 +65,7 @@ $innerFolder = Split-Path $specSubDirectory -Leaf $tempFolder = "$ProjectDirectory/TempTypespecFiles" $npmWorkingDir = Resolve-Path $tempFolder/$innerFolder -$mainTypespecFile = If (Test-Path "$npmWorkingDir/client.tsp") { Resolve-Path "$npmWorkingDir/client.tsp" } Else { Resolve-Path "$npmWorkingDir/main.tsp"} +$mainTypespecFile = If (Test-Path "$npmWorkingDir/client.*") { Resolve-Path "$npmWorkingDir/client.*" } Else { Resolve-Path "$npmWorkingDir/main.*"} try { Push-Location $npmWorkingDir From fe3d6bf4936925c044e30c1c064a16ed8f1dc151 Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Wed, 15 Mar 2023 10:48:20 +0800 Subject: [PATCH 4/5] rename --- .../scripts/Typespec-Project-Generate.ps1 | 101 -------------- eng/common/scripts/Typespec-Project-Sync.ps1 | 127 ------------------ 2 files changed, 228 deletions(-) delete mode 100644 eng/common/scripts/Typespec-Project-Generate.ps1 delete mode 100644 eng/common/scripts/Typespec-Project-Sync.ps1 diff --git a/eng/common/scripts/Typespec-Project-Generate.ps1 b/eng/common/scripts/Typespec-Project-Generate.ps1 deleted file mode 100644 index 3dc9a6ec1b..0000000000 --- a/eng/common/scripts/Typespec-Project-Generate.ps1 +++ /dev/null @@ -1,101 +0,0 @@ -# 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, - [Parameter(Position=1)] - [string] $TypespecAdditionalOptions ## addtional typespec emitter options, separated by semicolon if more than one, e.g. option1=value1;option2=value2 -) - -$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 -$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/typespec-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" - } - } - Write-Host($typespecCompileCommand) - Invoke-Expression $typespecCompileCommand - - if ($LASTEXITCODE) { exit $LASTEXITCODE } -} -finally { - Pop-Location -} - -$shouldCleanUp = $configuration["cleanup"] ?? $true -if ($shouldCleanUp) { - Remove-Item $tempFolder -Recurse -Force -} \ No newline at end of file diff --git a/eng/common/scripts/Typespec-Project-Sync.ps1 b/eng/common/scripts/Typespec-Project-Sync.ps1 deleted file mode 100644 index 24f68a8c54..0000000000 --- a/eng/common/scripts/Typespec-Project-Sync.ps1 +++ /dev/null @@ -1,127 +0,0 @@ -# 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 -) - -$ErrorActionPreference = "Stop" -. $PSScriptRoot/Helpers/PSModule-Helpers.ps1 -Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module -$sparseCheckoutFile = ".git/info/sparse-checkout" - -function AddSparseCheckoutPath([string]$subDirectory) { - if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) { - Write-Output $subDirectory >> .git/info/sparse-checkout - } -} - -function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) { - $source = "$specCloneRoot/$mainSpecDir" - Copy-Item -Path $source -Destination $dest -Recurse -Force - Write-Host "Copying spec from $source to $dest" - - foreach ($additionalDir in $specAdditionalSubDirectories) { - $source = "$specCloneRoot/$additionalDir" - Write-Host "Copying spec from $source to $dest" - Copy-Item -Path $source -Destination $dest -Recurse -Force - } -} - -function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) { - AddSparseCheckoutPath $mainSpecDir - foreach ($subDir in $specAdditionalSubDirectories) { - AddSparseCheckoutPath $subDir - } -} - -function GetGitRemoteValue([string]$repo) { - Push-Location $ProjectDirectory - $result = "" - try { - $gitRemotes = (git remote -v) - foreach ($remote in $gitRemotes) { - if ($remote.StartsWith("origin")) { - if ($remote -match 'https://github.com/\S+') { - $result = "https://github.com/$repo.git" - break - } elseif ($remote -match "git@github.com:\S+"){ - $result = "git@github.com:$repo.git" - break - } else { - throw "Unknown git remote format found: $remote" - } - } - } - } - finally { - Pop-Location - } - - return $result -} - -function InitializeSparseGitClone([string]$repo) { - git clone --no-checkout --filter=tree:0 $repo . - if ($LASTEXITCODE) { exit $LASTEXITCODE } - git sparse-checkout init - if ($LASTEXITCODE) { exit $LASTEXITCODE } - Remove-Item $sparseCheckoutFile -Force -} - -function GetSpecCloneDir([string]$projectName) { - Push-Location $ProjectDirectory - try { - $root = git rev-parse --show-toplevel - } - finally { - Pop-Location - } - - $sparseSpecCloneDir = "$root/../sparse-spec/$projectName" - New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null - $createResult = Resolve-Path $sparseSpecCloneDir - return $createResult -} - -$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/typespec-location.yaml" -Write-Host "Reading configuration from $typespecConfigurationFile" -$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml - -$pieces = $typespecConfigurationFile.Path.Replace("\","/").Split("/") -$projectName = $pieces[$pieces.Count - 2] - -$specSubDirectory = $configuration["directory"] - -if ( $configuration["repo"] -and $configuration["commit"]) { - $specCloneDir = GetSpecCloneDir $projectName - $gitRemoteValue = GetGitRemoteValue $configuration["repo"] - - Write-Host "Setting up sparse clone for $projectName at $specCloneDir" - - Push-Location $specCloneDir.Path - try { - if (!(Test-Path ".git")) { - InitializeSparseGitClone $gitRemoteValue - UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"] - } - git checkout $configuration["commit"] - if ($LASTEXITCODE) { exit $LASTEXITCODE } - } - finally { - Pop-Location - } -} elseif ( $configuration["spec-root-dir"] ) { - $specCloneDir = $configuration["spec-root-dir"] -} - - -$tempTypespecDir = "$ProjectDirectory/TempTypespecFiles" -New-Item $tempTypespecDir -Type Directory -Force | Out-Null -CopySpecToProjectIfNeeded ` - -specCloneRoot $specCloneDir ` - -mainSpecDir $specSubDirectory ` - -dest $tempTypespecDir ` - -specAdditionalSubDirectories $configuration["additionalDirectories"] From 47da546cd8bf896491e3ad71d4ad2fc9edf547a4 Mon Sep 17 00:00:00 2001 From: "FAREAST\\chunyu" Date: Thu, 16 Mar 2023 10:54:01 +0800 Subject: [PATCH 5/5] add newline at the end of file --- eng/common/scripts/TypeSpec-Project-Generate.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/TypeSpec-Project-Generate.ps1 b/eng/common/scripts/TypeSpec-Project-Generate.ps1 index d1e728895a..feba00d37e 100644 --- a/eng/common/scripts/TypeSpec-Project-Generate.ps1 +++ b/eng/common/scripts/TypeSpec-Project-Generate.ps1 @@ -98,4 +98,4 @@ finally { $shouldCleanUp = $configuration["cleanup"] ?? $true if ($shouldCleanUp) { Remove-Item $tempFolder -Recurse -Force -} \ No newline at end of file +}