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/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/KotlinLibraryBuilder.java b/test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java new file mode 100644 index 00000000000..47ccfbc539e --- /dev/null +++ b/test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java @@ -0,0 +1,62 @@ +/* + * 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 static com.facebook.buck.jvm.java.JavaCompilationConstants.DEFAULT_JAVAC_OPTIONS; + +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, DEFAULT_JAVAC_OPTIONS), + 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)); + } + +}