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();
}
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;
}
Aggregations