generateWhiteLabelSource function
Generates the lib/white_label.g.dart source text for config's
tenant tenantId — a pure function (no file I/O) so it's directly
testable; see writeGeneratedFile for the CLI's actual file-writing
entry point.
envName selects which of TenantConfig.environments to bake in —
same tenant/brand, different runtime config (typically just
api_base_url) per deploy target (staging, production, ...).
null (the default) uses TenantConfig.environment, the tenant's
single default/fallback — unchanged behavior for a tenant that never
declared environments: at all. Throws ArgumentError if envName is
non-null but not declared for this tenant (see
TenantConfig.resolveEnvironment) — never silently falls back to the
default environment, since that would mean a "staging" build shipping
production's API URL without any indication it happened.
Implementation
String generateWhiteLabelSource(
WhiteLabelConfig config,
String tenantId, {
String? envName,
}) {
final List<String> tenantIds = config.tenants.keys.toList()..sort();
final TenantConfig selected = config[tenantId];
final TenantEnvironment resolvedEnvironment = selected.resolveEnvironment(
envName,
);
final String envSuffix = envName == null ? '' : ' (environment: "$envName")';
final buffer = StringBuffer()
..writeln('// GENERATED CODE - DO NOT MODIFY BY HAND')
..writeln('//')
..writeln('// Generated by `dart run white_label_kit:generate` from')
..writeln(
'// white_label.yaml for tenant "$tenantId"$envSuffix — re-run after',
)
..writeln('// editing white_label.yaml or changing tenants.')
..writeln('//')
..writeln('// SECURITY: only "$tenantId"\'s data is in this file — see')
..writeln("// lib/src/generation/dart_config_generator.dart's doc")
..writeln('// comment for why that matters.')
..writeln()
..writeln("import 'package:white_label_kit/white_label_kit.dart';")
..writeln()
..writeln('/// The `default_tenant` declared in white_label.yaml.')
..writeln(
'const String whiteLabelDefaultTenant = ${_dartString(config.defaultTenant)};',
)
..writeln()
..writeln(
'/// Every tenant id declared in white_label.yaml, sorted — NAMES\n'
'/// only, not their config. Fine to ship in every build; use this\n'
'/// for things like a tenant picker in an internal/dev build, not\n'
"/// for reading another tenant's actual data (which isn't here).",
)
..writeln(
'const List<String> whiteLabelTenantIds = ${_dartStringList(tenantIds)};',
)
..writeln()
..writeln(
"/// The environment name this build was generated for (\"staging\",\n"
'/// "production", ...), or an empty string if `--env` wasn\'t passed\n'
"/// (the tenant's default/fallback environment was used).",
)
..writeln(
'const String whiteLabelEnvironmentName = ${_dartString(envName ?? '')};',
)
..writeln()
..writeln(
"/// This build's tenant, resolved when `generate` last ran (see\n"
"/// resolveGeneratorTenantId) — the ONLY tenant's data in this file.",
)
..writeln(
'const WhiteLabelRuntime whiteLabelRuntime = '
'${_runtimeLiteral(selected, resolvedEnvironment)};',
);
return buffer.toString();
}