提交 958bfdf0 编写于 作者: fxy060608's avatar fxy060608

wip(uts): compiler

上级 1e5c4850
import { existsSync, rmSync } from 'fs'
import { resolve } from 'path'
import { toKotlin, toSwift } from '../src/index'
const inputDir = resolve(__dirname, 'examples/demo')
const outputDir = resolve(
__dirname,
'examples/demo/unpackage/dist/dev/app-plus'
)
const pluginDir = resolve(inputDir, 'uni_modules/test-uts')
const outputPluginDir = resolve(outputDir, 'uni_modules/test-uts')
describe('compiler', () => {
test('toKotlin', async () => {
const relativeFile = 'utssdk/app-android/index'
const kotlinFile = resolve(outputPluginDir, relativeFile + '.kt')
if (existsSync(kotlinFile)) {
rmSync(kotlinFile)
}
await toKotlin(resolve(pluginDir, relativeFile + '.uts'), {
inputDir,
outputDir,
sourceMap: false,
})
expect(existsSync(kotlinFile)).toBeTruthy()
})
test('toSwift', async () => {
const relativeFile = 'utssdk/app-ios/index'
const swiftFile = resolve(outputPluginDir, relativeFile + '.swift')
if (existsSync(swiftFile)) {
rmSync(swiftFile)
}
await toSwift(resolve(pluginDir, relativeFile + '.uts'), {
inputDir,
outputDir,
sourceMap: false,
})
expect(existsSync(swiftFile)).toBeTruthy()
})
})
package uts.sdk.modules.testUts;
import kotlinx.coroutines.async;
import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Deferred;
import kotlinx.coroutines.Dispatchers;
import io.dcloud.uts.*;
fun test() {}
import DCUTSFoundation;
func test() {}
@objc(UTSSDKModulesTestUtsIndexSwift)
@objcMembers
class IndexSwift : NSObject {
public static func s_test() {
return test();
}
}
......@@ -20,6 +20,9 @@ export const sourcemap = {
export * from './sourceMap'
export { compile as toKotlin } from './kotlin'
export { compile as toSwift } from './swift'
export async function compile(module: string) {
const pkg = resolvePackage(module)
if (!pkg) {
......
......@@ -4,6 +4,7 @@ import path from 'path'
import AdmZip from 'adm-zip'
import { sync } from 'fast-glob'
import { isArray } from '@vue/shared'
import type { UtsResult } from '@dcloudio/uts'
import {
isInHBuilderX,
normalizePath,
......@@ -20,9 +21,9 @@ import {
resolvePackage,
resolveUTSPlatformFile,
resolveUTSSourceMapPath,
ToKotlinOptions,
} from './utils'
import { Module } from '../types/types'
import { UtsResult } from '@dcloudio/uts'
interface KotlinCompilerServer extends CompilerServer {
getKotlincHome(): string
......@@ -63,10 +64,12 @@ export async function runKotlinProd(filename: string) {
if (filename.includes('app-ios')) {
return
}
await compile(filename)
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
await compile(filename, { inputDir, outputDir, sourceMap: true })
genUTSPlatformResource(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
inputDir,
outputDir,
platform: 'app-android',
extname: '.kt',
})
......@@ -84,14 +87,20 @@ export async function runKotlinDev(
if (filename.includes('app-ios')) {
return
}
const result = (await compile(filename)) as RunKotlinDevResult
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
const result = (await compile(filename, {
inputDir,
outputDir,
sourceMap: true,
})) as RunKotlinDevResult
result.type = 'kotlin'
result.changed = []
const kotlinFile = resolveUTSPlatformFile(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
inputDir,
outputDir,
platform: 'app-android',
extname: '.kt',
})
......@@ -130,13 +139,10 @@ export async function runKotlinDev(
.concat(resDeps)
),
d8: resolveD8Args(jarFile),
sourceRoot: process.env.UNI_INPUT_DIR,
sourceMapPath: resolveSourceMapFile(
process.env.UNI_OUTPUT_DIR,
kotlinFile
),
sourceRoot: inputDir,
sourceMapPath: resolveSourceMapFile(outputDir, kotlinFile),
}
const res = await compile(options, process.env.UNI_INPUT_DIR)
const res = await compile(options, inputDir)
// console.log('dex compile time: ' + (Date.now() - time) + 'ms')
if (res) {
try {
......@@ -146,9 +152,7 @@ export async function runKotlinDev(
} catch (e) {}
const dexFile = resolveDexFile(jarFile)
if (fs.existsSync(dexFile)) {
result.changed = [
normalizePath(path.relative(process.env.UNI_OUTPUT_DIR, dexFile)),
]
result.changed = [normalizePath(path.relative(outputDir, dexFile))]
}
}
// else {
......@@ -275,13 +279,11 @@ const DEFAULT_IMPORTS = [
'io.dcloud.uts.*',
]
async function compile(filename: string) {
if (!process.env.UNI_HBUILDERX_PLUGINS) {
throw 'process.env.UNI_HBUILDERX_PLUGINS is not found'
}
export async function compile(
filename: string,
{ inputDir, outputDir, sourceMap }: ToKotlinOptions
) {
const { bundle, UtsTarget } = getUtsCompiler()
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
// let time = Date.now()
const imports = [...DEFAULT_IMPORTS]
const rClass = resolveAndroidResourceClass(filename)
......@@ -297,19 +299,20 @@ async function compile(filename: string) {
isPlugin: true,
outDir: outputDir,
package: parseKotlinPackage(filename).package,
sourceMap: resolveUTSSourceMapPath(filename),
sourceMap: sourceMap ? resolveUTSSourceMapPath(filename) : false,
extname: 'kt',
imports,
logFilename: true,
noColor: isInHBuilderX(),
},
})
moveRootIndexSourceMap(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
platform: 'app-android',
extname: '.kt',
})
sourceMap &&
moveRootIndexSourceMap(filename, {
inputDir,
outputDir,
platform: 'app-android',
extname: '.kt',
})
return result
}
......
......@@ -8,6 +8,7 @@ import {
resolvePackage,
resolveUTSPlatformFile,
resolveUTSSourceMapPath,
ToSwiftOptions,
} from './utils'
import { isInHBuilderX } from './shared'
import { UtsResult } from '@dcloudio/uts'
......@@ -32,10 +33,12 @@ export async function runSwiftProd(filename: string) {
if (filename.includes('app-android')) {
return
}
await compile(filename)
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
await compile(filename, { inputDir, outputDir, sourceMap: true })
genUTSPlatformResource(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
inputDir,
outputDir,
platform: 'app-ios',
extname: '.swift',
})
......@@ -72,20 +75,26 @@ export async function runSwiftDev(filename: string) {
return
}
}
const result = (await compile(filename)) as RunSwiftDevResult
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
const result = (await compile(filename, {
inputDir,
outputDir,
sourceMap: true,
})) as RunSwiftDevResult
result.type = 'swift'
const swiftFile = resolveUTSPlatformFile(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
inputDir,
outputDir,
platform: 'app-ios',
extname: '.swift',
})
result.changed = []
// 开发模式下,需要生成 framework
if (fs.existsSync(swiftFile)) {
let projectPath = process.env.UNI_INPUT_DIR
let projectPath = inputDir
const isCli = isCliProject(projectPath)
if (isCli) {
projectPath = path.resolve(projectPath, '..')
......@@ -96,14 +105,8 @@ export async function runSwiftDev(filename: string) {
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
),
utsPath: resolveCompilerUtsPath(inputDir, is_uni_modules),
swiftPath: resolveCompilerSwiftPath(outputDir, is_uni_modules),
})
result.code = code
result.msg = msg
......@@ -127,13 +130,11 @@ function isCliProject(projectPath: string) {
return false
}
async function compile(filename: string) {
if (!process.env.UNI_HBUILDERX_PLUGINS) {
throw 'process.env.UNI_HBUILDERX_PLUGINS is not found'
}
export async function compile(
filename: string,
{ inputDir, outputDir, sourceMap }: ToSwiftOptions
) {
const { bundle, UtsTarget } = getUtsCompiler()
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
// let time = Date.now()
const result = await bundle(UtsTarget.SWIFT, {
input: {
......@@ -144,19 +145,20 @@ async function compile(filename: string) {
isPlugin: true,
outDir: outputDir,
package: parseSwiftPackage(filename).namespace,
sourceMap: resolveUTSSourceMapPath(filename),
sourceMap: sourceMap ? resolveUTSSourceMapPath(filename) : false,
extname: 'swift',
imports: ['DCUTSFoundation'],
logFilename: true,
noColor: isInHBuilderX(),
},
})
moveRootIndexSourceMap(filename, {
inputDir: process.env.UNI_INPUT_DIR,
outputDir: process.env.UNI_OUTPUT_DIR,
platform: 'app-ios',
extname: '.swift',
})
sourceMap &&
moveRootIndexSourceMap(filename, {
inputDir,
outputDir,
platform: 'app-ios',
extname: '.swift',
})
return result
}
......
......@@ -10,6 +10,14 @@ import {
runByHBuilderX,
} from './shared'
interface ToOptions {
inputDir: string
outputDir: string
sourceMap: boolean
}
export type ToKotlinOptions = ToOptions
export type ToSwiftOptions = ToOptions
export function resolveUTSSourceMapPath(_filename: string) {
return resolveSourceMapPath()
}
......@@ -84,7 +92,7 @@ export function genUTSPlatformResource(
export function moveRootIndexSourceMap(
filename: string,
{ inputDir, outputDir, platform, extname }: UTSPlatformResourceOptions
{ inputDir, platform, extname }: UTSPlatformResourceOptions
) {
if (isRootIndex(filename, platform)) {
const sourceMapFilename = path
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册