Skip to content

Commit 2f37fb9

Browse files
committed
Persist installer checkout in .cwc and update it in place instead of cloning into a temporary directory
- to make updates faster, wouldn't it be a good idea to clone to user dir .cwc/repository (use .cwc for something alrady) - remove redundant comments - add sensible comments making scripts easy to understand - assess commets usefulness - remove comments ├── apps │ └── editor │ ├── package.json │ ├── src │ │ └── utils │ │ └── shared-token.ts │ └── webpack.config.ts ├── install.ps1 └── install.sh
1 parent 82205cc commit 2f37fb9

2 files changed

Lines changed: 59 additions & 27 deletions

File tree

‎install.ps1‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ function Invoke-Step {
8989

9090
$prevPreference = $ErrorActionPreference
9191
$ErrorActionPreference = "Continue"
92+
$ok = $true
9293
try {
9394
& $Action
94-
$ok = $?
95+
if (-not $?) { $ok = $false }
96+
} catch {
97+
$ok = $false
9598
} finally {
9699
$ErrorActionPreference = $prevPreference
97100
}
@@ -106,26 +109,48 @@ function Invoke-Step {
106109
Write-Host ""
107110

108111
$originalLocation = Get-Location
109-
$tmpDir = Join-Path $env:TEMP ("CWC_Install_" + [Guid]::NewGuid().ToString().Substring(0,8))
112+
113+
$cwcDir = Join-Path $env:USERPROFILE ".cwc"
114+
$repoDir = Join-Path $cwcDir "repository"
110115

111116
try {
112117

113-
New-Item -ItemType Directory -Path $tmpDir | Out-Null
118+
if (!(Test-Path $cwcDir)) {
119+
New-Item -ItemType Directory -Path $cwcDir | Out-Null
120+
}
114121

115-
$extractedDir = Join-Path $tmpDir "source"
122+
$updated = $false
123+
if (Test-Path (Join-Path $repoDir ".git")) {
124+
$updated = Invoke-Step "Updating repository..." {
125+
git -C $repoDir fetch origin main --quiet *>$null
126+
if ($LASTEXITCODE -ne 0) { throw "Failed to fetch" }
127+
git -C $repoDir reset --hard FETCH_HEAD --quiet *>$null
128+
if ($LASTEXITCODE -ne 0) { throw "Failed to reset" }
129+
git -C $repoDir clean -fd --quiet *>$null
130+
if ($LASTEXITCODE -ne 0) { throw "Failed to clean" }
131+
}
132+
}
116133

117-
$null = Invoke-Step "Cloning repository..." {
118-
git clone -b main -c core.longpaths=true --quiet https://github.com/CodeWebChat/CWC.git $extractedDir *>$null
119-
} "Cloning repository failed."
134+
if (-not $updated) {
135+
if (Test-Path $repoDir) {
136+
Remove-Item -Path $repoDir -Recurse -Force -ErrorAction SilentlyContinue
137+
}
138+
$null = Invoke-Step "Cloning repository..." {
139+
git clone -b main -c core.longpaths=true --quiet https://github.com/CodeWebChat/CWC.git $repoDir *>$null
140+
if ($LASTEXITCODE -ne 0) { throw "Failed to clone" }
141+
} "Cloning repository failed."
142+
}
120143

121-
Set-Location $extractedDir
144+
Set-Location $repoDir
122145

123146
$null = Invoke-Step "Installing dependencies..." {
124147
npx.cmd --yes pnpm install *>$null
125148
} "Failed to install dependencies."
126149

127150
Set-Location apps/editor
128151

152+
Remove-Item *.vsix -Force -ErrorAction SilentlyContinue
153+
129154
$null = Invoke-Step "Building extension..." {
130155
npx.cmd --yes pnpm run build *>$null
131156
} "Build failed."
@@ -159,18 +184,8 @@ foreach ($i in $indices) { Install-ToEditor $i }
159184

160185
Set-Location $originalLocation
161186

162-
Write-Host ""
163-
$null = Invoke-Step "Cleaning up..." {
164-
if (Test-Path $tmpDir) {
165-
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
166-
}
167-
} "Failed to clean up."
168-
169187
} finally {
170188
Set-Location $originalLocation -ErrorAction SilentlyContinue
171-
if (Test-Path $tmpDir) {
172-
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
173-
}
174189
}
175190

176191
Pause

‎install.sh‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ if [ -z "$choice" ]; then
8989
esac
9090
fi
9191

92-
TMP_DIR=$(mktemp -d)
9392
ORIGINAL_DIR="$(pwd)"
9493

9594
kill_spinner() {
@@ -103,9 +102,6 @@ kill_spinner() {
103102

104103
cleanup() {
105104
kill_spinner
106-
if [ -d "$TMP_DIR" ]; then
107-
rm -rf "$TMP_DIR" >/dev/null 2>&1
108-
fi
109105
}
110106
trap cleanup EXIT INT TERM
111107

@@ -145,13 +141,37 @@ run_step() {
145141

146142
echo ""
147143

148-
run_step "Cloning repository" git clone -b main -c core.longpaths=true --quiet https://github.com/CodeWebChat/CWC.git "$TMP_DIR/source"
144+
CWC_DIR="$HOME/.cwc"
145+
REPO_DIR="$CWC_DIR/repository"
146+
147+
mkdir -p -m 700 "$CWC_DIR"
148+
chmod 700 "$CWC_DIR" 2>/dev/null || true
149149

150-
cd "$TMP_DIR/source"
150+
update_repo() {
151+
git -C "$REPO_DIR" fetch origin main --quiet && \
152+
git -C "$REPO_DIR" reset --hard FETCH_HEAD --quiet && \
153+
git -C "$REPO_DIR" clean -fd --quiet
154+
}
155+
156+
clone_repo() {
157+
git clone -b main -c core.longpaths=true --quiet https://github.com/CodeWebChat/CWC.git "$REPO_DIR"
158+
}
159+
160+
if [ -d "$REPO_DIR/.git" ] && run_step "Updating repository" update_repo; then
161+
:
162+
else
163+
rm -rf "$REPO_DIR"
164+
run_step "Cloning repository" clone_repo
165+
fi
166+
167+
cd "$REPO_DIR"
151168

152169
run_step "Installing dependencies" npx --yes pnpm install
153170

154171
cd apps/editor
172+
173+
rm -f ./*.vsix
174+
155175
run_step "Building extension" npx --yes pnpm run build
156176

157177
vsix_files=(*.vsix)
@@ -189,6 +209,3 @@ fi
189209
for i in "${indices[@]}"; do install_to_editor "$i"; done
190210

191211
cd "$ORIGINAL_DIR" || cd /
192-
193-
echo ""
194-
run_step "Cleaning up" rm -rf "$TMP_DIR"

0 commit comments

Comments
 (0)