Skip to content

Commit

Permalink
Add Kotlin support
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur McGibbon committed Aug 12, 2024
1 parent cd93aa3 commit ca10fcc
Show file tree
Hide file tree
Showing 17 changed files with 760 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

package com.microsoft.java.bs.gradle.model;

import java.util.List;

/**
* The extension model for Kotlin language.
*/
public interface KotlinExtension extends LanguageExtension {

String getKotlinLanguageVersion();

String getKotlinApiVersion();

List<String> getKotlincOptions();

List<String> getKotlinAssociates();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public interface LanguageExtension extends Serializable {
*/
boolean isScalaExtension();

/**
* Checks if the implementing class is a {@link ScalaExtension}.
*
* @return true if the extension is for Scala, false otherwise.
*/
boolean isKotlinExtension();

/**
* Attempts to cast the current object to a {@link JavaExtension} instance.
* <p>
Expand All @@ -80,4 +87,17 @@ public interface LanguageExtension extends Serializable {
* or null if the cast fails.
*/
ScalaExtension getAsScalaExtension();

/**
* Attempts to cast the current object to a {@link KotlinExtension} instance.
* <p>
* This method should ideally be used only when the implementing class
* is known to be a {@link KotlinExtension}.
* </p>
*
* @return the current object cast to a {@link KotlinExtension} instance,
* or null if the cast fails.
*/
KotlinExtension getAsKotlinExtension();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.java.bs.gradle.model;

import com.microsoft.java.bs.gradle.model.impl.DefaultJavaLanguage;
import com.microsoft.java.bs.gradle.model.impl.DefaultKotlinLanguage;
import com.microsoft.java.bs.gradle.model.impl.DefaultScalaLanguage;

import java.util.LinkedList;
Expand All @@ -16,6 +17,7 @@
public class SupportedLanguages {
public static final DefaultJavaLanguage JAVA = new DefaultJavaLanguage();
public static final DefaultScalaLanguage SCALA = new DefaultScalaLanguage();
public static final DefaultKotlinLanguage KOTLIN = new DefaultKotlinLanguage();

public static final List<SupportedLanguage<?>> all;
public static final List<String> allBspNames;
Expand All @@ -24,6 +26,7 @@ public class SupportedLanguages {
all = new LinkedList<>();
all.add(JAVA);
all.add(SCALA);
all.add(KOTLIN);
allBspNames = all.stream().map(SupportedLanguage::getBspName).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ private LanguageExtension convertLanguageExtension(LanguageExtension object) {
return object.getAsScalaExtension();
}

if (object.isKotlinExtension()) {
return object.getAsKotlinExtension();
}

throw new IllegalArgumentException("No conversion methods defined for object: " + object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.java.bs.gradle.model.impl;

import com.microsoft.java.bs.gradle.model.JavaExtension;
import com.microsoft.java.bs.gradle.model.KotlinExtension;
import com.microsoft.java.bs.gradle.model.ScalaExtension;

import java.io.File;
Expand Down Expand Up @@ -155,6 +156,11 @@ public boolean isScalaExtension() {
return false;
}

@Override
public boolean isKotlinExtension() {
return false;
}

@Override
public JavaExtension getAsJavaExtension() {
return this;
Expand All @@ -164,4 +170,9 @@ public JavaExtension getAsJavaExtension() {
public ScalaExtension getAsScalaExtension() {
return null;
}

@Override
public KotlinExtension getAsKotlinExtension() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

package com.microsoft.java.bs.gradle.model.impl;

import com.microsoft.java.bs.gradle.model.JavaExtension;
import com.microsoft.java.bs.gradle.model.KotlinExtension;
import com.microsoft.java.bs.gradle.model.ScalaExtension;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* Default implementation of {@link KotlinExtension}.
*/
public class DefaultKotlinExtension implements KotlinExtension {
private static final long serialVersionUID = 1L;

private Set<File> sourceDirs;

private Set<File> generatedSourceDirs;

private String compileTaskName;

private File classesDir;

private String kotlinLanguageVersion;

private String kotlinApiVersion;

private List<String> kotlincOptions;

private List<String> kotlinAssociates;

@Override
public Set<File> getSourceDirs() {
return sourceDirs;
}

public void setSourceDirs(Set<File> sourceDirs) {
this.sourceDirs = sourceDirs;
}

@Override
public Set<File> getGeneratedSourceDirs() {
return generatedSourceDirs;
}

public void setGeneratedSourceDirs(Set<File> generatedSourceDirs) {
this.generatedSourceDirs = generatedSourceDirs;
}

@Override
public String getCompileTaskName() {
return compileTaskName;
}

public void setCompileTaskName(String compileTaskName) {
this.compileTaskName = compileTaskName;
}

@Override
public File getClassesDir() {
return classesDir;
}

public void setClassesDir(File classesDir) {
this.classesDir = classesDir;
}

@Override
public String getKotlinLanguageVersion() {
return kotlinLanguageVersion;
}

public void setKotlinLanguageVersion(String kotlinLanguageVersion) {
this.kotlinLanguageVersion = kotlinLanguageVersion;
}

@Override
public String getKotlinApiVersion() {
return kotlinApiVersion;
}

public void setKotlinApiVersion(String kotlinApiVersion) {
this.kotlinApiVersion = kotlinApiVersion;
}

@Override
public List<String> getKotlincOptions() {
return kotlincOptions;
}

public void setKotlincOptions(List<String> kotlincOptions) {
this.kotlincOptions = kotlincOptions;
}

@Override
public List<String> getKotlinAssociates() {
return kotlinAssociates;
}

public void setKotlinAssociates(List<String> kotlinAssociates) {
this.kotlinAssociates = kotlinAssociates;
}

@Override
public int hashCode() {
return Objects.hash(sourceDirs, generatedSourceDirs, compileTaskName, classesDir,
kotlinLanguageVersion, kotlinApiVersion, kotlincOptions, kotlinAssociates);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
DefaultKotlinExtension other = (DefaultKotlinExtension) obj;
return Objects.equals(sourceDirs, other.sourceDirs)
&& Objects.equals(generatedSourceDirs, other.generatedSourceDirs)
&& Objects.equals(compileTaskName, other.compileTaskName)
&& Objects.equals(classesDir, other.classesDir)
&& Objects.equals(kotlinLanguageVersion, other.kotlinLanguageVersion)
&& Objects.equals(kotlinApiVersion, other.kotlinApiVersion)
&& Objects.equals(kotlincOptions, other.kotlincOptions)
&& Objects.equals(kotlinAssociates, other.kotlinAssociates);
}

@Override
public boolean isJavaExtension() {
return false;
}

@Override
public boolean isScalaExtension() {
return false;
}

@Override
public boolean isKotlinExtension() {
return true;
}

@Override
public JavaExtension getAsJavaExtension() {
return null;
}

@Override
public ScalaExtension getAsScalaExtension() {
return null;
}

@Override
public KotlinExtension getAsKotlinExtension() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

package com.microsoft.java.bs.gradle.model.impl;

import com.microsoft.java.bs.gradle.model.KotlinExtension;
import com.microsoft.java.bs.gradle.model.LanguageExtension;
import com.microsoft.java.bs.gradle.model.SupportedLanguage;

import java.util.Map;

/**
* Default Kotlin implementation of {@link SupportedLanguage}.
*/
public class DefaultKotlinLanguage implements SupportedLanguage<KotlinExtension> {

@Override
public String getBspName() {
return "kotlin";
}

@Override
public String getGradleName() {
return "kotlin";
}

@Override
public KotlinExtension getExtension(Map<String, LanguageExtension> extensions) {
LanguageExtension extension = extensions.get(getBspName());
if (extension == null) {
return null;
}
if (extension.isKotlinExtension()) {
return extension.getAsKotlinExtension();
}
throw new IllegalArgumentException(
"LanguageExtension: " + extension + " is not a KotlinExtension."
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.java.bs.gradle.model.impl;

import com.microsoft.java.bs.gradle.model.JavaExtension;
import com.microsoft.java.bs.gradle.model.KotlinExtension;
import com.microsoft.java.bs.gradle.model.ScalaExtension;

import java.io.File;
Expand Down Expand Up @@ -155,6 +156,11 @@ public boolean isScalaExtension() {
return true;
}

@Override
public boolean isKotlinExtension() {
return false;
}

@Override
public JavaExtension getAsJavaExtension() {
return null;
Expand All @@ -164,4 +170,9 @@ public JavaExtension getAsJavaExtension() {
public ScalaExtension getAsScalaExtension() {
return this;
}

@Override
public KotlinExtension getAsKotlinExtension() {
return null;
}
}
Loading

0 comments on commit ca10fcc

Please sign in to comment.