Skip to content

Commit

Permalink
Migrate to kotlin in memory compiler support in buck
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Korlam committed May 26, 2017
1 parent b29730d commit b7a43db
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public final class DotBuckConfigLocalFile extends BuckConfigFile {
private final String target;
private final List<String> ignore;
private final String groovyHome;
private final String kotlinCompiler;
private final String kotlinRuntime;
private final String kotlinHome;
private final String proguardJar;
private final Set<String> defs;

Expand All @@ -25,17 +24,15 @@ public DotBuckConfigLocalFile(
String target,
List<String> ignore,
String groovyHome,
String kotlinCompiler,
String kotlinRuntime,
String kotlinHome,
String proguardJar,
Set<String> defs) {
this.aliases = aliases;
this.buildToolVersion = buildToolVersion;
this.target = target;
this.ignore = ignore;
this.groovyHome = groovyHome;
this.kotlinCompiler = kotlinCompiler;
this.kotlinRuntime = kotlinRuntime;
this.kotlinHome = kotlinHome;
this.proguardJar = proguardJar;
this.defs = defs;
}
Expand Down Expand Up @@ -67,10 +64,9 @@ public final void print(PrintStream printer) {
printer.println();
}

if (!StringUtils.isEmpty(kotlinCompiler) && !StringUtils.isEmpty(kotlinRuntime)) {
if (!StringUtils.isEmpty(kotlinHome)) {
printer.println("[kotlin]");
printer.println("\tcompiler = " + kotlinCompiler);
printer.println("\truntime_jar = " + kotlinRuntime);
printer.println("\tkotlin_home = " + kotlinHome);
printer.println();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ final class DotBuckConfigLocalGenerator {
*/
static DotBuckConfigLocalFile generate(OkBuckExtension okbuck,
String groovyHome,
String kotlinCompiler,
String kotlinRuntime,
String kotlinHome,
String proguardJar,
Set<String> defs) {
Map<String, String> aliases = [:]
Expand All @@ -37,8 +36,7 @@ final class DotBuckConfigLocalGenerator {
okbuck.target,
[".git", "**/.svn"],
groovyHome,
kotlinCompiler,
kotlinRuntime,
kotlinHome,
proguardJar,
defs)
}
Expand Down
12 changes: 4 additions & 8 deletions buildSrc/src/main/java/com/uber/okbuck/core/task/OkBuckTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ void okbuck() {

// Fetch Kotlin support deps if needed
boolean hasKotlinLib = okBuckExtension.buckProjects.parallelStream().anyMatch(project -> ProjectUtil.getType(project) == ProjectType.KOTLIN_LIB);
Pair<String, String> kotlinDeps = null;
if (hasKotlinLib) {
kotlinDeps = KotlinUtil.setupKotlinHome(getProject());
KotlinUtil.setupKotlinHome(getProject());
}

generate(okBuckExtension,
hasGroovyLib ? GroovyUtil.GROOVY_HOME_LOCATION : null,
kotlinDeps != null ? kotlinDeps.getLeft(): null,
kotlinDeps != null ? kotlinDeps.getRight(): null);
hasKotlinLib ? KotlinUtil.KOTLIN_HOME_LOCATION : null);
}

@Override public String getGroup() {
Expand All @@ -78,8 +76,7 @@ public File dotBuckConfigLocal() {
return getProject().file(".buckconfig.local");
}

private void generate(OkBuckExtension okbuckExt, String groovyHome,
String kotlinCompiler, String KotlinRuntime) {
private void generate(OkBuckExtension okbuckExt, String groovyHome, String kotlinHome) {
// generate empty .buckconfig if it does not exist
if (!dotBuckConfig().exists()) {
try {
Expand All @@ -101,8 +98,7 @@ private void generate(OkBuckExtension okbuckExt, String groovyHome,
PrintStream configPrinter = new PrintStream(dotBuckConfigLocal())) {
DotBuckConfigLocalGenerator.generate(okbuckExt,
groovyHome,
kotlinCompiler,
KotlinRuntime,
kotlinHome,
ProguardUtil.getProguardJarPath(getProject()),
defs).print(configPrinter);
} catch (IOException e) {
Expand Down
31 changes: 7 additions & 24 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/KotlinUtil.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package com.uber.okbuck.core.util;

import com.google.common.collect.ImmutableMap;
import com.uber.okbuck.OkBuckGradlePlugin;
import com.uber.okbuck.core.dependency.DependencyCache;

import org.apache.commons.lang3.tuple.Pair;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Collections;

public final class KotlinUtil {

private static final String KOTLIN_DEPS_CONFIG = "okbuck_kotlin_deps";
private static final String KOTLIN_GROUP = "org.jetbrains.kotlin";
private static final String KOTLIN_COMPILER_MODULE = "kotlin-compiler-embeddable";
private static final String KOTLIN_RUNTIME_MODULE = "kotlin-runtime";
private static final String KOTLIN_HOME_LOCATION = OkBuckGradlePlugin.DEFAULT_CACHE_PATH + "/kotlin_installation";
private static final String KOTLIN_COMPILER_MODULE = "kotlin-compiler";
private static final String KOTLIN_STDLIB_MODULE = "kotlin-stdlib";
public static final String KOTLIN_HOME_LOCATION = OkBuckGradlePlugin.DEFAULT_CACHE_PATH + "/kotlin_home";

private KotlinUtil() {}

@Nullable
public static String getKotlinVersion(Project project) {
static String getKotlinVersion(Project project) {
return project.getBuildscript()
.getConfigurations()
.getByName("classpath")
Expand All @@ -44,31 +40,18 @@ public static String getKotlinVersion(Project project) {
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static Pair<String, String> setupKotlinHome(Project rootProject) {
public static void setupKotlinHome(Project rootProject) {
String kotlinVersion = getKotlinVersion(rootProject);

Configuration kotlinConfig = rootProject.getConfigurations().maybeCreate(KOTLIN_DEPS_CONFIG);
DependencyHandler handler = rootProject.getDependencies();
handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_COMPILER_MODULE, kotlinVersion));
handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_RUNTIME_MODULE, kotlinVersion));
handler.add(KOTLIN_DEPS_CONFIG, String.format("%s:%s:%s", KOTLIN_GROUP, KOTLIN_STDLIB_MODULE, kotlinVersion));

new DependencyCache("kotlin",
rootProject,
KOTLIN_HOME_LOCATION + "/lib",
KOTLIN_HOME_LOCATION,
Collections.singleton(kotlinConfig),
null);

File kotlinHome = new File(KOTLIN_HOME_LOCATION);

File kotlinc = new File(kotlinHome, "bin/kotlinc");
FileUtil.copyResourceToProject("kotlin/bin/kotlinc",
new File(kotlinHome, "bin/kotlinc"),
ImmutableMap.of("template-kotlin-version", kotlinVersion));
kotlinc.setExecutable(true);

String kotlinCompiler = rootProject.relativePath(kotlinc);
String kotlinRuntime = String.format("%s/lib/%s-%s.jar", KOTLIN_HOME_LOCATION, KOTLIN_RUNTIME_MODULE,
kotlinVersion);
return Pair.of(kotlinCompiler, kotlinRuntime);
}
}

This file was deleted.

0 comments on commit b7a43db

Please sign in to comment.