Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- I learned that to use Gradle plugin extensions, the properties of t… #21

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ replay_pid*
**/build/
!src/**/build/

gradle.properties

# Ignore Gradle GUI config
gradle-app.setting

Expand Down
21 changes: 10 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* User Manual available at https://docs.gradle.org/7.3/userguide/building_java_projects.html
*/

buildscript {
repositories {
jcenter()
}
dependencies {
classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
}
}
// buildscript {
// repositories {
// jcenter()
// }
// dependencies {
// classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
// }
// }

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
Expand All @@ -27,19 +27,18 @@ plugins {



apply plugin: 'com.github.johnrengelman.shadow'
// apply plugin: 'com.github.johnrengelman.shadow'


group = 'io.github.mainmethod0126'
version = "0.1.2"
version = "0.1.4"
sourceCompatibility = 11
targetCompatibility = 11

tasks.withType(Javadoc) {
options.encoding = 'UTF-8'
}


compileJava {
options.encoding = 'UTF-8'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public void apply(Project project) {

this.project = project;

initExtensions();
project.getExtensions().create("ssv",
SimpleSemanticVersionPluginExtension.class);

initUtils();

BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning",
Expand All @@ -49,11 +51,14 @@ public void apply(Project project) {
buildAndVersioning.setBm(bm);

buildAndVersioning.setProject(project);
try {
buildAndVersioning.doExcute();
} catch (IOException e) {
throw new IllegalStateException(e);
}

project.afterEvaluate(pj -> {
try {
buildAndVersioning.doExcute();
} catch (IOException e) {
throw new IllegalStateException(e);
}
});

project.getGradle().addBuildListener(new BuildListener() {

Expand Down Expand Up @@ -87,10 +92,6 @@ public void settingsEvaluated(Settings buildResult) {

}

private void initExtensions() {
SimpleSemanticVersionPluginExtension.init(this.project);
}

private void initUtils() {
SsvPaths.init(this.project);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,8 @@
package io.github.mainmethod0126.gradle.simple.versioning.extension;

import org.gradle.api.Project;
import org.gradle.api.provider.Property;

import io.github.mainmethod0126.gradle.simple.versioning.utils.DateUtils;
public interface SimpleSemanticVersionPluginExtension {
Property<Boolean> getIsDateInBuildArtifactDirPath();

/**
*
* I wanted to create it as a singleton object, but it seemed like we would
* always have to pass the 'project' object as an argument when calling
* {@code getInstance()} from outside. Therefore, I created the object to be
* conveniently used after calling the initial {@code initExtension()} function.
*
* warn! : This class was not designed with consideration for multi-threading
* and is not thread-safe. Please be cautious when using it in a multi-threaded
* environment.
*/
public class SimpleSemanticVersionPluginExtension {

/**
* This is not a singleton object, but rather a global variable used for the
* convenience of users.
*/
private static SimpleSemanticVersionPluginExtension extension;

public static void init(Project project) {
if (extension == null) {
extension = project.getExtensions().create("ssv",
SimpleSemanticVersionPluginExtension.class);
}
}

public static SimpleSemanticVersionPluginExtension getExtension() {
return extension;
}

private String buildDate = DateUtils.getCurrentDate(DateUtils.DateUnit.DAY);
private boolean isDateInBuildArtifactDirPath = false;
private String applicationVersion = "0.0.0";

public boolean isDateInBuildArtifactDirPath() {
return isDateInBuildArtifactDirPath;
}

public void setDateInBuildPath(boolean isDateInBuildArtifactDirPath) {
this.isDateInBuildArtifactDirPath = isDateInBuildArtifactDirPath;
}

public String getBuildDate() {
return buildDate;
}

public void setBuildDate(String buildDate) {
this.buildDate = buildDate;
}

public String getApplicationVersion() {
return applicationVersion;
}

public void setApplicationVersion(String applicationVersion) {
this.applicationVersion = applicationVersion;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import org.gradle.api.tasks.bundling.Jar;
import org.json.JSONObject;

import io.github.mainmethod0126.gradle.simple.versioning.extension.SimpleSemanticVersionPluginExtension;
import io.github.mainmethod0126.gradle.simple.versioning.task.version.SemanticVersionFile;
import io.github.mainmethod0126.gradle.simple.versioning.utils.DateUtils;
import io.github.mainmethod0126.gradle.simple.versioning.utils.DateUtils.DateUnit;
import io.github.mainmethod0126.gradle.simple.versioning.utils.NumberUtils;
import io.github.mainmethod0126.gradle.simple.versioning.utils.SsvPaths;

public class BuildAndVersioning extends DefaultTask {

@Inject
private Project project;

@Input
Expand All @@ -49,6 +49,30 @@ public class BuildAndVersioning extends DefaultTask {

private SemanticVersionFile semanticVersionFile;

private String buildDate;

@Inject
public BuildAndVersioning(Project project) {

// set default application version
Path versionFilePath = Paths.get("version.json");
try {
this.semanticVersionFile = new SemanticVersionFile(versionFilePath);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}

// set buildDate
this.buildDate = DateUtils.getCurrentDate(DateUnit.DAY);

// set default java version
String sourceCompatibility = this.javav;
if (sourceCompatibility.isEmpty()) {
sourceCompatibility = System.getProperty("java.version");
}
project.setProperty("sourceCompatibility", sourceCompatibility);
}

/**
* default version 정보가 적혀있는 version.json 파일을 생성합니다.
*
Expand Down Expand Up @@ -138,37 +162,22 @@ public void taskParamResolve(final TaskParam taskParam) {
printVersionChangeInfo("buildMetadata", prevBm, nextBm);
}

private void init() throws IOException {

// set default application version
Path versionFilePath = Paths.get("version.json");
semanticVersionFile = new SemanticVersionFile(versionFilePath);
SimpleSemanticVersionPluginExtension.getExtension().setApplicationVersion(semanticVersionFile.getFullString());

// set default java version
String sourceCompatibility = this.javav;
if (sourceCompatibility.isEmpty()) {
sourceCompatibility = System.getProperty("java.version");
}
project.setProperty("sourceCompatibility", sourceCompatibility);
}

private Artifact createArtifact() throws IOException {

String applicationVersion = SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion();
String applicationVersion = semanticVersionFile.getFullString();

if (SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion() == null) {
if (applicationVersion == null) {
throw new NullPointerException("applicationVersion is null");
}

Path buildDirPath = SsvPaths.getBuildDir();
Path buildDirPath = SsvPaths.getBuildDir(this.buildDate, applicationVersion);

project.setProperty("version", applicationVersion);

mkdir(buildDirPath);

setJar(buildDirPath.toAbsolutePath().toString(), applicationVersion,
SimpleSemanticVersionPluginExtension.getExtension().getBuildDate());
this.buildDate);

return new Artifact(buildDirPath, applicationVersion);

Expand All @@ -195,8 +204,6 @@ private void setJar(String buildDirPath, String applicationVersion, String build
@TaskAction
public void doExcute() throws IOException {

init();

TaskParam userInputVersion = new TaskParam(this.javav, this.major, this.minor, this.patch, this.pr, this.bm);
taskParamResolve(userInputVersion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ public static void init(Project pj) {
project = pj;
}

public static Path getBuildDir() {
if (SimpleSemanticVersionPluginExtension.getExtension().isDateInBuildArtifactDirPath()) {
public static Path getBuildDir(String buildDate, String applicationVersion) {

SimpleSemanticVersionPluginExtension extension = (SimpleSemanticVersionPluginExtension) project.getExtensions()
.findByName("ssv");

if (extension == null) {
throw new IllegalStateException("Could not find SimpleSemanticVersionPluginExtension");
}

if (extension.getIsDateInBuildArtifactDirPath().getOrElse(false).booleanValue()) {
return Paths.get(project.getProjectDir().toString(), "dist",
SimpleSemanticVersionPluginExtension.getExtension().getBuildDate(),
SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion());
buildDate,
applicationVersion);
} else {
return Paths.get(project.getProjectDir().toString(), "dist",
SimpleSemanticVersionPluginExtension.getExtension().getApplicationVersion());
applicationVersion);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ void buildAndVersioningTaskTest() throws IOException {
BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning",
BuildAndVersioning.class);

SimpleSemanticVersionPluginExtension.init(project);
project.getExtensions().create("ssv",
SimpleSemanticVersionPluginExtension.class);
SsvPaths.init(project);

// when, then
Expand All @@ -45,7 +46,8 @@ void buildAndVersioningTask_whenBuildSucceed_thenIncreaseCommitTest() throws IOE
BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning",
BuildAndVersioning.class);

SimpleSemanticVersionPluginExtension.init(project);
project.getExtensions().create("ssv",
SimpleSemanticVersionPluginExtension.class);
SsvPaths.init(project);

buildAndVersioning.setMajor("++");
Expand Down Expand Up @@ -73,8 +75,9 @@ void buildAndVersioningTask_whenBuildFailed_thenNotIncresaseCommitTest() throws
BuildAndVersioning buildAndVersioning = project.getTasks().create("BuildAndVersioning",
BuildAndVersioning.class);

SimpleSemanticVersionPluginExtension.init(project);
SimpleSemanticVersionPluginExtension.getExtension().setDateInBuildPath(false);
SimpleSemanticVersionPluginExtension extension = project.getExtensions().create("ssv",
SimpleSemanticVersionPluginExtension.class);
extension.getIsDateInBuildArtifactDirPath().set(false);
SsvPaths.init(project);

buildAndVersioning.setMajor("++");
Expand Down
Loading