create method

Future create(
  1. String distribution,
  2. String filename,
  3. String installPath,
  4. dynamic status(
    1. String
    ),
  5. {bool image = false}
)

Import a WSL distro by name

Implementation

Future<dynamic> create(String distribution, String filename,
    String installPath, Function(String) status,
    {bool image = false}) async {
  if (installPath == '') {
    installPath = getInstancePath(distribution).path;
  } else {
    installPath = SafePath(installPath).path;
  }

  // Download
  String downloadPath = getDistroPath().file('$filename.tar.gz');
  String downloadPathTmp = getDistroPath().file('$filename.tar.gz.tmp');
  bool fileExists = await File(downloadPath).exists();
  if (!image && distroRootfsLinks[filename] != null && !fileExists) {
    String url = distroRootfsLinks[filename]!;
    // Download file
    try {
      var downloader = ChunkedDownloader(
          url: url,
          saveFilePath: downloadPathTmp,
          onProgress: (int count, int total, double speed) {
            status('${'downloading-text'.i18n()}'
                ' ${(count / total * 100).toStringAsFixed(0)}%');
          })
        ..start();
      // Await download
      while (!downloader.done) {
        await Future.delayed(const Duration(milliseconds: 500));
      }
      File file = File(downloadPathTmp);
      file.rename(downloadPath);
      status('${'downloaded-text'.i18n()} $filename');
    } catch (error) {
      status('${'errordownloading-text'.i18n()} $filename');
    }
  }

  // Downloaded or extracted
  if (!image && distroRootfsLinks[filename] == null) {
    downloadPath = filename;
  }

  // Create from local file
  ProcessResult results = await Process.run(
      'wsl', ['--import', distribution, installPath, downloadPath],
      stdoutEncoding: null);
  String output = utf8Convert(results.stdout);

  return results;
}