File size: 8,177 Bytes
dc91bc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#Requires -RunAsAdministrator

<#
.SYNOPSIS
    A dedicated update script for the ComfyUI installation.
.DESCRIPTION
    This script performs a 'git pull' on the main ComfyUI repository,
    all custom nodes, and the workflow repository. It then updates all
    Python dependencies from any found 'requirements.txt' files.
    It is designed to be run from a subfolder.
#>

#===========================================================================
# SECTION 1: SCRIPT CONFIGURATION & HELPER FUNCTIONS
#===========================================================================

# === CORRECTION : Le script s'exécute depuis un sous-dossier, on cible donc le dossier parent ===
$InstallPath = (Split-Path -Path $PSScriptRoot -Parent)

$comfyPath = Join-Path $InstallPath "ComfyUI"
$customNodesPath = Join-Path $InstallPath "custom_nodes"
$workflowPath = Join-Path $InstallPath "user\default\workflows\UmeAiRT-Workflow"
$venvPython = Join-Path $comfyPath "venv\Scripts\python.exe"
$logPath = Join-Path $InstallPath "logs"
$logFile = Join-Path $logPath "update_log.txt"

if (-not (Test-Path $logPath)) { New-Item -ItemType Directory -Force -Path $logPath | Out-Null }

function Write-Log {
    param([string]$Message, [string]$Color = "White")
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $formattedMessage = "[$timestamp] $Message"
    Write-Host $Message -ForegroundColor $Color
    Add-Content -Path $logFile -Value $formattedMessage
}

function Invoke-Git-Pull {
    param([string]$DirectoryPath)
    if (Test-Path (Join-Path $DirectoryPath ".git")) {
        # On utilise cmd.exe pour lancer git pull et rediriger la sortie vers le log
        $commandToRun = "git -C `"$DirectoryPath`" pull"
        $cmdArguments = "/C `"$commandToRun >> `"`"$logFile`"`" 2>&1`""
        try {
            Start-Process -FilePath "cmd.exe" -ArgumentList $cmdArguments -Wait -WindowStyle Hidden
        } catch {
            Write-Log "  - FAILED to run git pull in '$DirectoryPath'" -Color Red
        }
    } else {
        Write-Log "  - Skipping: Not a git repository." -Color Gray
    }
}

function Invoke-Pip-Install {
    param([string]$RequirementsPath)
    if (Test-Path $RequirementsPath) {
        Write-Log "  - Found requirements: $RequirementsPath. Updating..." -Color Cyan
        $tempLogFile = Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString() + ".tmp")
        try {
            $commandToRun = "`"$venvPython`" -m pip install -r `"$RequirementsPath`""
            $cmdArguments = "/C `"$commandToRun > `"`"$tempLogFile`"`" 2>&1`""
            Start-Process -FilePath "cmd.exe" -ArgumentList $cmdArguments -Wait -WindowStyle Hidden
            if (Test-Path $tempLogFile) { $output = Get-Content $tempLogFile; if($output){ Add-Content -Path $logFile -Value $output } }
        } finally {
            if (Test-Path $tempLogFile) { Remove-Item $tempLogFile }
        }
    }
}
function Invoke-AndLog {
    param(
        [string]$File,
        [string]$Arguments
    )
    
    # Chemin vers un fichier de log temporaire unique
    $tempLogFile = Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString() + ".tmp")

    try {
        # Exécute la commande et redirige TOUTE sa sortie vers le fichier temporaire
        $commandToRun = "`"$File`" $Arguments"
        $cmdArguments = "/C `"$commandToRun > `"`"$tempLogFile`"`" 2>&1`""
        Start-Process -FilePath "cmd.exe" -ArgumentList $cmdArguments -Wait -WindowStyle Hidden
        
        # Une fois la commande terminée, on lit le fichier temporaire
        if (Test-Path $tempLogFile) {
            $output = Get-Content $tempLogFile
            # Et on l'ajoute au log principal en toute sécurité
            Add-Content -Path $logFile -Value $output
        }
    } catch {
        Write-Log "FATAL ERROR trying to execute command: $commandToRun" -Color Red
    } finally {
        # On s'assure que le fichier temporaire est toujours supprimé
        if (Test-Path $tempLogFile) {
            Remove-Item $tempLogFile
        }
    }
}
#===========================================================================
# SECTION 2: UPDATE PROCESS
#===========================================================================
Clear-Host
Write-Log "==============================================================================="
Write-Log "             Starting UmeAiRT ComfyUI Update Process" -Color Yellow
Write-Log "==============================================================================="

# --- 1. Update ComfyUI Core ---
Write-Log "`n[1/4] Updating ComfyUI Core..." -Color Green
Invoke-Git-Pull -DirectoryPath $comfyPath

# --- 2. Update and Install Custom Nodes ---
Write-Log "`n[2/4] Updating and Installing Custom Nodes..." -Color Green

$csvUrl = "https://github.com/UmeAiRT/ComfyUI-Auto_installer/raw/refs/heads/main/csv/custom_nodes.csv"
$scriptsFolder = Join-Path $InstallPath "scripts"
$csvPath = Join-Path $scriptsFolder "custom_nodes.csv"

# Télécharge la dernière liste de custom nodes
try {
    Invoke-WebRequest -Uri $csvUrl -OutFile $csvPath
} catch {
    Write-Log "  - ERREUR: Impossible de télécharger la liste des custom nodes. Mise à jour des nodes ignorée." -Color Red
    return
}

$customNodesList = Import-Csv -Path $csvPath
$existingNodeDirs = Get-ChildItem -Path $customNodesPath -Directory

# D'abord, on met à jour les nodes existants
Write-Log "  - Updating existing nodes..."
foreach ($dir in $existingNodeDirs) {
    Write-Log "    - Checking node: $($dir.Name)"
    Invoke-Git-Pull -DirectoryPath $dir.FullName
}

# Ensuite, on vérifie s'il y a de nouveaux nodes à installer
Write-Log "  - Checking for new nodes to install..."
foreach ($node in $customNodesList) {
    $nodeName = $node.Name
    $nodePath = if ($node.Subfolder) { Join-Path $customNodesPath $node.Subfolder } else { Join-Path $customNodesPath $nodeName }

    if (-not (Test-Path $nodePath)) {
        Write-Log "    - New node found: $nodeName. Installing..." -Color Yellow
        
        $repoUrl = $node.RepoUrl
        $cloneTargetPath = if ($node.Subfolder) { (Split-Path $nodePath -Parent) } else { $nodePath }
        if ($nodeName -eq 'ComfyUI-Impact-Subpack') { $clonePath = Join-Path $cloneTargetPath "impact_subpack" } else { $clonePath = $cloneTargetPath }
        
        $tempLogFile = Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString() + ".tmp")
        try {
            $commandToRun = "git clone $repoUrl `"$clonePath`""
            $cmdArguments = "/C `"$commandToRun > `"`"$tempLogFile`"`" 2>&1`""
            Start-Process -FilePath "cmd.exe" -ArgumentList $cmdArguments -Wait -WindowStyle Hidden
            if (Test-Path $tempLogFile) { Add-Content -Path $logFile -Value (Get-Content $tempLogFile) }
        } finally {
            if (Test-Path $tempLogFile) { Remove-Item $tempLogFile }
        }
    }
}


# --- 3. Update Workflows ---
Write-Log "`n[3/4] Updating UmeAiRT Workflows..." -Color Green
if (Test-Path $workflowPath) {
    Invoke-Git-Pull -DirectoryPath $workflowPath
} else {
    Write-Log "  - Workflow directory not found, skipping." -Color Gray
}

# --- 4. Update Python Dependencies ---
Write-Log "`n[4/4] Updating all Python dependencies..." -Color Green
Write-Log "  - Checking main ComfyUI requirements..."
Invoke-Pip-Install -RequirementsPath (Join-Path $comfyPath "requirements.txt")

Write-Log "  - Checking custom node requirements..."
if (Test-Path $customNodesPath) {
    foreach ($dir in (Get-ChildItem -Path $customNodesPath -Directory)) {
        Invoke-Pip-Install -RequirementsPath (Join-Path $dir.FullName "requirements.txt")
    }
}
Write-Log "  - Fixing Numpy..."
Invoke-AndLog "$venvPython" @('-m', 'pip', 'uninstall', 'numpy', 'pandas', '--yes')
Invoke-AndLog "$venvPython" "-m pip install numpy==1.26.4 pandas"

Write-Log "  - Fixing transformers..."
Invoke-AndLog "$venvPython" "-m pip install --upgrade transformers==4.49.0"

Write-Log "==============================================================================="
Write-Log "Update process complete!" -Color Yellow
Write-Log "==============================================================================="
Read-Host "Press Enter to exit."