Exemple #1
0
        private static bool SynchronizeFolders(List <string> resxFiles1Paths, List <string> resxFiles2Paths, string folderBackupPath, ref string log)
        {
            bool           hasErrors      = false;
            bool           hasConflicts   = false;
            CustomTraceLog customTraceLog = new CustomTraceLog(log);

            #region prepare backup folder
            //make structure:
            //..folderBackupPath\2012.12.12_12:12:00\1\*.resx
            //..folderBackupPath\2012.12.12_12:12:00\2\*.resx
            //..folderBackupPath\2012.12.12_12:12:00\log.txt
            customTraceLog.AddLine("Creating backup folder structure in:'" + folderBackupPath + "' ...");

            folderBackupPath = folderBackupPath.TrimEnd('\\');
            if (!Directory.Exists(folderBackupPath))
            {
                Directory.CreateDirectory(folderBackupPath);
            }
            DateTime now    = DateTime.UtcNow;
            string   moment = string.Format("{0}.{1:00}.{2:00}._{3:00}-{4:00}-{5:00}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
            string   folderBackupPathWithMoment = folderBackupPath + "\\" + moment;
            Directory.CreateDirectory(folderBackupPathWithMoment);
            string folderBackupPathWithMomentForFolder1 = folderBackupPathWithMoment + "\\1";
            Directory.CreateDirectory(folderBackupPathWithMomentForFolder1);
            string folderBackupPathWithMomentForFolder2 = folderBackupPathWithMoment + "\\2";
            Directory.CreateDirectory(folderBackupPathWithMomentForFolder2);
            foreach (var resxFiles1Path in resxFiles1Paths)
            {
                string fileName     = Path.GetFileName(resxFiles1Path);
                string destFilePath = Path.Combine(folderBackupPathWithMomentForFolder1, fileName);
                File.Copy(resxFiles1Path, destFilePath);
            }
            foreach (var resxFiles2Path in resxFiles2Paths)
            {
                string fileName     = Path.GetFileName(resxFiles2Path);
                string destFilePath = Path.Combine(folderBackupPathWithMomentForFolder2, fileName);
                File.Copy(resxFiles2Path, destFilePath);
            }
            customTraceLog.AddLine("Finished creating backup folder structure.");
            #endregion

            #region find pairs and log non paired
            customTraceLog.AddLine("Finding pairs in two folders...");
            List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >();
            for (int f1 = 0; f1 < resxFiles1Paths.Count; f1++)
            {
                string pairFile = resxFiles2Paths.SingleOrDefault(f => string.Compare(Path.GetFileName(resxFiles1Paths[f1]), Path.GetFileName(f), StringComparison.OrdinalIgnoreCase) == 0);
                if (pairFile == null)
                {
                    hasErrors = true;
                    customTraceLog.AddLine(string.Format("Error: No pair file for: {0}", resxFiles1Paths[f1]));
                }
                else
                {
                    pairs.Add(new KeyValuePair <string, string>(resxFiles1Paths[f1], pairFile));
                }
            }

            for (int f2 = 0; f2 < resxFiles1Paths.Count; f2++)
            {
                bool allreadyPaired = pairs.Where(p => string.Compare(resxFiles2Paths[f2], p.Value, StringComparison.OrdinalIgnoreCase) == 0).Count() > 0;
                if (!allreadyPaired)
                {
                    hasErrors = true;
                    customTraceLog.AddLine(string.Format("Error: No pair file for: {0}", resxFiles2Paths[f2]));
                }
            }
            customTraceLog.AddLine("Finished finding pairs in two folders.");
            #endregion

            foreach (var pair in pairs)
            {
                bool hasC;
                bool hasE = Synchronizer.SynchronizeResxFiles(pair.Key, pair.Value, ref customTraceLog, now, out hasC);
                hasErrors    = hasErrors || hasE;
                hasConflicts = hasConflicts || hasC;
            }
            customTraceLog.AddLine(string.Format("Finished. Has errors:{0}, Has conflicts:{1}", hasErrors, hasConflicts));



            log = customTraceLog.ToString();
            string logFilePath = Path.Combine(folderBackupPathWithMoment, "Log.txt");
            File.WriteAllText(logFilePath, log);
            return(hasErrors);
        }
Exemple #2
0
 private static void AddDates(ref ResxItem L, ref ResxItem R, CustomTraceLog customTraceLog, DateTime now)
 {
     L.LastSynchronizationDate = L.LastModificationDate = R.LastSynchronizationDate = R.LastModificationDate = now;
     customTraceLog.ExtendLastLine("Dates updated.");
 }
Exemple #3
0
 private static void RequireRebuild(CustomTraceLog customTraceLog)
 {
     customTraceLog.ExtendLastLine("Rebuild is required.");
 }
Exemple #4
0
 private static void CopyToLeft(ref ResxItem L, ref ResxItem R, CustomTraceLog customTraceLog)
 {
     L.Value   = R.Value;
     L.Comment = R.Comment;
     customTraceLog.ExtendLastLine("Copied to Left.");
 }
Exemple #5
0
 private static void CopyToRight(ref ResxItem L, ref ResxItem R, CustomTraceLog customTraceLog)
 {
     R.Value   = L.Value;
     R.Comment = L.Comment;
     customTraceLog.ExtendLastLine("Copied to right.");
 }
Exemple #6
0
        /// <summary>
        /// See Resolution matrix in ReadMe.txt!
        /// </summary>
        /// <param name="L"></param>
        /// <param name="R"></param>
        /// <param name="customTraceLog"></param>
        /// <returns></returns>
        private static bool SynchronizeResxItems(ref ResxItem L, ref ResxItem R, CustomTraceLog customTraceLog, DateTime now, out bool hasConflicts)
        {
            bool hasErrors = false;

            hasConflicts = false;

            //See Resolution matrix in ReadMe.txt!
            if (L == null && R != null && R.Value != null &&
                R.LastSynchronizationDate == null)
            {
                L     = new ResxItem();
                L.Key = R.Key;
                CopyToLeft(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                RequireRebuild(customTraceLog);
                L.Changed = true;
                R.Changed = true;
                L.Created = true;
            }
            else
            if (L == null && R != null && R.Value != null &&
                R.LastSynchronizationDate != null)
            {
                customTraceLog.ExtendLastLine("Deleted right.");
                RequireRebuild(customTraceLog);
                L         = new ResxItem();
                L.Key     = R.Key;
                L.Deleted = true;
                R.Changed = true;
                L.Changed = true;
                R.Deleted = true;
            }
            else
            if (R == null && L != null && L.Value != null &&
                L.LastSynchronizationDate == null)
            {
                R     = new ResxItem();
                R.Key = L.Key;
                CopyToRight(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                RequireRebuild(customTraceLog);
                R.Changed = true;
                L.Changed = true;
                R.Created = true;
            }
            else
            if (R == null && L != null && L.Value != null &&
                L.LastSynchronizationDate != null)
            {
                customTraceLog.ExtendLastLine("Deleted left.");
                RequireRebuild(customTraceLog);
                R         = new ResxItem();
                R.Key     = L.Key;
                R.Deleted = true;
                L.Changed = true;
                R.Changed = true;
                L.Deleted = true;
            }
            else
            if (L.LastSynchronizationDate != null && R.LastSynchronizationDate != null &&
                L.LastSynchronizationDate != R.LastSynchronizationDate)
            {
                customTraceLog.ExtendLastLine("Error: Last sync dates does not match. Are you syncing right versions of resx folders?!");
                hasErrors = true;
                L.Changed = false;
                R.Changed = false;
            }
            else
            if (
                L.Value != null && R.Value != null && L.Value == R.Value &&
                (L.LastModificationDate == null || L.LastSynchronizationDate == null || R.LastModificationDate == null || R.LastSynchronizationDate == null ||
                 (L.LastModificationDate != L.LastSynchronizationDate || R.LastSynchronizationDate != R.LastModificationDate))
                )
            {
                AddDates(ref L, ref R, customTraceLog, now);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate == null && L.LastModificationDate == null &&
                R.Value == null && R.LastSynchronizationDate == null && R.LastModificationDate == null)
            {
                CopyToRight(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                RequireRebuild(customTraceLog);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value == null && L.LastSynchronizationDate == null && L.LastModificationDate == null &&
                R.Value != null && R.LastSynchronizationDate == null && R.LastModificationDate == null)
            {
                CopyToLeft(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                RequireRebuild(customTraceLog);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate == null && R.LastModificationDate == null &&
                L.Value != R.Value && L.LastSynchronizationDate == L.LastModificationDate)
            {
                CopyToLeft(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate == null && L.LastModificationDate == null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value != R.Value && R.LastSynchronizationDate == R.LastModificationDate)
            {
                CopyToRight(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate == null && R.LastModificationDate == null &&
                L.Value != R.Value && L.LastSynchronizationDate != L.LastModificationDate)
            {
                customTraceLog.ExtendLastLine("Conflict. None changed.");
                hasConflicts = true;
                L.Changed    = false;
                R.Changed    = false;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate == null && L.LastModificationDate == null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value != R.Value && R.LastSynchronizationDate != R.LastModificationDate)
            {
                customTraceLog.ExtendLastLine("Conflict. None changed.");
                hasConflicts = true;
                L.Changed    = false;
                R.Changed    = false;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value == R.Value && L.LastModificationDate == R.LastModificationDate)
            {
                //customTraceLog.ExtendLastLine("None changed.");
                L.Changed = false;
                R.Changed = false;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value != R.Value && L.LastSynchronizationDate == L.LastModificationDate && R.LastSynchronizationDate == R.LastModificationDate)
            {
                customTraceLog.ExtendLastLine("Error: Sync and modifications dates are same but text is different.");
                hasErrors = true;
                L.Changed = false;
                R.Changed = false;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value != R.Value && L.LastSynchronizationDate != L.LastModificationDate && R.LastSynchronizationDate == R.LastModificationDate)
            {
                CopyToRight(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value != R.Value && L.LastSynchronizationDate == L.LastModificationDate && R.LastSynchronizationDate != R.LastModificationDate)
            {
                CopyToLeft(ref L, ref R, customTraceLog);
                AddDates(ref L, ref R, customTraceLog, now);
                L.Changed = true;
                R.Changed = true;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate != null && L.LastModificationDate != null &&
                R.Value != null && R.LastSynchronizationDate != null && R.LastModificationDate != null &&
                L.Value != R.Value && L.LastSynchronizationDate != L.LastModificationDate && R.LastSynchronizationDate != R.LastModificationDate)
            {
                customTraceLog.ExtendLastLine("Conflict. None changed.");
                hasConflicts = true;
                L.Changed    = false;
                R.Changed    = false;
            }
            else
            if (L.Value != null && L.LastSynchronizationDate == null && L.LastModificationDate == null &&
                R.Value != null && R.LastSynchronizationDate == null && R.LastModificationDate == null &&
                L.Value != R.Value)
            {
                customTraceLog.ExtendLastLine("Conflict. None changed.");
                hasConflicts = true;
                L.Changed    = false;
                R.Changed    = false;
            }
            else
            {
                customTraceLog.ExtendLastLine(string.Format("Error: Unsupported case happened.Send log to devel to exemine.\r\nLeft:\r\n{0}\r\nRight:\r\n{1}", L.ToString(), R.ToString()));
                hasErrors = true;
                L.Changed = false;
                R.Changed = false;
            }

            return(hasErrors);
        }
Exemple #7
0
        private static void SynchronizeResxFilesInner(CustomTraceLog customTraceLog, ref XmlDocument sourceXmlDoc, ref XmlDocument destinationXmlDoc, DateTime now, ref bool hasErrors, ref bool hasConflicts, ref bool leftFileChanged, ref bool rightFileChanged, ref List <string> processedKeys)
        {
            XmlNodeList nodes = sourceXmlDoc.SelectNodes("/root/data");

            for (int n = nodes.Count - 1; n >= 0; n--)
            {
                XmlNode node = nodes[n];

                ResxItem sourceResxItem = new ResxItem();
                sourceResxItem.Key     = node.Attributes["name"].Value;
                sourceResxItem.Value   = node["value"].InnerText;
                sourceResxItem.Comment = node["comment"] == null ? null : node["comment"].InnerText;
                sourceResxItem.MoveTimestampsFromCommentToProperties();                //sourceResxItem.Comment!=null && sourceResxItem.Comment.Contains("min pass size")

                if (!processedKeys.Contains(sourceResxItem.Key))
                {
                    processedKeys.Add(sourceResxItem.Key);

                    ResxItem destinationResxItem  = null;
                    XmlNode  pairInDestinationXml =
                        destinationXmlDoc.SelectSingleNode("/root/data[@name='" + node.Attributes["name"].Value + "']");
                    if (pairInDestinationXml != null)
                    {
                        destinationResxItem         = new ResxItem();
                        destinationResxItem.Key     = pairInDestinationXml.Attributes["name"].Value;
                        destinationResxItem.Value   = pairInDestinationXml["value"].InnerText;
                        destinationResxItem.Comment = pairInDestinationXml["comment"] == null
                                                                                                                ? null
                                                                                                                : pairInDestinationXml["comment"].InnerText;
                        destinationResxItem.MoveTimestampsFromCommentToProperties();
                    }

                    bool           hasC;
                    CustomTraceLog tLog = new CustomTraceLog(string.Empty);
                    bool           hasE = SynchronizeResxItems(ref sourceResxItem, ref destinationResxItem, tLog, now, out hasC);
                    hasErrors    = hasErrors || hasE;
                    hasConflicts = hasConflicts || hasC;

                    if (sourceResxItem.Changed || destinationResxItem.Changed || hasE || hasC)
                    {
                        customTraceLog.ExtendLastLine(node.Attributes["name"].Value.PadRight(80, ' ') + ": ");
                        customTraceLog.ExtendLastLine(tLog.ToString());
                    }
                    if (sourceResxItem.Changed)
                    {
                        if (sourceResxItem.Deleted)
                        {
                            sourceResxItem.DeleteNode(ref sourceXmlDoc, sourceResxItem.Key);
                            leftFileChanged = true;
                        }
                        else
                        {
                            sourceResxItem.ReapplyDatesToComment();
                            sourceResxItem.CopyToNode(ref sourceXmlDoc, ref node);
                            leftFileChanged = true;
                        }
                    }
                    if (destinationResxItem.Changed)
                    {
                        if (destinationResxItem.Deleted)
                        {
                            destinationResxItem.DeleteNode(ref destinationXmlDoc, destinationResxItem.Key);
                            rightFileChanged = true;
                        }
                        else
                        {
                            destinationResxItem.ReapplyDatesToComment();
                            destinationResxItem.CopyToNode(ref destinationXmlDoc, ref pairInDestinationXml);
                            rightFileChanged = true;
                        }
                    }
                    if (sourceResxItem.Changed || destinationResxItem.Changed)
                    {
                        customTraceLog.AddLine("");
                    }
                }
            }
        }
Exemple #8
0
        public static bool SynchronizeResxFiles(string resxFilePath1, string resxFilePath2, ref CustomTraceLog customTraceLog, DateTime now, out bool hasConflicts)
        {
            customTraceLog.AddLine(string.Format("Synchronizing {0} and {1}:", Path.GetFileName(resxFilePath1), Path.GetFileName(resxFilePath2)).PadRight(100, '-'));
            customTraceLog.IncreaseIdent();

            bool hasErrors = false;

            hasConflicts = false;

            XmlDocument sourceXmlDoc = new XmlDocument();

            sourceXmlDoc.Load(resxFilePath1);

            XmlDocument destinationXmlDoc = new XmlDocument();

            destinationXmlDoc.Load(resxFilePath2);

            bool leftFileChanged  = false;
            bool rightFileChanged = false;

            List <string> processedKeys = new List <string>();

            SynchronizeResxFilesInner(customTraceLog, ref sourceXmlDoc, ref destinationXmlDoc, now, ref hasErrors, ref hasConflicts, ref leftFileChanged, ref rightFileChanged, ref processedKeys);
            SynchronizeResxFilesInner(customTraceLog, ref destinationXmlDoc, ref sourceXmlDoc, now, ref hasErrors, ref hasConflicts, ref leftFileChanged, ref rightFileChanged, ref processedKeys);

            if (leftFileChanged)
            {
                sourceXmlDoc.Save(resxFilePath1);
            }
            if (rightFileChanged)
            {
                destinationXmlDoc.Save(resxFilePath2);
            }

            customTraceLog.DecreaseIdent();
            customTraceLog.AddLine(string.Format("Finished synchronizing {0} and {1}. Has errors:{2}, Has conflicts:{3}", Path.GetFileName(resxFilePath1), Path.GetFileName(resxFilePath2), hasErrors, hasConflicts).PadRight(100, '-'));

            return(hasErrors);
        }