acquire method

Future<Workspace> acquire(
  1. String platform, {
  2. RefSource? source,
})

Locks a free workspace for platform (creating one if needed) and syncs the project into it, or, with source, that ref's files. Each ref has workspaces of its own, so switching between building it and the working copy doesn't throw away either's build caches.

Implementation

Future<Workspace> acquire(String platform, {RefSource? source}) async {
  final pool = source == null
      ? root
      : Directory(p.join(root.path, RefSource.poolName(source.ref)));
  pool.createSync(recursive: true);
  for (var n = 1;; n++) {
    final dir = Directory(p.join(pool.path, '$platform-$n'));
    dir.createSync(recursive: true);
    final lock = File(p.join(dir.path, _lockName));
    if (_tryLock(lock)) {
      final ws = Workspace(dir, lock);
      try {
        if (source == null) {
          await sync(Directory(projectDir), dir);
        } else {
          // Generated code stays, so build_runner only redoes what changed.
          await sync(source.tree, dir, keep: RefSource.isGenerated);
        }
        rewriteExternalPathDependencies(dir.path);
      } catch (e) {
        ws.release();
        throw BuildException(
            'Could not prepare a build workspace at ${dir.path}: $e',
            fix:
                'Check free disk space, or run "udara_cli clean --workspaces" to reset them.');
      }
      return ws;
    }
  }
}