Search in sources :

Example 1 with Replacer

use of com.google.copybara.transform.RegexTemplateTokens.Replacer in project copybara by google.

the class ReferenceMigrator method transform.

@Override
public TransformationStatus transform(TransformWork work) throws ValidationException {
    AtomicReference<ValidationException> thrown = new AtomicReference<>();
    Replacer replacer = before.callbackReplacer(after, (groupValues, template) -> {
        if (groupValues.get(0) != null) {
            try {
                String destinationRef = findChange(groupValues.get(1), work.getMigrationInfo().getOriginLabel(), work.getMigrationInfo().destinationVisitable());
                if (destinationRef != null) {
                    // issue, a non-naive implementation might be required.
                    return Pattern.compile("[$]1").matcher(template).replaceAll(destinationRef);
                } else {
                    return groupValues.get(0);
                }
            } catch (ValidationException exception) {
                thrown.compareAndSet(null, exception);
                return groupValues.get(0);
            }
        }
        return template;
    }, false, false, null);
    String replaced = replacer.replace(work.getMessage());
    if (thrown.get() != null) {
        throw thrown.get();
    }
    if (!replaced.equals(work.getMessage())) {
        work.setMessage(replaced);
    }
    return TransformationStatus.success();
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Replacer(com.google.copybara.transform.RegexTemplateTokens.Replacer)

Example 2 with Replacer

use of com.google.copybara.transform.RegexTemplateTokens.Replacer in project copybara by google.

the class ReplaceMapper method apply.

@Override
public String apply(String s) {
    LoadingCache<Replace, Replacer> cache = REPLACE_CACHE.get();
    String replacement = s;
    try {
        for (Replace replace : replaces) {
            Replacer replacer = cache.get(replace);
            replacement = replacer.replace(replacement);
            if (all) {
                continue;
            }
            if (replacement.equals(s)) {
                continue;
            }
            return replacement;
        }
    } catch (ExecutionException e) {
        throw new RuntimeException("Shouldn't happen", e);
    }
    return replacement;
}
Also used : Replacer(com.google.copybara.transform.RegexTemplateTokens.Replacer) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Replacer (com.google.copybara.transform.RegexTemplateTokens.Replacer)2 ValidationException (com.google.copybara.exception.ValidationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1