Search in sources :

Example 1 with ScriptBlock

use of org.gradle.groovy.scripts.internal.ScriptBlock in project gradle by gradle.

the class ModelBlockTransformer method call.

/*
        TODO change this so that we extract all the information at compile time.

        At the moment we use the transform to:

        1. validate/restrict the syntax
        2. transform rules into something more robust (e.g. foo.bar.baz {} into configure("foo.bar.baz", {})) - no dynamic propertyMissing() nonsense
        3. hoist out input references (i.e. $()) into an annotation on rule closure classes to make available

        This means we actually have to execute the code block in order to find the rule information within.
        This is also problematic because it means we have to serialize this information into some form that fits into annotations.

        Later, we will extract all the “up-front” information we need to know during compile time.
        This will mean that we only need to execute the rules themselves, and not any code to actually register the rules.
     */
@Override
public void call(SourceUnit source) throws CompilationFailedException {
    List<Statement> statements = source.getAST().getStatementBlock().getStatements();
    for (Statement statement : statements) {
        ScriptBlock scriptBlock = AstUtils.detectScriptBlock(statement, SCRIPT_BLOCK_NAMES);
        if (scriptBlock == null) {
            // Look for model(«») (i.e. call to model with anything other than non literal closure)
            MethodCallExpression methodCall = AstUtils.extractBareMethodCall(statement);
            if (methodCall == null) {
                continue;
            }
            String methodName = AstUtils.extractConstantMethodName(methodCall);
            if (methodName == null) {
                continue;
            }
            if (methodName.equals(MODEL)) {
                source.getErrorCollector().addError(new SyntaxException(NON_LITERAL_CLOSURE_TO_TOP_LEVEL_MODEL_MESSAGE, statement.getLineNumber(), statement.getColumnNumber()), source);
            }
        } else {
            RuleVisitor ruleVisitor = new RuleVisitor(source, scriptSourceDescription, location);
            RulesVisitor rulesVisitor = new RulesVisitor(source, ruleVisitor);
            scriptBlock.getClosureExpression().getCode().visit(rulesVisitor);
        }
    }
}
Also used : ScriptBlock(org.gradle.groovy.scripts.internal.ScriptBlock) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Aggregations

MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)1 ScriptBlock (org.gradle.groovy.scripts.internal.ScriptBlock)1