installFromArchive method

String installFromArchive(
  1. String defaultDartSdkPath, {
  2. bool askUser = true,
})

Installs the latest version of DartSdk from the official google archives This is simply the process of downloading and extracting the sdk to the defaultDartSdkPath.

If askUser is true (the default) the user is asked to confirm the install path and can modifiy it if desired.

returns the directory where the dartSdk was installed.

Implementation

String installFromArchive(String defaultDartSdkPath, {bool askUser = true}) {
  // verbose(() => 'Architecture: ${SysInfo.kernelArchitecture}');
  final zipRelease = _fetchDartSdk();

  var installDir = defaultDartSdkPath;

  if (askUser) {
    installDir = _askForDartSdkInstallDir(defaultDartSdkPath);
  }

  Shell.current.withPrivileges(() {
    if (!exists(installDir)) {
      createDir(installDir, recursive: true);
    } else {
      print(
        'The install directory $installDir already exists. '
        'If you proceed all files under $installDir will be deleted.',
      );
      if (confirm('Proceed to delete $installDir')) {
        /// I've added this incase we have a failed install and
        /// need to do a restart.
        ///
        deleteDir(installDir);
      } else {
        throw InstallException(
            'Install Directory $installDir already exists.');
      }
    }
  });

  // Read the Zip file from disk.
  _extractDartSdk(zipRelease, installDir);
  delete(zipRelease);

  /// the archive creates a root of 'dart-sdk' we need to move
  /// all of the files directly under the [installDir] (/usr/bin/dart).
  print('Preparing dart sdk');
  moveTree(join(installDir, 'dart-sdk'), installDir, includeHidden: true);
  deleteDir(join(installDir, 'dart-sdk'));

  if (core.Settings().isLinux || core.Settings().isMacOS) {
    /// make execs executable.
    find('*', workingDirectory: join(installDir, 'bin'), recursive: false)
        .forEach((file) => posix.chmod(file, permission: '500'));
  }

  // The normal dart detection process won't work here
  // as dart is not on the path so for the moment we force it
  // to the path we just downloaded it to.
  setPathToDartSdk(installDir);

  return installDir;
}