install method

Future<void> install(
  1. ServiceDescriptor d, {
  2. bool force = false,
  3. bool startNow = true,
})

Installs (or, with force, replaces) the task for d and optionally starts it immediately.

Implementation

Future<void> install(
  svc.ServiceDescriptor d, {
  bool force = false,
  bool startNow = true,
}) async {
  final tn = taskName(d.serviceName);
  final log = logPathFor(d);
  Directory(File(log).parent.path).createSync(recursive: true);

  // Best-effort removal of a prior, broken SCM registration from the old
  // code path so the two backends do not collide.
  await _run('sc.exe', ['delete', _scmName(d.serviceName)]);

  final xml = buildTaskXml(d, logPath: log, currentUser: _currentUser());
  // `schtasks /Create /XML` requires a UTF-16 file; a UTF-8 one fails with
  // "cannot switch encoding".
  final tmp = File(
    '${Directory.systemTemp.createTempSync('omnyshell_task').path}'
    '${Platform.pathSeparator}$xmlBasename',
  )..writeAsBytesSync(encodeUtf16Le(xml));
  try {
    await _check(
      'schtasks.exe',
      createArgs(tn, tmp.path, force: force),
      'create task',
    );
  } finally {
    try {
      tmp.parent.deleteSync(recursive: true);
    } on Object {
      // Temp cleanup is best-effort.
    }
  }
  if (startNow) {
    await _check('schtasks.exe', runArgs(tn), 'start task');
  }
}