Exemplo n.º 1
0
        static async Task <DownloadResult> DownloadFilesAsync(BuildConfig config, string buildFolder)
        {
            using (ISourceController ctrl = GetController(config))
            {
                using (LocalRepository rep = new LocalRepository(buildFolder, config.SiteId))
                {
                    ctrl.Initialize(config);

                    SourceRepository r = await ctrl.FetchAllFiles(config);

                    List <ISourceItem> remoteItems = r.Files;
                    var changes = rep.GetChanges(remoteItems).ToList();

                    var changeTypes = changes
                                      .Where(x => !x.RepositoryFile.IsDirectory)
                                      .GroupBy(x => x.Type);
                    foreach (var item in changeTypes)
                    {
                        Console.WriteLine("Changes {0}: {1}", item.Key, item.Count());
                    }

                    var updatedFiles = changes.Where(x => x.Type == ChangeType.Added || x.Type == ChangeType.Modified)
                                       .Select(x => x.RepositoryFile).Where(x => !x.IsDirectory).ToList();

                    foreach (var item in changes.Where(x => x.Type == ChangeType.Removed))
                    {
                        string filePath = item.RepositoryFile.FilePath;
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                    }

                    foreach (var slice in updatedFiles.Slice(25))
                    {
                        var downloadList = slice.Select(x =>
                        {
                            string filePath          = rep.LocalFolder + x.Folder + "/" + x.Name;
                            System.IO.FileInfo finfo = new System.IO.FileInfo(filePath);
                            if (finfo.Exists)
                            {
                                finfo.Delete();
                            }
                            else
                            {
                                if (!finfo.Directory.Exists)
                                {
                                    finfo.Directory.Create();
                                }
                            }
                            x.FilePath = filePath;
                            return(ctrl.DownloadAsync(config, x, filePath));
                        });

                        await Task.WhenAll(downloadList);

                        rep.UpdateFiles(slice);
                    }

                    return(new DownloadResult {
                        LastVersion = r.LatestVersion,
                        UpdatedFiles = updatedFiles.Count()
                    });
                }
            }
        }