-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclone-sample-app.ps1
More file actions
93 lines (80 loc) · 3.4 KB
/
Copy pathclone-sample-app.ps1
File metadata and controls
93 lines (80 loc) · 3.4 KB
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
<#
.SYNOPSIS
Clone (or refresh) the SREinProd workshop sample application.
.DESCRIPTION
Pulls Azure-Samples/app-service-dotnet-agent-tutorial into ./sample-app/.
Safe to run multiple times: if the directory already contains the repo it
is updated to the requested ref; otherwise it is freshly cloned.
The sample app is NOT vendored in this repository (sample-app/ is
gitignored) so the workshop always demos against the latest upstream code.
.PARAMETER RepoUrl
Git URL of the sample app. Defaults to the upstream Azure-Samples repo.
.PARAMETER Ref
Branch, tag, or commit to check out. Defaults to 'main'.
.PARAMETER TargetDir
Local directory the sample is cloned into. Defaults to './sample-app'.
.PARAMETER Force
Wipe the target directory before cloning.
#>
[CmdletBinding()]
param(
[string]$RepoUrl = 'https://github.com/Azure-Samples/app-service-dotnet-agent-tutorial.git',
[string]$Ref = 'main',
[string]$TargetDir = (Join-Path $PSScriptRoot '..' 'sample-app'),
[switch]$Force
)
$ErrorActionPreference = 'Stop'
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
throw "git is required but was not found on PATH."
}
$TargetDir = [System.IO.Path]::GetFullPath($TargetDir)
if ($Force -and (Test-Path $TargetDir)) {
Write-Host "Removing existing $TargetDir (Force)" -ForegroundColor Yellow
Remove-Item -Recurse -Force $TargetDir
}
if (Test-Path (Join-Path $TargetDir '.git')) {
Write-Host "Updating existing clone in $TargetDir" -ForegroundColor Cyan
Push-Location $TargetDir
try {
git fetch --depth 1 origin $Ref | Out-Null
git checkout -B $Ref FETCH_HEAD | Out-Null
Write-Host "Sample app refreshed to '$Ref'." -ForegroundColor Green
}
finally {
Pop-Location
}
}
else {
# Use `git init + fetch + checkout` instead of `git clone` so we can lay
# the upstream source down alongside any pre-existing files in the target
# (e.g. the committed sample-app/PLACEHOLDER.md). This makes the script
# idempotent regardless of whether the directory is empty, has only the
# placeholder, or is a stale partial clone.
Write-Host "Initializing $RepoUrl ($Ref) into $TargetDir" -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path $TargetDir | Out-Null
Push-Location $TargetDir
try {
git init --quiet
# If the remote already exists from a prior failed run, replace its URL.
$existingRemote = git remote 2>$null
if ($existingRemote -contains 'origin') {
git remote set-url origin $RepoUrl | Out-Null
} else {
git remote add origin $RepoUrl | Out-Null
}
git fetch --depth 1 origin $Ref | Out-Null
if ($LASTEXITCODE -ne 0) { throw "git fetch failed for $RepoUrl ($Ref)." }
git checkout -f -B $Ref FETCH_HEAD | Out-Null
if ($LASTEXITCODE -ne 0) { throw "git checkout failed for ref '$Ref'." }
Write-Host "Sample app initialized at '$Ref'." -ForegroundColor Green
}
finally {
Pop-Location
}
}
# Sanity check: the project file we expect must be present.
$projects = Get-ChildItem -Path $TargetDir -Filter '*.csproj' -File -ErrorAction SilentlyContinue
if (-not $projects -or $projects.Count -eq 0) {
throw "No .csproj found in $TargetDir after clone. Did the upstream layout change?"
}
Write-Host ("Sample project: {0}" -f $projects[0].FullName) -ForegroundColor Green