Skip to content

Commit

Permalink
Fixed GradleBasePlugin not fetching basePlugin & updated .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
faab007nl committed Jan 23, 2024
1 parent dbafc67 commit 36845b2
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 47 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def artifactId = 'GradleBasePlugin'
// /---> Major Version - Increment this when you make a breaking change
// | /---> Patch Version - Increment this when you add a new feature
// | | /---> Minor Version - Increment this when you make a bug fix
def versionNum = '4.0.2'
def versionNum = '4.0.3'

group = groupId
version = versionNum
Expand Down
60 changes: 31 additions & 29 deletions src/main/java/me/TechsCode/GradeBasePlugin/GradleBasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.io.IOException;
import java.util.Arrays;

import me.TechsCode.GradeBasePlugin.resource.ResourceResponse;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar;

Expand Down Expand Up @@ -70,14 +70,18 @@ public void apply(Project project) {
getShadowJar(project).dependsOn("generateMetaFiles");
getShadowJar(project).setProperty("archiveClassifier", "");

// project.getTasks().getByName("build").dependsOn("shadowJar");
project.getTasks().getByName("build").doLast(this::uploadToRemotes);
project.getTasks().getByName("build").dependsOn("shadowJar");

// Add onProjectEvaluation hook
project.afterEvaluate(this::onProjectEvaluation);
// Add projectEvaluation hooks
project.afterEvaluate(this::afterProjectEvaluation);
}

private void onProjectEvaluation(Project project) {
private void afterProjectEvaluation(Project project) {
// Setting properties
project.setProperty("version", meta.pluginVersion);
project.setProperty("sourceCompatibility", "1.8");
project.setProperty("targetCompatibility", "1.8");

if (!meta.configValid()) {
return;
}
Expand All @@ -98,46 +102,44 @@ private void onProjectEvaluation(Project project) {
return;
}

// Setting properties
project.setProperty("version", meta.pluginVersion);
project.setProperty("sourceCompatibility", "1.8");
project.setProperty("targetCompatibility", "1.8");
ResourceResponse response = ResourceManager.loadBasePlugin(project, meta, username, password, meta.baseVersion);
if (response == ResourceResponse.SUCCESS) {
log("Successfully retrieved BasePlugin.jar from the techscode repo...");
project.getDependencies().add("implementation", project.files("libs/BasePlugin.jar"));
} else if (response == ResourceResponse.FAIL_USERNAME) {
log(Color.RED + "Could not retrieve BasePlugin.jar from the techscode repo...");
log(Color.RED_BRIGHT + "Make sure that you have set the TECHSCODE_USERNAME environment variable that has access to the maven-private repository!");
} else if (response == ResourceResponse.FAIL_PASSWORD) {
log(Color.RED + "Could not retrieve BasePlugin.jar from the techscode repo...");
log(Color.RED_BRIGHT + "Make sure that you have set the TECHSCODE_PASSWORD environment variable that has access to the maven-private repository!");
} else if (response == ResourceResponse.FAIL) {
log(Color.RED + "Could not retrieve BasePlugin.jar from the techscode repo...");
log(Color.RED_BRIGHT + "There was an error downloading the BasePlugin.jar from the techscode repo...");
} else if (response == ResourceResponse.NOT_FETCH) {
log(Color.YELLOW + "Not fetching the build, if this is a mistake, please set fetch to true!");
project.getDependencies().add("implementation", project.files("libs/BasePlugin.jar"));
} else {
log(Color.RED + "Could not retrieve BasePlugin.jar from the techscode repo...");
log(Color.RED_BRIGHT + "There was an error downloading the BasePlugin.jar from the techscode repo...");
log(Color.RED_BRIGHT + "Error: " + response.name());
}

// Setting up repositories
project.getRepositories().mavenLocal();
project.getRepositories().mavenCentral();
Arrays.stream(repositories)
.forEach(url -> project.getRepositories().maven((maven) -> maven.setUrl(url)));
if(meta.fetch){
project.getRepositories().maven((maven) ->
maven.setUrl("https://repo.techscode.com/repository/maven-private/")
);
}

// Setting up dependencies
Arrays.stream(dependencies).map(entry -> entry.split("#"))
.forEach(confAndUrl -> project.getDependencies().add(confAndUrl[0], confAndUrl[1]));
if(meta.fetch){
project.getDependencies().add("implementation", "me.TechsCode:BasePlugin:" + meta.baseVersion);
}else{
project.getDependencies().add("implementation", project.files("libs/BasePlugin.jar"));
}

// Setting up relocations
Arrays.stream(relocations).map(entry -> entry.split("#"))
.forEach(fromTo -> getShadowJar(project).relocate(fromTo[0],
fromTo[1].replace("PROJECT_NAME", project.getName())));
}

/* After the build prcoess is completed, the file will be uploaded to all remotes */
private void uploadToRemotes(Task buildTask) {
File file = new File(meta.localDeploymentPath + '/'
+ buildTask.getProject().getName() + "-" + meta.pluginVersion + ".jar");

// meta.remotes.stream().filter(Remote::isEnabled)
// .forEach(all -> all.uploadFile(file));
}

private ShadowJar getShadowJar(Project project) {
return (ShadowJar) project.getTasks().getByName("shadowJar");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.URL;
import java.util.Base64;

import me.TechsCode.GradeBasePlugin.GradleBasePlugin;
import org.apache.commons.io.FileUtils;

import org.apache.hc.client5.http.classic.methods.HttpGet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class MetaExtension {
public ArrayList<String> libraries;
public boolean fetch;
public boolean isAPI = false;
public String localDeploymentPath;

public boolean configValid() {
if (pluginVersion == null) {
Expand All @@ -28,12 +27,6 @@ public boolean configValid() {
GradleBasePlugin.log(Color.RED + "Please check the build.gradle of your project");
return false;
}
if (localDeploymentPath == null) {
GradleBasePlugin.log("Could not find a 'localDeploymentPath' field in your build.gradle");
GradleBasePlugin.log();
GradleBasePlugin.log(Color.RED + "Please check the build.gradle of your project");
return false;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,56 @@
import me.TechsCode.GradeBasePlugin.extensions.MetaExtension;

public class ResourceManager {

public static ResourceResponse loadBasePlugin(Project project, MetaExtension meta, String username, String password, String version) {
if (!meta.fetch) {
return ResourceResponse.NOT_FETCH;
}
if (username == null) {
return ResourceResponse.FAIL_USERNAME;
}
if (password == null) {
return ResourceResponse.FAIL_PASSWORD;
}

File libraryFolder = new File(project.getProjectDir().getAbsolutePath() + "/libs");
libraryFolder.mkdirs();

File libraryFile = new File(libraryFolder.getAbsolutePath() + "/BasePlugin.jar");
libraryFile.delete();

String RETRIEVE_RELEASES;
if(!meta.isAPI){
RETRIEVE_RELEASES = "https://repo.techscode.com/repository/maven-private/me/TechsCode/BasePlugin/"+version+"/BasePlugin-"+version+"-all.jar?enable-custom-header=true";
}else{
RETRIEVE_RELEASES = "https://repo.techscode.com/repository/maven-private/me/TechsCode/BasePluginAPI/"+version+"/BasePluginAPI-"+version+"-all.jar?enable-custom-header=true";
}

try {
Downloader downloader = new Downloader();
downloader.authorize(username, password);
downloader.download(new URL(RETRIEVE_RELEASES), libraryFile);
return ResourceResponse.SUCCESS;
}
catch (Exception e) {
e.printStackTrace();
return ResourceResponse.FAIL;
}
}

public static void createGitIgnore(Project project) throws IOException {
File gitIgnoreDestination = new File(project.getProjectDir().getAbsolutePath() + "/.gitignore");
File gitIgnoreDestination = new File(project.getProjectDir().getPath() + "/.gitignore");
gitIgnoreDestination.mkdirs();

InputStream src = ResourceManager.class.getResourceAsStream("/gitignore.file");
if(src == null) throw new IOException("Gitignore file not found in resources");

Files.copy(src, Paths.get(gitIgnoreDestination.toURI()), StandardCopyOption.REPLACE_EXISTING);
GradleBasePlugin.log(Color.GREEN + "Copied .gitignore file to " + gitIgnoreDestination.getAbsolutePath());
GradleBasePlugin.log(Color.GREEN + "Copied .gitignore file to " + gitIgnoreDestination.getPath());
}

public static void createWorkflow(Project project, boolean isApi) throws IOException {
File destination = new File(project.getProjectDir().getAbsolutePath() + "/.github/workflows/build.yml");
File destination = new File(project.getProjectDir().getPath() + "/.github/workflows/build.yml");
destination.mkdirs();

String workflowFile;
Expand All @@ -46,7 +82,7 @@ public static void createWorkflow(Project project, boolean isApi) throws IOExcep
if(src == null) throw new IOException("Workflow file not found in resources");

Files.copy(src, Paths.get(destination.toURI()), StandardCopyOption.REPLACE_EXISTING);
GradleBasePlugin.log(Color.GREEN + "Copied workflow file to " + destination.getAbsolutePath());
GradleBasePlugin.log(Color.GREEN + "Copied workflow file to " + destination.getPath());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.TechsCode.GradeBasePlugin.resource;

public enum ResourceResponse {

SUCCESS,
FAIL,
FAIL_USERNAME,
FAIL_PASSWORD,
NOT_FETCH;

}
113 changes: 107 additions & 6 deletions src/main/resources/gitignore.file
Original file line number Diff line number Diff line change
@@ -1,8 +1,109 @@
*.DS_Store
#################
## IDEA
#################

# User-specific stuff
.idea/*.xml
.idea_modules/
*.iml
.idea/
.gradle/
out/
build/
libs/

#################
## VISUALSTUDIOCODE
#################
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/

#################
## Eclipse
#################

*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
.apt_generated/
.apt_generated_tests/

# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
!/.mvn/wrapper/maven-wrapper.jar

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

#################
## GRADLE
#################

# Ignore Gradle files
.gradle
**/build/
!src/**/build/

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Avoid ignore Gradle wrappper properties
!gradle-wrapper.properties

# Cache of project
.gradletasknamecache

# Eclipse Gradle plugin generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

#################
## CUSTOM
#################

# Ignore libs folder
libs/

#############
## Windows detritus
#############

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac crap
.DS_Store
REBASE.bat

0 comments on commit 36845b2

Please sign in to comment.