Пример #1
0
        public async Task ExportToDocumentAsync <TModel>(string path, IReadOnlyList <ResGroupModel <TModel> > groups, IStatusProgress progress, CancellationToken cancellationToken) where TModel : IRowModel
        {
            progress.Report(StatusRes.GettingSpreadsheet);
            SpreadsheetFeed spreadSheets = await Service.GetFeedAsync(new SpreadsheetQuery(path), progress, cancellationToken);

            SpreadsheetEntry spreadsheet = (SpreadsheetEntry)spreadSheets.Entries.First();

            progress.Report(StatusRes.GettingWorksheets);
            WorksheetFeed worksheetsFeed = await Service.GetFeedAsync(spreadsheet.GetWorkSheetQuery(), progress, cancellationToken);

            int columsCount = groups.First().Tables.First().Rows.First().DataList.Count;
            List <KeyValuePair <string, WorksheetEntry> > newWorksheets = groups
                                                                          .Select(g => new KeyValuePair <string, WorksheetEntry>(g.GroupTitle, new WorksheetEntry((uint)(g.Tables.Sum(t => t.Rows.Count + 3) + /*Table name rows*/ g.Tables.Count - 1), (uint)columsCount, g.GroupTitle)))
                                                                          .ToList();

            List <WorksheetEntry> worksheets2Rename = worksheetsFeed.Entries.Cast <WorksheetEntry>()
                                                      .Where(ws => newWorksheets.Any(nws => String.Equals(nws.Key, ws.Title.Text, StringComparison.OrdinalIgnoreCase)))
                                                      .ToList();

            if (worksheets2Rename.Count != 0)
            {
                foreach (var worksheetEntry in worksheets2Rename)
                {
                    worksheetEntry.Title.Text = Guid.NewGuid().ToString("N");
                    worksheetEntry.BatchData  = new GDataBatchEntryData(GDataBatchOperationType.update);
                }

                progress.Report(StatusRes.RenamingOldWorksheets);
                var progresses = progress.CreateParallelProgresses(worksheets2Rename.Count);
                //Renaming worksheets with matching names.
                await Task.WhenAll(worksheets2Rename.Zip(progresses, (ws, p) => Service.UpdateItemAsync(ws, p, cancellationToken)));
            }

            progress.Report(StatusRes.InsertingNewWorksheets);
            //Creating new worksheets.
            var insertingProgresses = progress.CreateParallelProgresses(newWorksheets.Count);
            var createdWorksheets   = await Task.WhenAll(newWorksheets.Zip(insertingProgresses, (kvp, p) => Service.InsertItemAsync(spreadsheet.GetWorkSheetLink(), kvp.Value, p, cancellationToken)));

            newWorksheets = createdWorksheets.Select(ws => new KeyValuePair <string, WorksheetEntry>(ws.Title.Text, ws)).ToList();

            progress.Report(StatusRes.DeletingOldWorksheets);
            //Clearing of previous worksheets.
            var deletingProgresses = progress.CreateParallelProgresses(worksheetsFeed.Entries.Count);
            await Task.WhenAll(worksheetsFeed.Entries.Cast <WorksheetEntry>().Zip(deletingProgresses, (ws, p) => Service.DeleteItemAsync(ws, p, cancellationToken)).ToArray());

            progress.Report(StatusRes.PushingCells);
            var groupWorksheetsJoin = newWorksheets.Join(groups, ws => ws.Key, g => g.GroupTitle, (kvp, group) => new { Group = group, Worksheet = kvp.Value }).ToList();
            var groupProgresses     = progress.CreateParallelProgresses(groupWorksheetsJoin.Select(j => (double)j.Worksheet.Cols * j.Worksheet.Rows).ToList());

            SemaphoreSlim semaphore = new SemaphoreSlim(MaxPushRequestsCount);
            await Task.WhenAll(groupWorksheetsJoin.Zip(groupProgresses, (j, p) => PushCellsAsync(j.Worksheet, j.Group, semaphore, p, cancellationToken)));
        }
Пример #2
0
        public async Task <IReadOnlyList <ResGroupModel <TModel> > > ImportFromDocumentAsync <TModel>(string path, IStatusProgress progress, CancellationToken cancellationToken) where TModel : IRowModel, new()
        {
            progress.Report(StatusRes.GettingSpreadsheet);
            SpreadsheetFeed spreadSheets = await Service.GetFeedAsync(new SpreadsheetQuery(path), progress, cancellationToken);

            SpreadsheetEntry spreadsheet = (SpreadsheetEntry)spreadSheets.Entries.First();

            progress.Report(StatusRes.GettingWorksheets);
            WorksheetFeed worksheetsFeed = await Service.GetFeedAsync(spreadsheet.GetWorkSheetQuery(), progress, cancellationToken);

            List <WorksheetEntry> worksheets = worksheetsFeed.Entries.Cast <WorksheetEntry>().ToList();

            var progresses = progress.CreateParallelProgresses(worksheets.Select(ws => (double)ws.Cols * ws.Rows).ToList());

            progress.Report(StatusRes.GettingWorksheetsData);

            SemaphoreSlim semaphore  = new SemaphoreSlim(MaxPushRequestsCount);
            var           groupTasks = worksheets.Zip(progresses, (entry, p) => GetGroupAsync <TModel>(entry, semaphore, p, cancellationToken));

            return((await Task.WhenAll(groupTasks)).ToList());
        }
Пример #3
0
        private SolutionResources GetSolutionResources(IEnumerable <string> selectedCultures, IReadOnlyCollection <Project> projects, IStatusProgress progress, CancellationToken cancellationToken)
        {
            Func <ResourceData, bool> resourceFilesFilter;

            if (selectedCultures == null)
            {
                resourceFilesFilter = r => true;
            }
            else
            {
                var selectedCulturesHashSet = new HashSet <string>(selectedCultures);

                resourceFilesFilter = r => selectedCulturesHashSet.Contains(r.Culture.Name);
            }

            var progresses              = progress.CreateParallelProgresses(0.7, 0.3);
            var dteProjectsProgress     = progresses[0];
            var resourceContentProgress = progresses[1];

            double projectsCount = projects.Count;

            var projectResourceItems = new SolutionResources
            {
                ProjectResources = projects.Select((project, index) =>
                {
                    string projectDirectory  = Path.GetDirectoryName(project.FullName);
                    var resourceProjectItems = project.GetAllItems().Where(projItem => Path.GetExtension(projItem.FileNames[0]) == ".resx").ToList();

                    dteProjectsProgress.Report(100 * (index + 1) / projectsCount);
                    cancellationToken.ThrowIfCancellationRequested();

                    return(new ProjectResources
                    {
                        ProjectName = project.Name,
                        ProjectDirectory = projectDirectory,
                        ProjectId = project.UniqueName,
                        ResourceProjectItems = resourceProjectItems
                    });
                })
                                   .ToList()
            };

            double resourceFilesCount = projectResourceItems.ProjectResources.Sum(pr => pr.ResourceProjectItems.Count);
            int    resourceIndex      = 0;

            foreach (var projectResourceItem in projectResourceItems.ProjectResources)
            {
                int projectDirectoryPathLength = projectResourceItem.ProjectDirectory.Length;

                projectResourceItem.Resources = projectResourceItem.ResourceProjectItems
                                                .Select(projItem =>
                {
                    string fileName = projItem.FileNames[0];

                    //Removing .resx extension.
                    string resName = Path.GetFileNameWithoutExtension(fileName);

                    string cultureName = Path.GetExtension(resName);
                    if (NonCultureExtensions.Contains(cultureName))
                    {
                        cultureName = string.Empty;
                    }
                    else
                    {
                        //Removing culture extension.
                        resName = Path.GetFileNameWithoutExtension(resName);
                    }

                    string directoryName = (Path.GetDirectoryName(fileName) ?? string.Empty).Substring(projectDirectoryPathLength);
                    //Relative path to the resource.
                    resName = Path.Combine(directoryName, resName);

                    resourceContentProgress.Report(100 * (++resourceIndex) / resourceFilesCount);
                    cancellationToken.ThrowIfCancellationRequested();

                    return(new ResourceData
                           (
                               resourceName: resName,
                               resourcePath: fileName,
                               culture: String.IsNullOrEmpty(cultureName) ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(cultureName.Substring(1)),
                               projectItem: projItem,
                               resources: GetResourceContent(fileName)
                           ));
                })
                                                .Where(resourceFilesFilter)
                                                .GroupBy(res => res.ResourceName)
                                                .ToDictionary(resGroup => resGroup.Key, resGroup => resGroup.ToDictionary(res => res.Culture.Name, res => res));
            }



            return(projectResourceItems);
        }