✨ feat(vscode): 代码阅读支持 Dockerfile:解析指令、每个 FROM 一个阶段(阶段名能跳过去)、RUN 交给 shell 按命令拆、复制、构建参数 / 环境变量、设置、标签、健康检查、启动,连着写的 ENV / ARG / COPY / LABEL 并成一块加一张表;语法包取自 npm 包 tree-sitter-wasm,打包时复制进扩展
This commit is contained in:
1 parent
0665f35821
commit
d4ec0e7de4
11 files changed
+1619
-4
No files matched your search
@@ -1,15 +1,18 @@
|
||||
import type { Memento } from 'vscode'
|
||||
import type { LibraryRule } from '../contract'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { packageCommandRules } from '../commands/packageCommands'
|
||||
import { shellCommandRules } from '../commands/shellCommands'
|
||||
import { createBashAdapter } from '../languages/bash'
|
||||
import { createDockerfileAdapter } from '../languages/dockerfile'
|
||||
import { createPythonAdapter } from '../languages/python'
|
||||
import { createTypeScriptAdapter } from '../languages/typescript'
|
||||
import { CodeReaderRegistry } from '../registry'
|
||||
import { fastapiRule } from '../rules/fastapi'
|
||||
import { sqlalchemyRule } from '../rules/sqlalchemy'
|
||||
import { runtime, WASM_DIR } from '../testSupport'
|
||||
import { DOCKERFILE_WASM_DIR, runtime, WASM_DIR } from '../testSupport'
|
||||
import { bashZhTranslator } from '../translators/bashZh'
|
||||
import { dockerfileZhTranslator } from '../translators/dockerfileZh'
|
||||
import { pythonZhTranslator } from '../translators/pythonZh'
|
||||
import { typescriptZhTranslator } from '../translators/typescriptZh'
|
||||
import { documentMessage } from './documentMessage'
|
||||
@@ -168,6 +171,24 @@ describe('预览的 document 消息', () => {
|
||||
expect(symbols.map(symbol => symbol.occurrences.map(range => source.slice(range.startIndex, range.endIndex)))).toEqual([['dest', 'dest']])
|
||||
})
|
||||
|
||||
it('dockerfile:RUN 里的 shell 交给注册的 shell 语言插件、翻译插件和命令规则读,名字连同 shell 里的一起交给宿主跳转', async () => {
|
||||
const registry = new CodeReaderRegistry({ get: () => undefined, update: async () => {} } as Pick<Memento, 'get' | 'update'>)
|
||||
registry.registerLanguage(createBashAdapter(WASM_DIR))
|
||||
registry.registerLanguage(createDockerfileAdapter(DOCKERFILE_WASM_DIR))
|
||||
registry.registerTranslator(bashZhTranslator)
|
||||
registry.registerTranslator(dockerfileZhTranslator)
|
||||
for (const rule of [...shellCommandRules, ...packageCommandRules])
|
||||
registry.registerCommandRule(rule)
|
||||
const source = 'FROM node:22 AS deps\nRUN x=1 && echo "$x" && apt-get update\nCMD ["node", "server.js"]\n'
|
||||
const { message, symbols } = await documentMessage(doc('dockerfile', source), registry, runtime, ignore)
|
||||
const [stage] = message.blocks
|
||||
expect(stage.kind).toBe('stage')
|
||||
expect(stage.children.map(child => [child.kind, child.children.map(step => step.kind)])).toEqual([['run', ['variable', 'command', 'command']], ['start', []]])
|
||||
expect(stage.children[0].children[2]).toMatchObject({ translation: [{ text: '更新软件包列表' }] })
|
||||
expect(message.attributes.find(attribute => attribute.id === 'kind')?.values.map(value => value.name)).toEqual(['阶段', '执行', '变量', '命令', '启动'])
|
||||
expect(symbols.map(symbol => symbol.occurrences.map(range => source.slice(range.startIndex, range.endIndex)))).toEqual([['deps'], ['x', 'x']])
|
||||
})
|
||||
|
||||
it('解析失败:把原因放进 error', async () => {
|
||||
const failing = { parse: async () => Promise.reject(new Error('语法包加载失败')) }
|
||||
const { message } = await documentMessage(doc('typescript', 'const a = 1'), registryWithTypeScript(), failing, ignore)
|
||||
|
||||
Reference in new issue
Block a user