Skip to content

Commit

Permalink
Fix update script, pop by stash guid
Browse files Browse the repository at this point in the history
* Resolves #75
  • Loading branch information
codyduong committed Mar 27, 2024
1 parent 5be2308 commit 61d18a2
Showing 1 changed file with 64 additions and 37 deletions.
101 changes: 64 additions & 37 deletions windows/components/update.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
. $PSScriptRoot\utils.ps1
$script:account = "codyduong"
$script:repo = "Dotfiles"
$script:branch = "main"
$script:DotfilesRemoteProfileUrl = "https://raw.githubusercontent.com/$account/$repo/$branch/windows/profile.ps1"
$script:DotfilesAvailableProfileVersionFile = [System.IO.Path]::Combine("$HOME", '.latest_profile_version')
$script:VersionRegEx = "# [vV]ersion (?<version>\d+\.\d+\.\d+)"
$script:Dotfiles = [System.IO.Path]::Combine("$HOME", "$repo")
$script:account = "codyduong"
$script:repo = "Dotfiles"
$script:branch = "main"
$script:DotfilesRemoteProfileUrl = "https://raw.githubusercontent.com/$account/$repo/$branch/windows/profile.ps1"
$script:DotfilesAvailableProfileVersionFile = [System.IO.Path]::Combine("$HOME", '.latest_profile_version')
$script:VersionRegEx = "# [vV]ersion (?<version>\d+\.\d+\.\d+)"
$script:Dotfiles = [System.IO.Path]::Combine("$HOME", "$repo")
Remove-Variable account
Remove-Variable repo
Remove-Variable branch
Expand All @@ -27,47 +27,74 @@ function Sync-Profile {
}

function Update-AvailableProfile {
param(
[switch]$Force
)

$ArgsToPassToThreadJob = @(
$DotfilesRemoteProfileUrl,
$DotfilesAvailableProfileVersionFile,
$VersionRegEx,
$Dotfiles
)

$null = Start-ThreadJob -Name "Get remote version" -StreamingHost $Host -ArgumentList $ArgsToPassToThreadJob -ScriptBlock {
param ($DotfilesRemoteProfileUrl, $DotfilesAvailableProfileVersionFile, $VersionRegEx, $Dotfiles)
$existingJob = Get-Job -Name "Get remote version" -ErrorAction SilentlyContinue

$DotfilesAvailableProfileVersion = [System.IO.File]::ReadAllText($DotfilesAvailableProfileVersionFile)
$startJob = $true
if ($Force -and ($null -ne $existingJob)) {
Remove-Job -Job $existingJob -Force
}
else {
if ($null -ne $existingJob) {

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$lastRunTime = $existingJob.PSBeginTime
$currentTime = Get-Date
$timeDifference = $currentTime - $lastRunTime

if (Test-Path "$Dotfiles\.git") {
Set-Location $Dotfiles
$stashed = $false
[string]$stashName = New-Guid
try {
$stashed = $(git stash push -u -m $stashName)
if ($timeDifference.TotalMinutes -gt 30) {
Remove-Job -Job $existingJob -Force
}
catch {
Write-Warning $_
else {
$startJob = $false
}
$old_branch = $(git rev-parse --abbrev-ref HEAD)
git checkout $branch
git fetch
git pull
$profileFile = [System.IO.File]::ReadAllText("$Dotfiles\windows\profile.ps1")
git checkout $old_branch
if ($stashed -ne $false) {
git stash pop
}
}
else {
# Check via remote if we can
$profileFile = Invoke-WebRequest -Uri $DotfilesRemoteProfileUrl -UseBasicParsing
}
[version]$remoteVersion = "0.0.0"
if ($profileFile -match $VersionRegEx) {
$remoteVersion = $matches.Version
if ($remoteVersion -gt [version]$DotfilesAvailableProfileVersion) {
Set-Content -Path $DotfilesAvailableProfileVersionFile -Value $remoteVersion
}

if ($startJob) {
$null = Start-Job -Name "Get remote version" -ArgumentList $ArgsToPassToThreadJob -ScriptBlock {
param ($DotfilesRemoteProfileUrl, $DotfilesAvailableProfileVersionFile, $VersionRegEx, $Dotfiles)

Check warning

Code scanning / PSScriptAnalyzer

The variable '$Dotfiles' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier. Warning

The variable '$Dotfiles' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.

Check warning

Code scanning / PSScriptAnalyzer

The variable '$DotfilesAvailableProfileVersionFile' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier. Warning

The variable '$DotfilesAvailableProfileVersionFile' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.

Check warning

Code scanning / PSScriptAnalyzer

The variable '$DotfilesRemoteProfileUrl' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier. Warning

The variable '$DotfilesRemoteProfileUrl' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.

Check warning

Code scanning / PSScriptAnalyzer

The variable '$VersionRegEx' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier. Warning

The variable '$VersionRegEx' is not declared within this ScriptBlock, and is missing the 'Using:' scope modifier.

$DotfilesAvailableProfileVersion = [System.IO.File]::ReadAllText($DotfilesAvailableProfileVersionFile)

if (Test-Path "$Dotfiles\.git") {
Set-Location $Dotfiles
$stashed = $false
[string]$stashName = New-Guid
try {
$stashed = $(git stash push -u -m $stashName)
}
catch {
Write-Warning $_
}
$old_branch = $(git rev-parse --abbrev-ref HEAD)
git checkout $branch
git fetch
git pull
$profileFile = [System.IO.File]::ReadAllText("$Dotfiles\windows\profile.ps1")
git checkout $old_branch
if ($stashed -ne $false) {
git stash pop
}
}
else {
# Check via remote if we can
$profileFile = Invoke-WebRequest -Uri $DotfilesRemoteProfileUrl -UseBasicParsing
}
[version]$remoteVersion = "0.0.0"
if ($profileFile -match $VersionRegEx) {
$remoteVersion = $matches.Version
if ($remoteVersion -gt [version]$DotfilesAvailableProfileVersion) {
Set-Content -Path $DotfilesAvailableProfileVersionFile -Value $remoteVersion
}
}
}
}
Expand Down Expand Up @@ -112,7 +139,7 @@ function Update-Profile {
. .\windows\scripts\bootstrap.ps1 -update;
git checkout $old_branch
if ($stashed -ne $false) {
git stash pop
git stash apply stash^{/$stashName}
}
}
else {
Expand Down

0 comments on commit 61d18a2

Please sign in to comment.