-
Notifications
You must be signed in to change notification settings - Fork 10
/
Start & sync SM.ps1
96 lines (78 loc) · 3.35 KB
/
Start & sync SM.ps1
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
# PowerShell script for running and syncing SM https://github.com/supermemo/SuperMemoScripts
# To be used with a git backup, as per https://www.supermemo.wiki/en/supermemo/backup-guide#internet-backups-git
# To run, put this script into your SM collection folder (the one with the .KNO file), right click the script and
# Sent to > Desktop. Then, right click the newly created shortcut and select Properties.
# Depending whether you use SM or SMA, put the respective string below (replacing the correct path) in the Target field (ignoring the #) and press OK
# SM
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'D:\path\to\Start & sync SM.ps1' C:\path\to\sm18.exe"
# SM + Pro mode
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'D:\path\to\Start & sync SM.ps1' C:\path\to\sm18.exe --pro"
# SMA
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'D:\path\to\Start & sync SM.ps1' C:\path\to\SuperMemoAssistant.exe"
# SMA + Pro mode
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'D:\path\to\Start & sync SM.ps1' C:\path\to\SuperMemoAssistant.exe --pro"
function Add-GitFiles {
git add -A *
}
function Clear-CurrentFolder {
Add-GitFiles
git stash
git reset --hard
git pull
Read-Host "Current folder is now clean. Google `"git stash`" if you need to get your changes back. (Enter to continue)"
}
function Remove-UselessFiles {
$cmdOutput = git status --porcelain=v1
$cmdOutput
if ($cmdOutput.Count -le 7) { # less or equals
$userInput = Read-Host -Prompt "It seems that SM was opened and closed without performing many actions. Type cl to clear them."
if ($userInput -eq "cl") {
Clear-CurrentFolder
}
}
}
if ($args[$args.Count - 1] -eq "--pro") {
$proMode = $true
$args[$args.Count - 1] = $null;
}
"git pull"
git pull
$pullCode = $LASTEXITCODE
"git status"
$statusOutput = cmd /c git status --porcelain=v1
if ($null -ne $statusOutput -or $pullCode) {
$statusOutput
"`r`nNon standard git output - double check above"
if ($proMode) {
$userInput = Read-Host -Prompt "Type:`r`ncl if you want to clear any unsaved changes (backup will be stashed)`r`ndiff if you want to see what's actually changed (q to quit - if needed)"
while ($userInput -eq "diff") {
git add --intent-to-add .
git diff
$userInput = Read-Host -Prompt "Type:`r`ncl if you want to clear any unsaved changes (backup will be stashed)`r`ndiff if you want to see what's actually changed (q to quit - if needed)"
}
if ($userInput -eq "cl") {
Clear-CurrentFolder
}
} else {
Read-Host -Prompt "Press Enter to continue"
}
} else {
"All OK - proceeding"
}
"`r`nStarted SM, will commit changes on close. Close this terminal if you don't want that"
& $args[0] $args[1] | Out-Null # start SM from provided path (&) and wait for it to close (Out-Null)
"Closed SM"
if ($proMode) {
Remove-UselessFiles
}
"Proceeding to commit & push changes"
Add-GitFiles
git commit -m "PowerShell script update"
"git pull"
git pull
$pullCode = $LASTEXITCODE
"git push"
git push -u
if ($pullCode -or $LASTEXITCODE) {
Read-Host "`r`nNon standard git output - double check above"
}