[Gradle, JS] Migrate onto destinationDirectory and outputName

This commit is contained in:
Ilya Goncharov
2022-07-27 21:03:05 +02:00
committed by teamcity
parent cd5b9d6356
commit 642fae1a5b
10 changed files with 150 additions and 80 deletions
@@ -238,9 +238,15 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
if (!checkKotlinPackageUsage(environmentForJS.configuration, sourcesFiles)) return ExitCode.COMPILATION_ERROR
val outputFilePath = arguments.outputFile
if (outputFilePath == null) {
messageCollector.report(ERROR, "IR: Specify output file via -output", null)
val outputDirPath = arguments.outputDir
val outputName = arguments.outputName
if (outputDirPath == null) {
messageCollector.report(ERROR, "IR: Specify output dir via -Xir-output-dir", null)
return ExitCode.COMPILATION_ERROR
}
if (outputName == null) {
messageCollector.report(ERROR, "IR: Specify output name via -Xir-output-name", null)
return ExitCode.COMPILATION_ERROR
}
@@ -257,12 +263,11 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
reportCompiledSourcesList(messageCollector, sourcesFiles)
}
val outputFile = File(outputFilePath)
val moduleName = arguments.irModuleName ?: FileUtil.getNameWithoutExtension(outputFile)
val moduleName = arguments.irModuleName ?: outputName
configurationJs.put(CommonConfigurationKeys.MODULE_NAME, moduleName)
val outputDir: File = outputFile.parentFile ?: outputFile.absoluteFile.parentFile!!
val outputDir = File(outputDirPath)
try {
configurationJs.put(JSConfigurationKeys.OUTPUT_DIR, outputDir.canonicalFile)
} catch (e: IOException) {
@@ -279,7 +284,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
val icCaches = if (!arguments.wasm && cacheDirectories.isNotEmpty()) {
messageCollector.report(INFO, "")
messageCollector.report(INFO, "Building cache:")
messageCollector.report(INFO, "to: ${outputFilePath}")
messageCollector.report(INFO, "to: ${outputDir}")
messageCollector.report(INFO, arguments.cacheDirectories ?: "")
messageCollector.report(INFO, libraries.toString())
@@ -347,21 +352,21 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
}
if (arguments.irProduceKlibDir || arguments.irProduceKlibFile) {
if (arguments.irProduceKlibFile) {
require(outputFile.extension == KLIB_FILE_EXTENSION) { "Please set up .klib file as output" }
}
generateKLib(
sourceModule,
irFactory = IrFactoryImpl,
outputKlibPath = outputFile.path,
outputKlibPath = if (arguments.irProduceKlibFile)
outputDir.resolve("$outputName.klib").normalize().absolutePath
else
outputDirPath,
nopack = arguments.irProduceKlibDir,
jsOutputName = arguments.irPerModuleOutputName,
)
}
if (arguments.irProduceJs) {
messageCollector.report(INFO, "Produce executable: $outputFilePath")
messageCollector.report(INFO, "Produce executable: $outputDirPath")
messageCollector.report(INFO, arguments.cacheDirectories ?: "")
if (icCaches.isNotEmpty()) {
@@ -383,10 +388,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
}
)
outputFile.write(outputs)
outputs.dependencies.forEach { (name, content) ->
outputFile.resolveSibling("$name.js").write(content)
}
outputs.write(outputDir, outputName)
messageCollector.report(INFO, "Executable production duration (IC): ${System.currentTimeMillis() - beforeIc2Js}ms")
@@ -416,6 +418,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
if (arguments.wasm) {
val outputFile = File(arguments.outputFile)
val (allModules, backendContext) = compileToLoweredIr(
depsDescriptors = module,
phaseConfig = PhaseConfig(wasmPhases),
@@ -455,10 +458,8 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
messageCollector.report(INFO, "Executable production duration: ${System.currentTimeMillis() - start}ms")
outputFile.write(outputs)
outputs.dependencies.forEach { (name, content) ->
outputFile.resolveSibling("$name.js").write(content)
}
outputs.write(outputDir, outputName)
if (arguments.generateDts) {
val dtsFile = outputFile.withReplacedExtensionOrNull(outputFile.extension, "d.ts")!!
dtsFile.writeText(tsDefinitions)
@@ -481,6 +482,18 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
return OK
}
private fun CompilationOutputs.write(outputDir: File, outputName: String) {
val outputFile = outputDir.resolve("$outputName.js")
outputFile.parentFile.mkdirs()
outputFile.write(this)
dependencies.forEach { (name, content) ->
outputDir.resolve("$name.js").let {
it.parentFile.mkdirs()
it.write(content)
}
}
}
private fun File.write(outputs: CompilationOutputs) {
writeText(outputs.jsCode)
outputs.writeSourceMapIfPresent(this)