-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodesys-import.ps1
More file actions
137 lines (98 loc) · 3.42 KB
/
codesys-import.ps1
File metadata and controls
137 lines (98 loc) · 3.42 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
<#
.SYNOPSIS
Import text files back into a CODESYS project
.DESCRIPTION
Imports .st text files into a CODESYS project using headless CODESYS execution.
Performs authoritative import - items not in the import directory will be deleted
from the project.
.PARAMETER ProjectPath
Path to the CODESYS .project file
.PARAMETER ImportDir
Directory containing .st files to import
.PARAMETER DryRun
Preview changes without modifying the project
.PARAMETER CodesysPath
Optional: Path to CODESYS.exe (auto-detected if not specified)
.PARAMETER Profile
Optional: CODESYS profile name (default: CODESYS V3.5 SP21 Patch 3)
.EXAMPLE
.\codesys-import.ps1 -ProjectPath "MyProject.project" -ImportDir ".\import" -DryRun
.EXAMPLE
.\codesys-import.ps1 -ProjectPath "MyProject.project" -ImportDir ".\import"
#>
param(
[Parameter(Mandatory=$true)]
[string]$ProjectPath,
[Parameter(Mandatory=$true)]
[string]$ImportDir,
[Parameter(Mandatory=$false)]
[switch]$DryRun,
[Parameter(Mandatory=$false)]
[string]$CodesysPath = "",
[Parameter(Mandatory=$false)]
[string]$Profile = "CODESYS V3.5 SP21 Patch 3"
)
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Validate inputs
if (-not (Test-Path $ProjectPath)) {
Write-Host "Error: Project file not found: $ProjectPath" -ForegroundColor Red
exit 1
}
if (-not (Test-Path $ImportDir)) {
Write-Host "Error: Import directory not found: $ImportDir" -ForegroundColor Red
exit 1
}
Write-Host "=== CODESYS Import ===" -ForegroundColor Cyan
Write-Host " Project: $ProjectPath" -ForegroundColor Gray
Write-Host " Import Dir: $ImportDir" -ForegroundColor Gray
Write-Host " Profile: $Profile" -ForegroundColor Gray
if ($DryRun) {
Write-Host " Mode: DRY RUN (no changes will be made)" -ForegroundColor Yellow
}
# List files to import
$stFiles = Get-ChildItem -Path $ImportDir -Filter "*.st" -ErrorAction SilentlyContinue
if ($stFiles) {
Write-Host "`n Files to import:" -ForegroundColor Gray
$stFiles | ForEach-Object {
$type = switch -Wildcard ($_.Name) {
"*.gvl.st" { "[GVL] " }
"*.prg.st" { "[PRG] " }
"*.fb.st" { "[FB] " }
"*.fun.st" { "[FUN] " }
"*.meth.st" { "[METH]" }
default { "[???] " }
}
Write-Host " $type $($_.Name)" -ForegroundColor Gray
}
} else {
Write-Host "`nWarning: No .st files found in import directory" -ForegroundColor Yellow
}
Write-Host "" # blank line
$pythonScript = Join-Path $scriptDir "scripts\codesys_import_external.py"
if (-not (Test-Path $pythonScript)) {
Write-Host "Error: Import script not found: $pythonScript" -ForegroundColor Red
exit 1
}
# Build arguments
$args = @($ProjectPath, $ImportDir)
if (-not [string]::IsNullOrEmpty($CodesysPath)) {
$args += "--codesys-path"
$args += $CodesysPath
}
$args += "--profile"
$args += $Profile
if ($DryRun) {
$args += "--dry-run"
}
& python $pythonScript @args
if ($LASTEXITCODE -eq 0) {
if ($DryRun) {
Write-Host "`nDry run completed. No changes were made." -ForegroundColor Yellow
Write-Host "Run without -DryRun to apply changes." -ForegroundColor Gray
} else {
Write-Host "`nImport completed successfully!" -ForegroundColor Green
}
} else {
Write-Host "`nImport failed with exit code: $LASTEXITCODE" -ForegroundColor Red
exit $LASTEXITCODE
}