Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
Improved project creator API
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolykt committed May 7, 2022
1 parent 99a3929 commit a044d59
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion brachyura/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.coolcrabs</groupId>
<artifactId>brachyura</artifactId>
<version>0.83.0</version>
<version>0.83.1</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

import io.github.coolcrabs.brachyura.dependency.JavaJarDependency;
import io.github.coolcrabs.brachyura.maven.HttpMavenRepository;
import io.github.coolcrabs.brachyura.maven.MavenId;
import io.github.coolcrabs.brachyura.maven.MavenResolver;
import io.github.coolcrabs.brachyura.project.java.SimpleJavaProject;

// Slbrachyura: Interactive project creation
Expand All @@ -25,6 +31,8 @@ public class BuildscriptCreator {
private String buildscriptName = "Buildscript";
private String buildscriptProjectName = "ExampleBuildscript";
private Type buildscriptSuperclass = SimpleJavaProject.class;
private List<MavenId> dependencies = new ArrayList<>();
private List<String> foreignRepositories = new ArrayList<>();
private int javaVersion = 8;
private MavenId projectId = new MavenId("com.example", "example", "0.0.1-SNAPSHOT");

Expand All @@ -44,6 +52,7 @@ public String getBuildscriptSource() {
}

List<MethodSpec> methods = new ArrayList<>();
List<FieldSpec> fields = new ArrayList<>();

methods.add(MethodSpec.methodBuilder("getId")
.returns(MavenId.class)
Expand All @@ -69,9 +78,39 @@ public String getBuildscriptSource() {
.addStatement(CodeBlock.of("return $S", buildscriptProjectName))
.build());

if (!dependencies.isEmpty() || !foreignRepositories.isEmpty()) {
TypeName jarDepArrayList = ParameterizedTypeName.get(ArrayList.class, JavaJarDependency.class);
FieldSpec field = FieldSpec.builder(jarDepArrayList, "dependencies", Modifier.PRIVATE, Modifier.FINAL)
.initializer("new $T()", jarDepArrayList)
.build();
fields.add(field);
MethodSpec.Builder builder = MethodSpec.methodBuilder("createDependencies")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addAnnotation(NotNull.class)
.returns(jarDepArrayList)
.addStatement("$T list = new $T(super.createDependencies())", jarDepArrayList, jarDepArrayList)
.beginControlFlow("if ($N.isEmpty())", field)
.addStatement("$T resolver = new $T($T.MAVEN_LOCAL)", MavenResolver.class, MavenResolver.class, MavenResolver.class);

for (String repo : foreignRepositories) {
builder.addStatement("resolver.addRepository(new $T($S))", HttpMavenRepository.class, repo);
}

for (MavenId dep : dependencies) {
builder.addStatement("$N.add(resolver.getJarDepend(new $T($S, $S, $S)))", field, MavenId.class, dep.groupId, dep.artifactId, dep.version);
}

methods.add(builder.endControlFlow()
.addStatement("list.addAll($N)", field)
.addStatement("return list")
.build());
}

TypeSpec type = TypeSpec.classBuilder(className)
.addSuperinterface(DescriptiveBuildscriptName.class)
.superclass(buildscriptSuperclass)
.addFields(fields)
.addMethods(methods)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.build();
Expand All @@ -96,6 +135,20 @@ public BuildscriptCreator withClassName(@NotNull String name) {
return this;
}

@NotNull
@Contract(mutates = "this", pure = false, value = "_ -> this")
public BuildscriptCreator withDependency(@NotNull MavenId dep) {
this.dependencies.add(dep);
return this;
}

@NotNull
@Contract(mutates = "this", pure = false, value = "_ -> this")
public BuildscriptCreator withHttpRepo(@NotNull String repoURL) {
this.foreignRepositories.add(repoURL);
return this;
}

@NotNull
@Contract(mutates = "this", pure = false, value = "_ -> this")
public BuildscriptCreator withId(@NotNull MavenId id) {
Expand Down

0 comments on commit a044d59

Please sign in to comment.