-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgate.ps1
135 lines (99 loc) · 3.65 KB
/
gate.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
param(
[int]$mode=0,
[switch]$update,
[String[]]$arguments
)
$DebugPreference = 'Continue'
$ErrorActionPreference = "Stop"
# Some Dynamic Constants
$ScriptPath = (Get-Location).Path
$PythonPath = Join-Path -Path $ScriptPath -ChildPath "Python";
$executable = Join-Path -Path $PythonPath -ChildPath "python.exe";
$CoPo = Join-Path -Path $ScriptPath -ChildPath "CO_PO"
function Get-RunningProjects{
$Pythons = Join-Path -Path $PythonPath -ChildPath "*";
return Get-WmiObject -Class "Win32_Process" -ComputerName "." | where-object {$_.Path -like $Pythons};
}
function Get-ProjectPyPath{
param(
[string]$file
)
return '"' + (Join-Path -Path $CoPo -ChildPath "$file.pyc") + '" '
}
function Start-PythonScript{
param(
[String]$file="main",
[String[]]$arg_s=@()
)
$arg_s = (Get-ProjectPyPath -file $file) + $arg_s
Start-Process $executable -WindowStyle Maximized -WorkingDirectory $ScriptPath -ArgumentList ($arg_s -Join " ")
}
function Show-Failed {
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::Ok
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageBody = "It seems Matlab was not installed in your System! Please Install Matlab and add it to path"
$MessageTitle = "Matlab Engine Not Found"
[console]::beep(2000,500)
Start-Process "https://in.mathworks.com/matlabcentral/answers/94933-how-do-i-edit-my-system-path-in-windows"
[System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
Exit;
}
function Show-Matlab{
$MatLab = (Get-Command matlab.exe -ErrorAction SilentlyContinue);
return $(If ($MatLab) {$matlab.Path} Else {Show-Failed});
}
function Get-Update{
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/RahulARanger/CO-PO-Mapping/releases/latest" -Method "GET"
$update_it = $response.tag_name -ne $arguments[0]
if (-not $update_it){
return
}
Write-Output "Updating..."
$version = (& "./Python/python.exe" --version).SubString(7, 3);
foreach($asset in $response.assets){
$url = $asset.browser_download_url;
if($url.EndsWith("${version}.exe")){
$temp = Join-Path -Path $env:TEMP -ChildPath $asset.Name
write-Output $temp
Invoke-WebRequest -Uri $url -OutFile $temp
Start-Process -FilePath $temp -Wait
Remove-Item -Path $temp
break;
}
}
}
switch($mode){
# for running the application
0{
Show-Matlab;
Start-Process $executable -WindowStyle Maximized -WorkingDirectory $ScriptPath -ArgumentList ('"' + (Join-Path -Path $CoPo -ChildPath "main.py") + '" ')
}
# Checking if all the instances are closed
1{
$store = @(Get-RunningProjects);
if($store.length -gt 0){
$store | Out-GridView -passthru -Title "These processes must be closed in-order to proceed forward!"
exit 5
}
}
# Checking for the updates
2{
Write-Debug "Checking for the updates...";
Get-Update
}
3{
Start-Sleep -Seconds 6;
# Since this scripts are started by python, it's very likely that it will be executed by them
$store = @(Get-RunningProjects);
Write-Debug "Deleting Temp folder";
if($store.length -eq 0){
$temp = Join-Path -Path $CoPo -ChildPath "temp"
if(Test-Path -Path $temp){
Remove-Item -Recurse -Force -Path $temp;
}
}
}
}
$DebugPreference = 'SilentlyContinue'
$ErrorActionPreference = "Continue"