Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Add kotlin rules support to buck project
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Korlam committed Feb 24, 2017
1 parent 69f6975 commit 8fcd329
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/com/facebook/buck/jvm/java/intellij/IjModuleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,6 +129,8 @@ public IjModuleFactory(
addToIndex(new RobolectricTestModuleRule());
addToIndex(new GroovyLibraryModuleRule());
addToIndex(new GroovyTestModuleRule());
addToIndex(new KotlinLibraryModuleRule());
addToIndex(new KotlinTestModuleRule());

this.moduleFactoryResolver = moduleFactoryResolver;

Expand Down Expand Up @@ -677,6 +683,42 @@ public void apply(TargetNode<JavaTestDescription.Arg, ?> target, ModuleBuildCont
}
}

private class KotlinLibraryModuleRule implements IjModuleRule<KotlinLibraryDescription.Arg> {

@Override
public Class<? extends Description<?>> getDescriptionClass() {
return KotlinLibraryDescription.class;
}

@Override
public void apply(
TargetNode<KotlinLibraryDescription.Arg, ?> target,
ModuleBuildContext context) {
addDepsAndSources(
target,
false /* wantsPackagePrefix */,
context);
}
}

private class KotlinTestModuleRule implements IjModuleRule<KotlinTestDescription.Arg> {

@Override
public Class<? extends Description<?>> getDescriptionClass() {
return KotlinTestDescription.class;
}

@Override
public void apply(
TargetNode<KotlinTestDescription.Arg, ?> target,
ModuleBuildContext context) {
addDepsAndTestSources(
target,
false /* wantsPackagePrefix */,
context);
}
}

private class RobolectricTestModuleRule extends JavaTestModuleRule {

@Override
Expand Down
26 changes: 26 additions & 0 deletions test/com/facebook/buck/jvm/java/intellij/IjModuleFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.<TargetNode<?, ?>>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();
Expand Down
62 changes: 62 additions & 0 deletions test/com/facebook/buck/jvm/kotlin/KotlinLibraryBuilder.java
Original file line number Diff line number Diff line change
@@ -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<KotlinLibraryDescription.Arg, KotlinLibraryDescription, BuildRule> {

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));
}

}

0 comments on commit 8fcd329

Please sign in to comment.