-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-bbot.ps1
117 lines (99 loc) · 3.64 KB
/
install-bbot.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
$pythonFullVersion = "3.10.8" # 填写你需要的 Python 版本
$compatiblePythonVersion = "3.9" # 填写可以兼容的 Python 版本
$installPathName = "bbot" # 填写安装路径名,可代指 bot 的名字
$gitRepo = "https://github.com/djkcyl/BBot-Graia.git" # 填写你的 bot 的 git 仓库地址
$gitBranch = "master" # 填写仓库分支
$mainFile = "main.py" # 填写你的 bot 的主文件名
# 以下内容请不要修改
$pythonPath = "python/Scripts/python.exe"
$needClear = $args[0] -eq "--clear"
$wingetInstalled = Get-Command winget -ErrorAction SilentlyContinue
$gitInstalled = Get-Command git -ErrorAction SilentlyContinue
$pythonInstalled = Get-Command python -ErrorAction SilentlyContinue
$pythonVersion = $pythonFullVersion.Split(".")[0..1] -join "."
$pythonVersionMinor = $pythonVersion.split(".")[1]
$compatiblePythonVersionMinor = $compatiblePythonVersion.split(".")[1]
if ($needClear) {
Write-Host "Clearing ./$installPathName"
Remove-Item -Path $installPathName -Recurse -Force
}
if ($PSVersionTable.PSVersion.Major -lt 5 -or $PSVersionTable.PSVersion.Minor -lt 1) {
Write-Host "This script only supports Windows 10 or Windows Server 2019 and above." -ForegroundColor Red
exit
}
function InstallScoop {
Write-Host "Installing Scoop"
Invoke-Expression "& {$(Invoke-RestMethod get.scoop.sh)} -RunAsAdmin"
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
if ($gitInstalled) {
Write-Host "Git is already installed"
}
else {
if ($wingetInstalled) {
Write-Host "Installing Git with winget"
& winget install --id Git.Git
}
else {
Write-Host "Installing Git with Scoop"
if (!(Test-Path "$env:USERPROFILE\scoop")) {
InstallScoop
}
& scoop install git
}
}
Write-Host "Cloning $gitRepo"
& git clone $gitRepo -b $gitBranch $installPathName
function CreatePythonVenv {
& python -m venv $installPathName/python
}
$pythonInstalledVersion = $pythonInstalled.Version
if ($pythonInstalled -and $pythonInstalled.Version.Major -eq 3 -and ($pythonInstalled.Version.Minor -eq $pythonVersionMinor -or $pythonInstalled.Version.Minor -eq $compatiblePythonVersionMinor)) {
Write-Host "Python $pythonInstalledVersion is already installed"
CreatePythonVenv
}
else {
if ($wingetInstalled) {
Write-Host "Installing Python $pythonVersion with winget"
& winget install --id Python.Python.$pythonVersion
CreatePythonVenv
}
else {
Write-Host "Installing Python $pythonVersion with Scoop"
& scoop install python@$pythonFullVersion
CreatePythonVenv
}
}
Write-Host "Installing dependencies"
& $installPathName/$pythonPath -m pip install -r ./$installPathName/requirements.txt
$script = "`$runingProcess = Get-Process -Name 'python' -ErrorAction SilentlyContinue
if (`$runingProcess) {
`$runingProcess | Format-Table -AutoSize
`$choice = Read-Host -Prompt 'There are currently Python processes running, choose whether to continue? (y/n)'
if (!(`$choice -eq 'y')) {
exit
}
}
function Update {
& git pull
& $pythonPath -m pip install -r ./requirements.txt --upgrade
}
function Uninstall {
Remove-Item -Path ./$installPathName -Recurse -Force
}
function Start {
& $pythonPath ./$mainFile
}
if (`$args[0] -eq '--update') {
Update
}
elseif (`$args[0] -eq '--uninstall') {
Uninstall
}
elseif (`$args[0] -eq '--start') {
Start
}
else {
Write-Host 'Usage: ./$installPathName.ps1 [--update|--uninstall|--start]'
}"
Set-Content -Path ./$installPathName/$installPathName.ps1 -Value $script