Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. $OFS = "`r`n"
  2. $updateLogDir = "~/.swupdates"
  3.  
  4. if (!(Test-Path -Path "$updateLogDir")) {
  5. New-Item -Path "$updateLogDir" -ItemType Directory
  6. }
  7.  
  8. function SetLastRun([string]$key, [DateTime] $lastRun) {
  9. if (!(Test-Path -Path "$updateLogDir")) {
  10. New-Item -Path "$updateLogDir" -ItemType Directory
  11. }
  12. Set-Content -Path "$updateLogDir/$key" -Value $lastRun.ToString("o")
  13. }
  14.  
  15. function GetLastRun([string]$key) {
  16. if (Test-Path -Path "$updateLogDir/$key") {
  17. $val = Get-Content -Path "$updateLogDir/$key"
  18. if ($val) {
  19. $result = [datetime]::Parse($val)
  20. return $result
  21. }
  22. }
  23. $result = [datetime]::MinValue
  24. return $result
  25. }
  26.  
  27.  
  28. function Update() {
  29. $logFile = "$updateLogDir/scoop-update.log"
  30. $header = "Running update: " + $(Get-Date -format "o") + $OFS
  31. Add-Content -Path $logFile -Value $header
  32. scoop update * | Out-File -FilePath $logFile -Append -Encoding UTF8
  33. $finished = Get-Date
  34. if ($LASTEXITCODE -eq 0) {
  35. SetLastRun "scoop-update" $finished
  36. $footer = "Update finished: " + $finished.ToString("o") + $OFS
  37. }
  38. else {
  39. $footer = "Update failed: " + $finished.ToString("o") + $OFS
  40. }
  41. Add-Content -Path $logFile -Value $footer
  42. }
  43.  
  44. function Clean() {
  45. $logFile = "$updateLogDir/scoop-clean.log"
  46. $header = "Running clean: " + $(Get-Date -format "o") + $OFS
  47. Add-Content -Path $logFile -Value $header
  48. scoop cleanup * --cache | Out-File -FilePath $logFile -Append -Encoding UTF8
  49. $finished = Get-Date
  50. if ($LASTEXITCODE -eq 0) {
  51. SetLastRun "scoop-clean" $finished
  52. $footer = "Clean finished: " + $finished.ToString("o") + $OFS
  53. }
  54. else {
  55. $footer = "Clean failed: " + $finished.ToString("o") + $OFS
  56. }
  57. Add-Content -Path $logFile -Value $footer
  58. }
  59.  
  60. $now = Get-Date
  61.  
  62. # if no update in last week (check registry)
  63. $lastUpdate = GetLastRun "scoop-update"
  64. if ($lastUpdate.AddDays(6) -lt $now) {
  65. Update
  66. }
  67.  
  68. # if not cleaned in last month
  69. $lastUpdate = GetLastRun "scoop-update"
  70. $lastClean = GetLastRun "scoop-clean"
  71. if ($lastUpdate.AddDays(6) -gt $now) {
  72. if ($lastClean.AddDays(30) -lt $now) {
  73. Clean
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement