Ejemplo n.º 1
0
        public async Task OpenSdkWithPickerAsync()
        {
            FolderPicker folderPicker = new FolderPicker();

            folderPicker.FileTypeFilter.Add("*");
            StorageFolder?folder = await folderPicker.PickSingleFolderAsync();

            if (folder != null)
            {
                StorageFolder sdksFolder = await GetSdksFolderAsync();

                SdkViewModel sdk = new SdkViewModel(folder.Name, Path.Combine(sdksFolder.Path, folder.Name))
                {
                    DownloadProgess = 1
                };

                if (!RecentSdks.Contains(sdk))
                {
                    RecentSdks.Add(sdk);

                    StorageFolder sdkFolder = await folder.CopyAsync(sdksFolder, NameCollisionOption.ReplaceExisting,
                                                                     new Progress <double>(p => sdk.InstallProgess = p));

                    ActiveSdk = sdk;
                    ActiveSdkChanged?.Invoke(this, EventArgs.Empty);
                    ApplyActiveSdk();
                }
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }