Repository navigation
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
192 lines (167 loc) · 7.84 KB
/
Copy pathMicrosoft.PowerShell_profile.ps1
File metadata and controls
192 lines (167 loc) · 7.84 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
function Show-GitStatusColored {
$statusLines = git status --porcelain
if (-not $statusLines) {
Write-Host "No changes detected" -ForegroundColor Gray
return
}
Write-Host "`n=== GIT STATUS (colored) ===" -ForegroundColor Cyan
foreach ($line in $statusLines) {
if ($line.Length -lt 3) { Write-Host $line; continue }
$status = $line.Substring(0, 2)
$color = 'White'
switch -Regex ($status) {
'^\?\?' { $color = 'Magenta' }
'^A' { $color = 'Green' }
'^M' { $color = 'Yellow' }
'^D' { $color = 'Red' }
'^R' { $color = 'Cyan' }
'^C' { $color = 'Cyan' }
'^U' { $color = 'Red' }
default { $color = 'White' }
}
Write-Host $line -ForegroundColor $color
}
}
function push-staging {
# 1. Configuration
$rootPath = "C:\ppdo"
$stagingPath = "$rootPath\ppdo-staging"
$sourcePath = "$rootPath\ppdo-next"
$protectedItems = @(".git", "node_modules")
$remoteUrl = "https://github.com/mviner000/ppdo-staging.git"
$targetBranch = "main"
$sw = [System.Diagnostics.Stopwatch]::StartNew()
Write-Host "`n--- Starting Sync & Deploy: next -> staging ---" -ForegroundColor Cyan
if (!(Test-Path $stagingPath) -or !(Test-Path $sourcePath)) {
Write-Host "ERROR: Folder paths not found." -ForegroundColor Red
Write-Host "Expected paths:" -ForegroundColor Yellow
Write-Host " Source: $sourcePath" -ForegroundColor Gray
Write-Host " Staging: $stagingPath" -ForegroundColor Gray
return
}
try {
# --- STEP 1: CAPTURE COLLEAGUE'S MESSAGE ---
Write-Host "Step 1: Capturing last commit message from next..." -ForegroundColor Yellow
Set-Location $sourcePath
# Capture as string and trim whitespace
$lastMessage = (git log -1 --pretty=%B 2>&1 | Out-String).Trim()
# Check if message is empty or git command failed
if ([string]::IsNullOrWhiteSpace($lastMessage) -or $LASTEXITCODE -ne 0) {
throw "Could not capture git message from ppdo-next."
}
Write-Host "Captured Message: '$($lastMessage.Split("`n")[0])...'" -ForegroundColor Gray
# --- STEP 2: CLEANING STAGING ---
Write-Host "Step 2: Cleaning staging (preserving .git/modules)..." -ForegroundColor Yellow
$itemsToDelete = Get-ChildItem -Path $stagingPath -Force | Where-Object {
$name = $_.Name
($protectedItems -notcontains $name) -and ($name -notlike ".env*")
}
$itemsToDelete | ForEach-Object {
Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction Stop
}
# --- STEP 3: COPYING FILES ---
Write-Host "Step 3: Copying updated files..." -ForegroundColor Yellow
$itemsToCopy = Get-ChildItem -Path $sourcePath -Force | Where-Object {
$name = $_.Name
($protectedItems -notcontains $name) -and ($name -notlike ".env*")
}
$itemsToCopy | ForEach-Object {
Copy-Item -LiteralPath $_.FullName -Destination $stagingPath -Recurse -Force -ErrorAction Stop
}
# --- STEP 4: NPM INSTALL ---
Set-Location $stagingPath
if (Test-Path "package.json") {
Write-Host "Step 4: Running 'npm install'..." -ForegroundColor Yellow
cmd /c "npm install"
if ($LASTEXITCODE -ne 0) { throw "npm install failed." }
}
# --- STEP 5: CONFIGURE GIT REMOTE & BRANCH ---
Write-Host "Step 5: Configuring git remote and branch..." -ForegroundColor Yellow
# Check if origin exists
$remoteExists = git remote get-url origin 2>$null
if ($remoteExists) {
# Update existing remote
git remote set-url origin $remoteUrl
Write-Host "Remote 'origin' updated to: $remoteUrl" -ForegroundColor Gray
}
else {
# Add new remote
git remote add origin $remoteUrl
Write-Host "Remote 'origin' added: $remoteUrl" -ForegroundColor Gray
}
# Ensure we're on the correct branch
$currentBranch = git branch --show-current
if ($currentBranch -ne $targetBranch) {
# Check if main branch exists
$branchExists = git rev-parse --verify $targetBranch 2>$null
if ($branchExists) {
# Switch to existing main branch
git checkout $targetBranch
Write-Host "Switched to existing branch: $targetBranch" -ForegroundColor Gray
}
else {
# Create and switch to new main branch
git checkout -b $targetBranch
Write-Host "Created and switched to new branch: $targetBranch" -ForegroundColor Gray
}
}
else {
Write-Host "Already on branch: $targetBranch" -ForegroundColor Gray
}
# --- STEP 6: GIT STATUS & REVIEW ---
Write-Host "`nStep 6: Reviewing changes to be committed..." -ForegroundColor Yellow
git add .
# Show git status (colored)
$statusLines = git status --porcelain
Show-GitStatusColored
# Show commit summary
Write-Host "`n=== COMMIT SUMMARY ===" -ForegroundColor Cyan
Write-Host "Branch: $targetBranch" -ForegroundColor White
Write-Host "Remote: $remoteUrl" -ForegroundColor White
Write-Host "Commit Message: $lastMessage" -ForegroundColor White
# Count changes
$statusOutput = $statusLines
if ($statusOutput) {
$fileCount = ($statusOutput | Measure-Object).Count
Write-Host "Files Changed: $fileCount" -ForegroundColor White
}
else {
Write-Host "Files Changed: 0 (No changes detected)" -ForegroundColor Gray
}
# --- STEP 7: CONFIRMATION PROMPT ---
Write-Host "`n=== CONFIRMATION ===" -ForegroundColor Yellow
Write-Host "Ready to commit and push to staging repository." -ForegroundColor White
$confirmationInput = Read-Host "Proceed with push? (Y/n)"
$confirmation = if ([string]::IsNullOrWhiteSpace($confirmationInput)) { 'y' } else { $confirmationInput.Trim().ToLowerInvariant() }
if ($confirmation -in @('n', 'no')) {
Write-Host "`nPush cancelled by user." -ForegroundColor Yellow
$sw.Stop()
Write-Host "Time: $($sw.Elapsed.TotalSeconds.ToString('N2'))s" -ForegroundColor Cyan
return
}
# --- STEP 8: GIT COMMIT & PUSH ---
Write-Host "`nStep 7: Committing and Pushing to staging..." -ForegroundColor Yellow
# We use the captured message exactly as it was
git commit -m "$lastMessage"
if ($LASTEXITCODE -eq 0) {
Write-Host "Pushing to origin/$targetBranch..." -ForegroundColor Yellow
# Set upstream and push to main
git push --set-upstream origin $targetBranch
if ($LASTEXITCODE -ne 0) { throw "git push failed." }
Write-Host "Success: Code pushed to staging repository on $targetBranch branch." -ForegroundColor Green
# Return to development folder after success
Set-Location $sourcePath
}
else {
Write-Host "Note: No changes detected to commit." -ForegroundColor Gray
}
$sw.Stop()
Write-Host "`nTOTAL SUCCESS: Sync, Install, and Push complete!" -ForegroundColor Green
Write-Host "Time: $($sw.Elapsed.TotalSeconds.ToString('N2'))s" -ForegroundColor Cyan
}
catch {
$sw.Stop()
Write-Host "`nFATAL ERROR: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Process halted." -ForegroundColor DarkRed
}
}