forked from ynput/ayon-unreal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.ps1
281 lines (243 loc) · 9.24 KB
/
manage.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
<#
.SYNOPSIS
Helper script to run various tasks on ayon-unreal addon repository.
.DESCRIPTION
This script is used to run various tasks on ayon-unreal addon repository.
It uses Poetry to manage virtual environment and dependencies.
.EXAMPLE
PS> .\tools\manage.ps1
.EXAMPLE
To create virtual environment using Poetry:
PS> .\tools\manage.ps1 create-env
.EXAMPLE
To run Ruff check:
PS> .\tools\manage.ps1 ruff-check
.LINK
https://github.com/ynput/ayon-core
#>
# Settings and gitmodule init
$CurrentDir = Get-Location
$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$RepoRoot = (Get-Item $ScriptDir).parent.FullName
& git submodule update --init --recursive
$env:PSModulePath = $env:PSModulePath + ";$($openpype_root)\tools\modules\powershell"
$FunctionName=$ARGS[0]
$Arguments=@()
if ($ARGS.Length -gt 1) {
$Arguments = $ARGS[1..($ARGS.Length - 1)]
}
function Exit-WithCode($exitcode) {
# Only exit this host process if it's a child of another PowerShell parent process...
$parentPID = (Get-CimInstance -ClassName Win32_Process -Filter "ProcessId=$PID" | Select-Object -Property ParentProcessId).ParentProcessId
$parentProcName = (Get-CimInstance -ClassName Win32_Process -Filter "ProcessId=$parentPID" | Select-Object -Property Name).Name
if ('powershell.exe' -eq $parentProcName) { $host.SetShouldExit($exitcode) }
exit $exitcode
}
function Test-CommandExists {
param (
[Parameter(Mandatory=$true)]
[string]$command
)
$commandExists = $null -ne (Get-Command $command -ErrorAction SilentlyContinue)
return $commandExists
}
function Write-Info {
<#
.SYNOPSIS
Write-Info function to write information messages.
It uses Write-Color if that is available, otherwise falls back to Write-Host.
#>
[CmdletBinding()]
param (
[alias ('T')] [String[]]$Text,
[alias ('C', 'ForegroundColor', 'FGC')] [ConsoleColor[]]$Color = [ConsoleColor]::White,
[alias ('B', 'BGC')] [ConsoleColor[]]$BackGroundColor = $null,
[alias ('Indent')][int] $StartTab = 0,
[int] $LinesBefore = 0,
[int] $LinesAfter = 0,
[int] $StartSpaces = 0,
[alias ('L')] [string] $LogFile = '',
[Alias('DateFormat', 'TimeFormat')][string] $DateTimeFormat = 'yyyy-MM-dd HH:mm:ss',
[alias ('LogTimeStamp')][bool] $LogTime = $true,
[int] $LogRetry = 2,
[ValidateSet('unknown', 'string', 'unicode', 'bigendianunicode', 'utf8', 'utf7', 'utf32', 'ascii', 'default', 'oem')][string]$Encoding = 'Unicode',
[switch] $ShowTime,
[switch] $NoNewLine
)
if (Test-CommandExists "Write-Color") {
Write-Color -Text $Text -Color $Color -BackGroundColor $BackGroundColor -StartTab $StartTab -LinesBefore $LinesBefore -LinesAfter $LinesAfter -StartSpaces $StartSpaces -LogFile $LogFile -DateTimeFormat $DateTimeFormat -LogTime $LogTime -LogRetry $LogRetry -Encoding $Encoding -ShowTime $ShowTime -NoNewLine $NoNewLine
} else {
$message = $Text -join ' '
if ($NoNewLine)
{
Write-Host $message -NoNewline
}
else
{
Write-Host $message
}
}
}
$art = @"
▄██▄
▄███▄ ▀██▄ ▀██▀ ▄██▀ ▄██▀▀▀██▄ ▀███▄ █▄
▄▄ ▀██▄ ▀██▄ ▄██▀ ██▀ ▀██▄ ▄ ▀██▄ ███
▄██▀ ██▄ ▀ ▄▄ ▀ ██ ▄██ ███ ▀██▄ ███
▄██▀ ▀██▄ ██ ▀██▄ ▄██▀ ███ ▀██ ▀█▀
▄██▀ ▀██▄ ▀█ ▀██▄▄▄▄██▀ █▀ ▀██▄
· · - =[ by YNPUT ]:[ http://ayon.ynput.io ]= - · ·
"@
function Write-AsciiArt() {
Write-Host $art -ForegroundColor DarkGreen
}
function Show-PSWarning() {
if ($PSVersionTable.PSVersion.Major -lt 7) {
Write-Info -Text "!!! ", "You are using old version of PowerShell - ", "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" -Color Red, Yellow, White
Write-Info -Text " Please update to at least 7.0 - ", "https://github.com/PowerShell/PowerShell/releases" -Color Yellow, White
Exit-WithCode 1
}
}
function Install-Poetry() {
Write-Info -Text ">>> ", "Installing Poetry ... " -Color Green, Gray
$python = "python"
if (Get-Command "pyenv" -ErrorAction SilentlyContinue) {
if (-not (Test-Path -PathType Leaf -Path "$($RepoRoot)\.python-version")) {
$result = & pyenv global
if ($result -eq "no global version configured") {
Write-Info "!!! Using pyenv but having no local or global version of Python set." -Color Red, Yellow
Exit-WithCode 1
}
}
$python = & pyenv which python
}
$env:POETRY_HOME="$RepoRoot\.poetry"
(Invoke-WebRequest -Uri https://install.python-poetry.org/ -UseBasicParsing).Content | & $($python) -
}
function Set-Cwd() {
Set-Location -Path $RepoRoot
}
function Restore-Cwd() {
$tmp_current_dir = Get-Location
if ("$tmp_current_dir" -ne "$CurrentDir") {
Write-Info -Text ">>> ", "Restoring current directory" -Color Green, Gray
Set-Location -Path $CurrentDir
}
}
function Initialize-Environment {
Write-Info -Text ">>> ", "Reading Poetry ... " -Color Green, Gray -NoNewline
if (-not(Test-Path -PathType Container -Path "$( $env:POETRY_HOME )\bin"))
{
Write-Info -Text "NOT FOUND" -Color Yellow
Install-Poetry
Write-Info -Text "INSTALLED" -Color Cyan
}
else
{
Write-Info -Text "OK" -Color Green
}
if (-not(Test-Path -PathType Leaf -Path "$( $repo_root )\poetry.lock"))
{
Write-Info -Text ">>> ", "Installing virtual environment and creating lock." -Color Green, Gray
}
else
{
Write-Info -Text ">>> ", "Installing virtual environment from lock." -Color Green, Gray
}
$startTime = [int][double]::Parse((Get-Date -UFormat %s))
& "$env:POETRY_HOME\bin\poetry" config virtualenvs.in-project true --local
& "$env:POETRY_HOME\bin\poetry" config virtualenvs.create true --local
& "$env:POETRY_HOME\bin\poetry" install --no-root $poetry_verbosity --ansi
if ($LASTEXITCODE -ne 0)
{
Write-Info -Text "!!! ", "Poetry command failed." -Color Red, Yellow
Restore-Cwd
Exit-WithCode 1
}
if (Test-Path -PathType Container -Path "$( $repo_root )\.git")
{
Write-Info -Text ">>> ", "Installing pre-commit hooks ..." -Color Green, White
& "$env:POETRY_HOME\bin\poetry" run pre-commit install
if ($LASTEXITCODE -ne 0)
{
Write-Info -Text "!!! ", "Installation of pre-commit hooks failed." -Color Red, Yellow
}
}
$endTime = [int][double]::Parse((Get-Date -UFormat %s))
Restore-Cwd
try
{
if (Test-CommandExists "New-BurntToastNotification")
{
$app_logo = "$repo_root\tools\icons\ayon.ico"
New-BurntToastNotification -AppLogo "$app_logo" -Text "AYON", "Virtual environment created.", "All done in $( $endTime - $startTime ) secs."
}
}
catch {}
Write-Info -Text ">>> ", "Virtual environment created." -Color Green, White
}
function Invoke-Ruff {
param (
[switch] $Fix
)
$Poetry = "$RepoRoot\.poetry\bin\poetry.exe"
$RuffArgs = @( "run", "ruff", "check" )
if ($Fix) {
$RuffArgs += "--fix"
}
& $Poetry $RuffArgs
}
function Invoke-Codespell {
param (
[switch] $Fix
)
$Poetry = "$RepoRoot\.poetry\bin\poetry.exe"
$CodespellArgs = @( "run", "codespell" )
if ($Fix) {
$CodespellArgs += "--fix"
}
& $Poetry $CodespellArgs
}
function Write-Help {
<#
.SYNOPSIS
Write-Help function to write help messages.
#>
Write-Host ""
Write-Host "AYON Addon management script"
Write-Host ""
Write-Info -Text "Usage: ", "./manage.ps1 ", "[command]" -Color Gray, White, Cyan
Write-Host ""
Write-Host "Commands:"
Write-Info -Text " create-env ", "Install Poetry and update venv by lock file" -Color White, Cyan
Write-Info -Text " ruff-check ", "Run Ruff check for the repository" -Color White, Cyan
Write-Info -Text " ruff-fix ", "Run Ruff fix for the repository" -Color White, Cyan
Write-Info -Text " codespell ", "Run codespell check for the repository" -Color White, Cyan
Write-Host ""
}
function Resolve-Function {
if ($null -eq $FunctionName) {
Write-Help
return
}
$FunctionName = $FunctionName.ToLower() -replace "\W"
if ($FunctionName -eq "createenv") {
Set-Cwd
Initialize-Environment
} elseif ($FunctionName -eq "ruffcheck") {
Set-Cwd
Invoke-Ruff
} elseif ($FunctionName -eq "rufffix") {
Set-Cwd
Invoke-Ruff -Fix
} elseif ($FunctionName -eq "codespell") {
Set-Cwd
Invoke-CodeSpell
} else {
Write-Host "Unknown function ""$FunctionName"""
Write-Help
}
}
# -----------------------------------------------------
Show-PSWarning
Write-AsciiArt
Resolve-Function