private void ExecuteSteps() { Task.Run(async() => { try { // step 1 --- download Flutter SDK OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = 0, Status = "Memulai unduhan..." }); _downloadPath = Path.GetTempFileName(); var uri = _flutterGitClient.GetFlutterDownloadUri(FlutterVersion); _logger.LogDebug("Flutter download URI {0}", uri); _logger.LogDebug("Flutter download path {0}", _downloadPath); _logger.LogInformation("Start Flutter download"); using var response = await _httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, _cancellationToken); using var downloadStream = await response.Content.ReadAsStreamAsync(_cancellationToken); using var fileWriterStream = new FileStream(_downloadPath, FileMode.Create); var totalSize = response.Content.Headers.ContentLength ?? 1; var totalDownloaded = 0D; var buffer = new byte[8192]; var readed = 0; do { readed = await downloadStream.ReadAsync(buffer, _cancellationToken); if (readed == 0) { break; } await fileWriterStream.WriteAsync(buffer.AsMemory(0, readed), _cancellationToken); totalDownloaded += readed; var progressPercentage = Convert.ToInt32(totalDownloaded / totalSize * 100.0); _logger.LogDebug("Download progress {0}/{1} ({2}%)", totalDownloaded, totalSize, progressPercentage); OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = progressPercentage, Status = "Mengunduh paket instalasi..." }); } while (readed > 0); // step 2 --- extract to installfolder OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = 45, Status = "Mengekstrak instalasi..." }); _logger.LogInformation("Start Flutter extraction"); _cancellationSource?.Token.ThrowIfCancellationRequested(); await fileWriterStream.DisposeAsync(); ZipFile.ExtractToDirectory(_downloadPath, InstallPath); // step 3 --- set environment variable OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = 70, Status = "Mengatur environment variable..." }); _logger.LogInformation("Start Flutter environment configuration"); _cancellationToken.ThrowIfCancellationRequested(); var pathEnv = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); if (pathEnv?.Contains(@"flutter\bin") == false) { pathEnv += ";" + Path.Combine(InstallPath, @"flutter\bin"); Debug.Print(pathEnv); //Environment.SetEnvironmentVariable("PATH", pathEnv, EnvironmentVariableTarget.User); } // --- all done! _logger.LogInformation("Flutter install sequence finished"); OnInstallFinished(this, new FlutterInstallFinishedEventArgs { ErrorMessage = "Instalasi selesai.", Success = true }); } catch (TaskCanceledException cancelledEx) { _logger.LogInformation("Flutter install sequence cancelled.", cancelledEx); OnInstallFinished(this, new FlutterInstallFinishedEventArgs { ErrorMessage = "Instalasi dibatalkan.", Success = false }); } catch (Exception ex) { _logger.LogError("Flutter install sequence failed", ex); OnInstallFinished(this, new FlutterInstallFinishedEventArgs { ErrorMessage = ex.Message, Success = false }); } }); }
private void ExecuteSteps() { Task.Run(async() => { try { // step 1 --- download Flutter SDK OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = 0, Status = "Memulai unduhan..." }); _downloadPath = Path.GetTempFileName(); var uri = _flutterService.GetFlutterDownloadUri(FlutterVersion); _logger.DebugFormat("Flutter download path {0}", _downloadPath); _logger.DebugFormat("Flutter URI path {0}", uri); _logger.Info("Start Flutter download"); _cancellationSource.Token.Register(() => _webClient.CancelAsync()); await _webClient.DownloadFileTaskAsync(new Uri(uri), _downloadPath); // step 2 --- extract to installfolder OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = 0, Status = "Memulai ekstraksi..." }); _logger.Info("Start Flutter extraction"); _cancellationSource.Token.ThrowIfCancellationRequested(); using (var zip = new ZipFile(_downloadPath)) { var totalEntries = zip.Entries.Count; var processedEntry = 0; foreach (var entry in zip) // non-parallelism, the lib doesn't support parallel { _cancellationSource.Token.ThrowIfCancellationRequested(); var path = Path.GetFullPath(Path.Combine(InstallPath, "../" + entry.FileName)); if (entry.IsDirectory) { Directory.CreateDirectory(path); } else { using (var fs = new FileStream(path, FileMode.Create)) { entry.Extract(fs); } } Interlocked.Increment(ref processedEntry); OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = (int)((double)processedEntry / totalEntries * 100), Status = "Memindahkan direktori..." }); } } // step 3 --- set environment variable OnProgressChanged(this, new FlutterInstallProgressEventArgs { ProgressPercentage = 70, Status = "Mengatur environment variable..." }); _logger.Info("Start Flutter environment configuration"); _cancellationSource.Token.ThrowIfCancellationRequested(); var pathEnv = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); if (pathEnv?.Contains(@"flutter\bin") == false) { pathEnv += ";" + Path.Combine(InstallPath, @"bin"); Environment.SetEnvironmentVariable("PATH", pathEnv, EnvironmentVariableTarget.User); } // --- all done! _logger.Info("Flutter install sequence finished"); OnInstallFinished(this, new FlutterInstallFinishedEventArgs { ErrorMessage = "Instalasi selesai.", Success = true }); } catch (TaskCanceledException cancelledEx) { _logger.Info("Flutter install sequence cancelled.", cancelledEx); OnInstallFinished(this, new FlutterInstallFinishedEventArgs { ErrorMessage = "Instalasi dibatalkan.", Success = false }); } catch (Exception ex) { _logger.Error("Flutter install sequence failed", ex); OnInstallFinished(this, new FlutterInstallFinishedEventArgs { ErrorMessage = ex.Message, Success = false }); } }); }