Beispiel #1
0
 public Task UpdateResourcesAsync(IReadOnlyCollection <string> selectedCultures, IReadOnlyCollection <Project> selectedProjects, IStatusProgress progress, CancellationToken cancellationToken, UpdateResourcesOptions options)
 {
     return(Task.Run(() => UpdateResources(selectedCultures, selectedProjects, progress, cancellationToken, options), cancellationToken));
 }
Beispiel #2
0
        public void UpdateResources(IReadOnlyCollection <string> selectedCultures, IReadOnlyCollection <Project> selectedProjects, IStatusProgress progress, CancellationToken cancellationToken, UpdateResourcesOptions options)
        {
            IReadOnlyDictionary <string, CultureInfo> selectedCultureInfos = selectedCultures.Select(CultureInfo.GetCultureInfo)
                                                                             .ToDictionary(cult => cult.Name, cult => cult);

            IReadOnlyDictionary <string, Project> projectsDictionary = selectedProjects.ToDictionary(proj => proj.UniqueName, proj => proj);

            progress.Report(StatusRes.GettingProjectsResources);
            SolutionResources solutionResources = GetSolutionResources(null, selectedProjects, progress, cancellationToken);

            progress.Report(StatusRes.GeneratingResx);
            cancellationToken.ThrowIfCancellationRequested();

            int resourceFilesProcessed = 0;
            int resourceFilesCount     = solutionResources.ProjectResources.Sum(pr => pr.Resources.Count);

            foreach (var projectResources in solutionResources.ProjectResources)
            {
                var project = projectsDictionary[projectResources.ProjectId];

                foreach (Dictionary <string, ResourceData> resourceFileGroup in projectResources.Resources.Values)
                {
                    //Removing culture files without neutral culture file. TODO: find if it is required.
                    ResourceData neutralCulture;
                    if (!resourceFileGroup.TryGetValue(InvariantCultureId, out neutralCulture))
                    {
                        _logger.Log(String.Format(LoggerRes.MissingNeutralCulture, resourceFileGroup.Values.First().ResourcePath, String.Join("\r\n", resourceFileGroup.Values.Select(r => r.ResourcePath))));

                        foreach (var projectItem in resourceFileGroup.Values)
                        {
                            _logger.Log(String.Format(LoggerRes.RemovedFormat, projectItem.ProjectItem.FileNames[0]));

                            projectItem.ProjectItem.Delete();
                        }

                        resourceFileGroup.Clear();
                        project.Save();
                        continue;
                    }

                    var items2Remove = resourceFileGroup.Where(f => !selectedCultureInfos.ContainsKey(f.Key)).ToList();

                    List <KeyValuePair <string, ProjectItem> > projectItems2Remove;
                    if (items2Remove.Count == 0)
                    {
                        projectItems2Remove = new List <KeyValuePair <string, ProjectItem> >(0);
                    }
                    else
                    {
                        projectItems2Remove = items2Remove.Select(d => new KeyValuePair <string, ProjectItem>(d.Key, d.Value.ProjectItem)).ToList();
                    }

                    var cultures2Add = selectedCultureInfos.Where(cult => !resourceFileGroup.ContainsKey(cult.Key)).Select(cult => cult.Value).ToList();

                    if (options.RemoveNotSelectedCultures)
                    {
                        foreach (var projectItem in projectItems2Remove)
                        {
                            _logger.Log(String.Format(LoggerRes.RemovedFormat, projectItem.Value.FileNames[0]));

                            projectItem.Value.Delete();
                            resourceFileGroup.Remove(projectItem.Key);
                        }
                    }

                    foreach (var cultureInfo in cultures2Add)
                    {
                        string resourcePath = Path.Combine(Path.GetDirectoryName(neutralCulture.ResourcePath), Path.GetFileName(neutralCulture.ResourceName) + "." + cultureInfo.Name.ToUpper() + ".resx");

                        using (File.Create(resourcePath)) { }

                        ProjectItem projectItem = project.ProjectItems.AddFromFile(resourcePath);

                        var newFile = new ResourceData
                                      (
                            resourceName: neutralCulture.ResourceName,
                            resourcePath: resourcePath,
                            culture: cultureInfo,
                            projectItem: projectItem,
                            resources: new Dictionary <string, ResXDataNode>(0)
                                      );

                        resourceFileGroup.Add(cultureInfo.Name, newFile);

                        _logger.Log(String.Format(LoggerRes.AddedNewResource, newFile.ResourcePath));
                    }

                    List <ResourceData> otherCultureResources = resourceFileGroup.Where(resData => resData.Key != InvariantCultureId).Select(resData => resData.Value).ToList();

                    if (options.EmbeedSubCultures.HasValue)
                    {
                        if (options.EmbeedSubCultures.Value)
                        {
                            EmbeedResources(neutralCulture.ProjectItem, otherCultureResources);
                        }
                    }

                    if (options.UseDefaultCustomTool.HasValue)
                    {
                        Property customToolProperty = neutralCulture.ProjectItem.Properties.Cast <Property>().First(p => p.Name == "CustomTool");

                        customToolProperty.Value = options.UseDefaultCustomTool.Value ? "PublicResXFileCodeGenerator" : "";
                    }

                    if (options.UseDefaultContentType.HasValue)
                    {
                        foreach (var resProjectItem in resourceFileGroup.Values.Select(g => g.ProjectItem))
                        {
                            Property itemTypeProperty = resProjectItem.Properties.Cast <Property>().First(p => p.Name == "ItemType");

                            itemTypeProperty.Value = options.UseDefaultContentType.Value ? "EmbeddedResource" : "None";
                        }
                    }

                    UpdateResourceFiles(neutralCulture, otherCultureResources);

                    project.Save();

                    progress.Report((int)Math.Round((double)100 * (++resourceFilesProcessed) / resourceFilesCount));
                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }