applyDartDefinesToWorkflow function

String applyDartDefinesToWorkflow(
  1. String source, {
  2. Set<String> buildNames = const {},
  3. Set<String> statusCheckNames = const {},
  4. String? suffix,
})

Applies dart-define environment variables to the GitHub Actions workflow source.

When buildNames is not empty, a step that expands kProductionDartDefinesFile with Secrets <NAME>_<suffix> is inserted before the build step, and the build uses the expanded file. When statusCheckNames is not empty, Secrets <NAME> are passed to the status check action. source is returned unchanged when both are empty.

GitHub Actionsのワークフローsourceにdart-defineの環境変数を適用します。

buildNamesが空でない場合、Secrets<NAME>_<suffix>でkProductionDartDefinesFileを展開するステップをビルドステップの前に挿入し、ビルドで展開済みファイルを使用します。 statusCheckNamesが空でない場合、Secrets<NAME>をステータスチェックのアクションに渡します。 どちらも空の場合はsourceをそのまま返します。

Implementation

String applyDartDefinesToWorkflow(
  String source, {
  Set<String> buildNames = const {},
  Set<String> statusCheckNames = const {},
  String? suffix,
}) {
  var result = source;
  if (statusCheckNames.isNotEmpty) {
    result = result.replaceAll(
      "        uses: ./.github/actions/status_check\n",
      "        uses: ./.github/actions/status_check\n"
          "${_environmentBlock(statusCheckNames, null)}",
    );
  }
  if (buildNames.isEmpty) {
    return result;
  }
  final lines = result.split("\n");
  final resolved =
      "$kResolvedDartDefinesDirectory/${kProductionDartDefinesFile.split("/").last}";
  final output = <String>[];
  for (final line in lines) {
    if (!line.contains("--dart-define-from-file=$kProductionDartDefinesFile")) {
      output.add(line);
      continue;
    }
    var index =
        output.lastIndexWhere((e) => e.trimLeft().startsWith("- name:"));
    while (index > 0 && output[index - 1].trimLeft().startsWith("#")) {
      index--;
    }
    if (index >= 0) {
      output.insertAll(index, _resolveStep(buildNames, suffix).split("\n"));
    }
    output.add(
      line.replaceAll(
        "--dart-define-from-file=$kProductionDartDefinesFile",
        "--dart-define-from-file=$resolved",
      ),
    );
  }
  return output.join("\n");
}