dev.jbang.source.CodeBuilderProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbang-cli Show documentation
Show all versions of jbang-cli Show documentation
JBang Command Line Interface
package dev.jbang.source;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import dev.jbang.source.sources.JavaSource;
public class CodeBuilderProvider implements Supplier> {
private final BuildContext buildContext;
/**
* Returns an initialized CodeBuilderProvider
using the given
* Project
which will use a default BuildContext
to
* store target files and intermediate results
*
* @param prj the Project
to use
* @return A CodeBuilder
*/
public static CodeBuilderProvider create(Project prj) {
return create(BuildContext.forProject(prj));
}
/**
* Returns an initialized CodeBuilderProvider
using the given
* BuildContext
to store target files and intermediate results
*
* @param ctx the BuildContext
to use
* @return A CodeBuilder
*/
public static CodeBuilderProvider create(BuildContext ctx) {
return new CodeBuilderProvider(ctx);
}
protected CodeBuilderProvider(BuildContext buildContext) {
this.buildContext = buildContext;
}
/**
* Returns a Builder
that can be used to turn this
* Project
into executable code.
*
* @return A Builder
*/
@Nonnull
@Override
public Builder get() {
return get(buildContext);
}
@Nonnull
protected Builder get(BuildContext ctx) {
Builder builder = getBuilder(ctx);
Project prj = ctx.getProject();
if (!prj.getSubProjects().isEmpty()) {
List> subBuilders = prj.getSubProjects()
.stream()
.map(p -> get(ctx.forSubProject(p)))
.collect(Collectors.toList());
return () -> {
for (Builder b : subBuilders) {
b.build();
}
return builder.build();
};
} else {
return builder;
}
}
@Nonnull
protected Builder getBuilder(BuildContext ctx) {
Project prj = ctx.getProject();
if (prj.getMainSource() != null) {
return prj.getMainSource().getBuilder(ctx);
} else {
if (prj.isJar() && prj.isNativeImage()) {
// JARs normally don't need building unless a native image
// was requested
return new JavaSource.JavaAppBuilder(ctx);
} else {
// Returns a no-op builder that when its build() gets called
// immediately returns a builder for a CmdGenerator
return () -> CmdGenerator.builder(ctx);
}
}
}
}