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