Created
May 16, 2013 08:31
-
-
Save danielpetisme/5590262 to your computer and use it in GitHub Desktop.
JGolo full Golo
https://github.com/danielpetisme/jgolo/blob/full-golo/src/main/golo/jgolo/JGolo.golo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gologolo src/main/golo/jgolo/JGolo.golo --args scripts/main.golo | |
Scanning path:/var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137 | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/src/main/golo/jgolo/JGolo.golo | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/scripts/main.golo | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/scripts/libs/extensions.golo | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/scripts/libs/core.golo | |
hello world | |
hello world from extensions | |
$ gologolo src/main/golo/jgolo/JGolo.golo --args scripts/main.golo index | |
Scanning path:/var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137 | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/src/main/golo/jgolo/JGolo.golo | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/scripts/main.golo | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/scripts/libs/extensions.golo | |
Loading: /var/lib/stickshift/5190de04e0b8cd7346000066/app-root/data/496137/scripts/libs/core.golo | |
TADA!!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module JGolo | |
import java.io.File | |
import java.nio.file.Files | |
import java.nio.file.LinkOption | |
import java.nio.file.StandardOpenOption | |
import java.nio.file.FileSystems | |
import java.util.ArrayList | |
import java.util.HashMap | |
import fr.insalyon.citi.golo.compiler.GoloClassLoader | |
function GOLO_PATTERN = -> "**.golo" | |
function DEFAULT_PATH = -> FileSystems.getDefault(): getPath(""): toAbsolutePath() | |
function DEFAULT_SCRIPT = -> "scripts/main.golo" | |
function DEFAULT_METHOD = -> "main" | |
pimp java.lang.Iterable { | |
function each = |this, func| { | |
foreach (element in this) { | |
func(element) | |
} | |
} | |
function map = |this, func| { | |
let mapped = this: newWithSameType() | |
foreach (element in this) { | |
mapped: append(func(element)) | |
} | |
return mapped | |
} | |
function reduce = |this, initialValue, func| { | |
var acc = initialValue | |
foreach (element in this) { | |
acc = func(acc, element) | |
} | |
return acc | |
} | |
} | |
pimp java.nio.file.Path { | |
function listFiles = |this| -> Files.newDirectoryStream(this): | |
reduce(ArrayList(), |list, it| { | |
if (Files.isDirectory(it, LinkOption.NOFOLLOW_LINKS())) { | |
list: addAll(it: listFiles()) | |
} else { | |
list: add(it) | |
} | |
return list | |
}) | |
function matches = |this, pattern| -> FileSystems.getDefault(): getPathMatcher("glob:" + pattern): matches(this) | |
function filterFiles = |this, pattern| -> this: listFiles(): filter(|it| -> it: matches(pattern)) | |
} | |
local function loadFiles = |loader, files| -> files: reduce(HashMap(), |loaded, it| { | |
println("Loading: " + it) | |
loaded: put(it: toAbsolutePath(), loader: load(it: toAbsolutePath(): toString(), Files.newInputStream(it: toAbsolutePath(), StandardOpenOption.READ()))) | |
return loaded | |
}) | |
local function discover = |path| -> path: filterFiles(GOLO_PATTERN()) | |
local function invoke = | klass, method, args| -> klass: getMethod(method, java.lang.Object.class): invoke(null, args) | |
function main = |args| { | |
let path = DEFAULT_PATH() | |
var script = DEFAULT_SCRIPT() | |
var method = DEFAULT_METHOD() | |
if ( alength(args) isnt 0 ) { | |
script = aget(args,0) | |
if ( alength(args) > 1 ) { | |
method = aget(args,1) | |
} | |
} | |
println("Scanning path:" + path) | |
let modulesLoaded = loadFiles(GoloClassLoader(), discover(path)) | |
let absoluteScriptPath = FileSystems.getDefault(): getPath(script): toAbsolutePath() | |
let klass = modulesLoaded: get(absoluteScriptPath) | |
invoke(klass, method, null) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
args: length()
now instead ofalength(args)
.