private async Task ApplyChangesToTemporaryFolderAsync(IReadOnlyList <StorageLibraryChange> changes) { if (RootFolder is null || TemporarySolutionFolder is null) { return; } foreach (StorageLibraryChange change in changes) { if (Path.GetFileName(change.Path).StartsWith(".") || Path.GetFileName(change.PreviousPath).StartsWith(".")) { continue; } switch (change.ChangeType) { case StorageLibraryChangeType.Created: case StorageLibraryChangeType.ContentsChanged: case StorageLibraryChangeType.MovedIntoLibrary: case StorageLibraryChangeType.ContentsReplaced: { string relativePath = StorageExtensions.GetRelativePath(RootFolder.Path, change.Path); string relativeDirectory = Path.GetDirectoryName(relativePath); IStorageItem?destinationItem = relativePath.Contains(Path.DirectorySeparatorChar) ? await TemporarySolutionFolder.TryGetItemAsync(relativeDirectory) : TemporarySolutionFolder; IStorageItem?item = await change.GetStorageItemAsync(); if (destinationItem is IStorageFolder destinationFolder) { if (item is IStorageFile file) { await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.ReplaceExisting); } else if (item is IStorageFolder folder) { await folder.CopyAsync(destinationFolder, NameCollisionOption.ReplaceExisting); } } } break; case StorageLibraryChangeType.Deleted: case StorageLibraryChangeType.MovedOutOfLibrary: { string relativePath = StorageExtensions.GetRelativePath(RootFolder.Path, change.Path); IStorageItem?item = await TemporarySolutionFolder.TryGetItemAsync(relativePath); if (item != null) { await item.DeleteAsync(); } } break; case StorageLibraryChangeType.MovedOrRenamed: { string relativePath = StorageExtensions.GetRelativePath(RootFolder.Path, change.Path); string relativeDirectory = Path.GetDirectoryName(relativePath); string previousRelativePath = StorageExtensions.GetRelativePath(RootFolder.Path, change.PreviousPath); IStorageItem?destinationItem = relativePath.Contains(Path.DirectorySeparatorChar) ? await TemporarySolutionFolder.TryGetItemAsync(relativeDirectory) : TemporarySolutionFolder; IStorageItem?item = await TemporarySolutionFolder.GetItemAsync(previousRelativePath); if (item != null) { if (Path.GetDirectoryName(change.Path).Equals(Path.GetDirectoryName(change.PreviousPath), StringComparison.OrdinalIgnoreCase)) { await item.RenameAsync(Path.GetFileName(change.Path)); } else { if (destinationItem is IStorageFolder destinationFolder) { if (item is IStorageFile file) { await file.MoveAsync(destinationFolder, file.Name, NameCollisionOption.ReplaceExisting); } else if (item is IStorageFolder folder) { await folder.MoveAsync(destinationFolder, NameCollisionOption.ReplaceExisting); } } } } } break; } } }
public async Task DownloadSdkAsync(string version) { StorageFolder sdksFolder = await GetSdksFolderAsync(); SdkViewModel sdk = new SdkViewModel(version, Path.Combine(sdksFolder.Path, version)); if (!RecentSdks.Contains(sdk)) { RecentSdks.Add(sdk); string architecture = RuntimeInformation.ProcessArchitecture.ToString().ToLower(); string sdkZipFileName = $"dotnet-sdk-{version}-win-{architecture}.zip"; if (!(await sdksFolder.TryGetItemAsync(sdkZipFileName) is StorageFile sdkZipFile)) { Uri azureFeed = new Uri("https://dotnetcli.azureedge.net/dotnet/Sdk/"); Uri downloadLink = new Uri(azureFeed, $"{version}/{sdkZipFileName}"); using WebClient client = new WebClient(); sdk.DownloadProgess = 0; client.DownloadProgressChanged += (s, e) => sdk.DownloadProgess = (double)e.ProgressPercentage / 100; await client.DownloadFileTaskAsync(downloadLink, Path.Combine(sdksFolder.Path, sdkZipFileName)); sdkZipFile = await sdksFolder.GetFileAsync(sdkZipFileName); } sdk.DownloadProgess = 1; using (Stream sdkZipFileStream = await sdkZipFile.OpenStreamForReadAsync()) using (ZipArchive zipArchive = new ZipArchive(sdkZipFileStream, ZipArchiveMode.Read)) { var archiveEntries = zipArchive.Entries.Where(e => e.FullName.StartsWith("sdk")); int archiveEntryCount = archiveEntries.Count(); int archiveEntryCounter = 0; sdk.InstallProgess = 0; foreach (ZipArchiveEntry archiveEntry in archiveEntries) { string relativeArchiveEntryPath = StorageExtensions.GetRelativePath("sdk", archiveEntry.FullName); using Stream fileStream = await sdksFolder.OpenStreamForWriteAsync(relativeArchiveEntryPath, CreationCollisionOption.ReplaceExisting); using Stream archiveEntryStream = archiveEntry.Open(); if (Path.GetFileName(relativeArchiveEntryPath) == "Microsoft.Build.Tasks.CodeAnalysis.dll") { using FileStream codeAnalysisLibrary = new FileStream("Microsoft.Build.Tasks.CodeAnalysis.dll", FileMode.Open, FileAccess.Read); await codeAnalysisLibrary.CopyToAsync(fileStream); } else { await archiveEntryStream.CopyToAsync(fileStream); } archiveEntryCounter++; sdk.InstallProgess = (double)archiveEntryCounter / archiveEntryCount; } } await sdkZipFile.DeleteAsync(StorageDeleteOption.PermanentDelete); ActiveSdk = sdk; ActiveSdkChanged?.Invoke(this, EventArgs.Empty); ApplyActiveSdk(); } }