patchManifestPlaceholders function

String patchManifestPlaceholders(
  1. String content, {
  2. required String commit,
  3. String? tag,
})

Updates tagPlaceholder / commitPlaceholder in a manifest's raw YAML text.

If tag is null or empty, the tag: placeholder line is removed.

Implementation

String patchManifestPlaceholders(
  String content, {
  required String commit,
  String? tag,
}) {
  var result = content;

  final hasTagPlaceholder = result.contains(tagPlaceholder);
  final hasCommitPlaceholder = result.contains(commitPlaceholder);

  if (hasTagPlaceholder || hasCommitPlaceholder) {
    // Fast path: manifest still has template placeholders.
    if (tag != null && tag.isNotEmpty) {
      result = result.replaceAll(tagPlaceholder, tag);
    } else {
      result = result.replaceAll(
        RegExp(r'[ \t]*tag:\s*' + RegExp.escape(tagPlaceholder) + r'\n?'),
        '',
      );
    }
    result = result.replaceAll(commitPlaceholder, commit);
  } else {
    // Manifest was previously pinned — placeholders are gone.
    // Locate the app source block by its unique `disable-submodules: true`
    // marker and re-pin tag/commit in-place.
    result = result.replaceFirstMapped(
      RegExp(
        r'(?:[ \t]*tag:\s+\S+\n)?([ \t]*)commit:\s+\S+(?=\n[ \t]*disable-submodules:)',
        multiLine: true,
      ),
      (m) {
        final indent = m.group(1)!;
        final tagLine =
            (tag != null && tag.isNotEmpty) ? '${indent}tag: $tag\n' : '';
        return '$tagLine${indent}commit: $commit';
      },
    );
  }

  return result;
}