-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.ps1
More file actions
298 lines (245 loc) · 10 KB
/
sync.ps1
File metadata and controls
298 lines (245 loc) · 10 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
param($profileName)
# If /? is specified then print help
if ($args -contains "/?") {
Write-Output "Usage: sync.ps1 [profileName] [-wipe] [-prod] [-force] [-nopref] [-precompile]"
Write-Output "profileName: The name of the profile to sync"
Write-Output "-wipe: Wipe all files on the board before syncing"
Write-Output "-prod: Sync only the minimum files for flashing, with version 1.0.0"
Write-Output "-force: Sync all files, regardless of last edit time"
Write-Output "-nopref: Skip sending prefs.json"
Write-Output "-precompile: Precompile .py files to .mpy files"
exit 0
}
# Globals
Remove-Module -Name serial-toys 2>$null
Import-Module .\serial-toys.psm1
# Set env var for baud
$env:AMPY_BAUD = 115200
# Load the profile
try {
if ($null -eq $profileName) {
throw "Profile Name Required!"
}
if (!(Test-Path -Path ".\profiles\$profileName.json")) {
# list all available profiles
$profiles = Get-ChildItem -Path ".\profiles" -Filter "*.json" | Select-Object -ExpandProperty BaseName
Write-Output "Available profiles: $profiles"
throw "Profile file .\profiles\$profileName.json does not exist!"
}
$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"
Write-Output "Placeholder" > $dest
Invoke-Expression "xcopy .\profiles\$profileName.json $dest /Y"
}
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
}
$MAX = 0
$MAXEDITTIME = 0
# check basic connectivity to the board
Write-Host "Checking basic connectivity on $port to the board.."
$job = Start-Job -ScriptBlock { ampy --port $using:port ls }
Wait-Job -Job $job -Timeout 10 | Out-Null
if ($job.State -eq "Running") {
Stop-Job -Job $job
Write-Error "Could not connect - timed out" -ErrorAction Stop
} else {
$output = Receive-Job -Job $job
if (-not $output -or $output -match "Error" -or $output -match "Traceback") {
Write-Output "Error: $LASTEXITCODE $output"
Write-Error "Could not connect to the board. Please check the connection." -ErrorAction Stop
}
}
# if the user specified -wipe or -prod, delete all files on the board
if ($args -contains "-wipe" -or $args -contains "-prod") {
Write-Host "Wipe option specified. Deleting all files on the board.."
# wipe all files on the board, recursively
$files = ampy --port $port ls -r
$i = 0
foreach ($f in $files) {
$fn = $f -replace "\s", ""
$i++
# Use Write Progress to show progress
$percentComplete = [math]::Round(($i / $files.Count) * 100)
Write-Progress "Deleting:" -Status "($percentComplete%) Deleting file $fn.." -PercentComplete (($i / $files.Count) * 100) -Id 1
# skip prefs.json if -nopref is specified
if ($args -contains "-nopref" -and $fn -eq "prefs.json") {
Write-Output "Skipping deleting prefs.json (-nopref specified)"
continue;
}
ampy --port $port rm $fn > $null
}
Write-Output "All files deleted."
}
# 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!"
} elseif ($args -contains "-wipe") {
Write-Output 0 | Out-File -Encoding ascii .\lastedit.dat
Write-Output "Wipe 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
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
}
else {
Write-Output "Last edit time on board: $MAX"
}
}
# 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
# Remove all .venv files from $files
$files = $files | Where-Object {$_ -notlike ".venv*"}
$files = $files | Where-Object {$_ -notlike ".vscode*"}
# Create an array to store the files to send
$filesToSend = @()
for ($i = 0; $i -lt $files.Count; $i++) {
$f = $files[$i]
$percentComplete = [math]::Round(($i / $files.Count) * 100)
Write-Progress "Processing files:" -Status "($percentComplete%) Checking $f..." -PercentComplete $percentComplete -Id 1
$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;
}
# Skip prefs.json if "-no-prefs" is specified
if ($args -contains "-nopref" -and $f -eq "prefs.json") {
Write-Output "Skipping sending prefs.json (-nopref specified)"
continue;
}
# Add the file to the array to send
$filesToSend += $f
}
}
# Let user know how many files will be transferred
Write-Output "Files to send: $($filesToSend.Count)"
# Send all the identified files
for ($i = 0; $i -lt $filesToSend.Count; $i++) {
$f = $filesToSend[$i]
$percentComplete = [math]::Round(($i / $filesToSend.Count) * 100)
# Ok send the file, all conditions satisfied
Write-Progress "Uploading files:" -Status "($percentComplete%) Processing file $f..." -PercentComplete $percentComplete -Id 1
# 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 "\\", "/"
# Only precompile if -precompile is specified
if ($args -contains "-precompile") {
# if the file is a .py file cross compile it, skip main.py, boot.py
if ($fn -like "*.py" -and $fn -ne "main.py" -and $fn -ne "boot.py") {
Write-Progress "Uploading files:" -Status "($percentComplete%) Cross Compiling file $fn..." -PercentComplete $percentComplete -Id 1
python -m mpy_cross -march=xtensawin $fn
$fnn = $fnn -replace ".py", ".mpy"
}
}
# send the file using ampy
Write-Progress "Uploading files:" -Status "($percentComplete%) Sending file $fnn..." -PercentComplete $percentComplete -Id 1
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"
}
else {
$percentComplete = [math]::Round((($i+1) / $filesToSend.Count) * 100)
Write-Progress "Uploading files:" -Status "($percentComplete%) Sent $fnn!" -PercentComplete $percentComplete -Id 1
}
$sent++
}
if ($sent -gt 0) {
# Write "8.1.0" to the version file
Write-Output "8.1.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 "8.1.0" | Out-File -Encoding ascii .\version
}
# Clean up
Remove-Item profile.json
Write-Output "Rebooting..."
Restart-Microcontroller $port
# Remove the progress bar
Write-Progress -Id 1 -Completed "Clearing Progress Bar"
Show-SerialLog $port