-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomfyui_installer.ps1
6455 lines (5675 loc) · 293 KB
/
comfyui_installer.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
param (
[string]$InstallPath = "$PSScriptRoot/ComfyUI",
[switch]$UseUpdateMode,
[switch]$DisablePipMirror,
[switch]$DisableProxy,
[string]$UseCustomProxy,
[switch]$DisableUV,
[switch]$DisableGithubMirror,
[string]$UseCustomGithubMirror,
[switch]$Help
)
# 有关 PowerShell 脚本保存编码的问题: https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.4#the-byte-order-mark
# ComfyUI Installer 版本和检查更新间隔
$COMFYUI_INSTALLER_VERSION = 185
$UPDATE_TIME_SPAN = 3600
# Pip 镜像源
$PIP_INDEX_ADDR = "https://mirrors.cloud.tencent.com/pypi/simple"
$PIP_INDEX_ADDR_ORI = "https://pypi.python.org/simple"
$PIP_EXTRA_INDEX_ADDR = "https://mirrors.cernet.edu.cn/pypi/web/simple"
$PIP_EXTRA_INDEX_ADDR_ORI = "https://download.pytorch.org/whl"
$PIP_FIND_ADDR = "https://mirror.sjtu.edu.cn/pytorch-wheels/torch_stable.html"
$PIP_FIND_ADDR_ORI = "https://download.pytorch.org/whl/torch_stable.html"
$USE_PIP_MIRROR = if ((!(Test-Path "$PSScriptRoot/disable_pip_mirror.txt")) -and (!($DisablePipMirror))) { $true } else { $false }
$PIP_INDEX_MIRROR = if ($USE_PIP_MIRROR) { $PIP_INDEX_ADDR } else { $PIP_INDEX_ADDR_ORI }
$PIP_EXTRA_INDEX_MIRROR = if ($USE_PIP_MIRROR) { $PIP_EXTRA_INDEX_ADDR } else { $PIP_EXTRA_INDEX_ADDR_ORI }
$PIP_FIND_MIRROR = if ($USE_PIP_MIRROR) { $PIP_FIND_ADDR } else { $PIP_FIND_ADDR_ORI }
# $PIP_FIND_MIRROR_CU121 = "https://download.pytorch.org/whl/cu121/torch_stable.html"
$PIP_EXTRA_INDEX_MIRROR_PYTORCH = "https://download.pytorch.org/whl"
$PIP_EXTRA_INDEX_MIRROR_CU121 = "https://download.pytorch.org/whl/cu121"
$PIP_EXTRA_INDEX_MIRROR_CU124 = "https://download.pytorch.org/whl/cu124"
# Github 镜像源列表
$GITHUB_MIRROR_LIST = @(
"https://ghfast.top/https://github.com",
"https://mirror.ghproxy.com/https://github.com",
"https://ghproxy.net/https://github.com",
"https://gh.api.99988866.xyz/https://github.com",
"https://gitclone.com/github.com",
"https://gh-proxy.com/https://github.com",
"https://ghps.cc/https://github.com",
"https://gh.idayer.com/https://github.com"
)
# PyTorch 版本
$PYTORCH_VER = "torch==2.3.0+cu118 torchvision==0.18.0+cu118 torchaudio==2.3.0+cu118"
$XFORMERS_VER = "xformers===0.0.26.post1+cu118"
# uv 最低版本
$UV_MINIMUM_VER = "0.5.22"
# ComfyUI 仓库地址
$COMFYUI_REPO = "https://github.com/comfyanonymous/ComfyUI"
# PATH
$PYTHON_PATH = "$InstallPath/python"
$PYTHON_EXTRA_PATH = "$InstallPath/ComfyUI/python"
$PYTHON_SCRIPTS_PATH = "$InstallPath/python/Scripts"
$PYTHON_SCRIPTS_EXTRA_PATH = "$InstallPath/ComfyUI/python/Scripts"
$GIT_PATH = "$InstallPath/git/bin"
$GIT_EXTRA_PATH = "$InstallPath/ComfyUI/git/bin"
$Env:PATH = "$PYTHON_EXTRA_PATH$([System.IO.Path]::PathSeparator)$PYTHON_SCRIPTS_EXTRA_PATH$([System.IO.Path]::PathSeparator)$GIT_EXTRA_PATH$([System.IO.Path]::PathSeparator)$PYTHON_PATH$([System.IO.Path]::PathSeparator)$PYTHON_SCRIPTS_PATH$([System.IO.Path]::PathSeparator)$GIT_PATH$([System.IO.Path]::PathSeparator)$Env:PATH"
# 环境变量
$Env:PIP_INDEX_URL = $PIP_INDEX_MIRROR
$Env:PIP_EXTRA_INDEX_URL = $PIP_EXTRA_INDEX_MIRROR
$Env:PIP_FIND_LINKS = $PIP_FIND_MIRROR
$Env:UV_INDEX_URL = $PIP_INDEX_MIRROR
# $Env:UV_EXTRA_INDEX_URL = $PIP_EXTRA_INDEX_MIRROR
$Env:UV_FIND_LINKS = $PIP_FIND_MIRROR
$Env:UV_LINK_MODE = "copy"
$Env:UV_HTTP_TIMEOUT = 30
$Env:UV_CONCURRENT_DOWNLOADS = 50
$Env:PIP_DISABLE_PIP_VERSION_CHECK = 1
$Env:PIP_NO_WARN_SCRIPT_LOCATION = 0
$Env:PIP_TIMEOUT = 30
$Env:PIP_RETRIES = 5
$Env:PYTHONUTF8 = 1
$Env:PYTHONIOENCODING = "utf8"
$Env:CACHE_HOME = "$InstallPath/cache"
$Env:HF_HOME = "$InstallPath/cache/huggingface"
$Env:MATPLOTLIBRC = "$InstallPath/cache"
$Env:MODELSCOPE_CACHE = "$InstallPath/cache/modelscope/hub"
$Env:MS_CACHE_HOME = "$InstallPath/cache/modelscope/hub"
$Env:SYCL_CACHE_DIR = "$InstallPath/cache/libsycl_cache"
$Env:TORCH_HOME = "$InstallPath/cache/torch"
$Env:U2NET_HOME = "$InstallPath/cache/u2net"
$Env:XDG_CACHE_HOME = "$InstallPath/cache"
$Env:PIP_CACHE_DIR = "$InstallPath/cache/pip"
$Env:PYTHONPYCACHEPREFIX = "$InstallPath/cache/pycache"
$Env:UV_CACHE_DIR = "$InstallPath/cache/uv"
$Env:UV_PYTHON = "$InstallPath/python/python.exe"
# 消息输出
function Print-Msg ($msg) {
Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -ForegroundColor Yellow -NoNewline
Write-Host "[ComfyUI Installer]" -ForegroundColor Cyan -NoNewline
Write-Host ":: " -ForegroundColor Blue -NoNewline
Write-Host "$msg"
}
# 显示 ComfyUI Installer 版本
function Get-ComfyUI-Installer-Version {
$ver = $([string]$COMFYUI_INSTALLER_VERSION).ToCharArray()
$major = ($ver[0..($ver.Length - 3)])
$minor = $ver[-2]
$micro = $ver[-1]
Print-Msg "ComfyUI Installer 版本: v${major}.${minor}.${micro}"
}
# Pip 镜像源状态
function Pip-Mirror-Status {
if ($USE_PIP_MIRROR) {
Print-Msg "使用 Pip 镜像源"
} else {
Print-Msg "检测到 disable_pip_mirror.txt 配置文件 / 命令行参数 -DisablePipMirror, 已将 Pip 源切换至官方源"
}
}
# 代理配置
function Set-Proxy {
$Env:NO_PROXY = "localhost,127.0.0.1,::1"
# 检测是否禁用自动设置镜像源
if ((Test-Path "$PSScriptRoot/disable_proxy.txt") -or ($DisableProxy)) {
Print-Msg "检测到本地存在 disable_proxy.txt 代理配置文件 / 命令行参数 -DisableProxy, 禁用自动设置代理"
return
}
$internet_setting = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if ((Test-Path "$PSScriptRoot/proxy.txt") -or ($UseCustomProxy -ne "")) { # 本地存在代理配置
if ($UseCustomProxy -ne "") {
$proxy_value = $UseCustomProxy
} else {
$proxy_value = Get-Content "$PSScriptRoot/proxy.txt"
}
$Env:HTTP_PROXY = $proxy_value
$Env:HTTPS_PROXY = $proxy_value
Print-Msg "检测到本地存在 proxy.txt 代理配置文件 / 命令行参数 -UseCustomProxy, 已读取代理配置文件并设置代理"
} elseif ($internet_setting.ProxyEnable -eq 1) { # 系统已设置代理
$proxy_addr = $($internet_setting.ProxyServer)
# 提取代理地址
if (($proxy_addr -match "http=(.*?);") -or ($proxy_addr -match "https=(.*?);")) {
$proxy_value = $matches[1]
# 去除 http / https 前缀
$proxy_value = $proxy_value.ToString().Replace("http://", "").Replace("https://", "")
$proxy_value = "http://${proxy_value}"
} elseif ($proxy_addr -match "socks=(.*)") {
$proxy_value = $matches[1]
# 去除 socks 前缀
$proxy_value = $proxy_value.ToString().Replace("http://", "").Replace("https://", "")
$proxy_value = "socks://${proxy_value}"
} else {
$proxy_value = "http://${proxy_addr}"
}
$Env:HTTP_PROXY = $proxy_value
$Env:HTTPS_PROXY = $proxy_value
Print-Msg "检测到系统设置了代理, 已读取系统中的代理配置并设置代理"
}
}
# 设置 uv 的使用状态
function Set-uv {
if ((Test-Path "$PSScriptRoot/disable_uv.txt") -or ($DisableUV)) {
Print-Msg "检测到 disable_uv.txt 配置文件 / 命令行参数 -DisableUV, 已禁用 uv, 使用 Pip 作为 Python 包管理器"
$Global:USE_UV = $false
} else {
Print-Msg "默认启用 uv 作为 Python 包管理器, 加快 Python 软件包的安装速度"
Print-Msg "当 uv 安装 Python 软件包失败时, 将自动切换成 Pip 重试 Python 软件包的安装"
$Global:USE_UV = $true
}
}
# 检查 uv 是否需要更新
function Check-uv-Version {
$content = "
import re
from importlib.metadata import version
def compare_versions(version1, version2) -> int:
try:
nums1 = re.sub(r'[a-zA-Z]+', '', version1).replace('-', '.').replace('+', '.').split('.')
nums2 = re.sub(r'[a-zA-Z]+', '', version2).replace('-', '.').replace('+', '.').split('.')
except:
return 0
for i in range(max(len(nums1), len(nums2))):
num1 = int(nums1[i]) if i < len(nums1) else 0
num2 = int(nums2[i]) if i < len(nums2) else 0
if num1 == num2:
continue
elif num1 > num2:
return 1
else:
return -1
return 0
def is_uv_need_update() -> bool:
try:
uv_ver = version('uv')
except:
return True
if compare_versions(uv_ver, uv_minimum_ver) == -1:
return True
else:
return False
uv_minimum_ver = '$UV_MINIMUM_VER'
print(is_uv_need_update())
"
Print-Msg "检测 uv 是否需要更新"
$status = $(python -c "$content")
if ($status -eq "True") {
Print-Msg "更新 uv 中"
python -m pip install -U "uv>=$UV_MINIMUM_VER"
if ($?) {
Print-Msg "uv 更新成功"
} else {
Print-Msg "uv 更新失败, 可能会造成 uv 部分功能异常"
}
} else {
Print-Msg "uv 无需更新"
}
}
# 下载并解压 Python
function Install-Python {
$url = "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/python-3.10.15-amd64.zip"
$cache_path = "$Env:CACHE_HOME/python_tmp"
$path = "$InstallPath/python"
# 下载 Python
Print-Msg "正在下载 Python"
Invoke-WebRequest -Uri $url -OutFile "$Env:CACHE_HOME/python-3.10.15-amd64.zip"
if ($?) { # 检测是否下载成功并解压
if (Test-Path "$cache_path") {
Remove-Item -Path "$cache_path" -Force -Recurse
}
# 解压 Python
Print-Msg "正在解压 Python"
Expand-Archive -Path "$Env:CACHE_HOME/python-3.10.15-amd64.zip" -DestinationPath "$cache_path" -Force
# 清理空文件夹
if (Test-Path "$path") {
$random_string = [Guid]::NewGuid().ToString().Substring(0, 18)
Move-Item -Path "$path" -Destination "$Env:CACHE_HOME/$random_string" -Force
}
New-Item -ItemType Directory -Path "$([System.IO.Path]::GetDirectoryName($path))" -Force > $null
Move-Item -Path "$cache_path" -Destination "$path" -Force
Remove-Item -Path "$Env:CACHE_HOME/python-3.10.15-amd64.zip" -Force -Recurse
Print-Msg "Python 安装成功"
} else {
Print-Msg "Python 安装失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 下载并解压 Git
function Install-Git {
$url = "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/PortableGit.zip"
$cache_path = "$Env:CACHE_HOME/git_tmp"
$path = "$InstallPath/git"
Print-Msg "正在下载 Git"
Invoke-WebRequest -Uri $url -OutFile "$Env:CACHE_HOME/PortableGit.zip"
if ($?) { # 检测是否下载成功并解压
if (Test-Path "$cache_path") {
Remove-Item -Path "$cache_path" -Force -Recurse
}
# 解压 Git
Print-Msg "正在解压 Git"
Expand-Archive -Path "$Env:CACHE_HOME/PortableGit.zip" -DestinationPath "$cache_path" -Force
# 清理空文件夹
if (Test-Path "$path") {
$random_string = [Guid]::NewGuid().ToString().Substring(0, 18)
Move-Item -Path "$path" -Destination "$Env:CACHE_HOME/$random_string" -Force
}
New-Item -ItemType Directory -Path "$([System.IO.Path]::GetDirectoryName($path))" -Force > $null
Move-Item -Path "$cache_path" -Destination "$path" -Force
Remove-Item -Path "$Env:CACHE_HOME/PortableGit.zip" -Force -Recurse
Print-Msg "Git 安装成功"
} else {
Print-Msg "Git 安装失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 下载 Aria2
function Install-Aria2 {
$url = "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/aria2c.exe"
Print-Msg "正在下载 Aria2"
Invoke-WebRequest -Uri $url -OutFile "$Env:CACHE_HOME/aria2c.exe"
if ($?) {
Move-Item -Path "$Env:CACHE_HOME/aria2c.exe" -Destination "$InstallPath/git/bin/aria2c.exe" -Force
Print-Msg "Aria2 下载成功"
} else {
Print-Msg "Aria2 下载失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 下载 uv
function Install-uv {
Print-Msg "正在下载 uv"
python -m pip install uv
if ($?) {
Print-Msg "uv 下载成功"
} else {
Print-Msg "uv 下载失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# Github 镜像测试
function Test-Github-Mirror {
$Env:GIT_CONFIG_GLOBAL = "$InstallPath/.gitconfig" # 设置 Git 配置文件路径
if (Test-Path "$InstallPath/.gitconfig") {
Remove-Item -Path "$InstallPath/.gitconfig" -Force -Recurse
}
# 默认 Git 配置
git config --global --add safe.directory "*"
git config --global core.longpaths true
if ((Test-Path "$PSScriptRoot/disable_gh_mirror.txt") -or ($DisableGithubMirror)) { # 禁用 Github 镜像源
Print-Msg "检测到本地存在 disable_gh_mirror.txt Github 镜像源配置文件 / 命令行参数 -DisableGithubMirror, 禁用 Github 镜像源"
return
}
# 使用自定义 Github 镜像源
if ((Test-Path "$PSScriptRoot/gh_mirror.txt") -or ($UseCustomGithubMirror -ne "")) {
if ($UseCustomGithubMirror -ne "") {
$github_mirror = $UseCustomGithubMirror
} else {
$github_mirror = Get-Content "$PSScriptRoot/gh_mirror.txt"
}
git config --global url."$github_mirror".insteadOf "https://github.com"
Print-Msg "检测到本地存在 gh_mirror.txt Github 镜像源配置文件 / 命令行参数 -UseCustomGithubMirror, 已读取 Github 镜像源配置文件并设置 Github 镜像源"
return
}
# 自动检测可用镜像源并使用
$status = 0
ForEach($i in $GITHUB_MIRROR_LIST) {
Print-Msg "测试 Github 镜像源: $i"
if (Test-Path "$Env:CACHE_HOME/github-mirror-test") {
Remove-Item -Path "$Env:CACHE_HOME/github-mirror-test" -Force -Recurse
}
git clone "$i/licyk/empty" "$Env:CACHE_HOME/github-mirror-test" --quiet
if ($?) {
Print-Msg "该 Github 镜像源可用"
$github_mirror = $i
$status = 1
break
} else {
Print-Msg "镜像源不可用, 更换镜像源进行测试"
}
}
if (Test-Path "$Env:CACHE_HOME/github-mirror-test") {
Remove-Item -Path "$Env:CACHE_HOME/github-mirror-test" -Force -Recurse
}
if ($status -eq 0) {
Print-Msg "无可用 Github 镜像源, 取消使用 Github 镜像源"
} else {
Print-Msg "设置 Github 镜像源"
git config --global url."$github_mirror".insteadOf "https://github.com"
}
}
# Git 仓库下载
function Git-CLone {
param (
[String]$url,
[String]$path
)
$name = [System.IO.Path]::GetFileNameWithoutExtension("$url")
$folder_name = [System.IO.Path]::GetFileName("$path")
Print-Msg "检测 $name 是否已安装"
$status = 0
if (!(Test-Path "$path")) {
$status = 1
} else {
$items = Get-ChildItem "$path" -Recurse
if ($items.Count -eq 0) {
$status = 1
}
}
if ($status -eq 1) {
Print-Msg "正在下载 $name"
$cache_path = "$Env:CACHE_HOME/${folder_name}_tmp"
# 清理缓存路径
if (Test-Path "$cache_path") {
Remove-Item -Path "$cache_path" -Force -Recurse
}
git clone --recurse-submodules $url "$cache_path"
if ($?) { # 检测是否下载成功
# 清理空文件夹
if (Test-Path "$path") {
$random_string = [Guid]::NewGuid().ToString().Substring(0, 18)
Move-Item -Path "$path" -Destination "$Env:CACHE_HOME/$random_string" -Force
}
# 将下载好的文件从缓存文件夹移动到指定路径
New-Item -ItemType Directory -Path "$([System.IO.Path]::GetDirectoryName($path))" -Force > $null
Move-Item -Path "$cache_path" -Destination "$path" -Force
Print-Msg "$name 安装成功"
} else {
Print-Msg "$name 安装失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
} else {
Print-Msg "$name 已安装"
}
}
# 安装 PyTorch
function Install-PyTorch {
Print-Msg "检测是否需要安装 PyTorch"
python -m pip show torch --quiet 2> $null
if (!($?)) {
Print-Msg "安装 PyTorch 中"
if ($USE_UV) {
uv pip install $PYTORCH_VER.ToString().Split()
if (!($?)) {
Print-Msg "检测到 uv 安装 Python 软件包失败, 尝试回滚至 Pip 重试 Python 软件包安装"
python -m pip install $PYTORCH_VER.ToString().Split()
}
} else {
python -m pip install $PYTORCH_VER.ToString().Split()
}
if ($?) {
Print-Msg "PyTorch 安装成功"
} else {
Print-Msg "PyTorch 安装失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
} else {
Print-Msg "PyTorch 已安装, 无需再次安装"
}
Print-Msg "检测是否需要安装 xFormers"
python -m pip show xformers --quiet 2> $null
if (!($?)) {
Print-Msg "安装 xFormers 中"
if ($USE_UV) {
uv pip install $XFORMERS_VER --no-deps
if (!($?)) {
Print-Msg "检测到 uv 安装 Python 软件包失败, 尝试回滚至 Pip 重试 Python 软件包安装"
python -m pip install $XFORMERS_VER --no-deps
}
} else {
python -m pip install $XFORMERS_VER --no-deps
}
if ($?) {
Print-Msg "xFormers 安装成功"
} else {
Print-Msg "xFormers 安装失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
} else {
Print-Msg "xFormers 已安装, 无需再次安装"
}
}
# 安装 ComfyUI 依赖
function Install-ComfyUI-Dependence {
# 记录脚本所在路径
$current_path = $(Get-Location).ToString()
Set-Location "$InstallPath/ComfyUI"
Print-Msg "安装 ComfyUI 依赖中"
if ($USE_UV) {
uv pip install -r requirements.txt
if (!($?)) {
Print-Msg "检测到 uv 安装 Python 软件包失败, 尝试回滚至 Pip 重试 Python 软件包安装"
python -m pip install -r requirements.txt
}
} else {
python -m pip install -r requirements.txt
}
if ($?) {
Print-Msg "ComfyUI 依赖安装成功"
} else {
Print-Msg "ComfyUI 依赖安装失败, 终止 ComfyUI 安装进程, 可尝试重新运行 ComfyUI Installer 重试失败的安装"
Set-Location "$current_path"
Read-Host | Out-Null
exit 1
}
Set-Location "$current_path"
}
# 安装
function Check-Install {
New-Item -ItemType Directory -Path "$InstallPath" -Force > $null
New-Item -ItemType Directory -Path "$Env:CACHE_HOME" -Force > $null
Print-Msg "检测是否安装 Python"
if ((Test-Path "$InstallPath/python/python.exe") -or (Test-Path "$InstallPath/ComfyUI/python/python.exe")) {
Print-Msg "Python 已安装"
} else {
Print-Msg "Python 未安装"
Install-Python
}
# 切换 uv 指定的 Python
if (Test-Path "$InstallPath/ComfyUI/python/python.exe") {
$Env:UV_PYTHON = "$InstallPath/ComfyUI/python/python.exe"
}
Print-Msg "检测是否安装 Git"
if ((Test-Path "$InstallPath/git/bin/git.exe") -or (Test-Path "$InstallPath/ComfyUI/git/bin/git.exe")) {
Print-Msg "Git 已安装"
} else {
Print-Msg "Git 未安装"
Install-Git
}
Print-Msg "检测是否安装 Aria2"
if ((Test-Path "$InstallPath/git/bin/aria2c.exe") -or (Test-Path "$InstallPath/ComfyUI/git/bin/aria2c.exe")) {
Print-Msg "Aria2 已安装"
} else {
Print-Msg "Aria2 未安装"
Install-Aria2
}
Print-Msg "检测是否安装 uv"
python -m pip show uv --quiet 2> $null
if ($?) {
Print-Msg "uv 已安装"
} else {
Print-Msg "uv 未安装"
Install-uv
}
Check-uv-Version
Test-Github-Mirror
$comfyui_path = "$InstallPath/ComfyUI"
$custom_node_path = "$comfyui_path/custom_nodes"
Git-CLone "$COMFYUI_REPO" "$comfyui_path"
# ComfyUI 扩展
Git-CLone "https://github.com/ltdrdata/ComfyUI-Manager" "$custom_node_path/ComfyUI-Manager"
Git-CLone "https://github.com/Fannovel16/comfyui_controlnet_aux" "$custom_node_path/comfyui_controlnet_aux"
Git-CLone "https://github.com/Kosinkadink/ComfyUI-Advanced-ControlNet" "$custom_node_path/ComfyUI-Advanced-ControlNet"
Git-CLone "https://github.com/cubiq/ComfyUI_IPAdapter_plus" "$custom_node_path/ComfyUI_IPAdapter_plus"
Git-CLone "https://github.com/kijai/ComfyUI-Marigold" "$custom_node_path/ComfyUI-Marigold"
Git-CLone "https://github.com/pythongosssss/ComfyUI-WD14-Tagger" "$custom_node_path/ComfyUI-WD14-Tagger"
Git-CLone "https://github.com/BlenderNeko/ComfyUI_TiledKSampler" "$custom_node_path/ComfyUI_TiledKSampler"
Git-CLone "https://github.com/pythongosssss/ComfyUI-Custom-Scripts" "$custom_node_path/ComfyUI-Custom-Scripts"
Git-CLone "https://github.com/LEv145/images-grid-comfy-plugin" "$custom_node_path/images-grid-comfy-plugin"
Git-CLone "https://github.com/ssitu/ComfyUI_UltimateSDUpscale" "$custom_node_path/ComfyUI_UltimateSDUpscale"
Git-CLone "https://github.com/AlekPet/ComfyUI_Custom_Nodes_AlekPet" "$custom_node_path/ComfyUI_Custom_Nodes_AlekPet"
Git-CLone "https://github.com/talesofai/comfyui-browser" "$custom_node_path/comfyui-browser"
Git-CLone "https://github.com/ltdrdata/ComfyUI-Inspire-Pack" "$custom_node_path/ComfyUI-Inspire-Pack"
Git-CLone "https://github.com/Suzie1/ComfyUI_Comfyroll_CustomNodes" "$custom_node_path/ComfyUI_Comfyroll_CustomNodes"
Git-CLone "https://github.com/crystian/ComfyUI-Crystools" "$custom_node_path/ComfyUI-Crystools"
Git-CLone "https://github.com/shiimizu/ComfyUI-TiledDiffusion" "$custom_node_path/ComfyUI-TiledDiffusion"
Git-CLone "https://github.com/huchenlei/ComfyUI-openpose-editor" "$custom_node_path/ComfyUI-openpose-editor"
Git-CLone "https://github.com/licyk/ComfyUI-Restart-Sampler" "$custom_node_path/ComfyUI-Restart-Sampler"
Git-CLone "https://github.com/weilin9999/WeiLin-ComfyUI-prompt-all-in-one" "$custom_node_path/WeiLin-ComfyUI-prompt-all-in-one"
Install-PyTorch
Install-ComfyUI-Dependence
if (!(Test-Path "$InstallPath/launch_args.txt")) {
Print-Msg "设置默认 ComfyUI 启动参数"
$content = "--auto-launch --preview-method auto --disable-cuda-malloc"
Set-Content -Encoding UTF8 -Path "$InstallPath/launch_args.txt" -Value $content
}
if (!(Test-Path "$InstallPath/ComfyUI/user/default/comfy.settings.json")) {
Print-Msg "设置默认 ComfyUI 设置"
$json_content = @{
"Comfy.Settings.ExtensionPanel" = $true
"DZ.Debug.enabled" = $true
"Comfy.UseNewMenu" = "Top"
"Comfy.Locale" = "zh"
"Comfy.RerouteBeta" = $true
}
$json_content = $json_content | ConvertTo-Json -Depth 4
New-Item -ItemType Directory -Path "$InstallPath/ComfyUI/user/default" -Force > $null
# 创建一个不带 BOM 的 UTF-8 编码器
$utf8Encoding = New-Object System.Text.UTF8Encoding($false)
# 使用 StreamWriter 来写入文件
$streamWriter = [System.IO.StreamWriter]::new("$InstallPath/ComfyUI/user/default/comfy.settings.json", $false, $utf8Encoding)
$streamWriter.Write($json_content)
$streamWriter.Close()
}
# 清理缓存
Print-Msg "清理下载 Python 软件包的缓存中"
python -m pip cache purge
uv cache clean
Set-Content -Encoding UTF8 -Path "$InstallPath/update_time.txt" -Value $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") # 记录更新时间
}
# 启动脚本
function Write-Launch-Script {
$content = "
# ComfyUI Installer 版本和检查更新间隔
`$COMFYUI_INSTALLER_VERSION = $COMFYUI_INSTALLER_VERSION
`$UPDATE_TIME_SPAN = $UPDATE_TIME_SPAN
# Pip 镜像源
`$PIP_INDEX_ADDR = `"$PIP_INDEX_ADDR`"
`$PIP_INDEX_ADDR_ORI = `"$PIP_INDEX_ADDR_ORI`"
`$PIP_EXTRA_INDEX_ADDR = `"$PIP_EXTRA_INDEX_ADDR`"
`$PIP_EXTRA_INDEX_ADDR_ORI = `"$PIP_EXTRA_INDEX_ADDR_ORI`"
`$PIP_FIND_ADDR = `"$PIP_FIND_ADDR`"
`$PIP_FIND_ADDR_ORI = `"$PIP_FIND_ADDR_ORI`"
`$USE_PIP_MIRROR = if (!(Test-Path `"`$PSScriptRoot/disable_pip_mirror.txt`")) { `$true } else { `$false }
`$PIP_INDEX_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_INDEX_ADDR } else { `$PIP_INDEX_ADDR_ORI }
`$PIP_EXTRA_INDEX_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_EXTRA_INDEX_ADDR } else { `$PIP_EXTRA_INDEX_ADDR_ORI }
`$PIP_FIND_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_FIND_ADDR } else { `$PIP_FIND_ADDR_ORI }
# `$PIP_FIND_MIRROR_CU121 = `"$PIP_FIND_MIRROR_CU121`"
`$PIP_EXTRA_INDEX_MIRROR_PYTORCH = `"$PIP_EXTRA_INDEX_MIRROR_PYTORCH`"
`$PIP_EXTRA_INDEX_MIRROR_CU121 = `"$PIP_EXTRA_INDEX_MIRROR_CU121`"
`$PIP_EXTRA_INDEX_MIRROR_CU124 = `"$PIP_EXTRA_INDEX_MIRROR_CU124`"
# Github 镜像源
`$GITHUB_MIRROR_LIST = @(
`"https://ghfast.top/https://github.com`",
`"https://mirror.ghproxy.com/https://github.com`",
`"https://ghproxy.net/https://github.com`",
`"https://gh.api.99988866.xyz/https://github.com`",
`"https://gitclone.com/github.com`",
`"https://gh-proxy.com/https://github.com`",
`"https://ghps.cc/https://github.com`",
`"https://gh.idayer.com/https://github.com`"
)
# uv 最低版本
`$UV_MINIMUM_VER = `"$UV_MINIMUM_VER`"
# PATH
`$PYTHON_PATH = `"`$PSScriptRoot/python`"
`$PYTHON_EXTRA_PATH = `"`$PSScriptRoot/ComfyUI/python`"
`$PYTHON_SCRIPTS_PATH = `"`$PSScriptRoot/python/Scripts`"
`$PYTHON_SCRIPTS_EXTRA_PATH = `"`$PSScriptRoot/ComfyUI/python/Scripts`"
`$GIT_PATH = `"`$PSScriptRoot/git/bin`"
`$GIT_EXTRA_PATH = `"`$PSScriptRoot/ComfyUI/git/bin`"
`$Env:PATH = `"`$PYTHON_EXTRA_PATH`$([System.IO.Path]::PathSeparator)`$PYTHON_SCRIPTS_EXTRA_PATH`$([System.IO.Path]::PathSeparator)`$GIT_EXTRA_PATH`$([System.IO.Path]::PathSeparator)`$PYTHON_PATH`$([System.IO.Path]::PathSeparator)`$PYTHON_SCRIPTS_PATH`$([System.IO.Path]::PathSeparator)`$GIT_PATH`$([System.IO.Path]::PathSeparator)`$Env:PATH`"
# 环境变量
`$Env:PIP_INDEX_URL = `"`$PIP_INDEX_MIRROR`"
`$Env:PIP_EXTRA_INDEX_URL = if (`$PIP_EXTRA_INDEX_MIRROR -ne `$PIP_EXTRA_INDEX_MIRROR_PYTORCH) { `"`$PIP_EXTRA_INDEX_MIRROR `$PIP_EXTRA_INDEX_MIRROR_PYTORCH`" } else { `$PIP_EXTRA_INDEX_MIRROR }
`$Env:PIP_FIND_LINKS = `"`$PIP_FIND_MIRROR`"
`$Env:UV_INDEX_URL = `"`$PIP_INDEX_MIRROR`"
`$Env:UV_EXTRA_INDEX_URL = `"`$PIP_EXTRA_INDEX_MIRROR_PYTORCH`"
`$Env:UV_FIND_LINKS = `"`$PIP_FIND_MIRROR`"
`$Env:UV_LINK_MODE = `"copy`"
`$Env:UV_HTTP_TIMEOUT = 30
`$Env:UV_CONCURRENT_DOWNLOADS = 50
`$Env:PIP_DISABLE_PIP_VERSION_CHECK = 1
`$Env:PIP_NO_WARN_SCRIPT_LOCATION = 0
`$Env:PIP_TIMEOUT = 30
`$Env:PIP_RETRIES = 5
`$Env:PYTHONUTF8 = 1
`$Env:PYTHONIOENCODING = `"utf8`"
`$Env:CACHE_HOME = `"`$PSScriptRoot/cache`"
`$Env:HF_HOME = `"`$PSScriptRoot/cache/huggingface`"
`$Env:MATPLOTLIBRC = `"`$PSScriptRoot/cache`"
`$Env:MODELSCOPE_CACHE = `"`$PSScriptRoot/cache/modelscope/hub`"
`$Env:MS_CACHE_HOME = `"`$PSScriptRoot/cache/modelscope/hub`"
`$Env:SYCL_CACHE_DIR = `"`$PSScriptRoot/cache/libsycl_cache`"
`$Env:TORCH_HOME = `"`$PSScriptRoot/cache/torch`"
`$Env:U2NET_HOME = `"`$PSScriptRoot/cache/u2net`"
`$Env:XDG_CACHE_HOME = `"`$PSScriptRoot/cache`"
`$Env:PIP_CACHE_DIR = `"`$PSScriptRoot/cache/pip`"
`$Env:PYTHONPYCACHEPREFIX = `"`$PSScriptRoot/cache/pycache`"
`$Env:UV_CACHE_DIR = `"`$PSScriptRoot/cache/uv`"
`$Env:UV_PYTHON = `"`$PSScriptRoot/python/python.exe`"
# 消息输出
function Print-Msg (`$msg) {
Write-Host `"[`$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`")]`" -ForegroundColor Yellow -NoNewline
Write-Host `"[ComfyUI Installer]`" -ForegroundColor Cyan -NoNewline
Write-Host `":: `" -ForegroundColor Blue -NoNewline
Write-Host `"`$msg`"
}
# 显示 ComfyUI Installer 版本
function Get-ComfyUI-Installer-Version {
`$ver = `$([string]`$COMFYUI_INSTALLER_VERSION).ToCharArray()
`$major = (`$ver[0..(`$ver.Length - 3)])
`$minor = `$ver[-2]
`$micro = `$ver[-1]
Print-Msg `"ComfyUI Installer 版本: v`${major}.`${minor}.`${micro}`"
}
# Pip 镜像源状态
function Pip-Mirror-Status {
if (`$USE_PIP_MIRROR) {
Print-Msg `"使用 Pip 镜像源`"
} else {
Print-Msg `"检测到 disable_pip_mirror.txt 配置文件, 已将 Pip 源切换至官方源`"
}
}
# 修复 PyTorch 的 libomp 问题
function Fix-PyTorch {
`$content = `"
import importlib.util
import shutil
import os
import ctypes
import logging
torch_spec = importlib.util.find_spec('torch')
for folder in torch_spec.submodule_search_locations:
lib_folder = os.path.join(folder, 'lib')
test_file = os.path.join(lib_folder, 'fbgemm.dll')
dest = os.path.join(lib_folder, 'libomp140.x86_64.dll')
if os.path.exists(dest):
break
with open(test_file, 'rb') as f:
contents = f.read()
if b'libomp140.x86_64.dll' not in contents:
break
try:
mydll = ctypes.cdll.LoadLibrary(test_file)
except FileNotFoundError as e:
logging.warning('检测到 PyTorch 版本存在 libomp 问题, 进行修复')
shutil.copyfile(os.path.join(lib_folder, 'libiomp5md.dll'), dest)
`"
Print-Msg `"检测 PyTorch 的 libomp 问题中`"
python -c `"`$content`"
Print-Msg `"PyTorch 检查完成`"
}
# ComfyUI Installer 更新检测
function Check-ComfyUI-Installer-Update {
# 可用的下载源
`$urls = @(
`"https://github.com/licyk/sd-webui-all-in-one/raw/main/comfyui_installer.ps1`",
`"https://gitee.com/licyk/sd-webui-all-in-one/raw/main/comfyui_installer.ps1`",
`"https://github.com/licyk/sd-webui-all-in-one/releases/download/comfyui_installer/comfyui_installer.ps1`",
`"https://gitee.com/licyk/sd-webui-all-in-one/releases/download/comfyui_installer/comfyui_installer.ps1`",
`"https://gitlab.com/licyk/sd-webui-all-in-one/-/raw/main/comfyui_installer.ps1`"
)
`$i = 0
New-Item -ItemType Directory -Path `"`$Env:CACHE_HOME`" -Force > `$null
if (Test-Path `"`$PSScriptRoot/disable_update.txt`") {
Print-Msg `"检测到 disable_update.txt 更新配置文件, 已禁用 ComfyUI Installer 的自动检查更新功能`"
return
}
# 获取更新时间间隔
try {
`$last_update_time = Get-Content `"`$PSScriptRoot/update_time.txt`" 2> `$null
`$last_update_time = Get-Date `$last_update_time -Format `"yyyy-MM-dd HH:mm:ss`"
}
catch {
`$last_update_time = Get-Date 0 -Format `"yyyy-MM-dd HH:mm:ss`"
}
finally {
`$update_time = Get-Date -Format `"yyyy-MM-dd HH:mm:ss`"
`$time_span = New-TimeSpan -Start `$last_update_time -End `$update_time
}
if (`$time_span.TotalSeconds -gt `$UPDATE_TIME_SPAN) {
Set-Content -Encoding UTF8 -Path `"`$PSScriptRoot/update_time.txt`" -Value `$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`") # 记录更新时间
ForEach (`$url in `$urls) {
Print-Msg `"检查 ComfyUI Installer 更新中`"
Invoke-WebRequest -Uri `$url -OutFile `"`$Env:CACHE_HOME/comfyui_installer.ps1`"
if (`$?) {
`$latest_version = [int]`$(Get-Content `"`$Env:CACHE_HOME/comfyui_installer.ps1`" | Select-String -Pattern `"COMFYUI_INSTALLER_VERSION`" | ForEach-Object { `$_.ToString() })[0].Split(`"=`")[1].Trim()
if (`$latest_version -gt `$COMFYUI_INSTALLER_VERSION) {
Print-Msg `"检测到 ComfyUI Installer 有新版本可用, 是否进行更新 (yes/no) ?`"
Print-Msg `"提示: 输入 yes 确认或 no 取消 (默认为 no)`"
`$arg = Read-Host `"========================================>`"
if (`$arg -eq `"yes`" -or `$arg -eq `"y`" -or `$arg -eq `"YES`" -or `$arg -eq `"Y`") {
Print-Msg `"调用 ComfyUI Installer 进行更新中`"
. `"`$Env:CACHE_HOME/comfyui_installer.ps1`" -InstallPath `"`$PSScriptRoot`" -UseUpdateMode
Print-Msg `"更新结束, 需重新启动 ComfyUI Installer 管理脚本以应用更新, 回车退出 ComfyUI Installer 管理脚本`"
Read-Host | Out-Null
exit 0
} else {
Print-Msg `"跳过 ComfyUI Installer 更新`"
}
} else {
Print-Msg `"ComfyUI Installer 已是最新版本`"
}
break
} else {
`$i += 1
if (`$i -lt `$urls.Length) {
Print-Msg `"重试检查 ComfyUI Installer 更新中`"
} else {
Print-Msg `"检查 ComfyUI Installer 更新失败`"
}
}
}
}
}
# 代理配置
function Set-Proxy {
`$Env:NO_PROXY = `"localhost,127.0.0.1,::1`"
# 检测是否禁用自动设置镜像源
if (Test-Path `"`$PSScriptRoot/disable_proxy.txt`") {
Print-Msg `"检测到本地存在 disable_proxy.txt 代理配置文件, 禁用自动设置代理`"
return
}
`$internet_setting = Get-ItemProperty -Path `"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings`"
if (Test-Path `"`$PSScriptRoot/proxy.txt`") { # 本地存在代理配置
`$proxy_value = Get-Content `"`$PSScriptRoot/proxy.txt`"
`$Env:HTTP_PROXY = `$proxy_value
`$Env:HTTPS_PROXY = `$proxy_value
Print-Msg `"检测到本地存在 proxy.txt 代理配置文件, 已读取代理配置文件并设置代理`"
} elseif (`$internet_setting.ProxyEnable -eq 1) { # 系统已设置代理
`$proxy_addr = `$(`$internet_setting.ProxyServer)
# 提取代理地址
if ((`$proxy_addr -match `"http=(.*?);`") -or (`$proxy_addr -match `"https=(.*?);`")) {
`$proxy_value = `$matches[1]
# 去除 http / https 前缀
`$proxy_value = `$proxy_value.ToString().Replace(`"http://`", `"`").Replace(`"https://`", `"`")
`$proxy_value = `"http://`${proxy_value}`"
} elseif (`$proxy_addr -match `"socks=(.*)`") {
`$proxy_value = `$matches[1]
# 去除 socks 前缀
`$proxy_value = `$proxy_value.ToString().Replace(`"http://`", `"`").Replace(`"https://`", `"`")
`$proxy_value = `"socks://`${proxy_value}`"
} else {
`$proxy_value = `"http://`${proxy_addr}`"
}
`$Env:HTTP_PROXY = `$proxy_value
`$Env:HTTPS_PROXY = `$proxy_value
Print-Msg `"检测到系统设置了代理, 已读取系统中的代理配置并设置代理`"
}
}
# HuggingFace 镜像源
function Set-HuggingFace-Mirror {
if (!(Test-Path `"`$PSScriptRoot/disable_hf_mirror.txt`")) { # 检测是否禁用了自动设置 HuggingFace 镜像源
if (Test-Path `"`$PSScriptRoot/hf_mirror.txt`") { # 本地存在 HuggingFace 镜像源配置
`$hf_mirror_value = Get-Content `"`$PSScriptRoot/hf_mirror.txt`"
`$Env:HF_ENDPOINT = `$hf_mirror_value
Print-Msg `"检测到本地存在 hf_mirror.txt 配置文件, 已读取该配置并设置 HuggingFace 镜像源`"
} else { # 使用默认设置
`$Env:HF_ENDPOINT = `"https://hf-mirror.com`"
Print-Msg `"使用默认 HuggingFace 镜像源`"
}
} else {
Print-Msg `"检测到本地存在 disable_hf_mirror.txt 镜像源配置文件, 禁用自动设置 HuggingFace 镜像源`"
}
}
# Github 镜像源
function Set-Github-Mirror {
`$Env:GIT_CONFIG_GLOBAL = `"`$PSScriptRoot/.gitconfig`" # 设置 Git 配置文件路径
if (Test-Path `"`$PSScriptRoot/.gitconfig`") {
Remove-Item -Path `"`$PSScriptRoot/.gitconfig`" -Force -Recurse
}
# 默认 Git 配置
git config --global --add safe.directory `"*`"
git config --global core.longpaths true
if (Test-Path `"`$PSScriptRoot/disable_gh_mirror.txt`") { # 禁用 Github 镜像源
Print-Msg `"检测到本地存在 disable_gh_mirror.txt Github 镜像源配置文件, 禁用 Github 镜像源`"
return
}
if (Test-Path `"`$PSScriptRoot/gh_mirror.txt`") { # 使用自定义 Github 镜像源
`$github_mirror = Get-Content `"`$PSScriptRoot/gh_mirror.txt`"
git config --global url.`"`$github_mirror`".insteadOf `"https://github.com`"
Print-Msg `"检测到本地存在 gh_mirror.txt Github 镜像源配置文件, 已读取 Github 镜像源配置文件并设置 Github 镜像源`"
return
}
# 自动检测可用镜像源并使用
`$status = 0
ForEach(`$i in `$GITHUB_MIRROR_LIST) {
Print-Msg `"测试 Github 镜像源: `$i`"
if (Test-Path `"`$Env:CACHE_HOME/github-mirror-test`") {
Remove-Item -Path `"`$Env:CACHE_HOME/github-mirror-test`" -Force -Recurse
}
git clone `$i/licyk/empty `"`$Env:CACHE_HOME/github-mirror-test`" --quiet
if (`$?) {
Print-Msg `"该 Github 镜像源可用`"
`$github_mirror = `$i
`$status = 1
break
} else {
Print-Msg `"镜像源不可用, 更换镜像源进行测试`"
}
}
if (Test-Path `"`$Env:CACHE_HOME/github-mirror-test`") {
Remove-Item -Path `"`$Env:CACHE_HOME/github-mirror-test`" -Force -Recurse
}
if (`$status -eq 0) {
Print-Msg `"无可用 Github 镜像源, 取消使用 Github 镜像源`"
} else {
Print-Msg `"设置 Github 镜像源`"
git config --global url.`"`$github_mirror`".insteadOf `"https://github.com`"
}
}
# 检查 uv 是否需要更新
function Check-uv-Version {
`$content = `"
import re
from importlib.metadata import version
def compare_versions(version1, version2) -> int:
try:
nums1 = re.sub(r'[a-zA-Z]+', '', version1).replace('-', '.').replace('+', '.').split('.')
nums2 = re.sub(r'[a-zA-Z]+', '', version2).replace('-', '.').replace('+', '.').split('.')
except:
return 0
for i in range(max(len(nums1), len(nums2))):
num1 = int(nums1[i]) if i < len(nums1) else 0
num2 = int(nums2[i]) if i < len(nums2) else 0
if num1 == num2:
continue
elif num1 > num2:
return 1
else:
return -1
return 0
def is_uv_need_update() -> bool:
try:
uv_ver = version('uv')
except:
return True
if compare_versions(uv_ver, uv_minimum_ver) == -1:
return True
else:
return False
uv_minimum_ver = '`$UV_MINIMUM_VER'
print(is_uv_need_update())
`"
Print-Msg `"检测 uv 是否需要更新`"
`$status = `$(python -c `"`$content`")
if (`$status -eq `"True`") {
Print-Msg `"更新 uv 中`"
python -m pip install -U `"uv>=`$UV_MINIMUM_VER`"
if (`$?) {
Print-Msg `"uv 更新成功`"
} else {
Print-Msg `"uv 更新失败, 可能会造成 uv 部分功能异常`"
}
} else {
Print-Msg `"uv 无需更新`"
}
}
# 设置 uv 的使用状态