From 9b952329a3ffba2d6b764f1471e407c981ad04aa Mon Sep 17 00:00:00 2001 From: Leo Mozoloa Date: Fri, 30 Dec 2022 23:27:53 +0100 Subject: [PATCH] More fixes and updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Commenting functions better and general code cleanup • New logging improvements • Checking for Git better and adding it to PATH if not present --- .../A1111's Web UI Autoinstaller.aip | 11 +- Launcher/Functions.psm1 | 177 ++++-------------- Launcher/LauncherUI.ps1 | 9 +- Launcher/logger.psm1 | 10 +- 4 files changed, 63 insertions(+), 144 deletions(-) diff --git a/Advanced Installer/A1111's Web UI Autoinstaller.aip b/Advanced Installer/A1111's Web UI Autoinstaller.aip index 2cc5381..2a358ab 100644 --- a/Advanced Installer/A1111's Web UI Autoinstaller.aip +++ b/Advanced Installer/A1111's Web UI Autoinstaller.aip @@ -24,10 +24,10 @@ - + - + @@ -51,9 +51,10 @@ + - + @@ -61,6 +62,7 @@ + @@ -72,6 +74,8 @@ + + @@ -356,6 +360,7 @@ + diff --git a/Launcher/Functions.psm1 b/Launcher/Functions.psm1 index 605027a..e3e8bf4 100644 --- a/Launcher/Functions.psm1 +++ b/Launcher/Functions.psm1 @@ -1,7 +1,9 @@ . "$PSScriptRoot\shared.ps1" Import-Module "$PSScriptRoot\logger.psm1" -Force -Global -Prefix "logger." -# General Utilities +#----------------------------- +#Startup fonctions +#----------------------------- function Search-RegForPyPath { $pyCore = Get-ItemProperty -path "hkcu:\Software\Python\PythonCore\3.10\InstallPath" -ErrorAction SilentlyContinue if ($pyCore) { @@ -28,26 +30,37 @@ function Install-py { Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exe" -OutFile "$tempFolder\python.exe" logger.action "Download successful, installing" ."$tempFolder\python.exe" /quiet InstallAllUsers=0 PrependPath=1 - logger.info "Done" + logger.success return } } function Install-git { - if (!(Test-Path "$env:ProgramFiles\Git")) { + if (!(Test-Path "$gitPath\bin\git.exe")) { logger.action "Git not found, downloading, please be patient" Invoke-WebRequest -Uri "https://github.com/git-for-windows/git/releases/download/v2.38.1.windows.1/Git-2.38.1-64-bit.exe" -OutFile "$tempFolder\git.exe" logger.action "Download successful, installing" ."$tempFolder\git.exe" /VERYSILENT /NORESTART - logger.info "Done" + logger.success + } + else { + logger.info "Git found at $("$env:ProgramFiles\Git")" + } + if (!(Get-Command git -ErrorAction SilentlyContinue)) { + logger.action "Git not found in PATH, adding it" + $env:Path += ";$gitPath\bin" + logger.success return } - logger.info "Git found at $("$env:ProgramFiles\Git")" + else { + logger.info "Git is in PATH" + } + } function Install-WebUI { if (!(Test-Path $webuiPath)) { logger.action "Automatic1111 SD WebUI was not found, cloning git" git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui $webuiPath - logger.info "Done" + logger.success return } logger.info "Automatic1111 SD WebUI found at $webuiPath" @@ -60,7 +73,7 @@ function Import-BaseModel { logger.action "Downloading Base Model, this can take a while" $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile("https://anga.tv/ems/model.ckpt", "$modelsPath\SD15NewVAEpruned.ckpt") - logger.info "Done" + logger.success } } } @@ -106,6 +119,9 @@ function Get-WebUICommitHash { $hash = Get-Content $hashPath if ($hash) { return $hash } } +#----------------------------- +#Settings related functions +#----------------------------- function Write-Settings($settings) { logger.action "Updating Settings File" $settings | ConvertTo-Json -Depth 100 | Out-File $settingsPath @@ -204,7 +220,6 @@ function Convert-SettingsToArguments ($settings) { } return $string } - function Convert-BatToGitOptions ($batFile) { $GitOptions = @( @{ @@ -230,26 +245,16 @@ function Convert-BatToGitOptions ($batFile) { return $GitOptions } -function Format-Config($config) { - $config2 = @() - foreach ($param in $config) { - $object = @{ - arg = $param.arg - value = $param.value - } - $config2 += $object - } - return $config2 -} - +#----------------------------- +#UI related functions +#----------------------------- function Select-Folder ([string]$InitialDirectory) { $app = New-Object -ComObject Shell.Application $folder = $app.BrowseForFolder(0, "Please select a folder", 0, "") if ($folder) { return $folder.Self.Path } else { return '' } } - -function Select-File([string]$InitialDirectory) { +function Select-File([string]$InitialDirectory) { $dialog = New-Object System.Windows.Forms.OpenFileDialog $dialog.InitialDirectory = $webuiPath $dialog.Title = "Please Select a File" @@ -259,28 +264,17 @@ function Select-File([string]$InitialDirectory) { return $dialog.FileName } -function Clear-Outputs { - $Exprompt = [system.windows.messagebox]::Show("All previously generated images will be deleted, are you sure ?`n`nClick 'No' to not delete any image this time`n`nUncheck 'Clear Generated Images' in the launcher to disable it", 'Warning', 'YesNo', 'Warning') - if ($Exprompt -eq "Yes") { - logger.action "Clearing all outputs in default output directories" - if ($outputsPath) { - Get-ChildItem $outputsPath -Force -Recurse -File -Filter *.png | Remove-Item -Force - Get-ChildItem $outputsPath -Force -Recurse -File -Filter *.jpg | Remove-Item -Force - } - logger.info "Done" - } -} - -#Updates +#----------------------------- +#General Settings functions +#----------------------------- function Update-WebUI ($enabled) { if ($enabled) { logger.action "Updating Webui" Set-Location $webuiPath git pull origin - logger.info "Done" + logger.success } } - function Update-Extensions ($enabled) { if ($enabled) { Set-Location $extPath @@ -291,111 +285,20 @@ function Update-Extensions ($enabled) { Set-Location $ext.Fullname git pull origin } - logger.info "Done" + logger.success return } logger.info "No extension found in the extensions folder" } } - -#Clean JSon function -function Format-Json { - <# - .SYNOPSIS - Prettifies JSON output. - .DESCRIPTION - Reformats a JSON string so the output looks better than what ConvertTo-Json outputs. - .PARAMETER Json - Required: [string] The JSON text to prettify. - .PARAMETER Minify - Optional: Returns the json string compressed. - .PARAMETER Indentation - Optional: The number of spaces (1..1024) to use for indentation. Defaults to 4. - .PARAMETER AsArray - Optional: If set, the output will be in the form of a string array, otherwise a single string is output. - .EXAMPLE - $json | ConvertTo-Json | Format-Json -Indentation 2 - #> - [CmdletBinding(DefaultParameterSetName = 'Prettify')] - Param( - [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] - [string]$Json, - - [Parameter(ParameterSetName = 'Minify')] - [switch]$Minify, - - [Parameter(ParameterSetName = 'Prettify')] - [ValidateRange(1, 1024)] - [int]$Indentation = 4, - - [Parameter(ParameterSetName = 'Prettify')] - [switch]$AsArray - ) - - if ($PSCmdlet.ParameterSetName -eq 'Minify') { - return ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100 -Compress - } - - # If the input JSON text has been created with ConvertTo-Json -Compress - # then we first need to reconvert it without compression - if ($Json -notmatch '\r?\n') { - $Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100 - } - - $indent = 0 - $regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)' - - $result = $Json -split '\r?\n' | - ForEach-Object { - # If the line contains a ] or } character, - # we need to decrement the indentation level unless it is inside quotes. - if ($_ -match "[}\]]$regexUnlessQuoted") { - $indent = [Math]::Max($indent - $Indentation, 0) - } - - # Replace all colon-space combinations by ": " unless it is inside quotes. - $line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ') - - # If the line contains a [ or { character, - # we need to increment the indentation level unless it is inside quotes. - if ($_ -match "[\{\[]$regexUnlessQuoted") { - $indent += $Indentation - } - - $line - } - - if ($AsArray) { return $result } - return $result -Join [Environment]::NewLine -} -#Converts Object to hashtable -function Convert-PSObjectToHashtable { - param ( - [Parameter(ValueFromPipeline)] - $InputObject - ) - - process { - if ($null -eq $InputObject) { return $null } - - if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) { - $collection = @( - foreach ($object in $InputObject) { Convert-PSObjectToHashtable $object } - ) - - Write-Output -NoEnumerate $collection - } - elseif ($InputObject -is [psobject]) { - $hash = @{} - - foreach ($property in $InputObject.PSObject.Properties) { - $hash[$property.Name] = Convert-PSObjectToHashtable $property.Value - } - - $hash - } - else { - $InputObject +function Clear-Outputs { + $Exprompt = [system.windows.messagebox]::Show("All previously generated images will be deleted, are you sure ?`n`nClick 'No' to not delete any image this time`n`nUncheck 'Clear Generated Images' in the launcher to disable it", 'Warning', 'YesNo', 'Warning') + if ($Exprompt -eq "Yes") { + logger.action "Clearing all outputs in default output directories" + if ($outputsPath) { + Get-ChildItem $outputsPath -Force -Recurse -File -Filter *.png | Remove-Item -Force + Get-ChildItem $outputsPath -Force -Recurse -File -Filter *.jpg | Remove-Item -Force } + logger.success } } \ No newline at end of file diff --git a/Launcher/LauncherUI.ps1 b/Launcher/LauncherUI.ps1 index 849af49..c0e908d 100644 --- a/Launcher/LauncherUI.ps1 +++ b/Launcher/LauncherUI.ps1 @@ -56,6 +56,10 @@ function Invoke-WebUI { # Parsing args from settings $arguments = Convert-SettingsToArguments $settings + logger.pop "WEBUI LAUNCHING VIA EMS LAUNCHER, EXIT THIS WINDOW TO STOP THE WEBUI" + logger.warn "If you see the line 'commit hash : XXXX' or 'Installing Torch...' bellow, any error happening afterwards is not related to the launcher, but the WebUI itself which EMS is not affiliated with, we won't be able to help you fix those, you'll have to ask them on Automatic1111's github, not ours." + logger.space + Set-Location $webuiPath $env:PYTHON = "`"$pyPath`"" @@ -67,9 +71,6 @@ function Invoke-WebUI { Start-Process "$webuiPath/webui.bat" -NoNewWindow Set-Location $PSScriptRoot $form.Close() - logger.pop "WEBUI LAUNCHING VIA EMS LAUNCHER, EXIT THIS WINDOW TO STOP THE WEBUI" - logger.warn "If you see the line 'commit hash : XXXX' or 'Installing Torch...' bellow, any error happening afterwards is not related to the launcher, but the WebUI itself which EMS is not affiliated with, we won't be able to help you fix those, you'll have to ask them on Automatic1111's github, not ours." - logger.space } Function Reset-Path($param) { @@ -390,5 +391,7 @@ function Makeform { $Form.ShowDialog() } +logger.space logger.action "Opening A1111 WebUI Launcher" +logger.space MakeForm \ No newline at end of file diff --git a/Launcher/logger.psm1 b/Launcher/logger.psm1 index 25251cf..4952607 100644 --- a/Launcher/logger.psm1 +++ b/Launcher/logger.psm1 @@ -16,5 +16,13 @@ function pop ($object) { space } function space { - Write-Host "------------------------------`n------------------------------`n------------------------------" -ForegroundColor "Cyan" + for ($i = 1; $i -le 60; $i++) { + # Print the character "X" + Write-Host -NoNewline ([char]126) -ForegroundColor "DarkGray" + } + Write-Host "" +} +function success { + Write-Host "[Success]" -ForegroundColor "Cyan" + space } \ No newline at end of file