<commit_before>import ceylon.build.task {
    Writer,
    setContextForTask,
    clearTaskContext
}

String argumentPrefix = "-D";

ExecutionResult runGoals([String*] goals, [String*] arguments, GoalDefinitions definitions, Writer writer) {
    value results = SequenceBuilder<GoalExecutionResult>();
    if (goals.empty) {
        if (definitions.availableGoals.empty) {
            writer.error("# no available goals");
            if (!definitions.internalGoals.empty) {
                writer.error("# following internal goals exist: ``displayGoalsList(definitions.internalGoals)``");
                writer.error("# did you forgot to make them shared?");
            }
        } else {
            writer.error("# no goal to run, available goals are: ``displayGoalsList(definitions.availableGoals)``");
        }
        return ExecutionResult([], noGoalToRun);
    } else {
        Status status;
        writer.info("# running goals: ``goals`` in order");
        for (goal in goals) {
            value goalArguments = filterArgumentsForGoal(goal, arguments);
            writer.info("# running ``goal``(``", ".join(goalArguments)``)");
            value result = executeTasks(goal, definitions, goalArguments, writer);
            results.append(result);
            if (!result.success) {
                status = errorOnTaskExecution;
                results.appendAll(notRunGoalsExecutionResult(goals.skipping(results.size), arguments));
                break;
            }
        } else {
            status = success;
        }
        return ExecutionResult(results.sequence, status);
    }
}

String displayGoalsList([String*] goals) => "[``", ".join(goals)``]";

[String*] filterArgumentsForGoal(String goal, [String*] arguments) {
    String prefix = "``argumentPrefix````goal``:";
    return [for (argument in arguments) if (argument.startsWith(prefix)) argument.spanFrom(prefix.size)];
}

GoalExecutionResult executeTasks(String goal, GoalDefinitions definitions, String[] arguments, Writer writer) {
    value properties = definitions.properties(goal);
    value outcome = executeTask(properties.task, arguments, writer);
    reportOutcome(outcome, goal, writer);
    return GoalExecutionResult(goal, arguments, outcome);
}

Outcome executeTask(Anything() task, [String*] arguments, Writer writer) {
    variable Outcome outcome;
    setContextForTask(arguments, writer);
    try {
        task();
        outcome = ok;
    } catch (Exception exception) {
        outcome = Failure(exception);
    }
    clearTaskContext();
    return outcome;
}

void reportOutcome(Outcome outcome, String goal, Writer writer) {
    if (is Failure outcome) {<commit_msg>rename method (replace Tasks by Goal)<commit_after>import ceylon.build.task {
    Writer,
    setContextForTask,
    clearTaskContext
}

String argumentPrefix = "-D";

ExecutionResult runGoals([String*] goals, [String*] arguments, GoalDefinitions definitions, Writer writer) {
    value results = SequenceBuilder<GoalExecutionResult>();
    if (goals.empty) {
        if (definitions.availableGoals.empty) {
            writer.error("# no available goals");
            if (!definitions.internalGoals.empty) {
                writer.error("# following internal goals exist: ``displayGoalsList(definitions.internalGoals)``");
                writer.error("# did you forgot to make them shared?");
            }
        } else {
            writer.error("# no goal to run, available goals are: ``displayGoalsList(definitions.availableGoals)``");
        }
        return ExecutionResult([], noGoalToRun);
    } else {
        Status status;
        writer.info("# running goals: ``goals`` in order");
        for (goal in goals) {
            value goalArguments = filterArgumentsForGoal(goal, arguments);
            writer.info("# running ``goal``(``", ".join(goalArguments)``)");
            value result = executeGoal(goal, definitions, goalArguments, writer);
            results.append(result);
            if (!result.success) {
                status = errorOnTaskExecution;
                results.appendAll(notRunGoalsExecutionResult(goals.skipping(results.size), arguments));
                break;
            }
        } else {
            status = success;
        }
        return ExecutionResult(results.sequence, status);
    }
}

String displayGoalsList([String*] goals) => "[``", ".join(goals)``]";

[String*] filterArgumentsForGoal(String goal, [String*] arguments) {
    String prefix = "``argumentPrefix````goal``:";
    return [for (argument in arguments) if (argument.startsWith(prefix)) argument.spanFrom(prefix.size)];
}

GoalExecutionResult executeGoal(String goal, GoalDefinitions definitions, String[] arguments, Writer writer) {
    value properties = definitions.properties(goal);
    value outcome = executeTask(properties.task, arguments, writer);
    reportOutcome(outcome, goal, writer);
    return GoalExecutionResult(goal, arguments, outcome);
}

Outcome executeTask(Anything() task, [String*] arguments, Writer writer) {
    variable Outcome outcome;
    setContextForTask(arguments, writer);
    try {
        task();
        outcome = ok;
    } catch (Exception exception) {
        outcome = Failure(exception);
    }
    clearTaskContext();
    return outcome;
}

void reportOutcome(Outcome outcome, String goal, Writer writer) {
    if (is Failure outcome) {
{'fraction_non_alphanumeric': 0.12165178571428571, 'fraction_numerical': 0.0, 'mean_word_length': 3.0734848484848483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3737918', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1407, 'n_words': 359}