|
| 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] $ProjectDirectory |
| 8 | +) |
| 9 | + |
| 10 | +$ErrorActionPreference = "Stop" |
| 11 | +. $PSScriptRoot/Helpers/PSModule-Helpers.ps1 |
| 12 | +Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module |
| 13 | +$sparseCheckoutFile = ".git/info/sparse-checkout" |
| 14 | + |
| 15 | +function AddSparseCheckoutPath([string]$subDirectory) { |
| 16 | + if (!(Test-Path $sparseCheckoutFile) -or !((Get-Content $sparseCheckoutFile).Contains($subDirectory))) { |
| 17 | + Write-Output $subDirectory >> .git/info/sparse-checkout |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function CopySpecToProjectIfNeeded([string]$specCloneRoot, [string]$mainSpecDir, [string]$dest, [string[]]$specAdditionalSubDirectories) { |
| 22 | + $source = "$specCloneRoot/$mainSpecDir" |
| 23 | + Copy-Item -Path $source -Destination $dest -Recurse -Force |
| 24 | + Write-Host "Copying spec from $source to $dest" |
| 25 | + |
| 26 | + foreach ($additionalDir in $specAdditionalSubDirectories) { |
| 27 | + $source = "$specCloneRoot/$additionalDir" |
| 28 | + Write-Host "Copying spec from $source to $dest" |
| 29 | + Copy-Item -Path $source -Destination $dest -Recurse -Force |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function UpdateSparseCheckoutFile([string]$mainSpecDir, [string[]]$specAdditionalSubDirectories) { |
| 34 | + AddSparseCheckoutPath $mainSpecDir |
| 35 | + foreach ($subDir in $specAdditionalSubDirectories) { |
| 36 | + AddSparseCheckoutPath $subDir |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function GetGitRemoteValue([string]$repo) { |
| 41 | + Push-Location $ProjectDirectory |
| 42 | + $result = "" |
| 43 | + try { |
| 44 | + $gitRemotes = (git remote -v) |
| 45 | + foreach ($remote in $gitRemotes) { |
| 46 | + if ($remote.StartsWith("origin")) { |
| 47 | + if ($remote -match 'https://github.com/\S+') { |
| 48 | + $result = "https://github.com/$repo.git" |
| 49 | + break |
| 50 | + } elseif ( $remote -match "[email protected]:\S+"){ |
| 51 | + $result = "[email protected]:$repo.git" |
| 52 | + break |
| 53 | + } else { |
| 54 | + throw "Unknown git remote format found: $remote" |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + finally { |
| 60 | + Pop-Location |
| 61 | + } |
| 62 | + |
| 63 | + return $result |
| 64 | +} |
| 65 | + |
| 66 | +function InitializeSparseGitClone([string]$repo) { |
| 67 | + git clone --no-checkout --filter=tree:0 $repo . |
| 68 | + if ($LASTEXITCODE) { exit $LASTEXITCODE } |
| 69 | + git sparse-checkout init |
| 70 | + if ($LASTEXITCODE) { exit $LASTEXITCODE } |
| 71 | + Remove-Item $sparseCheckoutFile -Force |
| 72 | +} |
| 73 | + |
| 74 | +function GetSpecCloneDir([string]$projectName) { |
| 75 | + Push-Location $ProjectDirectory |
| 76 | + try { |
| 77 | + $root = git rev-parse --show-toplevel |
| 78 | + } |
| 79 | + finally { |
| 80 | + Pop-Location |
| 81 | + } |
| 82 | + |
| 83 | + $sparseSpecCloneDir = "$root/../sparse-spec/$projectName" |
| 84 | + New-Item $sparseSpecCloneDir -Type Directory -Force | Out-Null |
| 85 | + $createResult = Resolve-Path $sparseSpecCloneDir |
| 86 | + return $createResult |
| 87 | +} |
| 88 | + |
| 89 | +$typespecConfigurationFile = Resolve-Path "$ProjectDirectory/tsp-location.yaml" |
| 90 | +Write-Host "Reading configuration from $typespecConfigurationFile" |
| 91 | +$configuration = Get-Content -Path $typespecConfigurationFile -Raw | ConvertFrom-Yaml |
| 92 | + |
| 93 | +$pieces = $typespecConfigurationFile.Path.Replace("\","/").Split("/") |
| 94 | +$projectName = $pieces[$pieces.Count - 2] |
| 95 | + |
| 96 | +$specSubDirectory = $configuration["directory"] |
| 97 | + |
| 98 | +if ( $configuration["repo"] -and $configuration["commit"]) { |
| 99 | + $specCloneDir = GetSpecCloneDir $projectName |
| 100 | + $gitRemoteValue = GetGitRemoteValue $configuration["repo"] |
| 101 | + |
| 102 | + Write-Host "Setting up sparse clone for $projectName at $specCloneDir" |
| 103 | + |
| 104 | + Push-Location $specCloneDir.Path |
| 105 | + try { |
| 106 | + if (!(Test-Path ".git")) { |
| 107 | + InitializeSparseGitClone $gitRemoteValue |
| 108 | + UpdateSparseCheckoutFile $specSubDirectory $configuration["additionalDirectories"] |
| 109 | + } |
| 110 | + git checkout $configuration["commit"] |
| 111 | + if ($LASTEXITCODE) { exit $LASTEXITCODE } |
| 112 | + } |
| 113 | + finally { |
| 114 | + Pop-Location |
| 115 | + } |
| 116 | +} elseif ( $configuration["spec-root-dir"] ) { |
| 117 | + $specCloneDir = $configuration["spec-root-dir"] |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +$tempTypeSpecDir = "$ProjectDirectory/TempTypeSpecFiles" |
| 122 | +New-Item $tempTypeSpecDir -Type Directory -Force | Out-Null |
| 123 | +CopySpecToProjectIfNeeded ` |
| 124 | + -specCloneRoot $specCloneDir ` |
| 125 | + -mainSpecDir $specSubDirectory ` |
| 126 | + -dest $tempTypeSpecDir ` |
| 127 | + -specAdditionalSubDirectories $configuration["additionalDirectories"] |
0 commit comments