use of com.google.copybara.transform.FilterReplace in project copybara by google.
the class Core method filterReplace.
@SuppressWarnings("unused")
@StarlarkMethod(name = "filter_replace", doc = "Applies an initial filtering to find a substring to be replaced and then applies" + " a `mapping` of replaces for the matched text.", parameters = { @Param(name = "regex", named = true, doc = "A re2 regex to match a substring of the file", allowedTypes = { @ParamType(type = String.class) }), @Param(name = "mapping", named = true, doc = "A mapping function like core.replace_mapper or a dict with mapping values.", defaultValue = "{}"), @Param(name = "group", named = true, allowedTypes = { @ParamType(type = StarlarkInt.class), @ParamType(type = NoneType.class) }, doc = "Extract a regex group from the matching text and pass this as parameter to" + " the mapping instead of the whole matching text.", defaultValue = "None"), @Param(name = "paths", named = true, allowedTypes = { @ParamType(type = Glob.class), @ParamType(type = NoneType.class) }, doc = "A glob expression relative to the workdir representing the files to apply the" + " transformation. For example, glob([\"**.java\"]), matches all java files" + " recursively. Defaults to match all the files recursively.", defaultValue = "None"), @Param(name = "reverse", named = true, allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, doc = "A re2 regex used as reverse transformation", defaultValue = "None") }, useStarlarkThread = true)
@DocDefault(field = "paths", value = "glob([\"**\"])")
@DocDefault(field = "reverse", value = "regex")
@DocDefault(field = "group", value = "Whole text")
@Example(title = "Simple replace with mapping", before = "Simplest mapping", code = SIMPLE_FILTER_REPLACE_EXAMPLE)
@Example(title = "TODO replace", before = "This replace is similar to what it can be achieved with core.todo_replace:", code = TODO_FILTER_REPLACE_EXAMPLE)
public FilterReplace filterReplace(String regex, Object mapping, Object group, Object paths, Object reverse, StarlarkThread thread) throws EvalException {
ReversibleFunction<String, String> func = getMappingFunction(mapping);
String afterPattern = convertFromNoneable(reverse, regex);
int numGroup = convertFromNoneable(group, StarlarkInt.of(0)).toInt("group");
Pattern before = Pattern.compile(regex);
check(numGroup <= before.groupCount(), "group idx is greater than the number of groups defined in '%s'. Regex has %s groups", before.pattern(), before.groupCount());
Pattern after = Pattern.compile(afterPattern);
check(numGroup <= after.groupCount(), "reverse_group idx is greater than the number of groups defined in '%s'." + " Regex has %s groups", after.pattern(), after.groupCount());
return new FilterReplace(workflowOptions, before, after, numGroup, numGroup, func, convertFromNoneable(paths, Glob.ALL_FILES), thread.getCallerLocation());
}
Aggregations