compiler.ts 3.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import os from 'os'
import fs from 'fs'
fxy060608's avatar
fxy060608 已提交
3
import path from 'path'
fxy060608's avatar
fxy060608 已提交
4 5 6 7
import execa from 'execa'
import { once } from '@dcloudio/uni-shared'
import type { parse, bundle, UtsTarget } from '@dcloudio/uts'

fxy060608's avatar
fxy060608 已提交
8 9
export function getUtsCompiler(): {
  parse: typeof parse
fxy060608's avatar
fxy060608 已提交
10
  bundle: typeof bundle
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15
  UtsTarget: typeof UtsTarget
} {
  // eslint-disable-next-line no-restricted-globals
  return require('@dcloudio/uts')
}
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24 25
export async function compile(filename: string) {
  if (!process.env.UNI_HBUILDERX_PLUGINS) {
    return
  }
  const { bundle, UtsTarget } = getUtsCompiler()
  const inputDir = process.env.UNI_INPUT_DIR
  const outputDir = process.env.UNI_OUTPUT_DIR
  let time = Date.now()
  await bundle({
    target: UtsTarget.KOTLIN,
fxy060608's avatar
fxy060608 已提交
26
    input: {
fxy060608's avatar
fxy060608 已提交
27 28
      root: inputDir,
      filename,
fxy060608's avatar
fxy060608 已提交
29 30
    },
    output: {
fxy060608's avatar
fxy060608 已提交
31
      outDir: outputDir,
fxy060608's avatar
fxy060608 已提交
32
      sourceMap: true,
fxy060608's avatar
fxy060608 已提交
33
      extname: 'kt',
fxy060608's avatar
fxy060608 已提交
34 35
    },
  })
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
  console.log('uts compile time: ' + (Date.now() - time) + 'ms')
  const kotlinFile = resolveKotlinFile(filename, inputDir, outputDir)
  if (fs.existsSync(kotlinFile)) {
    time = Date.now()
    await compileKotlin(kotlinFile)
    console.log('kotlin compile time: ' + (Date.now() - time) + 'ms')
    const jarFile = resolveJarPath(kotlinFile)
    if (fs.existsSync(jarFile)) {
      time = Date.now()
      await d8(jarFile)
      console.log('d8 compile time: ' + (Date.now() - time) + 'ms')
    }
  }
fxy060608's avatar
fxy060608 已提交
49
}
fxy060608's avatar
fxy060608 已提交
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

function resolveKotlinFile(
  filename: string,
  inputDir: string,
  outputDir: string
) {
  return path
    .resolve(outputDir, path.relative(inputDir, filename))
    .replace(path.extname(filename), '.kt')
}

function resolveDirs(): { kotlinc: string; d8: string; lib: string } {
  // eslint-disable-next-line no-restricted-globals
  return require(path.resolve(
    process.env.UNI_HBUILDERX_PLUGINS,
    'uts-kotlin-compiler'
  ))
}

const resolveKotlinc = once(() => {
  const { kotlinc } = resolveDirs()
  return path.resolve(
    kotlinc,
    'bin',
    'kotlinc' + (os.platform() === 'win32' ? '.bat' : '')
  )
})

async function compileKotlin(filename: string) {
  const kotlinc = resolveKotlinc()
  await execa(
    kotlinc,
    [filename, '-cp', resolveClassPath(), '-d', resolveJarPath(filename)],
    {
      stdio: 'inherit',
    }
  )
}

async function d8(filename: string) {
  const java = resolveJavaPath()
  const d8 = resolveD8Path()
  await execa(
    java,
    [
      '-cp',
      d8,
      'com.android.tools.r8.D8',
      filename,
      '--min-api',
      '19',
      '--output',
      resolveDexPath(filename),
    ],
    {
      stdio: 'inherit',
    }
  )
}

function resolveDexPath(filename: string) {
  return path.dirname(filename)
}

function resolveJarPath(filename: string) {
  return filename.replace(path.extname(filename), '.jar')
}

const resolveBuiltInClassPath = once(() => {
  const libDir = resolveDirs().lib
  return fs
    .readdirSync(libDir)
    .filter((file) => file.endsWith('.jar'))
    .map((file) => path.resolve(libDir, file))
})

function resolveClassPath() {
  return resolveBuiltInClassPath().join(os.platform() === 'win32' ? ';' : ':')
}

const resolveJavaPath = once(() => {
  return path.resolve(
    process.env.UNI_HBUILDERX_PLUGINS,
    'amazon-corretto',
    'bin/java'
  )
})

const resolveD8Path = once(() => {
  const { d8 } = resolveDirs()
  return path.resolve(d8, 'd8.jar')
})