Skip to content

Commit beb5de8

Browse files
committed
Enhance golo golo command to support folders and specific module
--files parse folders to load golo files --module is added to specify a Module name for the main function this command is valid $ golo golo --files samples/ --module hello.World Hello World! $ And - shell completion updated with --module - documentation for this enhancement - some samples have a new module name (to avoid duplication in classpath)
1 parent dd4d49b commit beb5de8

File tree

8 files changed

+73
-47
lines changed

8 files changed

+73
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ dependency-reduced-pom.xml
88
.benchmarks.h2.db
99
benchmarks/*.json
1010
benchmarks/*.html
11+
doc/output/

doc/basics.asciidoc

+8-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ follows:
7575
----
7676
$ golo golo --files samples/helloworld.golo
7777
Hello world!
78+
$ golo golo --files samples/ --module hello.World
79+
Hello world!
7880
$
7981
----
8082

81-
`golo golo` takes several Golo source files as input. It expects the last
82-
one to have a `main` function to call. The Golo code is compiled on the
83-
fly and executed straight into a JVM.
83+
`golo golo` takes several Golo source files as input (folders accepted).
84+
It expects the last one to have a `main` function to call (or use
85+
`--module` to define the golo module with the `main` function).
86+
The Golo code is compiled on the fly and executed straight into a JVM.
8487

8588
You may also pass arguments to the `main` function by appending `--args`
8689
on the command line invocation. Suppose that we have a module `EchoArgs`
@@ -158,7 +161,7 @@ Simple, isn't it?
158161
=== Passing JVM-specific flags ===
159162

160163
Both `golo` and `run` commands can be given JVM-specific flags using the `JAVA_OPTS` environment
161-
variable.
164+
variable.
162165

163166
As an example, the following runs `fibonacci.golo` and prints JIT compilation along the way:
164167

@@ -176,7 +179,7 @@ $ JAVA_OPTS=-XX:+PrintCompilation golo golo --files samples/fibonacci.golo
176179

177180
A bash script can be found in `share/shell-completion/` called `golo-bash-completion` that will provide autocomplete support for the `golo` and `vanilla-golo` CLI scripts. You may either `source` the script, or drop the script into your `bash_completion.d/` folder and restart your terminal.
178181

179-
TIP: Not sure where your `bash_completion.d/` folder is? Try `/etc/bash_completion.d/` on Linux or `/usr/local/etc/bash_completion.d/` for Mac Homebrew users.
182+
TIP: Not sure where your `bash_completion.d/` folder is? Try `/etc/bash_completion.d/` on Linux or `/usr/local/etc/bash_completion.d/` for Mac Homebrew users.
180183

181184
=== Zsh autocompletion ===
182185

@@ -515,4 +518,3 @@ You can of course take advantage of the `array` collection literal, too:
515518
let a = array[1, 2, 3, 4]
516519
let b = array["a", "b"]
517520
----
518-

samples/dynamic-object-person.golo

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
# Copyright 2012-2014 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
2-
#
2+
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
6-
#
6+
#
77
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
8+
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
module test
15+
module samples.DynamicObjectPerson
1616

17-
local function mrbean = -> DynamicObject():
18-
name("Mr Bean"):
17+
local function mrbean = -> DynamicObject():
18+
name("Mr Bean"):
1919
2020
define("toString", |this| -> this: name() + " <" + this: email() + ">")
2121

22-
function main = |args| {
22+
function main = |args| {
2323

2424
let bean = mrbean()
2525
println(bean: toString())
2626

2727
bean: email("[email protected]")
2828
println(bean: toString())
2929
}
30-

samples/swing-actionlistener.golo

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Copyright 2012-2014 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
2-
#
2+
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
6-
#
6+
#
77
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
8+
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
module test
15+
module samples.SwingActionListener
1616

1717
import java.awt.event
1818
import javax.swing
@@ -41,4 +41,3 @@ function main = |args| {
4141
frame: pack()
4242
frame: setVisible(true)
4343
}
44-

samples/swing-helloworld.golo

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Copyright 2012-2014 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
2-
#
2+
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
6-
#
6+
#
77
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
8+
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
module test
15+
module samples.SwingHelloWorld
1616

1717
import javax.swing
1818
import javax.swing.WindowConstants
@@ -29,4 +29,3 @@ function main = |args| {
2929
frame: pack()
3030
frame: setVisible(true)
3131
}
32-

samples/templates-chat-webapp.golo

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Copyright 2012-2014 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
2-
#
2+
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
6-
#
6+
#
77
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
8+
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
module samples.WebServer
15+
module samples.TemplatesChatWebapp
1616

1717
import java.lang
1818
import java.io

share/shell-completion/golo-bash-completion

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
_golo ()
1818
{
1919
local cur prev special i
20-
20+
2121
COMPREPLY=()
2222
_get_comp_words_by_ref cur prev
23-
23+
2424
# Find out if we have a special word and our previous flag.
2525
for (( i=1; i < ${#COMP_WORDS[@]}-1; i++ )); do
2626
if [[ ${COMP_WORDS[i]} == @(compile|run|golo|diagnose|doc|version|new) ]]; then
@@ -30,7 +30,7 @@ _golo ()
3030
lastflag=${COMP_WORDS[i]}
3131
fi
3232
done
33-
33+
3434
# We know the special and that we're not trying to use a flag.
3535
# We probably want to show files.
3636
if [[ $cur != -* && ! -z ${special} ]]; then
@@ -41,13 +41,13 @@ _golo ()
4141
;;
4242
esac
4343
fi
44-
44+
4545
# Our special wasn't defined. We need to start there.
4646
if [ -z ${special} ]; then
4747
COMPREPLY=( $( compgen -W 'version compile run golo new doc diagnose --help' -- "$cur" ) )
4848
return 0
4949
fi
50-
50+
5151
# Match against flags with specific options.
5252
case "$prev" in
5353
--format)
@@ -67,8 +67,8 @@ _golo ()
6767
return 0
6868
;;
6969
esac
70-
71-
# We know a special, but we're looking to learn the flags.
70+
71+
# We know a special, but we're looking to learn the flags.
7272
case "${special}" in
7373
compile)
7474
COMPREPLY=( $( compgen -W '--output' -- "$cur" ) )
@@ -80,7 +80,7 @@ _golo ()
8080
COMPREPLY=( $( compgen -W '--module --classpath' -- "$cur" ) )
8181
;;
8282
golo)
83-
COMPREPLY=( $( compgen -W '--files --args --classpath' -- "$cur" ) )
83+
COMPREPLY=( $( compgen -W '--files --args --classpath --module' -- "$cur" ) )
8484
;;
8585
diagnose)
8686
COMPREPLY=( $( compgen -W '--tool' -- "$cur" ) )

src/main/java/fr/insalyon/citi/golo/cli/Main.java

+37-11
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ static class RunCommand {
8585
@Parameters(commandDescription = "Dynamically loads and runs from Golo source files")
8686
static class GoloGoloCommand {
8787

88-
@Parameter(names = "--files", variableArity = true, description = "Golo source files (the last one has a main function)", required = true)
88+
@Parameter(names = "--files", variableArity = true, description = "Golo source files (the last one has a main function or use --module)", required = true)
8989
List<String> files = new LinkedList<>();
9090

91+
@Parameter(names = "--module", description = "The Golo module with a main function")
92+
String module;
93+
9194
@Parameter(names = "--args", variableArity = true, description = "Program arguments")
9295
List<String> arguments = new LinkedList<>();
9396

@@ -431,22 +434,45 @@ private static void golo(GoloGoloCommand gologolo) throws Throwable {
431434
GoloClassLoader loader = new GoloClassLoader(primaryClassLoader);
432435
Class<?> lastClass = null;
433436
for (String goloFile : gologolo.files) {
434-
File file = new File(goloFile);
435-
if (!file.exists()) {
436-
System.out.println("Error: " + file + " does not exist.");
437-
return;
438-
}
439-
if (!file.isFile()) {
440-
System.out.println("Error: " + file + " is not a file.");
441-
return;
437+
lastClass = loadGoloFile(goloFile, gologolo.module, loader);
438+
}
439+
if (lastClass == null && gologolo.module != null) {
440+
System.out.println("The module " + gologolo.module + " does not exist in the classpath.");
441+
return;
442+
}
443+
callRun(lastClass, gologolo.arguments.toArray(new String[gologolo.arguments.size()]));
444+
}
445+
446+
private static Class<?> loadGoloFile(String goloFile, String module, GoloClassLoader loader) throws Throwable {
447+
File file = new File(goloFile);
448+
if (!file.exists()) {
449+
System.out.println("Error: " + file.getAbsolutePath() + " does not exist.");
450+
return null;
451+
}
452+
if (file.isDirectory()) {
453+
File[] folderFiles = file.listFiles();
454+
if (folderFiles != null) {
455+
Class<?> lastClass = null;
456+
for (File folderFile : folderFiles) {
457+
Class<?> loadedClass = loadGoloFile(folderFile.getAbsolutePath(), module, loader);
458+
if (module == null || (loadedClass != null && loadedClass.getCanonicalName().equals(module))) {
459+
lastClass = loadedClass;
460+
}
461+
}
462+
return lastClass;
442463
}
464+
}
465+
if (file.getName().endsWith(".golo")) {
443466
try (FileInputStream in = new FileInputStream(file)) {
444-
lastClass = loader.load(file.getName(), in);
467+
Class<?> loadedClass = loader.load(file.getName(), in);
468+
if (module == null || loadedClass.getCanonicalName().equals(module)) {
469+
return loadedClass;
470+
}
445471
} catch (GoloCompilationException e) {
446472
handleCompilationException(e);
447473
}
448474
}
449-
callRun(lastClass, gologolo.arguments.toArray(new String[gologolo.arguments.size()]));
475+
return null;
450476
}
451477

452478
private static void doc(DocCommand options) {

0 commit comments

Comments
 (0)