-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnbp.ts
205 lines (180 loc) · 5.21 KB
/
nbp.ts
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
import { createWriteStream } from 'node:fs'
import { cp, mkdir, readFile, writeFile } from 'node:fs/promises'
import { basename, join, resolve } from 'node:path'
import { finished } from 'node:stream'
import { promisify } from 'node:util'
import { extract } from 'tar'
import bz2 from 'unbzip2-stream'
import { register } from 'yakumo'
import { nameNumpy, namePil, pyodideSource } from './config'
import { download, exists, spawnOutput } from './utils'
const blacklist = [
'nonebot-adapter-onebot',
'nonebot-plugin-apscheduler',
'nonebot-plugin-htmlrender',
'nonebot2',
'httpx',
'aiohttp',
'pydantic',
'build',
'twine',
'pil',
'pillow',
'numpy',
'setuptools',
]
interface JohnnydepItem {
name: string
version_latest_in_spec: string
download_link: string
requires: string[]
}
function skip(name: string, blacklist: string[]) {
name = name
.split('[')[0]
.split('<')[0]
.split('>')[0]
.split('=')[0]
.split('!')[0]
.toLowerCase()
.replace(/_/g, '-')
return !blacklist.some((y) => name === y.toLowerCase().replace(/_/g, '-'))
}
const preparePyodide = async () => {
const pathCache = resolve(__dirname, '../build/cache')
await mkdir(pathCache, { recursive: true })
const pathExtracted = join(pathCache, 'pyodide')
await mkdir(pathExtracted, { recursive: true })
if (!await exists(join(pathExtracted, 'pyodide.js'))) {
const stream = await download(pyodideSource)
await promisify(finished)(stream.pipe(bz2()).pipe(extract({ cwd: pathExtracted, strip: 1 })))
}
return pathExtracted
}
const buildNonebot = async () => {
const pathPyodide = await preparePyodide()
const pathPackage = resolve(__dirname, '../packages/nonebot')
const pathDist = join(pathPackage, 'dist')
if (await exists(pathDist)) return
await mkdir(pathDist, { recursive: true })
await Promise.all(
[
// namePyyaml,
namePil,
nameNumpy,
].map((x) => cp(join(pathPyodide, x), join(pathDist, x)))
)
await writeFile(
join(pathDist, 'deps.json'),
JSON.stringify([
// {
// name: 'yaml',
// filename: namePyyaml,
// },
{
name: 'PIL',
filename: namePil,
},
{
name: 'numpy',
filename: nameNumpy,
},
])
)
}
const buildPlugin = async (path: string) => {
const pathPackage = resolve(__dirname, `..${path}`)
const pathDist = join(pathPackage, 'dist')
if (await exists(pathDist)) return
await mkdir(pathDist, { recursive: true })
// Parents of each cycle
const { name, version, exclude = [] } = JSON.parse(await readFile(join(pathPackage, 'nbp.json'), 'utf-8'))
let parents = [`${name}==${version}`]
// Finally collected deps
const deps: JohnnydepItem[] = []
while (parents.length) {
// Collected results
const results: JohnnydepItem[][] = []
// Collect
for (const parent of parents) {
results.push(JSON.parse(await spawnOutput('python', [
'-m',
'johnnydep',
'-f',
'ALL',
'-o',
'json',
'--no-deps',
parent,
])))
}
// Filter requirements of results info parents of next cycle
parents = results
.map((result) => result[0].requires.filter(x => skip(x, [...blacklist, ...exclude])))
.flat()
// Push deps
for (const result of results.flat()) {
const conflictedIndex = deps.findIndex((x) => x.name === result.name)
if (conflictedIndex === -1) {
deps.push(result)
continue
}
const confilcted = deps[conflictedIndex]
const versions_available: string[] = JSON.parse(
await spawnOutput('python', [
'-m',
'johnnydep',
'-f',
'versions_available',
'-o',
'json',
'--no-deps',
result.name,
])
)[0].versions_available
const selected =
versions_available.indexOf(confilcted.version_latest_in_spec) >
versions_available.indexOf(result.version_latest_in_spec)
? confilcted
: result
deps.splice(conflictedIndex, 1, selected)
}
}
const resultDeps = deps.slice(1).map((x) => ({
name: x.name,
version: x.version_latest_in_spec,
filename: x.download_link.endsWith('.tar.gz') ? x.name : basename(x.download_link),
url: x.download_link,
}))
await Promise.all(resultDeps.map(async (x) => {
const stream = await download(x.url)
if (!x.url.endsWith('.tar.gz')) {
return promisify(finished)(stream.pipe(createWriteStream(join(pathDist, x.filename))))
}
await mkdir(pathDist, { recursive: true })
await promisify(finished)(stream.pipe(extract({
cwd: pathDist,
newer: true,
strip: 1,
filter(path, stat) {
if (!path.endsWith('/')) return true
const segments = path.split(/\//g)
return segments[1] === x.name
},
})))
}))
await writeFile(
join(pathDist, 'deps.json'),
JSON.stringify(
resultDeps.map((x) => ({ name: x.name, filename: x.filename }))
)
)
}
register('nbp', (project) => {
const tasks = Object
.keys(project.targets)
.filter(path => path.startsWith('/plugins') && !path.includes('_template'))
.map(buildPlugin)
tasks.push(buildNonebot())
return Promise.all(tasks)
})