copyToRootEnv method

Future<File> copyToRootEnv(
  1. File sourceEnv, {
  2. bool trackForCleanup = true,
})

Copies a client env file to the root .env for the Flutter build.

When trackForCleanup is true, an existing root .env is backed up so cleanup can restore it, and a marker is recorded when no root .env existed so cleanup removes the copy again.

Implementation

Future<File> copyToRootEnv(File sourceEnv,
    {bool trackForCleanup = true}) async {
  if (!sourceEnv.existsSync()) {
    throw BuildException(
      'Source environment file missing at "${sourceEnv.path}"',
      fix:
          'Verify the client directory contains the expected environment file.',
    );
  }

  final target = File(p.join(projectDir, '.env'));
  try {
    if (trackForCleanup) {
      if (target.existsSync()) {
        await createBackup(target);
      } else {
        await markCreated(target);
      }
    }
    return await sourceEnv.copy(target.path);
  } catch (e, s) {
    if (e is BuildException) rethrow;
    throw BuildException(
      'Failed to copy environment file to root directory.',
      fix: 'Check write permissions for "${target.path}".',
      originalStackTrace: s,
    );
  }
}