Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikurube committed Nov 29, 2023
1 parent 9b4cd14 commit d3386b3
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gradlePlugin {
id = "org.embulk.runset"
displayName = "A Gradle plugin to prepare an environment for running Embulk"
description = "${project.description}"
implementationClass = "org.embulk.gradle.runset.RunsetPlugin"
implementationClass = "org.embulk.gradle.runset.EmbulkRunSetPlugin"
tags = ["embulk"]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
import org.gradle.api.Project;

/**
* A Gradle plugin to prepare an environment for running Embulk.
* A Gradle plugin to set up an environment for running Embulk.
*/
public class RunsetPlugin implements Plugin<Project> {
public class EmbulkRunSetPlugin implements Plugin<Project> {
@Override
public void apply(final Project project) {
project.getExtensions().create(
"embulkRunset",
EmbulkRunsetExtension.class,
project);
project.getTasks().register("installEmbulkRunSet", InstallEmbulkRunSet.class);
}
}
33 changes: 0 additions & 33 deletions src/main/java/org/embulk/gradle/runset/EmbulkRunsetExtension.java

This file was deleted.

108 changes: 108 additions & 0 deletions src/main/java/org/embulk/gradle/runset/InstallEmbulkRunSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2022 The Embulk project
*
* 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 org.embulk.gradle.runset;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.gradle.api.DefaultTask;
import org.gradle.api.IllegalDependencyNotation;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.TaskAction;

/**
* A Gradle Task to set up an environment for running Embulk.
*/
public class InstallEmbulkRunSet extends DefaultTask {
public InstallEmbulkRunSet() {
super();

this.project = this.getProject();

final ObjectFactory objectFactory = this.project.getObjects();

this.dependencies = new ArrayList<>();
this.targetDirectory = objectFactory.property(String.class);
}

@TaskAction
public void exec() {
}

/**
* Adds a Maven artifact to be installed.
*
* <p>It tries to simulate Gradle's dependency notations, but it is yet far from perfect.
*
* @see <a href="https://github.com/gradle/gradle/blob/v8.4.0/subprojects/dependency-management/src/main/java/org/gradle/api/internal/notations/DependencyNotationParser.java#L49-L86">org.gradle.api.internal.notations.DependencyNotationParser#create</a>
*/
public void artifact(final Object dependencyNotation) {
if (dependencyNotation instanceof CharSequence) {
this.dependencies.add(this.mapFromCharSequence((CharSequence) dependencyNotation));
} else if (dependencyNotation instanceof Map) {
this.dependencies.add(this.mapFromMap((Map) dependencyNotation));
} else {
throw new IllegalDependencyNotation("Supplied module notation is invalid.");
}
}

@InputFile
@org.gradle.api.tasks.Optional
public Property<String> getTargetDirectory() {
return this.targetDirectory;
}

List<Configuration> buildDetachedConfigurations() {
final ArrayList<Configuration> configurations = new ArrayList<>();
for (final Map.Entry<Object, Dependency> dependency : this.dependencies) {
final Configuration configuration = this.project.getConfigurations().detachedConfiguration(dependency.getValue());
configurations.add(configuration);
}
return Collections.unmodifiableList(configurations);
}

// https://github.com/gradle/gradle/blob/v8.4.0/subprojects/dependency-management/src/main/java/org/gradle/api/internal/notations/DependencyStringNotationConverter.java
private Map.Entry<Object, Dependency> mapFromCharSequence(final CharSequence dependencyNotation) {
System.out.println(dependencyNotation);
return new AbstractMap.SimpleImmutableEntry<>(
dependencyNotation.toString(),
this.project.getDependencies().create(dependencyNotation));
}

// https://github.com/gradle/gradle/blob/v8.4.0/subprojects/core/src/main/java/org/gradle/internal/typeconversion/MapNotationConverter.java
private Map.Entry<Object, Dependency> mapFromMap(final Map dependencyNotation) {
System.out.println(dependencyNotation);

return new AbstractMap.SimpleImmutableEntry<>(
new LinkedHashMap((Map) dependencyNotation),
this.project.getDependencies().create(dependencyNotation));
}

private final Project project;

private final ArrayList<Map.Entry<Object, Dependency>> dependencies;

private final Property<String> targetDirectory;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

package org.embulk.gradle.runset;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.embulk.gradle.runset.Util.prepareProjectDir;
import static org.embulk.gradle.runset.Util.runGradle;

import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class TestRunsetPlugin {
public class TestEmbulkRunSetPlugin {
@Test
public void test(@TempDir Path tempDir) throws IOException {
assertEquals(true, true);
final Path projectDir = prepareProjectDir(tempDir, "simple");

runGradle(projectDir, "installEmbulkRunSet");
}
}
Loading

0 comments on commit d3386b3

Please sign in to comment.