提交 4d0fe707 编写于 作者: fxy060608's avatar fxy060608

wip(uts): compiler

上级 e47a7913
......@@ -40,37 +40,45 @@ export async function compile(module: string) {
}
}
} else {
// dev 模式仅 android 支持
if (process.env.UNI_UTS_PLATFORM === 'app-android') {
if (
process.env.UNI_UTS_PLATFORM === 'app-android' ||
process.env.UNI_UTS_PLATFORM === 'app-ios'
) {
// dev 模式
const filename =
resolvePlatformIndex('app-android', module, pkg) ||
resolvePlatformIndex(process.env.UNI_UTS_PLATFORM, module, pkg) ||
resolveRootIndex(module, pkg)
const compilerType =
process.env.UNI_UTS_PLATFORM === 'app-android' ? 'kotlin' : 'swift'
if (filename) {
deps.push(filename)
const res = await getCompiler('kotlin').runDev(filename)
const res = await getCompiler(compilerType).runDev(filename)
if (res) {
if (isArray(res.deps) && res.deps.length) {
// 添加其他文件的依赖
deps.push(...res.deps)
}
const files: string[] = []
if (process.env.UNI_APP_CHANGED_DEX_FILES) {
try {
files.push(...JSON.parse(process.env.UNI_APP_CHANGED_DEX_FILES))
} catch (e) {}
}
if (res.dex) {
files.push(res.dex)
if (res.type === 'kotlin') {
const files: string[] = []
if (process.env.UNI_APP_CHANGED_DEX_FILES) {
try {
files.push(...JSON.parse(process.env.UNI_APP_CHANGED_DEX_FILES))
} catch (e) {}
}
if (res.dex) {
files.push(res.dex)
}
process.env.UNI_APP_CHANGED_DEX_FILES = JSON.stringify([
...new Set(files),
])
} else if (res.type === 'swift') {
if (code) {
throw res.msg
}
}
process.env.UNI_APP_CHANGED_DEX_FILES = JSON.stringify([
...new Set(files),
])
}
}
} else if (process.env.UNI_UTS_PLATFORM === 'app-ios') {
process.env.UNI_APP_IOS_UTS = 'true'
}
}
return {
......
......@@ -5,16 +5,16 @@ import AdmZip from 'adm-zip'
import { sync } from 'fast-glob'
import { isArray } from '@vue/shared'
import {
installHBuilderXPlugin,
isInHBuilderX,
normalizePath,
parseJson,
resolveSourceMapPath,
runByHBuilderX,
} from './shared'
import {
CompilerServer,
genUTSPlatformResource,
getUtsCompiler,
getCompilerServer,
moveRootIndexSourceMap,
resolveAndroidDir,
resolvePackage,
......@@ -24,6 +24,23 @@ import {
import { Module } from '../types/types'
import { UtsResult } from '@dcloudio/uts'
interface KotlinCompilerServer extends CompilerServer {
getKotlincHome(): string
getDefaultJar(): string[]
compile(
options: { kotlinc: string[]; d8: string[] },
projectPath: string
): Promise<boolean>
checkDependencies?: (
configJsonPath: string
) => Promise<{ code: number; msg: string; data: string[] }>
checkRResources?: (resDir: string) => Promise<{
code: number
msg: string
data: { jarPath: string; uniModuleName: string }
}>
}
export function createKotlinResolveTypeReferenceName(
_namespace: string,
_ast: Module
......@@ -55,7 +72,7 @@ export async function runKotlinProd(filename: string) {
})
}
type RunKotlinDevResult = UtsResult & { dex?: string }
export type RunKotlinDevResult = UtsResult & { type: 'kotlin'; dex?: string }
export async function runKotlinDev(
filename: string
......@@ -65,6 +82,9 @@ export async function runKotlinDev(
return
}
const result = (await compile(filename)) as RunKotlinDevResult
result.type = 'kotlin'
const kotlinFile = resolveUTSPlatformFile(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
......@@ -73,7 +93,9 @@ export async function runKotlinDev(
})
// 开发模式下,需要生成 dex
if (fs.existsSync(kotlinFile)) {
const compilerServer = getCompilerServer()
const compilerServer = getCompilerServer<KotlinCompilerServer>(
'uniapp-runextension'
)
if (!compilerServer) {
throw `项目使用了uts插件,正在安装 uts Android 运行扩展...`
}
......@@ -314,37 +336,3 @@ function resolveJarPath(filename: string) {
function resolveClassPath(jars: string[]) {
return jars.join(os.platform() === 'win32' ? ';' : ':')
}
interface CompilerServer {
getKotlincHome(): string
getDefaultJar(): string[]
compile(
options: { kotlinc: string[]; d8: string[] },
projectPath: string
): Promise<boolean>
checkDependencies?: (
configJsonPath: string
) => Promise<{ code: number; msg: string; data: string[] }>
checkRResources?: (resDir: string) => Promise<{
code: number
msg: string
data: { jarPath: string; uniModuleName: string }
}>
}
function getCompilerServer(): CompilerServer | undefined {
const compilerServerPath = path.resolve(
process.env.UNI_HBUILDERX_PLUGINS,
'uniapp-runextension/out/main.js'
)
if (fs.existsSync(compilerServerPath)) {
// eslint-disable-next-line no-restricted-globals
return require(compilerServerPath)
} else {
if (runByHBuilderX()) {
installHBuilderXPlugin('uniapp-runextension')
} else {
console.error(compilerServerPath + ' is not found')
}
}
}
import fs from 'fs-extra'
import { capitalize } from '@vue/shared'
import {
genUTSPlatformResource,
getCompilerServer,
getUtsCompiler,
moveRootIndexSourceMap,
resolvePackage,
resolveUTSPlatformFile,
resolveUTSSourceMapPath,
} from './utils'
import { isInHBuilderX } from './shared'
import { UtsResult } from '@dcloudio/uts'
import path from 'path'
function parseSwiftPackage(filename: string) {
const res = resolvePackage(filename)
......@@ -36,7 +41,75 @@ export async function runSwiftProd(filename: string) {
})
}
export async function runSwiftDev(_filename: string) {}
export type RunSwiftDevResult = UtsResult & {
type: 'swift'
code: number
msg: string
}
export async function runSwiftDev(filename: string) {
// 文件有可能是 app-android 里边的,因为编译到 ios 时,为了保证不报错,可能会去读取 android 下的 uts
if (filename.includes('app-android')) {
return
}
const result = (await compile(filename)) as RunSwiftDevResult
result.type = 'swift'
const swiftFile = resolveUTSPlatformFile(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
platform: 'app-ios',
extname: '.swift',
})
// 开发模式下,需要生成 framework
if (fs.existsSync(swiftFile)) {
const compilerServer = getCompilerServer<SwiftCompilerServer>(
'uts-development-ios'
)
if (!compilerServer) {
throw `项目使用了uts插件,正在安装 uts iOS 运行扩展...`
}
let projectPath = process.env.UNI_INPUT_DIR
const isCli = isCliProject(projectPath)
if (isCli) {
projectPath = path.resolve(projectPath, '..')
}
const { id, is_uni_modules } = resolvePackage(filename)!
const { code, msg } = await compilerServer.compile({
projectPath,
isCli,
type: 1,
pluginName: id,
utsPath: resolveCompilerUtsPath(
process.env.UNI_INPUT_DIR,
is_uni_modules
),
swiftPath: resolveCompilerSwiftPath(
process.env.UNI_OUTPUT_DIR,
is_uni_modules
),
})
result.code = code
result.msg = msg
}
return result
}
function resolveCompilerUtsPath(projectPath: string, is_uni_modules: boolean) {
return path.resolve(projectPath, is_uni_modules ? 'uni_modules' : 'utssdk')
}
function resolveCompilerSwiftPath(outputDir: string, is_uni_modules: boolean) {
return path.resolve(outputDir, is_uni_modules ? 'uni_modules' : 'utssdk')
}
function isCliProject(projectPath: string) {
if (projectPath.endsWith('src')) {
return true
}
return false
}
async function compile(filename: string) {
if (!process.env.UNI_HBUILDERX_PLUGINS) {
......@@ -70,3 +143,14 @@ async function compile(filename: string) {
})
return result
}
interface SwiftCompilerServer {
compile(options: {
projectPath: string
isCli: boolean
type: 1 | 2 // 1 => uni_modules , 2 => utssdk
pluginName: string
utsPath: string
swiftPath: string
}): Promise<{ code: number; msg: string }>
}
......@@ -3,7 +3,12 @@ import fs from 'fs-extra'
import type { parse, bundle, UtsTarget } from '@dcloudio/uts'
import { camelize, capitalize } from '@vue/shared'
import { Module, ModuleItem } from '../types/types'
import { normalizePath, resolveSourceMapPath } from './shared'
import {
installHBuilderXPlugin,
normalizePath,
resolveSourceMapPath,
runByHBuilderX,
} from './shared'
export function resolveUTSSourceMapPath(_filename: string) {
return resolveSourceMapPath()
......@@ -26,11 +31,13 @@ export function resolvePackage(filename: string) {
? parts.findIndex((part) => part === 'uni_modules')
: parts.findIndex((part) => part === 'utssdk')
if (index > -1) {
const name = camelize(parts[index + 1])
const id = parts[index + 1]
const name = camelize(id)
return {
is_uni_modules: isUniModules,
id,
name,
namespace: 'UTSSDK' + (isUniModules ? 'Modules' : '') + capitalize(name),
is_uni_modules: isUniModules,
}
}
}
......@@ -163,3 +170,23 @@ export function createResolveTypeReferenceName(namespace: string, ast: Module) {
return name
}
}
export type CompilerServer = {}
export function getCompilerServer<T extends CompilerServer>(
pluginName: 'uts-development-ios' | 'uniapp-runextension'
): T | undefined {
const compilerServerPath = path.resolve(
process.env.UNI_HBUILDERX_PLUGINS,
`${pluginName}/out/external`
)
if (fs.existsSync(compilerServerPath)) {
// eslint-disable-next-line no-restricted-globals
return require(compilerServerPath)
} else {
if (runByHBuilderX()) {
installHBuilderXPlugin(pluginName)
} else {
console.error(compilerServerPath + ' is not found')
}
}
}
......@@ -37,10 +37,6 @@ export async function runDev(options: CliOptions & ServerOptions) {
event.result.close()
if (isFirstEnd) {
// 首次全量同步
// iOS 平台使用了 UTS
if (process.env.UNI_APP_IOS_UTS) {
console.log(`\u200B` + M['uts.ios.tips'] + `\u200B`)
}
process.env.UNI_APP_CHANGED_PAGES = ''
process.env.UNI_APP_CHANGED_FILES = ''
process.env.UNI_APP_CHANGED_DEX_FILES = ''
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册