Ejemplo n.º 1
0
        internal override void ExecuteInternal()
        {
            string originalFilePath = Regex.Replace(Config.FilePath, MergeConfiguration.PostactionRegex, ".");

            if (!File.Exists(originalFilePath))
            {
                // If original file does not exist, rename the postaction file, add it to projectitems and app.xamls mergedictionary
                File.Copy(Config.FilePath, originalFilePath);
                GenContext.Current.ProjectInfo.ProjectItems.Add(originalFilePath.GetDestinationPath());
                AddToAppMergeDictionary(originalFilePath);
            }
            else
            {
                var mergeRoot  = XElement.Load(Config.FilePath);
                var sourceRoot = XElement.Load(originalFilePath);

                foreach (var node in GetNodesToMerge(mergeRoot))
                {
                    var sourceNode = sourceRoot.Elements().FirstOrDefault(e => GetKey(e) == GetKey(node));
                    if (sourceNode == null)
                    {
                        AddNode(sourceRoot, node);
                    }
                    else
                    {
                        if (!XNode.DeepEquals(node, sourceNode))
                        {
                            var errorMessage = string.Format(StringRes.FailedMergePostActionKeyAlreadyDefined, GetKey(node), RelatedTemplate);
                            if (Config.FailOnError)
                            {
                                throw new InvalidDataException(errorMessage);
                            }
                            else
                            {
                                var relativeFilePath = originalFilePath.GetPathRelativeToGenerationParentPath();
                                HandleFailedMergePostActions(relativeFilePath, MergeFailureType.KeyAlreadyDefined, MergeConfiguration.Suffix, errorMessage);
                                return;
                            }
                        }
                    }
                }

                using (TextWriter writeFile = new StreamWriter(originalFilePath))
                {
                    var writer = new ResourceDictionaryWriter(writeFile);
                    writer.WriteResourceDictionary(sourceRoot);
                    writer.Flush();
                }
            }

            File.Delete(Config.FilePath);
        }
        internal override void ExecuteInternal()
        {
            string originalFilePath = GetFilePath();

            if (!File.Exists(originalFilePath))
            {
                File.Copy(Config.FilePath, originalFilePath);
                GenContext.Current.ProjectItems.Add(originalFilePath.Replace(GenContext.Current.OutputPath, GenContext.Current.DestinationPath));
                AddToMergeDictionary(originalFilePath);
            }
            else
            {
                var mergeRoot  = XElement.Load(Config.FilePath);
                var sourceRoot = XElement.Load(originalFilePath);

                foreach (var node in GetNodesToMerge(mergeRoot))
                {
                    var sourceNode = sourceRoot.Elements().FirstOrDefault(e => GetKey(e) == GetKey(node));
                    if (sourceNode == null)
                    {
                        AddNodeToSource(sourceRoot, node);
                    }
                    else
                    {
                        if (!XNode.DeepEquals(node, sourceNode))
                        {
                            var errorMessage = string.Format(StringRes.FailedMergePostActionKeyAlreadyDefined, GetKey(node), RelatedTemplate);
                            if (Config.FailOnError)
                            {
                                throw new InvalidDataException(errorMessage);
                            }
                            else
                            {
                                AddFailedMergePostActions(originalFilePath, MergeFailureType.KeyAlreadyDefined, errorMessage);
                                File.Delete(Config.FilePath);
                                return;
                            }
                        }
                    }
                }

                using (TextWriter writeFile = new StreamWriter(originalFilePath))
                {
                    var writer = new ResourceDictionaryWriter(writeFile);
                    writer.WriteResourceDictionary(sourceRoot);
                    writer.Flush();
                }
            }

            File.Delete(Config.FilePath);
        }