-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpdate-Files.ps1
339 lines (312 loc) · 13.4 KB
/
Update-Files.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
[cmdletbinding()]
param(
[switch]
$Force
)
function Get-Files {
@{
CAD = @{
'Assembly *' = {
Get-ChildItem | Compress-Archive -DestinationPath Assembly.zip -Force
Remove-Item Assembly* -Exclude Assembly.zip
}
}
Images = @{
render_1 = "Invoke-ScriptFu -Resize $($s=550; (($s/1024)*1280)),$s -CropCenter 400,400"
render_cw2 = "Invoke-ScriptFu -Resize $($s=550; (($s/1024)*1280)),$s -CropCenter 400,400"
render_ebay = "Invoke-ScriptFu -Resize $($s=500; (($s/1024)*1280)),$s -Crop 400,400,112,0"
render_feeder = "Invoke-ScriptFu -Resize $($s=700; (($s/1024)*1280)),$s -CropCenter 400,400"
render_splitter = "Invoke-ScriptFu -Resize $($s=500; (($s/1024)*1280)),$s -CropCenter 400,400"
render_heater = "Invoke-ScriptFu -Resize $($s=550; (($s/1024)*1280)),$s -CropCenter 400,400"
}
Manual = @{
Assembly = {
. $soffice --headless --convert-to pdf:writer_pdf_Export (Get-ChildItem).FullName
(Get-Process soffice).WaitForExit()
}
}
STLs = @{
'**/*.stl' = { Get-ChildItem | Update-Stl }
}
"$env:LOCALAPPDATA" = @{
'Temp\Neutron\*.stl' = { Get-ChildItem | Copy-ToProjectFolder }
}
}
}
# programms
$gimp = Get-ChildItem "$env:ProgramFiles\gimp*" -Directory |
Get-ChildItem -Directory -Filter bin |
Get-ChildItem -File -Filter gimp*console*.exe |
ForEach-Object FullName # download: https://www.gimp.org/downloads/
. $gimp --help | Out-Null; if ($LASTEXITCODE -ne 0) { throw "gimp is not installed" }
$soffice = Get-ChildItem "$env:ProgramFiles\LibreOffice*" -Directory |
Get-ChildItem -Directory -Filter program |
Get-ChildItem -File -Filter "soffice.exe" |
ForEach-Object FullName # download: https://www.libreoffice.org/download/download-libreoffice/
if ($null -eq $soffice) { throw "soffice is not installed" }
$stl_transform = "wsl stl_transform" # download: https://github.com/AllwineDesigns/stl_cmd
$stl_bbox = "wsl stl_bbox" # download: https://github.com/AllwineDesigns/stl_cmd
function Copy-ToProjectFolder {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileSystemInfo[]]
$file
)
begin {
}
process {
Write-Host -ForegroundColor Green $file.Name -NoNewline
$dest = Get-ChildItem $PSScriptRoot -Recurse -Filter $_.Name
switch (($dest | Measure-Object).Count) {
0 {
Write-Host -ForegroundColor Red " => could not be found in project folder."
}
1 {
if ($dest.LastWriteTime -gt $file.LastWriteTime) {
Write-Host -ForegroundColor Cyan "Nothing to do. (output is newer than input)"
} else {
Write-Host -ForegroundColor Cyan "`nCopy-Item $($file.Name) .$($dest.FullName.Substring($PSScriptRoot.Length)) -Force"
Copy-Item $file.FullName $dest.FullName -Force
}
}
default {
Write-Host -ForegroundColor Red " => was found multiple times in project folder."
}
}
}
end {}
}
function Update-Stl {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileSystemInfo[]]
$file,
[Parameter()]
[switch]
$PrintNoFileName
)
begin {
}
process {
Push-Location $file.DirectoryName
try {
if (!$PrintNoFileName) { Write-Host -ForegroundColor Green $file.Name -NoNewline }
@(
'_x1\.stl$'
'\s'
'Body\d.*\.stl$'
) | ForEach-Object {
if ($file.Name -match $_) {
Write-Host -ForegroundColor Red " => Illegal file name ($_)"
}
}
if ($file.Name -like "*(1)*" -or
$file.Name -like "*.stl.stl"
) {
$new_file = $file.Name.Replace('(1)', '').Replace('.stl.stl', '.stl')
$cmd = "Move-Item '$($file.Name)' '$new_file' -Force"
Write-Host -ForegroundColor Cyan "`n$cmd"
Invoke-Expression $cmd
$file = Get-ChildItem $new_file
Write-Host -ForegroundColor Green $file.Name -NoNewline
}
$bbox = Invoke-Expression "$stl_bbox $($file.Name)" |
Select-String -Pattern "\(([^,]+),\s*([^,]+),\s*([^,]+)\)" -AllMatches |
ForEach-Object { $_.Matches } |
ForEach-Object { [pscustomobject]@{x=[decimal]$_.Groups[1].value;y=[decimal]$_.Groups[2].value;z=[decimal]$_.Groups[3].value}}
$center = [pscustomobject]@{
x=[Math]::Round($bbox[2].x / 2 + $bbox[0].x, 3)
y=[Math]::Round($bbox[2].y / 2 + $bbox[0].y, 3)
}
Write-Debug $center
if ($bbox[2].x -gt 200 -or $bbox[2].y -gt 200) {
$out_45 = $file.Name.Replace('.stl', '_45.stl')
$cmd = "$stl_transform -rz 45 $($file.Name)"
Invoke-Expression "$cmd $out_45"
$bbox_45 = Invoke-Expression "$stl_bbox $out_45" |
Select-String -Pattern "\(([^,]+),\s*([^,]+),\s*([^,]+)\)" -AllMatches |
ForEach-Object { $_.Matches } |
ForEach-Object { [pscustomobject]@{x=[decimal]$_.Groups[1].value;y=[decimal]$_.Groups[2].value;z=[decimal]$_.Groups[3].value}}
$out_215 = $file.Name.Replace('.stl', '_215.stl')
$cmd = "$stl_transform -rz 215 $($file.Name)"
Invoke-Expression "$cmd $out_215"
$bbox_215 = Invoke-Expression "$stl_bbox $out_215" |
Select-String -Pattern "\(([^,]+),\s*([^,]+),\s*([^,]+)\)" -AllMatches |
ForEach-Object { $_.Matches } |
ForEach-Object { [pscustomobject]@{x=[decimal]$_.Groups[1].value;y=[decimal]$_.Groups[2].value;z=[decimal]$_.Groups[3].value}}
if ($bbox_45[2].x -le 200 -and $bbox_215[2].x -le 200 -and
$bbox_45[2].y -le 200 -and $bbox_215[2].y -le 200) {
Move-Item $out_45 $file.Name -Force
Remove-Item $out_215
$bbox = $bbox_45
} elseif ($bbox_45[2].x -le 200 -and $bbox_45[2].y -le 200) {
Move-Item $out_45 $file.Name -Force
Remove-Item $out_215
$bbox = $bbox_45
} elseif ($bbox_215[2].x -le 200 -and $bbox_215[2].y -le 200) {
Move-Item $out_215 $file.Name -Force
Remove-Item $out_45
$bbox = $bbox_215
} else {
Remove-Item $out_45
Remove-Item $out_215
}
}
if ($bbox[2].x -gt 200 -or $bbox[2].y -gt 200) {
Write-Host -ForegroundColor Red " => ($($bbox[2].x) x $($bbox[2].y)) does not fit 200x200 build plate"
}
$cmd = ""
if ($center.x -ne 0) { $cmd += " -tx $(-$center.x)" }
if ($center.y -ne 0) { $cmd += " -ty $(-$center.y)" }
if ($bbox[0].z -ne 0) { $cmd += " -tz $(-$bbox[0].z)" }
if ($cmd.Length -gt 0) {
$cmd = "$stl_transform $cmd $($file.Name)"
if (!$PrintNoFileName) { Write-Host -ForegroundColor Cyan "" }
Write-Host -ForegroundColor Cyan "$cmd"
Invoke-Expression "$cmd out.stl"
Move-Item out.stl $file.Name -Force
} else {
Write-Host
}
} catch {
} finally {
Pop-Location
}
}
end {}
}
function Invoke-ScriptFu {
param(
[int[]] $Crop, # width, height, x, y
[int[]] $CropCenter, # width, height
[int[]] $Resize, # width, height
[int] $Contrast, # -127 .. 127
[int] $Bightness # -127 .. 127
)
$script = '(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE "input.png" "input.png"))) (drawable (car (gimp-image-get-active-layer image)))) __script__(gimp-file-save RUN-NONINTERACTIVE image drawable "output.png" "output.png") (gimp-image-delete image))'
if ($Resize) {
$script = $script.Replace("__script__", "(gimp-image-scale image {0} {1}) __script__" -f $($Resize))
}
if ($Crop) {
$script = $script.Replace("__script__", "(gimp-image-crop image {0} {1} {2} {3}) __script__" -f $($Crop))
}
if ($CropCenter) {
$script = $script.Replace("__script__", "(gimp-image-crop image {0} {1} (/ (- (car (gimp-image-width image)) {0}) 2) (/ (- (car (gimp-image-height image)) {1}) 2)) __script__" -f $($CropCenter))
}
if ($Contrast -or $Bightness) {
$script = $script.Replace("__script__", "(gimp-brightness-contrast drawable {0} {1}) __script__" -f ($Bightness, $Contrast))
}
$script = $script.Replace("__script__", "")
$script = $script.Replace("input.png", $Global:op.Input)
$script = $script.Replace("output.png", $Global:op.Output)
Write-Debug $script
Invoke-Expression ". `"$gimp`" -i -b '$script' -b '(gimp-quit 0)'"
}
function Invoke-Command {
param ([object] $command)
switch ( $command.GetType().FullName ){
System.Object[] {
foreach ($item in $command) {
Invoke-Command $item
}
}
System.String {
Write-Host -ForegroundColor Cyan $command.ToString().Trim()
Invoke-Expression $command
}
System.Management.Automation.ScriptBlock {
(
$command.ToString().Split("`n") |
ForEach-Object { "`t" + $_.Trim() } |
Join-String -Separator `n
).Trim() |
ForEach-Object { "`t" + $_ } |
Write-Host -ForegroundColor Cyan
$item.Invoke()
}
default {
throw $command.GetType().FullName
}
}
}
try {
$yammu_file = $env:Temp + "\yammu.txt"
Push-Location $PSScriptRoot
(Get-Files).GetEnumerator() |
Sort-Object { $_.Key -notin @("$env:LOCALAPPDATA") } |
ForEach-Object {
try {
Push-Location $_.Name
foreach ($item in $_.Value.GetEnumerator()) {
$op = [pscustomobject]@{
Input = $item.Name
Output = $null
Commands = @( $item.Value )
}
# determine what is todo
switch ($_.Name){
Images {
$op.Output = $op.Input + "_processed.png"
$op.Input = $op.Input + ".png"
$op.Commands += "Invoke-Item $($op.Output)"
}
CAD {
$op.Output = $op.Input + ".zip"
$op.Input = $op.Input + ".step"
}
Manual {
$op.Output = $op.Input + ".pdf"
$op.Input = $op.Input + ".odp"
}
default {
$op.Output = $yammu_file
}
}
# check if output is outdated
if (!$Force) {
try {
if ((
Get-ChildItem $op.Input |
Sort-Object LastWriteTime |
Select-Object -Last 1
).LastWriteTime -lt (Get-Item $op.Output).LastWriteTime) {
$op.Output = $null
}
} catch {
}
}
Write-Host "$($op.Input) : "
# do or do not (there is no try)
if (!(Test-Path $op.Input)) {
Write-Host -ForegroundColor Cyan "`tNothing to do. (no input file present)"
} elseif ($null -eq $op.output -and !$Force) {
Write-Host -ForegroundColor Cyan "`tNothing to do. (output is newer than input)"
} else {
try {
$PSDefaultParameterValues['Get-ChildItem:Path'] = $op.Input
if ($PSDefaultParameterValues['Get-ChildItem:Path'].StartsWith("**")) {
$PSDefaultParameterValues['Get-ChildItem:Path'] = $PSDefaultParameterValues['Get-ChildItem:Path'].SubString(3)
$PSDefaultParameterValues['Get-ChildItem:Recurse'] = $true
}
if ((!$Force) -and (Test-Path $op.Output)) {
$PSDefaultParameterValues['Get-ChildItem:Path'] = Get-ChildItem |
Where-Object LastWriteTime -gt (Get-Item $op.Output).LastWriteTime |
ForEach-Object { [System.IO.Path]::Combine($_.DirectoryName, ($_.Name -replace '(\[|\])','``$1')) }
}
$Global:op = $op
Invoke-Command $op.Commands
} finally {
$PSDefaultParameterValues.Remove('Get-ChildItem:Path')
$PSDefaultParameterValues.Remove('Get-ChildItem:Recurse')
}
}
}
} finally {
Remove-Variable op -ErrorAction SilentlyContinue
Pop-Location
}
}
} finally {
Set-Content -Path $yammu_file -Value (Get-Date)
Pop-Location
}