Skip to content

Commit

Permalink
Ensure js has a clean working area.
Browse files Browse the repository at this point in the history
Revert intellij overenthusiasm.
  • Loading branch information
Corbin Smith committed Apr 3, 2020
1 parent 12e7b25 commit 8391491
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
26 changes: 20 additions & 6 deletions src/main/kotlin/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,8 @@ import io.bazel.kotlin.model.Platform
import io.bazel.kotlin.model.RuleKind
import java.io.PrintStream
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.util.regex.Pattern
import javax.inject.Inject
Expand Down Expand Up @@ -115,7 +115,7 @@ class KotlinBuilder @Inject internal constructor(
@Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA")
when (context.info.platform) {
Platform.JVM -> executeJvmTask(context, workingDir, argMap)
Platform.JS -> executeJsTask(context, argMap)
Platform.JS -> executeJsTask(context, workingDir, argMap)
Platform.UNRECOGNIZED -> throw IllegalStateException(
"unrecognized platform: ${context.info}")
}
Expand Down Expand Up @@ -170,15 +170,29 @@ class KotlinBuilder @Inject internal constructor(
this
}

private fun executeJsTask(context: CompilationTaskContext, argMap: ArgMap) =
buildJsTask(context.info, argMap).let { jsTask ->
private fun executeJsTask(
context: CompilationTaskContext,
workingDir: Path,
argMap: ArgMap
) =
buildJsTask(context.info, workingDir, argMap).let { jsTask ->
context.whenTracing { printProto("js task input", jsTask) }
jsTaskExecutor.execute(context, jsTask)
}

private fun buildJsTask(info: CompilationTaskInfo, argMap: ArgMap): JsCompilationTask =
private fun buildJsTask(
info: CompilationTaskInfo,
workingDir: Path,
argMap: ArgMap
): JsCompilationTask =
with(JsCompilationTask.newBuilder()) {
this.info = info

with(directoriesBuilder)
{
temp = workingDir.toString()
}

with(inputsBuilder) {
addAllLibraries(argMap.mandatory(KotlinBuilderFlags.JS_LIBRARIES))
addAllKotlinSources(argMap.mandatory(JavaBuilderFlags.SOURCES))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ import io.bazel.kotlin.builder.utils.jars.SourceJarCreator
import io.bazel.kotlin.builder.utils.resolveTwinVerified
import io.bazel.kotlin.model.JsCompilationTask
import java.io.FileOutputStream
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class Kotlin2JsTaskExecutor @Inject constructor(
private val invoker: KotlinToolchain.K2JSCompilerInvoker
) {
fun execute(context: CompilationTaskContext, task: JsCompilationTask) {
class Kotlin2JsTaskExecutor @Inject constructor(private val invoker: KotlinToolchain.K2JSCompilerInvoker) {

private val fileSystem: FileSystem = FileSystems.getDefault()

fun execute(
context: CompilationTaskContext,
task: JsCompilationTask
) {
task.compile(context)

val jsPath = Paths.get(task.outputs.js)
val jsPath = fileSystem.getPath(task.outputs.js)
val jsMetaFile = jsPath.resolveTwinVerified(".meta.js")
val jsDirectory = jsPath.parent.resolve(jsPath.toFile().nameWithoutExtension)
task.createJar(jsDirectory, listOf(jsPath, jsPath.resolveTwinVerified(".js.map"), jsMetaFile))
val jsDirectory = Files.createDirectories(
fileSystem.getPath(task.directories.temp)
.resolve(jsPath.toFile().nameWithoutExtension)
)
task.createJar(jsDirectory,
listOf(jsPath, jsPath.resolveTwinVerified(".js.map"), jsMetaFile))
// this mutates the jsPath file , so do it after creating the jar.
appendMetaToPrimary(jsPath, jsMetaFile)
task.createSourceJar()
Expand All @@ -45,8 +55,12 @@ class Kotlin2JsTaskExecutor @Inject constructor(
private fun JsCompilationTask.createSourceJar() {
try {
SourceJarCreator(Paths.get(outputs.srcjar), false).also { creator ->
creator.addSources(inputs.kotlinSourcesList.map { Paths.get(it) }.stream())
}.execute()
creator.addSources(
inputs.kotlinSourcesList.map { Paths.get(it) }
.stream()
)
}
.execute()
} catch (ex: Throwable) {
throw CompilationException("could not create source jar", ex)
}
Expand All @@ -56,15 +70,21 @@ class Kotlin2JsTaskExecutor @Inject constructor(
* Append the meta file to the JS file. This is an accepted pattern, and it allows us to not have to export the
* meta.js file with the js.
*/
private fun appendMetaToPrimary(jsPath: Path, jsMetaFile: Path) {
private fun appendMetaToPrimary(
jsPath: Path,
jsMetaFile: Path
) {
try {
FileOutputStream(jsPath.toFile(), true).use { Files.copy(jsMetaFile, it) }
} catch (ex: Throwable) {
throw CompilationException("could not normalize js file", ex)
}
}

private fun JsCompilationTask.createJar(jsDirectoryPath: Path, rootEntries: List<Path>) {
private fun JsCompilationTask.createJar(
jsDirectoryPath: Path,
rootEntries: List<Path>
) {
try {
val outputJarPath = Paths.get(outputs.jar)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/bazel/kotlin/builder/utils/IOUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
9 changes: 9 additions & 0 deletions src/main/protobuf/kotlin_model.proto
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ message JvmCompilationTask {
}

message JsCompilationTask {

// Directories used by the builder.
message Directories {
// A temp directory that the compiler may use.
string temp = 4;
}

message Outputs {
// The primary output
string js = 1;
Expand All @@ -150,4 +157,6 @@ message JsCompilationTask {

// flags that should be passed through straight to the kotlinc-js compiler.
repeated string pass_through_flags = 5;

Directories directories = 6;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Bazel Authors. All rights reserved.
* Copyright 2018 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 8391491

Please sign in to comment.