use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class SeansBug method testMarkupBug.
public void testMarkupBug() throws Exception {
String[] lines = { "package groovy.xml", "", "b = new MarkupBuilder()", "", "b.root1(a:5, b:7) { ", " elem1('hello1') ", " elem2('hello2') ", " elem3(x:7) ", "}" };
String code = asCode(lines);
GroovyShell shell = new GroovyShell();
shell.evaluate(code);
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class PlatformLineWriterTest method testPlatformLineWriter.
public void testPlatformLineWriter() throws IOException, ClassNotFoundException {
String LS = System.getProperty("line.separator");
Binding binding = new Binding();
binding.setVariable("first", "Tom");
binding.setVariable("last", "Adams");
StringWriter stringWriter = new StringWriter();
Writer platformWriter = new PlatformLineWriter(stringWriter);
GroovyShell shell = new GroovyShell(binding);
platformWriter.write(shell.evaluate("\"$first\\n$last\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
stringWriter = new StringWriter();
platformWriter = new PlatformLineWriter(stringWriter);
platformWriter.write(shell.evaluate("\"$first\\r\\n$last\\r\\n\"").toString());
platformWriter.flush();
assertEquals("Tom" + LS + "Adams" + LS, stringWriter.toString());
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class GroovyMain method process.
/**
* Process the users request.
*
* @param line the parsed command line.
* @throws ParseException if invalid options are chosen
*/
private static boolean process(CommandLine line) throws ParseException, IOException {
List args = line.getArgList();
if (line.hasOption('D')) {
String[] values = line.getOptionValues('D');
for (int i = 0; i < values.length; i++) {
setSystemPropertyFrom(values[i]);
}
}
GroovyMain main = new GroovyMain();
// add the ability to parse scripts with a specified encoding
main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));
main.isScriptFile = !line.hasOption('e');
main.debug = line.hasOption('d');
main.conf.setDebug(main.debug);
main.processFiles = line.hasOption('p') || line.hasOption('n');
main.autoOutput = line.hasOption('p');
main.editFiles = line.hasOption('i');
if (main.editFiles) {
main.backupExtension = line.getOptionValue('i');
}
main.autoSplit = line.hasOption('a');
String sp = line.getOptionValue('a');
if (sp != null)
main.splitPattern = sp;
if (main.isScriptFile) {
if (args.isEmpty())
throw new ParseException("neither -e or filename provided");
main.script = (String) args.remove(0);
if (main.script.endsWith(".java"))
throw new ParseException("error: cannot compile file with .java extension: " + main.script);
} else {
main.script = line.getOptionValue('e');
}
main.processSockets = line.hasOption('l');
if (main.processSockets) {
// default port to listen to
String p = line.getOptionValue('l', "1960");
main.port = Integer.parseInt(p);
}
// we use "," as default, because then split will create
// an empty array if no option is set
String disabled = line.getOptionValue("disableopt", ",");
String[] deopts = disabled.split(",");
for (String deopt_i : deopts) {
main.conf.getOptimizationOptions().put(deopt_i, false);
}
if (line.hasOption("indy")) {
CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
main.conf.getOptimizationOptions().put("indy", true);
}
if (line.hasOption("basescript")) {
main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
}
if (line.hasOption("configscript")) {
String path = line.getOptionValue("configscript");
File groovyConfigurator = new File(path);
Binding binding = new Binding();
binding.setVariable("configuration", main.conf);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
shell.evaluate(groovyConfigurator);
}
main.args = args;
return main.run();
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class GroovyMain method processOnce.
/**
* Process the standard, single script with args.
*/
private void processOnce() throws CompilationFailedException, IOException, URISyntaxException {
GroovyShell groovy = new GroovyShell(conf);
setupContextClassLoader(groovy);
groovy.run(getScriptSource(isScriptFile, script), args);
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class GroovyMain method processFiles.
/**
* Process the input files.
*/
private void processFiles() throws CompilationFailedException, IOException, URISyntaxException {
GroovyShell groovy = new GroovyShell(conf);
setupContextClassLoader(groovy);
Script s = groovy.parse(getScriptSource(isScriptFile, script));
if (args.isEmpty()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
try {
processReader(s, reader, writer);
} finally {
reader.close();
writer.close();
}
} else {
Iterator i = args.iterator();
while (i.hasNext()) {
String filename = (String) i.next();
//TODO: These are the arguments for -p and -i. Why are we searching using Groovy script extensions?
// Where is this documented?
File file = huntForTheScriptFile(filename);
processFile(s, file);
}
}
}
Aggregations