-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.ps1
181 lines (149 loc) · 5.47 KB
/
sync.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
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
param($profileName)
# Globals
Import-Module .\serial-toys.psm1
# Set env var for baud
$env:AMPY_BAUD = 921600
# Load the profile
try {
if ($null -eq $profileName) {
throw "Profile Name Required!"
}
$activeProfile = Get-Content -Raw ".\profiles\$profileName.json" | ConvertFrom-Json
# load active modules
$activeModules = $activeProfile.activeModules
Write-Host "Flashing Profile: $profileName"
Write-Host "Active Modules: $activeModules"
$dest = "$PSScriptRoot\profile.json"
Invoke-Expression "xcopy .\profiles\$profileName.json $dest /Y /-I"
}
catch {
Write-Error "Could not load profile from file .\profiles\$profileName.json"
Write-Error "Please specify the profile name as a parameter to this script" -ErrorAction Stop
}
# Connect to the board
$port = Find-MicrocontrollerPort
if ($port -eq "COM1")
{
Write-Error "Board not detected. Aborting." -ErrorAction Stop
}
Start-Sleep 4
$MAX = 0
$MAXEDITTIME = 0
# if the user added -prod to the command line, send a new file to the board containing 1.0.0 with the file name version
if ($args -contains "-prod") {
Write-Host "Prod option specified. Will upload minimum files for flashing, with version 1.0.0."
Write-Output 0 | Out-File -Encoding ascii .\lastedit.dat
$activeModules = @("basic", "ota", "wifi", "web")
} elseif ($args -contains "-force") {
Write-Output 0 | Out-File -Encoding ascii .\lastedit.dat
Write-Output "Force option specified. All files will be copied!"
} else {
Write-Host "Checking when board was last updated.."
Remove-Item ./lastedit.dat
ampy --port $port get lastedit.dat > lastedit.dat # 2> $null
$MAX = Get-Content -Path .\lastedit.dat
$MAXEDITTIME = $MAX
Write-Output "Last sync for this board was at $MAX"
if ((Get-Item "lastedit.dat").length -eq 0) {
Write-Output "The board does not have a lastedit.dat file, so all files will be copied."
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null
Write-Output 0 | Out-File -Encoding ascii .\lastedit.dat
}
}
# send all files to the device
$files = Get-ChildItem . -name -recurse -include *.py, *.html, *.sh, *.js, *.cfg, *.crt, *.key, *.c, *.raw, profile.json, *.json
$sent = 0
for ($i = 0; $i -lt $files.Count; $i++) {
$f = $files[$i]
$LE = (Get-ChildItem $f).LastWriteTimeUtc | Get-Date -UFormat %s
if ($LE -gt $MAX) {
# Skip unchanged files
if ($MAXEDITTIME -lt $LE)
{
$MAXEDITTIME = $LE
}
# Skip files from inactive modules
$activeModule = $False
$rootFile = $False
if ($activeModules.Contains("$((Get-Item $f).Directory.Name)")) {
$activeModule = $True
}
if (!$f.Contains("\")) {
$rootFile = $True
}
if ($f.Contains("board_system")) {
$rootFile = $True
}
if ($f.Contains("ulib")) {
$rootFile = $True
}
# Skip .package
if ($f.Contains(".package")) {
continue;
}
if (!$rootFile -and !$activeModule)
{
continue;
}
# Ok send the file, all conditions satisfied
Write-Output "Sending file $f..."
# MAKE SURE PATH EXISTS ON DEVICE
$bits = $f.ToString() -split '\\'
$dir = ""
for ($j = 0; $j -lt $bits.Count - 1; $j++) {
if ($j -gt 0) {
$dir = $dir + "/" + $bits[$j]
}
else {
$dir = $bits[$j]
}
ampy --port $port mkdir $dir > $null 2>&1
}
# SEND THE FILE
$fn = "$($f)"
$fnn = $fn -replace "\\", "/"
ampy --port $port put $fnn $fnn
if (!($?)) {
Write-Output "Failed to send file to the board, attempting to delete and send again.."
$boardFile = "$(ampy --port $port get $fnn)"
Write-Output "File on microcontoller is $($boardFile.length) bytes"
Write-Output "File on disk is $((Get-Item $fnn).length) bytes"
Write-Output "Deleting file on microcontroller:"
ampy --port $port rm $fnn
Write-Output "Trying another copy:"
ampy --port $port put $fnn $fnn
if (!($?)) {
Write-Output "Failed again. Giving up."
exit 3
}
Write-Output "Success, moving to next file"
}
$sent++
}
}
if ($sent -gt 0) {
# Write "7.0.0" to the version file
Write-Output "7.0.0" | Out-File -Encoding ascii .\version
Write-Host "Uploading new version file to board.."
ampy --port $port put version
# record the last time a file was edited
$MAXEDITTIME = [math]::Round($MAXEDITTIME)
Write-Output $MAXEDITTIME | Out-File -Encoding ascii .\lastedit.dat
Write-Host "Uploading lastedit.dat file to board.."
ampy --port $port put lastedit.dat
} else {
Write-Output "No changes since last sync."
}
# if the user added -prod to the command line, send a new file to the board containing 1.0.0 with the file name version
if ($args -contains "-prod") {
Write-Output "1.0.0" | Out-File -Encoding ascii .\version
Write-Host "Uploading 1.0.0 version file to board, so it will auto update.."
ampy --port $port put version
Write-Output "7.0.0" | Out-File -Encoding ascii .\version
}
# Clean up
Remove-Item profile.json
Write-Output "Rebooting..."
Restart-Microcontroller $port
Show-SerialLog $port