2
0
Fork 0
mirror of https://github.com/ethauvin/rife2.git synced 2025-04-30 18:48:13 -07:00

Build system cleanup and refactoring

This commit is contained in:
Geert Bevin 2023-03-12 09:41:24 -04:00
parent 1cd248ccb2
commit 162e536b4b
8 changed files with 173 additions and 102 deletions

View file

@ -18,7 +18,7 @@ public class BuildExecutor {
return arguments_;
}
public Map<String, Method> getBuildCommands() {
public Map<String, Method> buildCommands() {
if (buildCommands_ == null) {
var build_commands = new TreeMap<String, Method>();
@ -50,7 +50,6 @@ public class BuildExecutor {
public void processArguments(String[] arguments) {
arguments_ = new ArrayList<>(Arrays.asList(arguments));
var commands = getBuildCommands();
var show_help = arguments_.isEmpty();
@ -58,13 +57,7 @@ public class BuildExecutor {
var command = arguments_.remove(0);
try {
var method = commands.get(command);
if (method != null) {
method.invoke(this);
} else {
System.err.println("ERROR: unknown command '" + command + "'");
System.out.println();
new HelpCommand(this, arguments_).printFullHelp();
if (runCommand(command)) {
break;
}
} catch (Exception e) {
@ -78,6 +71,20 @@ public class BuildExecutor {
}
}
public boolean runCommand(String command)
throws Exception {
var method = buildCommands().get(command);
if (method != null) {
method.invoke(this);
} else {
System.err.println("ERROR: unknown command '" + command + "'");
System.out.println();
new HelpCommand(this, arguments_).printFullHelp();
return true;
}
return false;
}
@BuildCommand(help = HelpCommand.Help.class)
public void help() {
new HelpCommand(this, arguments_).execute();

View file

@ -6,11 +6,12 @@ package rife.bld;
import rife.bld.commands.*;
import rife.bld.dependencies.*;
import rife.tools.FileUtils;
import rife.tools.StringUtils;
import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.regex.Pattern;
public abstract class Project extends BuildExecutor {
public List<Repository> repositories = Collections.emptyList();
@ -19,10 +20,14 @@ public abstract class Project extends BuildExecutor {
public VersionNumber version = null;
public String mainClass = null;
public final DependencyScopes dependencies_ = new DependencyScopes();
public DependencyScopes dependencies = new DependencyScopes();
public abstract void setup();
/*
* Standard build commands
*/
@BuildCommand(help = CleanCommand.Help.class)
public void clean()
throws Exception {
@ -55,6 +60,10 @@ public abstract class Project extends BuildExecutor {
new RunCommand(this).execute();
}
/*
* Useful methods
*/
public static VersionNumber version(int major) {
return new VersionNumber(major);
}
@ -76,7 +85,7 @@ public abstract class Project extends BuildExecutor {
}
public DependencySet scope(Scope scope) {
return dependencies_.scope(scope);
return dependencies.scope(scope);
}
public static Dependency dependency(String groupId, String artifactId) {
@ -95,56 +104,142 @@ public abstract class Project extends BuildExecutor {
return new Dependency(groupId, artifactId, version, classifier, type);
}
public static String joinPaths(List<String> paths) {
return StringUtils.join(paths, File.pathSeparator);
}
/*
* Project directories
*/
public File srcDirectory() {
return new File("src");
}
public File srcMainDirectory() {
return new File(srcDirectory(), "main");
}
public File srcMainJavaDirectory() {
return Path.of("src", "main", "java").toFile();
return new File(srcMainDirectory(), "java");
}
public File srcMainResourcesDirectory() {
return new File(srcMainDirectory(), "resources");
}
public File srcMainResourcesTemplatesDirectory() {
return new File(srcMainResourcesDirectory(), "templates");
}
public File srcMainWebappDirectory() {
return new File(srcMainDirectory(), "webapp");
}
public File srcProjectDirectory() {
return new File(srcDirectory(), "project");
}
public File srcProjectJavaDirectory() {
return Path.of("src", "project", "java").toFile();
return new File(srcProjectDirectory(), "java");
}
public File srcTestJDirectory() {
return new File(srcDirectory(), "test");
}
public File srcTestJavaDirectory() {
return Path.of("src", "test", "java").toFile();
return new File(srcTestJDirectory(), "java");
}
public File libDirectory() {
return new File("lib");
}
public File libCompileDirectory() {
return Path.of("lib", "compile").toFile();
return new File(libDirectory(), "compile");
}
public File libProjectDirectory() {
return Path.of("lib", "project").toFile();
return new File(libDirectory(), "project");
}
public File libRuntimeDirectory() {
return Path.of("lib", "runtime").toFile();
return new File(libDirectory(), "runtime");
}
public File libStandaloneDirectory() {
return Path.of("lib", "standalone").toFile();
return new File(libDirectory(), "standalone");
}
public File libTestDirectory() {
return Path.of("lib", "test").toFile();
return new File(libDirectory(), "test");
}
public File buildDirectory() {
return new File("build");
}
public File buildDistDirectory() {
return Path.of("build", "dist").toFile();
return new File(buildDirectory(), "dist");
}
public File buildMainDirectory() {
return Path.of("build", "main").toFile();
return new File(buildDirectory(), "main");
}
public File buildProjectDirectory() {
return Path.of("build", "project").toFile();
return new File(buildDirectory(), "project");
}
public File buildTestDirectory() {
return Path.of("build", "test").toFile();
return new File(buildDirectory(), "test");
}
/*
* File collections
*/
public List<File> mainSourceFiles() {
// get all the main java sources
var src_main_java_dir_abs = srcMainJavaDirectory().getAbsoluteFile();
return FileUtils.getFileList(src_main_java_dir_abs, Pattern.compile("^.*\\.java$"), null)
.stream().map(file -> new File(src_main_java_dir_abs, file)).toList();
}
public List<File> testSourceFiles() {
// get all the test java sources
var src_test_java_dir_abs = srcTestJavaDirectory().getAbsoluteFile();
return FileUtils.getFileList(src_test_java_dir_abs, Pattern.compile("^.*\\.java$"), null)
.stream().map(file -> new File(src_test_java_dir_abs, file)).toList();
}
/*
* Project classpaths
*/
public List<String> compileClasspath() {
// detect the jar files in the compile lib directory
var lib_compile_dir_abs = libCompileDirectory().getAbsoluteFile();
var lib_compile_jar_files = FileUtils.getFileList(lib_compile_dir_abs, Pattern.compile("^.*\\.jar$"), null);
// build the compilation classpath
var compile_classpath_paths = new ArrayList<>(lib_compile_jar_files.stream().map(file -> new File(lib_compile_dir_abs, file).getAbsolutePath()).toList());
compile_classpath_paths.add(0, buildMainDirectory().getAbsolutePath());
return compile_classpath_paths;
}
public List<String> testClasspath() {
// detect the jar files in the test lib directory
var lib_test_dir_abs = libTestDirectory().getAbsoluteFile();
var lib_test_jar_files = FileUtils.getFileList(lib_test_dir_abs, Pattern.compile("^.*\\.jar$"), null);
// build the test classpath
var test_classpath_paths = new ArrayList<>(lib_test_jar_files.stream().map(file -> new File(lib_test_dir_abs, file).getAbsolutePath()).toList());
test_classpath_paths.addAll(0, compileClasspath());
return test_classpath_paths;
}
public void start(String[] args) {

View file

@ -20,33 +20,33 @@ public class CleanCommand {
}
}
private final Project project_;
public final Project project;
public CleanCommand(Project project) {
project_ = project;
this.project = project;
}
public void execute() {
try {
FileUtils.deleteDirectory(project_.buildDistDirectory());
FileUtils.deleteDirectory(project.buildDistDirectory());
} catch (FileUtilsErrorException e) {
// no-op
}
try {
FileUtils.deleteDirectory(project_.buildMainDirectory());
FileUtils.deleteDirectory(project.buildMainDirectory());
} catch (FileUtilsErrorException e) {
// no-op
}
try {
FileUtils.deleteDirectory(project_.buildProjectDirectory());
FileUtils.deleteDirectory(project.buildProjectDirectory());
} catch (FileUtilsErrorException e) {
// no-op
}
try {
FileUtils.deleteDirectory(project_.buildTestDirectory());
FileUtils.deleteDirectory(project.buildTestDirectory());
} catch (FileUtilsErrorException e) {
// no-op
}

View file

@ -6,14 +6,12 @@ package rife.bld.commands;
import rife.bld.BuildHelp;
import rife.bld.Project;
import rife.tools.FileUtils;
import rife.tools.StringUtils;
import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
public class CompileCommand {
public static class Help implements BuildHelp {
@ -29,61 +27,33 @@ public class CompileCommand {
}
}
private final Project project_;
public final Project project;
public CompileCommand(Project project) {
project_ = project;
this.project = project;
}
public void execute()
throws Exception {
project_.buildMainDirectory().mkdirs();
project_.buildProjectDirectory().mkdirs();
project_.buildTestDirectory().mkdirs();
// detect the jar files in the compile lib directory
var lib_compile_dir_abs = project_.libCompileDirectory().getAbsoluteFile();
var lib_compile_jar_files = FileUtils.getFileList(lib_compile_dir_abs, Pattern.compile("^.*\\.jar$"), null);
// detect the jar files in the test lib directory
var lib_test_dir_abs = project_.libTestDirectory().getAbsoluteFile();
var lib_test_jar_files = FileUtils.getFileList(lib_test_dir_abs, Pattern.compile("^.*\\.jar$"), null);
// get all the main java sources
var src_main_java_dir_abs = project_.srcMainJavaDirectory().getAbsoluteFile();
var main_java_files = FileUtils.getFileList(src_main_java_dir_abs, Pattern.compile("^.*\\.java$"), null)
.stream().map(file -> new File(src_main_java_dir_abs, file)).toList();
// get the main output path
var main_build_path = project_.buildMainDirectory().getAbsolutePath();
// get all the test java sources
var src_test_java_dir_abs = project_.srcTestJavaDirectory().getAbsoluteFile();
var test_java_files = FileUtils.getFileList(src_test_java_dir_abs, Pattern.compile("^.*\\.java$"), null)
.stream().map(file -> new File(src_test_java_dir_abs, file)).toList();
// get the test output path
var build_test_path = project_.buildTestDirectory().getAbsolutePath();
// build the compilation classpath
var compile_classpath_paths = new ArrayList<>(lib_compile_jar_files.stream().map(file -> new File(lib_compile_dir_abs, file).getAbsolutePath()).toList());
compile_classpath_paths.add(0, main_build_path);
var compile_classpath = StringUtils.join(compile_classpath_paths, File.pathSeparator);
// build the test classpath
var test_classpath_paths = new ArrayList<>(lib_test_jar_files.stream().map(file -> new File(lib_test_dir_abs, file).getAbsolutePath()).toList());
test_classpath_paths.addAll(0, compile_classpath_paths);
var test_classpath = StringUtils.join(test_classpath_paths, File.pathSeparator);
project.buildMainDirectory().mkdirs();
project.buildProjectDirectory().mkdirs();
project.buildTestDirectory().mkdirs();
// compile both the main and the test java sources
var compiler = ToolProvider.getSystemJavaCompiler();
try (var file_manager = compiler.getStandardFileManager(null, null, null)) {
buildProjectSources(compiler, file_manager, compile_classpath, main_java_files, main_build_path);
buildProjectSources(compiler, file_manager, test_classpath, test_java_files, build_test_path);
buildProjectSources(compiler, file_manager,
Project.joinPaths(project.compileClasspath()),
project.mainSourceFiles(),
project.buildMainDirectory().getAbsolutePath());
buildProjectSources(compiler, file_manager,
Project.joinPaths(project.testClasspath()),
project.testSourceFiles(),
project.buildTestDirectory().getAbsolutePath());
}
}
private static void buildProjectSources(JavaCompiler compiler, StandardJavaFileManager fileManager, String classpath, List<File> sources, String destination)
public void buildProjectSources(JavaCompiler compiler, StandardJavaFileManager fileManager, String classpath, List<File> sources, String destination)
throws IOException {
var compilation_units = fileManager.getJavaFileObjectsFromFiles(sources);
var diagnostics = new DiagnosticCollector<JavaFileObject>();
@ -94,7 +64,7 @@ public class CompileCommand {
}
}
private static void outputDiagnostics(DiagnosticCollector<JavaFileObject> diagnostics)
public void outputDiagnostics(DiagnosticCollector<JavaFileObject> diagnostics)
throws IOException {
for (var diagnostic : diagnostics.getDiagnostics()) {
var source = diagnostic.getSource().getCharContent(true).toString();

View file

@ -32,8 +32,6 @@ public class CreateCommand {
}
}
public static final String NAME = "create";
private final String packageName_;
private final String projectName_;
@ -142,7 +140,7 @@ public class CreateCommand {
downloadDependencies();
}
private void createProjectStructure() {
public void createProjectStructure() {
projectDir_.mkdirs();
srcMainJavaDir_.mkdirs();
srcMainResourcesTemplatesDir_.mkdirs();
@ -163,7 +161,7 @@ public class CreateCommand {
testPackageDir_.mkdirs();
}
private void populateProjectStructure()
public void populateProjectStructure()
throws FileUtilsErrorException {
// project gitignore
FileUtils.writeString(
@ -231,7 +229,7 @@ public class CreateCommand {
build_sh_file.setExecutable(true);
}
private void populateIdeaProject()
public void populateIdeaProject()
throws FileUtilsErrorException {
// IDEA project files
FileUtils.writeString(
@ -277,7 +275,7 @@ public class CreateCommand {
FileUtils.writeString(run_tests_template.getContent(), run_tests_file);
}
private void downloadDependencies() {
public void downloadDependencies() {
for (var dependency : NewProjectInfo.DEPENDENCIES.get(Scope.compile)) {
new DependencyResolver(NewProjectInfo.REPOSITORIES, dependency)
.downloadTransitivelyIntoFolder(libCompileDir_, Scope.compile);

View file

@ -19,42 +19,42 @@ public class DownloadCommand {
}
}
private final Project project_;
public final Project project;
public DownloadCommand(Project project) {
project_ = project;
this.project = project;
}
public void execute() {
var compile_deps = NewProjectInfo.DEPENDENCIES.get(Scope.compile);
var compile_deps = project.dependencies.get(Scope.compile);
if (compile_deps != null) {
for (var dependency : compile_deps) {
new DependencyResolver(NewProjectInfo.REPOSITORIES, dependency)
.downloadTransitivelyIntoFolder(project_.libCompileDirectory(), Scope.compile);
new DependencyResolver(project.repositories, dependency)
.downloadTransitivelyIntoFolder(project.libCompileDirectory(), Scope.compile);
}
}
var runtime_deps = NewProjectInfo.DEPENDENCIES.get(Scope.runtime);
var runtime_deps = project.dependencies.get(Scope.runtime);
if (runtime_deps != null) {
for (var dependency : runtime_deps) {
new DependencyResolver(NewProjectInfo.REPOSITORIES, dependency)
.downloadTransitivelyIntoFolder(project_.libRuntimeDirectory(), Scope.runtime);
new DependencyResolver(project.repositories, dependency)
.downloadTransitivelyIntoFolder(project.libRuntimeDirectory(), Scope.runtime);
}
}
var standalone_deps = NewProjectInfo.DEPENDENCIES.get(Scope.standalone);
var standalone_deps = project.dependencies.get(Scope.standalone);
if (standalone_deps != null) {
for (var dependency : standalone_deps) {
new DependencyResolver(NewProjectInfo.REPOSITORIES, dependency)
.downloadTransitivelyIntoFolder(project_.libStandaloneDirectory(), Scope.compile, Scope.runtime);
new DependencyResolver(project.repositories, dependency)
.downloadTransitivelyIntoFolder(project.libStandaloneDirectory(), Scope.compile, Scope.runtime);
}
}
var test_deps = NewProjectInfo.DEPENDENCIES.get(Scope.test);
var test_deps = project.dependencies.get(Scope.test);
if (test_deps != null) {
for (var dependency : test_deps) {
new DependencyResolver(NewProjectInfo.REPOSITORIES, dependency)
.downloadTransitivelyIntoFolder(project_.libTestDirectory(), Scope.compile, Scope.runtime);
new DependencyResolver(project.repositories, dependency)
.downloadTransitivelyIntoFolder(project.libTestDirectory(), Scope.compile, Scope.runtime);
}
}
}

View file

@ -7,7 +7,6 @@ package rife.bld.commands;
import rife.Version;
import rife.bld.*;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import static java.util.Comparator.comparingInt;
@ -38,7 +37,7 @@ public class HelpCommand {
boolean print_full_help = true;
try {
var commands = executor_.getBuildCommands();
var commands = executor_.buildCommands();
if (commands.containsKey(topic)) {
var method = commands.get(topic);
var annotation = method.getAnnotation(BuildCommand.class);
@ -66,7 +65,7 @@ public class HelpCommand {
public void printFullHelp()
throws Exception {
var commands = executor_.getBuildCommands();
var commands = executor_.buildCommands();
System.err.println("""
The RIFE2 CLI provides its features through a series of commands that
@ -78,6 +77,7 @@ public class HelpCommand {
The following commands are supported.
""");
// test
// jar Creates an uberJar archive for a RIFE2 application
// war Creates a war archive for a RIFE2 application

View file

@ -22,12 +22,13 @@ public class RunCommand {
}
}
private final Project project_;
public final Project project;
public RunCommand(Project project) {
project_ = project;
this.project = project;
}
public void execute() {
}
}