diff --git a/.github/workflows/cli-unit-tests.yml b/.github/workflows/cli-unit-tests.yml index 49a9608..3c3e377 100644 --- a/.github/workflows/cli-unit-tests.yml +++ b/.github/workflows/cli-unit-tests.yml @@ -27,4 +27,4 @@ jobs: - name: Test with JBang run: | export PATH="$HOME/.jbang/bin:$PATH" - jbang TestJythonCli.java execute --disable-ansi-colors --select-class=TestJythonCli + jbang src/TestJythonCli.java execute --disable-ansi-colors --select-class=TestJythonCli diff --git a/README.md b/README.md index be1ace8..62adc83 100644 --- a/README.md +++ b/README.md @@ -259,10 +259,10 @@ On Linux or MacOS the JythonCli.java script can be run directly for testing purp ## Java Source File Formatting -Use the `google-java-format` to format the `JythonCli.java` and `TestJythonCli.java` programs. +Use command `jython-java-fmt` to format the `JythonCli.java` and `TestJythonCli.java` source files. ``` -jbang run com.google.googlejavaformat:google-java-format:1.29.0 --aosp -r *.java +jbang run jython-java-fmt ``` ## Articles about Jython and JBang diff --git a/jbang-catalog.json b/jbang-catalog.json index 4c004f3..54a454a 100644 --- a/jbang-catalog.json +++ b/jbang-catalog.json @@ -1,8 +1,23 @@ { "aliases": { "jython-cli": { - "script-ref": "JythonCli.java", + "script-ref": "src/JythonCli.java", "description": "Run Jython 2.7" + }, + "jython-java-fmt": { + "script-ref": "jbang-fmt@jbangdev/jbang-fmt", + "description": "Formats Java Source Code in src folder", + "arguments": [ + "--line-length", + "90", + "--java", + "8", + "--indent-with", + "space", + "--indent-size", + "4", + "src" + ] } } } diff --git a/JythonCli.java b/src/JythonCli.java similarity index 95% rename from JythonCli.java rename to src/JythonCli.java index a9210dc..acebfe6 100755 --- a/JythonCli.java +++ b/src/JythonCli.java @@ -111,7 +111,7 @@ void readJBangBlock(Reader script) throws IOException { boolean found = false; printIfDebug(""); printIfDebug("TOML data in Jython script:"); - while ((line = lines.readLine())!=null) { + while ((line = lines.readLine()) != null) { int lineno = lines.getLineNumber(); if (found && !line.startsWith("# ")) { found = false; @@ -150,19 +150,20 @@ void interpretJBangBlock() throws IOException { int lineno = 0; printIfDebug(""); printIfDebug("TOML data extracted from Jython script:"); - for (String line: tomlText.toString().split("\\n", -1)) { + for (String line : tomlText.toString().split("\\n", -1)) { lineno += 1; printIfDebug(lineno, line); } tpr = Toml.parse(tomlText.toString()); if (tpr.hasErrors()) { - for (TomlParseError err: tpr.errors()) { + for (TomlParseError err : tpr.errors()) { System.err.println(err.toString()); } if (debug) { throw new IOException("Error interpreting JBang TOML data."); } else { - throw new IOException("Error interpreting JBang TOML data. Re-run with '--cli-debug' for details."); + throw new IOException( + "Error interpreting JBang TOML data. Re-run with '--cli-debug' for details."); } } } @@ -282,8 +283,8 @@ public static void main(String[] args) { // Normally we have a script file (but it's optional) if (jythonCli.scriptFilename != null) { Reader script = new BufferedReader( - new InputStreamReader( - new FileInputStream(jythonCli.scriptFilename))); + new InputStreamReader( + new FileInputStream(jythonCli.scriptFilename))); jythonCli.readJBangBlock(script); jythonCli.interpretJBangBlock(); } diff --git a/TestJythonCli.java b/src/TestJythonCli.java similarity index 92% rename from TestJythonCli.java rename to src/TestJythonCli.java index c6a7443..cfd0f27 100644 --- a/TestJythonCli.java +++ b/src/TestJythonCli.java @@ -21,11 +21,9 @@ */ public class TestJythonCli { - static final String[] ARGS_DEBUG_FOO = - {"--cli-debug", "foo.py", "bar", "baz"}; - static final String[] ARGS_FOO = - {"--version", "foo.py", "bar.py", "baz"}; - static final String[] ARGS_NONE = {"--cli-debug"}; + static final String[] ARGS_DEBUG_FOO = { "--cli-debug", "foo.py", "bar", "baz" }; + static final String[] ARGS_FOO = { "--version", "foo.py", "bar.py", "baz" }; + static final String[] ARGS_NONE = { "--cli-debug" }; /** The {@code --cli-debug} flag is spotted */ @Test @@ -59,8 +57,7 @@ void testJythonArgs() throws IOException { @Test @Disabled("readJBangBlock does not throw on an unterminated block") void testUnterminated() throws IOException { - String script = - """ + String script = """ # /// jbang # requires-jython = "2.7.2" # requires-java = "17" @@ -78,16 +75,16 @@ void testUnterminated() throws IOException { @Test @Disabled("readJBangBlock treats '/// jbang' inside another block as valid start") void testGobbledBlock() throws IOException { - JythonCli cli = new JythonCli(); - processScript(cli, - """ + String script = """ # /// script # requires-python = ">=3.11" # /// jbang # requires-jython = "2.7.2" # requires-java = "8" # /// - """); + """; + JythonCli cli = new JythonCli(); + processScript(cli, script); assertTrue(cli.tomlText.isEmpty(), "Check TOML text is empty"); assertNull(cli.tpr, "Check TOML parse not done"); } @@ -101,8 +98,7 @@ void testGobbledBlock() throws IOException { @Test @Disabled("interpretJBangBlock treats '/// script' as valid terminator") void testCollision() throws IOException { - String script = - """ + String script = """ # /// jbang # requires-jython = "2.7.2" # requires-java = "8" @@ -120,8 +116,7 @@ void testCollision() throws IOException { @Test @Disabled("readJBangBlock does not throw on a second jbang block") void testTwoBlocks() throws IOException { - String script = - """ + String script = """ # /// jbang # requires-jython = "2.7.2" # requires-java = "8" @@ -143,8 +138,7 @@ void testTwoBlocks() throws IOException { /** Invalid TOML is an error. */ @Test void testInvalidTOML() throws IOException { - String script = - """ + String script = """ # /// jbang # requires-jython = "2.7.4" # requires-java = "21"