registerDartDefinesSecrets function

Future<bool> registerDartDefinesSecrets({
  1. required String gh,
  2. required String path,
  3. String? suffix,
})

Stores the environment variables referenced by the dart-define file at path in GitHub Secrets.

Each value is read from the current environment and stored as <NAME>_<suffix>, or <NAME> when suffix is null. Variables with only default-value references are skipped when they are not set. Returns false and reports the names when a required variable is not set.

pathのdart-defineファイルが参照する環境変数をGitHub Secretsに保存します。

値は現在の環境変数から読み取り、<NAME>_<suffix>(suffixがnullの場合は<NAME>)として保存します。 デフォルト値付きの参照のみの変数は、未設定の場合スキップします。 必須の変数が未設定の場合は変数名を表示してfalseを返します。

Implementation

Future<bool> registerDartDefinesSecrets({
  required String gh,
  required String path,
  String? suffix,
}) async {
  final names = dartDefinesEnvironmentNames(path);
  if (names.isEmpty) {
    return true;
  }
  final required = dartDefinesEnvironmentNames(path, requiredOnly: true);
  final missing = required.where((name) {
    final value = Platform.environment[name];
    return value == null || value.isEmpty;
  }).toList();
  if (missing.isNotEmpty) {
    error(
      "Environment variable(s) referenced by `$path` are not set: ${missing.join(", ")}. Register them in Samurai Passwords and run again. `$path`が参照する環境変数が設定されていません。Samurai Passwordsに登録してから再実行してください。",
    );
    return false;
  }
  for (final name in names) {
    final value = Platform.environment[name];
    if (value == null || value.isEmpty) {
      continue;
    }
    final secret = _secretName(name, suffix);
    await githubSecret(
      "Store `$name` referenced by `$path` in `secrets.$secret`.",
      gh: gh,
      name: secret,
      value: value,
    );
  }
  return true;
}