コード例 #1
0
        public async Task <string> DownloadSampleAsync(ProgressMonitor progressMonitor = null)
        {
            try
            {
                string projectPath = string.Empty;

                if (!string.IsNullOrEmpty(SelectedSample.DownloadUrl))
                {
                    progressMonitor?.BeginTask("Downloading...", 1);

                    string zipProjectPath = Path.Combine(UserProfile.Current.TempDir, $"{SelectedSample.Name}.zip");
                    projectPath = Path.Combine(UserProfile.Current.TempDir, SelectedSample.Name);

                    if (!File.Exists(zipProjectPath))
                    {
                        await _sampleImporterService.DownloadSampleAsync(new Uri(SelectedSample.DownloadUrl), zipProjectPath);
                    }

                    progressMonitor?.Step();

                    if (!Directory.Exists(projectPath))
                    {
                        Directory.CreateDirectory(projectPath);
                        ZipFile.ExtractToDirectory(zipProjectPath, projectPath);
                    }

                    var solutions = Directory.GetFiles(projectPath, "*.sln", SearchOption.AllDirectories);
                    var solution  = solutions.FirstOrDefault();

                    if (!string.IsNullOrEmpty(solution))
                    {
                        projectPath = solution;
                    }
                }
                else
                {
                    var content = await _sampleImporterService.GetSampleContentAsync(SelectedSample);

                    progressMonitor?.BeginTask("Downloading...", content.Count);

                    var folders = content.Where(c => c.Type == ContentType.Dir);

                    foreach (var folder in folders)
                    {
                        string directoryPath = Path.Combine(_projectsPath, folder.Path);

                        if (!Directory.Exists(directoryPath))
                        {
                            progressMonitor?.Step();
                            progressMonitor?.Log.WriteLine("Creating folder" + ": " + directoryPath);
                            Directory.CreateDirectory(directoryPath);
                        }
                    }

                    var files = content.Where(c => c.Type == ContentType.File);

                    foreach (var file in files)
                    {
                        string filePath = Path.Combine(_projectsPath, file.Path);
                        progressMonitor?.Step();
                        progressMonitor?.Log.WriteLine("Downloading file" + ": " + filePath);
                        await _downloaderService.DownloadFileAsync(file.DownloadUrl, filePath, CancellationToken.None);
                    }

                    var projectFile = files.FirstOrDefault(c => c.Path.EndsWith(".sln", StringComparison.InvariantCultureIgnoreCase));

                    if (projectFile != null)
                    {
                        projectPath = Path.Combine(_projectsPath, projectFile.Path);
                    }
                }

                return(projectPath);
            }
            catch (Exception ex)
            {
                LoggingService.LogError(ex.Message);

                return(string.Empty);
            }
        }