Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

option ownClassPath now can be a String or a List

  • Loading branch information...
commit a0d6ef40890de338e93e46aa4abf4ca28421e95c 1 parent 8f62608
@chiquitinxx authored
View
8 src/main/groovy/org/grooscript/GrooScript.groovy
@@ -6,7 +6,7 @@ package org.grooscript
class GrooScript {
def private static GsConverter converter
- def static String ownClassPath
+ def static ownClassPath
def static debug = false
def private static HEAD = '[GrooScript]'
@@ -74,10 +74,10 @@ class GrooScript {
/**
* Set the dir where all your groovy starts, the mainSource ( src/main/groovy, src/groovy, ..)
- * @param dir
+ * @param dir String or List
* @return
*/
- def static setOwnClassPath(String dir) {
+ def static setOwnClassPath(dir) {
ownClassPath = dir
}
@@ -99,8 +99,8 @@ class GrooScript {
newFile.write(jsResult)
if (debug) {
println "${HEAD} Result -> ${jsResult.size()}"
+ println '***** Converted file: '+newFile.name + ' *****'
}
- println 'Converted file: '+newFile.name
}
} catch (e) {
println "${HEAD} Convert Exception: "+e.message
View
33 src/main/groovy/org/grooscript/GsConverter.groovy
@@ -106,9 +106,12 @@ class GsConverter {
* @param String classPath to add to classpath
* @return String script in javascript
*/
- def toJs(String script,String classPath) {
+ def toJs(String script,Object classPath) {
def result
- //println 'Classpath->'+classPath
+ //Classpath must be a String or a list
+ if (classPath && !(classPath instanceof String || classPath instanceof Collection)) {
+ throw new Exception('The classpath must be a String or a List')
+ }
//Script not empty plz!
def phase = 0
if (script) {
@@ -124,14 +127,15 @@ class GsConverter {
// println '->'+it
//}
- if (classPath && classPath.trim()!='') {
+ if (classPath) {
//println 'cp->'+ classPath
list = getAstFromText(script,classPath)
//println 'list->'+ list
} else {
- list = new AstBuilder().buildFromString(CompilePhase.SEMANTIC_ANALYSIS,script)
+ //list = new AstBuilder().buildFromString(CompilePhase.SEMANTIC_ANALYSIS,script)
+ list = getAstFromText(script,null)
}
phase++
@@ -154,7 +158,7 @@ class GsConverter {
* @param classpath
* @return
*/
- def getAstFromText(text,classpath) {
+ def getAstFromText(text,Object classpath) {
//By default, convertDependencies = true
//All the imports in a file are added to the source to be compiled, if not added, compiler fails
@@ -169,10 +173,25 @@ class GsConverter {
def scriptClassName = "script" + System.currentTimeMillis()
GroovyClassLoader classLoader = new GroovyClassLoader()
- classLoader.addClasspath(classpath)
+ //Add classpath to classloader
+ if (classpath) {
+ if (classpath instanceof Collection) {
+ classpath.each {
+ classLoader.addClasspath(it)
+ }
+ } else {
+ classLoader.addClasspath(classpath)
+ }
+ }
GroovyCodeSource codeSource = new GroovyCodeSource(text, scriptClassName + ".groovy", "/groovy/script")
CompilerConfiguration conf = CompilerConfiguration.DEFAULT
- conf.setClasspath(classpath)
+ //Add classpath to configuration
+ if (classpath && classpath instanceof String) {
+ conf.setClasspath(classpath)
+ }
+ if (classpath && classpath instanceof Collection) {
+ conf.setClasspathList(classpath)
+ }
CompilationUnit cu = new CompilationUnit(conf, codeSource.codeSource, classLoader)
cu.addSource(codeSource.getName(), text);
cu.compile(CompilePhase.SEMANTIC_ANALYSIS.phaseNumber)
View
15 src/test/groovy/org/grooscript/TestConversionOptions.groovy
@@ -8,6 +8,12 @@ import spock.lang.Specification
*/
class TestConversionOptions extends Specification {
+ //Reset options of GrooScript
+ def setup() {
+ GrooScript.setOwnClassPath(null)
+ GrooScript.setConversionProperty('convertDependencies',true)
+ }
+
def 'check dependency resolution'() {
when:
@@ -45,6 +51,15 @@ class TestConversionOptions extends Specification {
then: 'Need class not converted'
result
!result.contains('function Need()')
+ }
+ def 'can set classpath as List'() {
+ when: 'we set classpath as list'
+ GrooScript.setOwnClassPath(['need'])
+ def String result = GrooScript.convert("class B { def Need c}")
+
+ then: 'not fails and Need converted'
+ result
+ result.contains('function Need()')
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.