replaceVersionWithTemplate function

String replaceVersionWithTemplate(
  1. String contents,
  2. String template,
  3. String version
)

Implementation

String replaceVersionWithTemplate(
  String contents,
  String template,
  String version,
) {
  if (template == '') {
    final regex = RegExp(validSemverRegex);
    return contents.replaceAll(regex, version);
  }

  final versionPosition = template.indexOf('[VERSION]');
  if (versionPosition == -1) {
    return contents;
  }
  final prefix = template.substring(0, versionPosition);
  final suffix = template.substring(versionPosition + 9);
  final regex = RegExp(
    "(${RegExp.escape(prefix)})($validSemverRegex)(${RegExp.escape(suffix)})",
  );

  return contents.replaceAllMapped(regex, (match) {
    return '${match.group(1)}$version${match.group(8)}';
  });
}