diff --git a/build.gradle b/build.gradle index 6d3a2c9cc..72c43f6cf 100644 --- a/build.gradle +++ b/build.gradle @@ -255,40 +255,29 @@ configurations { } dependencies { - // The expose (Ant) task depends on classes compiled to here: + // The expose task depends on classes compiled to here: expose files(unexposedDir) } -// A (Gradle) task to run the Ant task 'expose'. -task expose (group: 'Custom', dependsOn: compileJava) { +// A (Gradle) task to run 'expose'. +task expose (type: JavaExec, group: 'Custom') { description = 'Expose Java types to Python using their annotations.' - // Allow Gradle to infer the need to regenreate the outputs + dependsOn compileJava + + // Allow Gradle to infer the need to regenerate the outputs inputs.files(fileTree("${project.ext.unexposedDir}/org/python")) + inputs.file('CoreExposed.includes') outputs.dir(project.ext.exposedDir) - def exposedDir = project.ext.exposedDir - def unexposedDir = project.ext.unexposedDir - - doLast { - /* - * Define an Ant task called 'expose' in the project's AntBuilder. - * We can't define it until ExposeTask has been compiled. - */ - ant.taskdef( - name: 'expose', - classname: 'org.python.expose.generate.ExposeTask', - classpath: configurations.expose.asPath - ) - - // Use the Gradle-conventional directory structure (not the legacy one). - ant.expose( - srcdir: file(unexposedDir), - destdir: mkdir(file(exposedDir)), - includesfile: file('CoreExposed.includes') - ) - } + classpath = configurations.expose + mainClass = 'org.python.expose.generate.ExposeTask' + args = [ + '--srcdir', project.ext.unexposedDir, + '--destdir', project.ext.exposedDir, + '--includesfile', 'CoreExposed.includes' + ] } // Task to merge the exposed and unexposed classes @@ -548,13 +537,15 @@ configurations { } dependencies { - // Jython as built so far should be on the path of the jycompile (Ant) task + // Jython as built so far should be on the path of the jycompile task pycompile files("$buildDir/classes/java/main") pycompile files("$buildDir/resources/main") } -// A (Gradle) task to run the Ant task 'jycompile' (not pycompile). -task pycompile(group: 'Custom', description: 'Compile the Python modules to .class files for the JAR') { +// A (Gradle) task to run 'jycompile' (not pycompile). +task pycompile(type: JavaExec, group: 'Custom') { + + description = 'Compile the Python modules to .class files for the JAR' // Compiler depends on rest of Jython being fully assembled in 'classes' dependsOn classes @@ -564,48 +555,33 @@ task pycompile(group: 'Custom', description: 'Compile the Python modules to .cla inputs.dir project.ext.buildLibDir outputs.dir project.ext.compiledLibDir - def inputsDir = project.ext.buildLibDir - def outputsDir = project.ext.compiledLibDir - - doFirst { - /* - * Define an Ant task called 'jycompile' in the project's AntBuilder. - * We can't define it until JythoncAntTask has been compiled, so this - * must happen during the execution of the task (early). - */ - ant.taskdef( - name: 'jycompile', - classname: 'org.python.util.JycompileAntTask', - classpath: configurations.pycompile.asPath - ) - } - - doLast { - /* - * Now use the 'jycompile' Ant task to compile the Python source we - * supply to users. The exclusions have been copied from build.xml, - * and also this comment: - - */ - def exclusions = ['test/**', 'lib2to3/tests/**', 'lib2to3/tests/data/myfixes/**'] - ant.jycompile( - srcdir: inputsDir, - destdir: outputsDir, - excludes: exclusions.join(',') // Yes, it's that way round :o - ) - } + classpath = configurations.pycompile + mainClass = 'org.python.util.JycompileAntTask' + + /* + * Now use JycompileAntTask to compile the Python source we + * supply to users. The exclusions have been copied from build.xml, + * and also this comment: + + */ + def exclusions = ['test/**', 'lib2to3/tests/**', 'lib2to3/tests/data/myfixes/**'] + args = [ + '--srcdir', project.ext.buildLibDir, + '--destdir', project.ext.compiledLibDir, + '--excludes', exclusions.join(',') + ] } diff --git a/src/org/python/expose/generate/ExposeTask.java b/src/org/python/expose/generate/ExposeTask.java index 00518a217..8653fb6aa 100644 --- a/src/org/python/expose/generate/ExposeTask.java +++ b/src/org/python/expose/generate/ExposeTask.java @@ -88,4 +88,8 @@ private void write(String destClass, byte[] newClassfile) { } } } + + public static void main(String[] args) { + new ExposeTask().runFromCommandLine(args); + } } diff --git a/src/org/python/util/GlobMatchingTask.java b/src/org/python/util/GlobMatchingTask.java index 48d9e80ed..81a54d7f0 100644 --- a/src/org/python/util/GlobMatchingTask.java +++ b/src/org/python/util/GlobMatchingTask.java @@ -4,6 +4,8 @@ import java.util.Set; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.GlobPatternMapper; @@ -98,4 +100,86 @@ protected void checkParameters() throws BuildException { + "or is not a directory", getLocation()); } } + + public void runFromCommandLine(String[] args) { + Project project = new Project(); + project.init(); + project.setBaseDir(new File(".")); + DefaultLogger logger = new DefaultLogger(); + logger.setOutputPrintStream(System.out); + logger.setErrorPrintStream(System.err); + logger.setMessageOutputLevel(Project.MSG_INFO); + project.addBuildListener(logger); + setProject(project); + if (getTaskName() == null || getTaskName().isEmpty()) { + setTaskName(getClass().getSimpleName()); + } + + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + if ("--srcdir".equals(arg) || "-srcdir".equals(arg)) { + if (++i >= args.length) { + throw new IllegalArgumentException("Missing value for " + arg); + } + setSrcdir(new Path(project, args[i])); + } else if ("--destdir".equals(arg) || "-destdir".equals(arg)) { + if (++i >= args.length) { + throw new IllegalArgumentException("Missing value for " + arg); + } + setDestdir(new File(args[i])); + } else if ("--includesfile".equals(arg) || "-includesfile".equals(arg)) { + if (++i >= args.length) { + throw new IllegalArgumentException("Missing value for " + arg); + } + setIncludesfile(new File(args[i])); + } else if ("--includes".equals(arg) || "-includes".equals(arg)) { + if (++i >= args.length) { + throw new IllegalArgumentException("Missing value for " + arg); + } + setIncludes(args[i]); + } else if ("--excludesfile".equals(arg) || "-excludesfile".equals(arg)) { + if (++i >= args.length) { + throw new IllegalArgumentException("Missing value for " + arg); + } + setExcludesfile(new File(args[i])); + } else if ("--excludes".equals(arg) || "-excludes".equals(arg)) { + if (++i >= args.length) { + throw new IllegalArgumentException("Missing value for " + arg); + } + setExcludes(args[i]); + } else if (arg.startsWith("--srcdir=")) { + setSrcdir(new Path(project, arg.substring("--srcdir=".length()))); + } else if (arg.startsWith("-srcdir=")) { + setSrcdir(new Path(project, arg.substring("-srcdir=".length()))); + } else if (arg.startsWith("--destdir=")) { + setDestdir(new File(arg.substring("--destdir=".length()))); + } else if (arg.startsWith("-destdir=")) { + setDestdir(new File(arg.substring("-destdir=".length()))); + } else if (arg.startsWith("--includesfile=")) { + setIncludesfile(new File(arg.substring("--includesfile=".length()))); + } else if (arg.startsWith("-includesfile=")) { + setIncludesfile(new File(arg.substring("-includesfile=".length()))); + } else if (arg.startsWith("--includes=")) { + setIncludes(arg.substring("--includes=".length())); + } else if (arg.startsWith("-includes=")) { + setIncludes(arg.substring("-includes=".length())); + } else if (arg.startsWith("--excludesfile=")) { + setExcludesfile(new File(arg.substring("--excludesfile=".length()))); + } else if (arg.startsWith("-excludesfile=")) { + setExcludesfile(new File(arg.substring("-excludesfile=".length()))); + } else if (arg.startsWith("--excludes=")) { + setExcludes(arg.substring("--excludes=".length())); + } else if (arg.startsWith("-excludes=")) { + setExcludes(arg.substring("-excludes=".length())); + } else { + throw new IllegalArgumentException("Unknown argument: " + arg); + } + } + + if (destDir != null && !destDir.exists()) { + destDir.mkdirs(); + } + + execute(); + } } \ No newline at end of file diff --git a/src/org/python/util/JycompileAntTask.java b/src/org/python/util/JycompileAntTask.java index 17d002df2..e04efff7c 100644 --- a/src/org/python/util/JycompileAntTask.java +++ b/src/org/python/util/JycompileAntTask.java @@ -76,4 +76,8 @@ protected String getFrom() { protected String getTo() { return imp.makeCompiledFilename(getFrom()); } + + public static void main(String[] args) { + new JycompileAntTask().runFromCommandLine(args); + } } diff --git a/tests/java/org/python/util/GlobMatchingTaskTest.java b/tests/java/org/python/util/GlobMatchingTaskTest.java new file mode 100644 index 000000000..ce6e78656 --- /dev/null +++ b/tests/java/org/python/util/GlobMatchingTaskTest.java @@ -0,0 +1,129 @@ +package org.python.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Set; + +import org.apache.tools.ant.BuildException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.python.expose.generate.ExposeTask; + +class GlobMatchingTaskTest { + + static class DummyGlobMatchingTask extends GlobMatchingTask { + boolean processed = false; + Set receivedMatches; + + @Override + protected void process(Set matches) { + this.processed = true; + this.receivedMatches = matches; + } + + @Override + protected String getFrom() { + return "*.txt"; + } + + @Override + protected String getTo() { + return "*.txt"; + } + } + + @Test + void testCommandLineOptions(@TempDir Path tempDir) throws IOException { + Path src = tempDir.resolve("src"); + Path dest = tempDir.resolve("dest"); + Files.createDirectories(src); + + File f1 = Files.createFile(src.resolve("a.txt")).toFile(); + File f2 = Files.createFile(src.resolve("b.txt")).toFile(); + + DummyGlobMatchingTask task = new DummyGlobMatchingTask(); + task.runFromCommandLine(new String[] { + "--srcdir", src.toString(), + "--destdir", dest.toString() + }); + + assertTrue(task.processed); + assertNotNull(task.receivedMatches); + assertEquals(2, task.receivedMatches.size()); + assertTrue(task.receivedMatches.contains(f1)); + assertTrue(task.receivedMatches.contains(f2)); + assertTrue(Files.exists(dest)); + } + + @Test + void testCommandLineEqualsOptions(@TempDir Path tempDir) throws IOException { + Path src = tempDir.resolve("src"); + Path dest = tempDir.resolve("dest"); + Files.createDirectories(src); + + File f1 = Files.createFile(src.resolve("a.txt")).toFile(); + File f2 = Files.createFile(src.resolve("b.txt")).toFile(); + + DummyGlobMatchingTask task = new DummyGlobMatchingTask(); + task.runFromCommandLine(new String[] { + "--srcdir=" + src.toString(), + "--destdir=" + dest.toString(), + "--excludes=b.txt" + }); + + assertTrue(task.processed); + assertNotNull(task.receivedMatches); + assertEquals(1, task.receivedMatches.size()); + assertTrue(task.receivedMatches.contains(f1)); + assertFalse(task.receivedMatches.contains(f2)); + } + + @Test + void testUnknownArgument() { + DummyGlobMatchingTask task = new DummyGlobMatchingTask(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> { + task.runFromCommandLine(new String[] {"--unknown"}); + }); + assertTrue(ex.getMessage().contains("Unknown argument")); + } + + @Test + void testMissingArgumentValue() { + DummyGlobMatchingTask task = new DummyGlobMatchingTask(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> { + task.runFromCommandLine(new String[] {"--srcdir"}); + }); + assertTrue(ex.getMessage().contains("Missing value")); + } + + @Test + void testMissingRequiredParameters() { + DummyGlobMatchingTask task = new DummyGlobMatchingTask(); + BuildException ex = assertThrows(BuildException.class, () -> { + task.runFromCommandLine(new String[] {}); + }); + assertTrue(ex.getMessage().contains("srcdir attribute must be set")); + } + + @Test + void testExposeTaskMainMissingArgs() { + assertThrows(BuildException.class, () -> { + ExposeTask.main(new String[] {}); + }); + } + + @Test + void testJycompileAntTaskMainMissingArgs() { + assertThrows(BuildException.class, () -> { + JycompileAntTask.main(new String[] {}); + }); + } +}