From 6fdc2ceb0f3f9d14684f3e3ded51d17730763e96 Mon Sep 17 00:00:00 2001 From: Gautam Korlam Date: Thu, 2 Mar 2017 14:30:46 -0800 Subject: [PATCH] Add kotlin rules support to buck project Summary: Closes https://github.com/facebook/buck/pull/1200 Reviewed By: krschultz, dsyang fbshipit-source-id: 14f9282 --- src/com/facebook/buck/cxx/BUCK.autodeps | 6 +- src/com/facebook/buck/file/BUCK.autodeps | 1 - src/com/facebook/buck/go/BUCK.autodeps | 1 - src/com/facebook/buck/js/BUCK.autodeps | 6 +- src/com/facebook/buck/jvm/java/BUCK.autodeps | 1 - .../buck/jvm/java/intellij/BUCK.autodeps | 1 + .../jvm/java/intellij/IjModuleFactory.java | 44 +++++++++++++- test/com/facebook/buck/android/BUCK.autodeps | 1 - test/com/facebook/buck/cxx/BUCK.autodeps | 1 + test/com/facebook/buck/eden/cli/BUCK.autodeps | 2 +- .../buck/jvm/java/intellij/BUCK.autodeps | 1 + .../java/intellij/IjModuleFactoryTest.java | 26 ++++++++ .../facebook/buck/jvm/kotlin/BUCK.autodeps | 11 +++- .../buck/jvm/kotlin/KotlinLibraryBuilder.java | 60 +++++++++++++++++++ test/com/facebook/buck/rules/BUCK.autodeps | 1 - 15 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java diff --git a/src/com/facebook/buck/cxx/BUCK.autodeps b/src/com/facebook/buck/cxx/BUCK.autodeps index 1df99a1c005..bad45a50ff6 100644 --- a/src/com/facebook/buck/cxx/BUCK.autodeps +++ b/src/com/facebook/buck/cxx/BUCK.autodeps @@ -54,7 +54,8 @@ "//src/com/facebook/buck/util:process_executor", "//src/com/facebook/buck/versions:types", "//third-party/java/immutables:immutables", - "//third-party/java/jackson:jackson-databind" + "//third-party/java/jackson:jackson-databind", + "//third-party/java/jsr:jsr305" ], "exported_deps" : [ "//src/com/facebook/buck/android:packageable", @@ -87,8 +88,7 @@ "//src/com/facebook/buck/versions:translator", "//src/com/facebook/buck/versions:versions", "//third-party/java/guava:guava", - "//third-party/java/infer-annotations:infer-annotations", - "//third-party/java/jsr:jsr305" + "//third-party/java/infer-annotations:infer-annotations" ] } } diff --git a/src/com/facebook/buck/file/BUCK.autodeps b/src/com/facebook/buck/file/BUCK.autodeps index 36182a6ef39..1053358e892 100644 --- a/src/com/facebook/buck/file/BUCK.autodeps +++ b/src/com/facebook/buck/file/BUCK.autodeps @@ -16,7 +16,6 @@ "//src/com/facebook/buck/event:event", "//src/com/facebook/buck/model:model", "//src/com/facebook/buck/rules:command_tool", - "//src/com/facebook/buck/rules/args:args", "//src/com/facebook/buck/step/fs:fs", "//src/com/facebook/buck/util:exceptions", "//src/com/facebook/buck/zip:steps" diff --git a/src/com/facebook/buck/go/BUCK.autodeps b/src/com/facebook/buck/go/BUCK.autodeps index d4e51e8d2b3..c2e30366dad 100644 --- a/src/com/facebook/buck/go/BUCK.autodeps +++ b/src/com/facebook/buck/go/BUCK.autodeps @@ -8,7 +8,6 @@ "//src/com/facebook/buck/io:executable-finder", "//src/com/facebook/buck/log:api", "//src/com/facebook/buck/rules:command_tool", - "//src/com/facebook/buck/rules/args:args", "//src/com/facebook/buck/step/fs:fs", "//src/com/facebook/buck/test/result/type:type", "//src/com/facebook/buck/util:exceptions", diff --git a/src/com/facebook/buck/js/BUCK.autodeps b/src/com/facebook/buck/js/BUCK.autodeps index 1209255ac85..ec734a0db4d 100644 --- a/src/com/facebook/buck/js/BUCK.autodeps +++ b/src/com/facebook/buck/js/BUCK.autodeps @@ -7,7 +7,8 @@ "//src/com/facebook/buck/shell:worker_pool_factory", "//src/com/facebook/buck/step/fs:fs", "//src/com/facebook/buck/util:exceptions", - "//src/com/facebook/buck/util:util" + "//src/com/facebook/buck/util:util", + "//third-party/java/jsr:jsr305" ], "exported_deps" : [ "//src/com/facebook/buck/android:packageable", @@ -22,8 +23,7 @@ "//src/com/facebook/buck/shell:worker_tool", "//src/com/facebook/buck/step:step", "//third-party/java/guava:guava", - "//third-party/java/infer-annotations:infer-annotations", - "//third-party/java/jsr:jsr305" + "//third-party/java/infer-annotations:infer-annotations" ] } } diff --git a/src/com/facebook/buck/jvm/java/BUCK.autodeps b/src/com/facebook/buck/jvm/java/BUCK.autodeps index 34acd34d7b5..0f94bd6f562 100644 --- a/src/com/facebook/buck/jvm/java/BUCK.autodeps +++ b/src/com/facebook/buck/jvm/java/BUCK.autodeps @@ -77,7 +77,6 @@ "//src/com/facebook/buck/maven:util", "//src/com/facebook/buck/model:build_id", "//src/com/facebook/buck/rules:command_tool", - "//src/com/facebook/buck/rules/args:args", "//src/com/facebook/buck/shell:steps", "//src/com/facebook/buck/step/fs:fs", "//src/com/facebook/buck/test/result/type:type", diff --git a/src/com/facebook/buck/jvm/java/intellij/BUCK.autodeps b/src/com/facebook/buck/jvm/java/intellij/BUCK.autodeps index 58194ba1b38..b4133d96189 100644 --- a/src/com/facebook/buck/jvm/java/intellij/BUCK.autodeps +++ b/src/com/facebook/buck/jvm/java/intellij/BUCK.autodeps @@ -11,6 +11,7 @@ "//src/com/facebook/buck/jvm/java:packagefinder", "//src/com/facebook/buck/jvm/java:rules", "//src/com/facebook/buck/jvm/java:support", + "//src/com/facebook/buck/jvm/kotlin:kotlin", "//src/com/facebook/buck/log:api", "//src/com/facebook/buck/shell:steps", "//src/com/facebook/buck/util:exceptions", diff --git a/src/com/facebook/buck/jvm/java/intellij/IjModuleFactory.java b/src/com/facebook/buck/jvm/java/intellij/IjModuleFactory.java index 1a5990a5186..af7c34d8f4c 100644 --- a/src/com/facebook/buck/jvm/java/intellij/IjModuleFactory.java +++ b/src/com/facebook/buck/jvm/java/intellij/IjModuleFactory.java @@ -29,6 +29,8 @@ import com.facebook.buck.jvm.java.JavaTestDescription; import com.facebook.buck.jvm.java.JavacOptions; import com.facebook.buck.jvm.java.JvmLibraryArg; +import com.facebook.buck.jvm.kotlin.KotlinLibraryDescription; +import com.facebook.buck.jvm.kotlin.KotlinTestDescription; import com.facebook.buck.model.BuildTarget; import com.facebook.buck.model.BuildTargets; import com.facebook.buck.rules.Description; @@ -72,7 +74,9 @@ public class IjModuleFactory { JavaTestDescription.class, RobolectricTestDescription.class, GroovyLibraryDescription.class, - GroovyTestDescription.class); + GroovyTestDescription.class, + KotlinLibraryDescription.class, + KotlinTestDescription.class); /** * Rule describing which aspects of the supplied {@link TargetNode} to transfer to the @@ -125,6 +129,8 @@ public IjModuleFactory( addToIndex(new RobolectricTestModuleRule()); addToIndex(new GroovyLibraryModuleRule()); addToIndex(new GroovyTestModuleRule()); + addToIndex(new KotlinLibraryModuleRule()); + addToIndex(new KotlinTestModuleRule()); this.moduleFactoryResolver = moduleFactoryResolver; @@ -677,6 +683,42 @@ public void apply(TargetNode target, ModuleBuildCont } } + private class KotlinLibraryModuleRule implements IjModuleRule { + + @Override + public Class> getDescriptionClass() { + return KotlinLibraryDescription.class; + } + + @Override + public void apply( + TargetNode target, + ModuleBuildContext context) { + addDepsAndSources( + target, + false /* wantsPackagePrefix */, + context); + } + } + + private class KotlinTestModuleRule implements IjModuleRule { + + @Override + public Class> getDescriptionClass() { + return KotlinTestDescription.class; + } + + @Override + public void apply( + TargetNode target, + ModuleBuildContext context) { + addDepsAndTestSources( + target, + false /* wantsPackagePrefix */, + context); + } + } + private class RobolectricTestModuleRule extends JavaTestModuleRule { @Override diff --git a/test/com/facebook/buck/android/BUCK.autodeps b/test/com/facebook/buck/android/BUCK.autodeps index e5b47e543e3..ea287eb1fb5 100644 --- a/test/com/facebook/buck/android/BUCK.autodeps +++ b/test/com/facebook/buck/android/BUCK.autodeps @@ -186,7 +186,6 @@ "//src/com/facebook/buck/rules:build_rule", "//src/com/facebook/buck/rules:rule_key", "//src/com/facebook/buck/rules:rules", - "//src/com/facebook/buck/rules/args:args", "//src/com/facebook/buck/rules/coercer:coercer", "//src/com/facebook/buck/rules/keys:keys", "//src/com/facebook/buck/rules/query:types", diff --git a/test/com/facebook/buck/cxx/BUCK.autodeps b/test/com/facebook/buck/cxx/BUCK.autodeps index caac366bea7..2ce42424f7d 100644 --- a/test/com/facebook/buck/cxx/BUCK.autodeps +++ b/test/com/facebook/buck/cxx/BUCK.autodeps @@ -38,6 +38,7 @@ "//src/com/facebook/buck/util:util", "//src/com/facebook/buck/util/cache:cache", "//src/com/facebook/buck/util/environment:platform", + "//src/com/facebook/buck/util/sha1:sha1", "//src/com/facebook/buck/versions:translator", "//src/com/facebook/buck/versions:types", "//src/com/facebook/buck/versions:versions", diff --git a/test/com/facebook/buck/eden/cli/BUCK.autodeps b/test/com/facebook/buck/eden/cli/BUCK.autodeps index be45acf359c..36c78fcd6de 100644 --- a/test/com/facebook/buck/eden/cli/BUCK.autodeps +++ b/test/com/facebook/buck/eden/cli/BUCK.autodeps @@ -1,11 +1,11 @@ { "cli" : { "deps" : [ - "//src/com/facebook/buck/eden:eden", "//src/com/facebook/buck/util/sha1:sha1", "//third-party/java/guava:guava" ], "exported_deps" : [ + "//src/com/facebook/buck/eden:eden", "//third-party/java/args4j:args4j", "//third-party/java/infer-annotations:infer-annotations" ] diff --git a/test/com/facebook/buck/jvm/java/intellij/BUCK.autodeps b/test/com/facebook/buck/jvm/java/intellij/BUCK.autodeps index 26f7fa82797..0ac2f392e3f 100644 --- a/test/com/facebook/buck/jvm/java/intellij/BUCK.autodeps +++ b/test/com/facebook/buck/jvm/java/intellij/BUCK.autodeps @@ -26,6 +26,7 @@ "//test/com/facebook/buck/jvm/java:fakepackagefinder", "//test/com/facebook/buck/jvm/java:javac-env", "//test/com/facebook/buck/jvm/java:testutil", + "//test/com/facebook/buck/jvm/kotlin:testutil", "//test/com/facebook/buck/model:testutil", "//test/com/facebook/buck/rules:testutil", "//test/com/facebook/buck/shell:testutil", diff --git a/test/com/facebook/buck/jvm/java/intellij/IjModuleFactoryTest.java b/test/com/facebook/buck/jvm/java/intellij/IjModuleFactoryTest.java index e69a18f77f0..c658f5cedbb 100644 --- a/test/com/facebook/buck/jvm/java/intellij/IjModuleFactoryTest.java +++ b/test/com/facebook/buck/jvm/java/intellij/IjModuleFactoryTest.java @@ -35,6 +35,7 @@ import com.facebook.buck.jvm.java.JavaLibraryBuilder; import com.facebook.buck.jvm.java.JavaTestBuilder; import com.facebook.buck.jvm.java.JvmLibraryArg; +import com.facebook.buck.jvm.kotlin.KotlinLibraryBuilder; import com.facebook.buck.model.BuildTarget; import com.facebook.buck.model.BuildTargetFactory; import com.facebook.buck.rules.BuildRuleResolver; @@ -337,6 +338,31 @@ public void testGroovyLibrary() { assertFalse(folder.getWantsPackagePrefix()); } + @Test + public void testKotlinLibrary() { + IjModuleFactory factory = createIjModuleFactory(); + + TargetNode kotlinLib = KotlinLibraryBuilder + .createBuilder(BuildTargetFactory.newInstance("//kotlin/com/example/base:base")) + .addSrc(Paths.get("kotlin/com/example/base/File.kt")) + .build(); + + Path moduleBasePath = Paths.get("kotlin/com/example/base"); + IjModule module = factory.createModule( + moduleBasePath, + ImmutableSet.>of(kotlinLib)); + + assertEquals(moduleBasePath, module.getModuleBasePath()); + assertFalse(module.getAndroidFacet().isPresent()); + assertEquals(1, module.getFolders().size()); + assertEquals(ImmutableSet.of(kotlinLib), module.getTargets()); + + IjFolder folder = module.getFolders().iterator().next(); + assertEquals(Paths.get("kotlin/com/example/base"), folder.getPath()); + assertFalse(folder instanceof TestFolder); + assertFalse(folder.getWantsPackagePrefix()); + } + @Test public void testJavaLibraryInRoot() { IjModuleFactory factory = createIjModuleFactory(); diff --git a/test/com/facebook/buck/jvm/kotlin/BUCK.autodeps b/test/com/facebook/buck/jvm/kotlin/BUCK.autodeps index 70b28495d3a..9423a8af76e 100644 --- a/test/com/facebook/buck/jvm/kotlin/BUCK.autodeps +++ b/test/com/facebook/buck/jvm/kotlin/BUCK.autodeps @@ -17,12 +17,19 @@ }, "testutil" : { "deps" : [ - "//src/com/facebook/buck/jvm/kotlin:kotlin", "//src/com/facebook/buck/util:exceptions", "//src/com/facebook/buck/util/environment:platform", "//test/com/facebook/buck/cli:FakeBuckConfig", + "//test/com/facebook/buck/testutil:testutil", "//third-party/java/junit:junit" ], - "exported_deps" : [ ] + "exported_deps" : [ + "//src/com/facebook/buck/io:io", + "//src/com/facebook/buck/jvm/kotlin:kotlin", + "//src/com/facebook/buck/model:model", + "//src/com/facebook/buck/rules:build_rule", + "//test/com/facebook/buck/rules:testutil", + "//third-party/java/guava:guava" + ] } } diff --git a/test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java b/test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java new file mode 100644 index 00000000000..d8bad5fa052 --- /dev/null +++ b/test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java @@ -0,0 +1,60 @@ +/* + * Copyright 2014-present Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.facebook.buck.jvm.kotlin; + +import com.facebook.buck.io.ProjectFilesystem; +import com.facebook.buck.model.BuildTarget; +import com.facebook.buck.rules.AbstractNodeBuilder; +import com.facebook.buck.rules.BuildRule; +import com.facebook.buck.rules.PathSourcePath; +import com.facebook.buck.rules.SourcePath; +import com.facebook.buck.testutil.FakeProjectFilesystem; +import com.google.common.hash.HashCode; + +import java.nio.file.Path; + +public class KotlinLibraryBuilder + extends AbstractNodeBuilder { + + private final ProjectFilesystem projectFilesystem; + + protected KotlinLibraryBuilder( + BuildTarget target, + ProjectFilesystem projectFilesystem, + HashCode hashCode) { + super( + new KotlinLibraryDescription(null, null), + target, + projectFilesystem, + hashCode); + this.projectFilesystem = projectFilesystem; + } + + public static KotlinLibraryBuilder createBuilder(BuildTarget target) { + return new KotlinLibraryBuilder(target, new FakeProjectFilesystem(), null); + } + + public KotlinLibraryBuilder addSrc(SourcePath path) { + arg.srcs = amend(arg.srcs, path); + return this; + } + + public KotlinLibraryBuilder addSrc(Path path) { + return addSrc(new PathSourcePath(projectFilesystem, path)); + } + +} diff --git a/test/com/facebook/buck/rules/BUCK.autodeps b/test/com/facebook/buck/rules/BUCK.autodeps index ff4d0bd6317..1cc1142ee19 100644 --- a/test/com/facebook/buck/rules/BUCK.autodeps +++ b/test/com/facebook/buck/rules/BUCK.autodeps @@ -31,7 +31,6 @@ "//src/com/facebook/buck/rules:rules", "//src/com/facebook/buck/rules:target_graph_hashing", "//src/com/facebook/buck/rules:types", - "//src/com/facebook/buck/rules/args:args", "//src/com/facebook/buck/rules/coercer:coercer", "//src/com/facebook/buck/rules/coercer:types", "//src/com/facebook/buck/rules/keys:keys",