Skip to content

Commit

Permalink
Fix Kotlin multiplatform support when the plugin is applied later.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Feb 7, 2024
1 parent 4a2afac commit 8866d3c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@

- Added missing support for the Kotlin multiplatform plugin ([#1](https://github.com/YumiProject/yumi-gradle-licenser/issues/1)).
- Improved functional tests.

## 1.1.1

- Fixed Kotlin multiplatform support when the plugin is applied after the licenser ([#1](https://github.com/YumiProject/yumi-gradle-licenser/issues/1#issuecomment-1931569894)).
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
plugins {
id("dev.yumi.gradle.licenser").version("1.0.+")
id("dev.yumi.gradle.licenser").version("1.1.+")
id("com.gradle.plugin-publish").version("1.2.0")
id("maven-publish")
id("signing")
}

group = "dev.yumi"
version = "1.1.0"
version = "1.1.1"
val javaVersion = 17

repositories {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Represents the Yumi Licenser Gradle plugin.
*
* @author LambdAurora
* @version 1.1.0
* @version 1.1.1
* @since 1.0.0
*/
public class YumiLicenserGradlePlugin implements Plugin<Project> {
Expand Down Expand Up @@ -60,13 +60,13 @@ public void apply(Project project) {
});
});

if (project.getPlugins().findPlugin("org.jetbrains.kotlin.multiplatform") != null) {
project.getPlugins().withId("org.jetbrains.kotlin.multiplatform", plugin -> {
try {
KotlinMultiplatformCompat.applyTasksForKotlinMultiplatform(project, ext);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
});

var globalCheck = this.registerGroupedTask(project, CHECK_TASK_PREFIX, task -> {
task.dependsOn(project.getTasks().withType(CheckLicenseTask.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import dev.yumi.gradle.licenser.YumiLicenserGradlePlugin;
import dev.yumi.gradle.licenser.task.ApplyLicenseTask;
import dev.yumi.gradle.licenser.task.CheckLicenseTask;
import dev.yumi.gradle.licenser.util.Utils;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.api.file.SourceDirectorySet;
Expand All @@ -25,7 +26,7 @@
* No direct reference to the Kotlin multiplatform plugin is made due to classloader isolation issues.
*
* @author LambdAurora
* @version 1.1.0
* @version 1.1.1
* @since 1.1.0
*/
@ApiStatus.Internal
Expand All @@ -37,6 +38,8 @@ private KotlinMultiplatformCompat() {
}

public static void applyTasksForKotlinMultiplatform(Project project, YumiLicenserGradleExtension ext) throws Throwable {
Utils.debugLog(project, "Found Kotlin Multiplatform plugin, applying special configuration...");

var kotlinExt = project.getExtensions().getByName("kotlin"); // Kotlin multiplatform has a Kotlin extension.

// We get the sourceSets of the Kotlin multiplatform plugin.
Expand All @@ -50,7 +53,8 @@ public static void applyTasksForKotlinMultiplatform(Project project, YumiLicense

if (ext.isSourceSetExcluded(name)) return;

var kotlinSet = (SourceDirectorySet) LOOKUP.unreflect(sourceSet.getClass().getMethod("getKotlin")).invoke(sourceSet);
var kotlinSet = (SourceDirectorySet) LOOKUP.unreflect(sourceSet.getClass().getMethod("getKotlin"))
.invoke(sourceSet);

project.getTasks().register(
getTaskName("check", name), CheckLicenseTask.class,
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/dev/yumi/gradle/licenser/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package dev.yumi.gradle.licenser.util;

import dev.yumi.gradle.licenser.YumiLicenserGradleExtension;
import dev.yumi.gradle.licenser.YumiLicenserGradlePlugin;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -31,7 +32,7 @@
* Provides various utilities.
*
* @author LambdAurora
* @version 1.0.0
* @version 1.1.1
* @since 1.0.0
*/
@ApiStatus.Internal
Expand All @@ -40,6 +41,19 @@ private Utils() {
throw new UnsupportedOperationException("Utils only contains static definitions.");
}

/**
* Logs a message if the plugin is in debug mode.
*
* @param project the project
* @param message the message to log
* @param params the parameters to replace in the message
*/
public static void debugLog(Project project, String message, Object... params) {
if (YumiLicenserGradlePlugin.DEBUG_MODE) {
project.getLogger().lifecycle("[" + project.getPath() + "][Yumi Gradle Licenser] " + message, params);
}
}

/**
* Matches a character in a string at a given index which could be out of bounds.
*
Expand Down

0 comments on commit 8866d3c

Please sign in to comment.