-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
220 lines (179 loc) · 8.36 KB
/
Copy pathinstall.ps1
File metadata and controls
220 lines (179 loc) · 8.36 KB
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
#
# AdMapix 安装脚本 (Windows)
#
# 用法:
# powershell -ExecutionPolicy Bypass -File install.ps1 -ApiKey <API_KEY>
# $env:ADMAPIX_API_KEY="<API_KEY>"; powershell -ExecutionPolicy Bypass -File install.ps1
#
param(
[Parameter(Mandatory=$false)]
[string]$ApiKey = $env:ADMAPIX_API_KEY
)
$ErrorActionPreference = "Stop"
if (-not $ApiKey) {
Write-Host "用法: powershell -ExecutionPolicy Bypass -File install.ps1 -ApiKey <API_KEY>"
Write-Host ' 或: $env:ADMAPIX_API_KEY="<API_KEY>"; powershell -ExecutionPolicy Bypass -File install.ps1'
exit 1
}
$InstallDir = Join-Path $env:USERPROFILE ".admapix"
$McporterConfig = Join-Path $env:USERPROFILE ".mcporter\mcporter.json"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$MinPyMajor = 3
$MinPyMinor = 10
function Write-Info { param($msg) Write-Host "[OK] $msg" -ForegroundColor Green }
function Write-Warn { param($msg) Write-Host "[!!] $msg" -ForegroundColor Yellow }
function Write-Err { param($msg) Write-Host "[ERR] $msg" -ForegroundColor Red; exit 1 }
# ── 工具函数 ──────────────────────────────────────────────────
function Find-Python {
$candidates = @("python3", "python")
foreach ($py in $candidates) {
$cmd = Get-Command $py -ErrorAction SilentlyContinue
if ($cmd) {
try {
$ver = & $py -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" 2>$null
$parts = $ver.Split(".")
if ([int]$parts[0] -ge $MinPyMajor -and [int]$parts[1] -ge $MinPyMinor) {
return $cmd.Source
}
} catch {}
}
}
return $null
}
function Refresh-Path {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("Path", "User")
}
# ── Step 1: Python ───────────────────────────────────────────
Write-Host ""
Write-Host "══════════════════════════════════════" -ForegroundColor Cyan
Write-Host " AdMapix 安装" -ForegroundColor Cyan
Write-Host "══════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
Write-Host "── [1/5] 检测 Python 环境 ──"
$PythonBin = Find-Python
if ($PythonBin) {
$pyVer = & $PythonBin --version 2>&1
Write-Info "Python 已就绪: $PythonBin ($pyVer)"
} else {
Write-Warn "未检测到 Python $MinPyMajor.$MinPyMinor+,开始安装..."
if (Get-Command winget -ErrorAction SilentlyContinue) {
Write-Host " → 通过 winget 安装 Python..."
winget install Python.Python.3.12 --accept-package-agreements --accept-source-agreements
} else {
Write-Err "请手动安装 Python $MinPyMajor.$MinPyMinor+: https://www.python.org/downloads/"
}
Refresh-Path
$PythonBin = Find-Python
if (-not $PythonBin) { Write-Err "Python 安装后仍未检测到,请重新打开终端后重试" }
Write-Info "Python 安装成功: $PythonBin"
}
# ── Step 2: Node.js ──────────────────────────────────────────
Write-Host ""
Write-Host "── [2/5] 检测 Node.js 环境 ──"
if (Get-Command node -ErrorAction SilentlyContinue) {
$nodeVer = node --version
Write-Info "Node.js 已就绪: $nodeVer"
} else {
Write-Warn "未检测到 Node.js,开始安装..."
if (Get-Command winget -ErrorAction SilentlyContinue) {
Write-Host " → 通过 winget 安装 Node.js..."
winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements
} else {
Write-Err "请手动安装 Node.js: https://nodejs.org/"
}
Refresh-Path
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Write-Err "Node.js 安装后仍未检测到,请重新打开终端后重试"
}
Write-Info "Node.js 安装成功: $(node --version)"
}
# ── Step 3: mcporter ─────────────────────────────────────────
Write-Host ""
Write-Host "── [3/5] 检测 mcporter ──"
if (Get-Command mcporter -ErrorAction SilentlyContinue) {
Write-Info "mcporter 已就绪"
} else {
Write-Warn "未检测到 mcporter,开始安装..."
npm install -g mcporter
Refresh-Path
if (-not (Get-Command mcporter -ErrorAction SilentlyContinue)) {
Write-Err "mcporter 安装失败,请检查 npm 全局路径是否在 PATH 中"
}
Write-Info "mcporter 安装成功"
}
# ── Step 4: MCP Server + 虚拟环境 ────────────────────────────
Write-Host ""
Write-Host "── [4/5] 安装 MCP Server ──"
if (-not (Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
Copy-Item (Join-Path $ScriptDir "server.py") (Join-Path $InstallDir "server.py") -Force
Copy-Item (Join-Path $ScriptDir "requirements.txt") (Join-Path $InstallDir "requirements.txt") -Force
$VenvDir = Join-Path $InstallDir ".venv"
if (-not (Test-Path $VenvDir)) {
Write-Host " → 创建虚拟环境..."
& $PythonBin -m venv $VenvDir
}
$VenvPython = Join-Path $VenvDir "Scripts\python.exe"
$VenvPip = Join-Path $VenvDir "Scripts\pip.exe"
Write-Host " → 安装依赖..."
& $VenvPip install -q -r (Join-Path $InstallDir "requirements.txt")
Write-Info "MCP Server 安装完成: $InstallDir"
# ── Step 5: mcporter.json ────────────────────────────────────
Write-Host ""
Write-Host "── [5/5] 配置 mcporter ──"
$ServerPy = Join-Path $InstallDir "server.py"
$McporterDir = Split-Path $McporterConfig
if (-not (Test-Path $McporterDir)) { New-Item -ItemType Directory -Path $McporterDir -Force | Out-Null }
# 用 Python 合并配置
$configScript = @"
import json, os
path = r'$McporterConfig'
cfg = {}
if os.path.exists(path):
with open(path) as f:
cfg = json.load(f)
servers = cfg.get('mcpServers', {})
servers['admapix'] = {
'command': r'$VenvPython $ServerPy',
'env': {'ADMAPIX_API_KEY': '$ApiKey'}
}
cfg['mcpServers'] = servers
cfg.setdefault('imports', [])
with open(path, 'w') as f:
json.dump(cfg, f, indent=2)
f.write('\n')
"@
& $VenvPython -c $configScript
Write-Info "mcporter 配置已写入: $McporterConfig"
# ── Step 6: 安装 Skill ───────────────────────────────────────
Write-Host ""
Write-Host "── [6/6] 安装 OpenClaw Skill ──"
$SkillDir = Join-Path $env:USERPROFILE ".openclaw\skills\admapix"
$SkillSrc = Join-Path $ScriptDir "skill"
if (Test-Path $SkillSrc) {
if (-not (Test-Path $SkillDir)) { New-Item -ItemType Directory -Path $SkillDir -Force | Out-Null }
Copy-Item (Join-Path $SkillSrc "*") $SkillDir -Recurse -Force
# Copy references subdirectory
$RefSrc = Join-Path $SkillSrc "references"
if (Test-Path $RefSrc) {
$RefDst = Join-Path $SkillDir "references"
if (-not (Test-Path $RefDst)) { New-Item -ItemType Directory -Path $RefDst -Force | Out-Null }
Copy-Item (Join-Path $RefSrc "*") $RefDst -Recurse -Force
}
Write-Info "Skill 已安装: $SkillDir"
} else {
Write-Warn "未找到 skill 目录,跳过 Skill 安装"
}
# ── 完成 ─────────────────────────────────────────────────────
Write-Host ""
Write-Host "══════════════════════════════════════" -ForegroundColor Cyan
Write-Host " 安装完成!" -ForegroundColor Cyan
Write-Host "══════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
Write-Host " MCP Server: $InstallDir"
Write-Host " mcporter: $McporterConfig"
Write-Host " Skill: $SkillDir"
Write-Host ""
Write-Host " 现在可以通过 OpenClaw 使用"
Write-Host " 「搜广告」「找素材」等指令了。"
Write-Host ""