Search in sources :

Example 1 with Token

use of com.google.copybara.templatetoken.Token in project copybara by google.

the class Workflow method getMigrationIdentity.

/**
 * Migration identity tries to create a stable identifier for the migration that is stable between
 * Copybara invocations for the same reference. For example it will contain the copy.bara.sky
 * config file location relative to the root, the workflow name or the context reference used in
 * the request.
 *
 * <p>This identifier can be used by destinations to reuse code reviews, etc.
 */
String getMigrationIdentity(Revision requestedRevision, TransformWork transformWork) {
    boolean contextRefDefined = requestedRevision.contextReference() != null;
    String ctxRef = contextRefDefined ? requestedRevision.contextReference() : requestedRevision.asString();
    if (changeIdentity.isEmpty()) {
        return computeIdentity("ChangeIdentity", ctxRef);
    }
    StringBuilder sb = new StringBuilder();
    for (Token token : changeIdentity) {
        if (token.getType().equals(TokenType.LITERAL)) {
            sb.append(token.getValue());
        } else if (token.getValue().equals(COPYBARA_CONFIG_PATH_IDENTITY_VAR)) {
            sb.append(mainConfigFile.relativeToRoot());
        } else if (token.getValue().equals(COPYBARA_WORKFLOW_NAME_IDENTITY_VAR)) {
            sb.append(this.name);
        } else if (token.getValue().equals(COPYBARA_REFERENCE_IDENTITY_VAR)) {
            sb.append(ctxRef);
        } else if (token.getValue().startsWith(COPYBARA_REFERENCE_LABEL_VAR)) {
            String label = token.getValue().substring(COPYBARA_REFERENCE_LABEL_VAR.length());
            String labelValue = transformWork.getLabel(label);
            if (labelValue == null) {
                console.warn(String.format("Couldn't find label '%s'. Using the default identity algorithm", label));
                return computeIdentity("ChangeIdentity", ctxRef);
            }
            sb.append(labelValue);
        }
    }
    return hashIdentity(MoreObjects.toStringHelper("custom_identity").add("text", sb.toString()));
}
Also used : Token(com.google.copybara.templatetoken.Token)

Example 2 with Token

use of com.google.copybara.templatetoken.Token in project copybara by google.

the class RegexTemplateTokens method after.

/**
 * How this template can be used when it is the "after" value of core.replace - as a string to
 * insert in place of the regex, possibly including $N, referring to captured groups.
 *
 * <p>Returns a template in which the literals are escaped (if they are a $ or {) and the
 * interpolations appear as $N, where N is the group's index as given by {@code groupIndexes}.
 */
private String after(RegexTemplateTokens before) {
    StringBuilder template = new StringBuilder();
    for (Token token : tokens) {
        switch(token.getType()) {
            case INTERPOLATION:
                template.append("$").append(before.groupIndexes.get(token.getValue()).iterator().next());
                break;
            case LITERAL:
                for (int c = 0; c < token.getValue().length(); c++) {
                    char thisChar = token.getValue().charAt(c);
                    if (thisChar == '$' || thisChar == '\\') {
                        template.append('\\');
                    }
                    template.append(thisChar);
                }
                break;
            default:
                throw new IllegalStateException(token.getType().toString());
        }
    }
    return template.toString();
}
Also used : Token(com.google.copybara.templatetoken.Token)

Example 3 with Token

use of com.google.copybara.templatetoken.Token in project copybara by google.

the class RegexTemplateTokens method buildBefore.

/**
 * Converts this sequence of tokens into a regex which can be used to search a string. It
 * automatically quotes literals and represents interpolations as named groups.
 *
 * <p>It also fills groupIndexes with all the interpolation locations.
 *
 * @param regexesByInterpolationName map from group name to the regex to interpolate when the
 * group is mentioned
 * @param repeatedGroups true if a regex group is allowed to be used multiple times
 */
private Pattern buildBefore(Map<String, Pattern> regexesByInterpolationName, boolean repeatedGroups) throws EvalException {
    int groupCount = 1;
    StringBuilder fullPattern = new StringBuilder();
    for (Token token : tokens) {
        switch(token.getType()) {
            case INTERPOLATION:
                Pattern subPattern = regexesByInterpolationName.get(token.getValue());
                if (subPattern == null) {
                    throw new EvalException(location, "Interpolation is used but not defined: " + token.getValue());
                }
                fullPattern.append(String.format("(%s)", subPattern.pattern()));
                if (groupIndexes.get(token.getValue()).size() > 0 && !repeatedGroups) {
                    throw new EvalException(location, "Regex group is used in template multiple times: " + token.getValue());
                }
                groupIndexes.put(token.getValue(), groupCount);
                groupCount += subPattern.groupCount() + 1;
                break;
            case LITERAL:
                fullPattern.append(Pattern.quote(token.getValue()));
                break;
            default:
                throw new IllegalStateException(token.getType().toString());
        }
    }
    return Pattern.compile(fullPattern.toString(), Pattern.MULTILINE);
}
Also used : Pattern(com.google.re2j.Pattern) Token(com.google.copybara.templatetoken.Token) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 4 with Token

use of com.google.copybara.templatetoken.Token in project copybara by google.

the class Core method getChangeIdentity.

private static ImmutableList<Token> getChangeIdentity(Object changeIdentityObj, Location location) throws EvalException {
    String changeIdentity = SkylarkUtil.convertFromNoneable(changeIdentityObj, null);
    if (changeIdentity == null) {
        return ImmutableList.of();
    }
    ImmutableList<Token> result = new Parser(location).parse(changeIdentity);
    boolean configVarFound = false;
    for (Token token : result) {
        if (token.getType() != TokenType.INTERPOLATION) {
            continue;
        }
        if (token.getValue().equals(Workflow.COPYBARA_CONFIG_PATH_IDENTITY_VAR)) {
            configVarFound = true;
            continue;
        }
        if (token.getValue().equals(Workflow.COPYBARA_WORKFLOW_NAME_IDENTITY_VAR) || token.getValue().equals(Workflow.COPYBARA_REFERENCE_IDENTITY_VAR) || token.getValue().startsWith(Workflow.COPYBARA_REFERENCE_LABEL_VAR)) {
            continue;
        }
        throw new EvalException(location, String.format("Unrecognized variable: %s", token.getValue()));
    }
    if (!configVarFound) {
        throw new EvalException(location, String.format("${%s} variable is required", Workflow.COPYBARA_CONFIG_PATH_IDENTITY_VAR));
    }
    return result;
}
Also used : Token(com.google.copybara.templatetoken.Token) EvalException(com.google.devtools.build.lib.syntax.EvalException) Parser(com.google.copybara.templatetoken.Parser)

Aggregations

Token (com.google.copybara.templatetoken.Token)4 EvalException (com.google.devtools.build.lib.syntax.EvalException)2 Parser (com.google.copybara.templatetoken.Parser)1 Pattern (com.google.re2j.Pattern)1