File size: 6,249 Bytes
3d968d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<#
.SYNOPSIS
    Sync upstream, build the static site, verify it, commit, and push to Hugging Face.

.DESCRIPTION
    The one command to run after updating SimpleDocReview:

        .\scripts\deploy.ps1

    It re-mirrors app/ from the local upstream checkout, builds the site into the
    repository root, runs the parity and browser checks, commits with the
    upstream commit in the message, and pushes to the Hugging Face Space remote.

    The Space serves files directly with no build step of its own, so what you
    push is exactly what visitors get - which is also why the checks run before
    the push rather than after it. A broken deploy is live immediately.

.PARAMETER UpstreamPath
    Local SimpleDocReview checkout. Defaults to C:\dev\SimpleDocReview.

.PARAMETER Message
    Commit message. Defaults to "chore: sync simpledocreview @ <sha>".

.PARAMETER Remote
    Git remote to push to. Defaults to 'hf' (the Hugging Face Space).

.PARAMETER AlsoPushOrigin
    Additionally push to 'origin' (the GitHub mirror of this deployment repo).

.PARAMETER SkipSync
    Do not re-mirror app/; build and deploy what is already there.

.PARAMETER SkipBuild
    Do not rebuild; verify, commit, and push the site already at the repo root.

.PARAMETER SkipTests
    Do not run the parity and browser checks. They are the only thing standing
    between an upstream change and a Space that quietly behaves differently, so
    skip them only when you have just run them by hand.

.PARAMETER AllowDirty
    Pass through to sync-upstream.ps1: allow a dirty upstream working tree.

.PARAMETER DryRun
    Sync, build, and verify, show what would be committed, but do not commit or push.

.EXAMPLE
    .\scripts\deploy.ps1
.EXAMPLE
    .\scripts\deploy.ps1 -Message "fix: correct the vague-language snippet" -AlsoPushOrigin
#>
[CmdletBinding()]
param(
    [string] $UpstreamPath = 'C:\dev\SimpleDocReview',
    [string] $Message,
    [string] $Remote = 'hf',
    [switch] $AlsoPushOrigin,
    [switch] $SkipSync,
    [switch] $SkipBuild,
    [switch] $SkipTests,
    [switch] $AllowDirty,
    [switch] $DryRun
)

$ErrorActionPreference = 'Stop'

$repoRoot = Split-Path -Parent $PSScriptRoot

if (-not $SkipSync) {
    $syncArgs = @{ UpstreamPath = $UpstreamPath }
    if ($AllowDirty) { $syncArgs['AllowDirty'] = $true }
    & (Join-Path $PSScriptRoot 'sync-upstream.ps1') @syncArgs
    Write-Host ''
}

if (-not $SkipBuild) {
    & (Join-Path $PSScriptRoot 'build-static.ps1')
    Write-Host ''
}

if (-not $SkipTests) {
    Push-Location $repoRoot
    try {
        Write-Host 'Checking the browser API against the real app.py' -ForegroundColor Cyan
        & node (Join-Path $repoRoot 'tests\parity\run-parity.mjs')
        if ($LASTEXITCODE -ne 0) {
            throw 'Parity check failed: the browser API no longer matches app.py. Nothing was deployed.'
        }
        Write-Host ''
        Write-Host 'Checking the built site in a browser' -ForegroundColor Cyan
        & node (Join-Path $repoRoot 'tests\browser\run-browser-check.mjs')
        if ($LASTEXITCODE -ne 0) {
            throw 'Browser check failed. Nothing was deployed.'
        }
        Write-Host ''
    }
    finally { Pop-Location }
}

Push-Location $repoRoot
try {
    # Verify the target remote exists before doing anything that needs undoing.
    $remotes = & git remote
    if ($remotes -notcontains $Remote) {
        $missingRemoteMessage = @"
Git remote '$Remote' is not configured.

Create the Space at https://huggingface.co/new-space (choose the Static SDK,
Blank template), then wire it up:

    git remote add $Remote https://huggingface.co/spaces/<user>/<space-name>

See DEPLOY.md for the full first-time setup.
"@
        # A dry run only reports, so a missing remote is a warning there and a
        # hard stop for a real deploy.
        if ($DryRun) { Write-Warning $missingRemoteMessage } else { throw $missingRemoteMessage }
    }

    $changes = & git status --porcelain
    if ([string]::IsNullOrWhiteSpace(($changes -join ''))) {
        Write-Host 'Nothing to deploy - working tree is clean.' -ForegroundColor Yellow
        return
    }

    if (-not $Message) {
        $upstreamJsonPath = Join-Path $repoRoot 'UPSTREAM.json'
        if (Test-Path -LiteralPath $upstreamJsonPath) {
            $u = Get-Content -LiteralPath $upstreamJsonPath -Raw | ConvertFrom-Json
            $short = if ($u.commit.Length -ge 7) { $u.commit.Substring(0, 7) } else { $u.commit }
            $Message = "chore: sync simpledocreview @ $short"
        }
        else {
            $Message = 'chore: sync simpledocreview'
        }
    }

    if ($DryRun) {
        Write-Host '--- dry run, nothing committed or pushed ---' -ForegroundColor Yellow
        Write-Host "Would commit: $Message"
        Write-Host "Would push to: $Remote$(if ($AlsoPushOrigin) { ' and origin' })"
        Write-Host ''
        $changed = (& git status --porcelain).Count
        Write-Host "$changed path(s) changed. Summary by top-level entry:"
        & git status --porcelain |
            ForEach-Object { ($_.Substring(3) -split '[/\\]')[0].Trim('"') } |
            Group-Object |
            Sort-Object -Property Count -Descending |
            ForEach-Object { Write-Host ("  {0,5}  {1}" -f $_.Count, $_.Name) }
        return
    }

    & git add -A
    if ($LASTEXITCODE -ne 0) { throw 'git add failed' }

    & git commit -m $Message
    if ($LASTEXITCODE -ne 0) { throw 'git commit failed' }

    $branch = (& git rev-parse --abbrev-ref HEAD).Trim()

    Write-Host ''
    Write-Host "Pushing $branch -> $Remote" -ForegroundColor Cyan
    & git push $Remote $branch
    if ($LASTEXITCODE -ne 0) {
        throw "Push to '$Remote' failed. If the Space has commits this repo lacks, run: git pull --rebase $Remote $branch"
    }

    if ($AlsoPushOrigin) {
        Write-Host "Pushing $branch -> origin" -ForegroundColor Cyan
        & git push origin $branch
        if ($LASTEXITCODE -ne 0) { throw 'Push to origin failed.' }
    }

    Write-Host ''
    Write-Host 'Deployed. A Static Space serves files directly, so the update is live' -ForegroundColor Green
    Write-Host 'as soon as the push lands - no build wait.' -ForegroundColor Green
}
finally { Pop-Location }