Beispiel #1
0
        public ChangeModel Commit(ChangeModel model)
        {
            ChangeModel wrongItem = new ChangeModel();

            foreach (ChangeItem item in model.Items)
            {
                Logger.Log("Try to change item : " + item.ToString());
                string[] lines = File.ReadAllLines(item.DomainPath);
                if (lines[item.Line].Contains(item.From))
                {
                    Logger.Log("OK");
                    lines[item.Line] = lines[item.Line].Replace(item.From, item.To);
                }
                else
                {
                    wrongItem.AddItem(item);
                    continue;
                }
                Logger.Log("");
                using (StreamWriter sw = new StreamWriter(item.DomainPath, false, System.Text.Encoding.Default))
                {
                    foreach (string line in lines)
                    {
                        sw.WriteLine(line);
                    }
                }
            }
            Logger.Log("Done");
            return(wrongItem);
        }
Beispiel #2
0
        // Call this.AnalyzeFile for all file in list and create general ChangeModel.
        public ChangeModel Prepare(ArrayList files)
        {
            ChangeModel changes = new ChangeModel();

            foreach (String fileName in files)
            {
                changes.AddItems(this.PrepareFile(fileName));
            }
            return(changes);
        }
Beispiel #3
0
        public static ChangeModel Load(string path)
        {
            string json = "";

            using (StreamReader sr = new StreamReader(path))
            {
                // TODO change to StringBuilder
                json += sr.ReadToEnd();
            }
            ChangeModel returnModel = new ChangeModel();
            dynamic     model       = JsonConvert.DeserializeObject(json);

            foreach (dynamic obj in model.Items)
            {
                returnModel.AddItem(new ChangeItem(
                                        (string)obj.DomainPath,
                                        (int)obj.Line,
                                        (string)obj.From,
                                        (string)obj.To));
            }
            return(returnModel);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            // Test data
            args = new string[] { "C:\\Development\\SonDar\\Paragon\\GuardAnalyzer", "Preview", "Default", "Example997*.cs" };
            // arg0 : path to folder
            string pathToStartFolder = args[0];
            // arg1 : work mode
            WorkMode mode;

            if (!Enum.TryParse(args[1], out mode))
            {
                throw new Exception("Unknown Mode [args[1] = " + mode + "]");
            }
            // arg2 : changeModel file (Optional)
            string path = Directory.GetCurrentDirectory() + "\\changes.txt";

            if (args.Length > 2 && !args[2].Equals("Default"))
            {
                path = args[2];
            }
            // arg3 - wirdcard(Optional)
            string wildcard = "*.cs";

            if (args.Length > 3)
            {
                wildcard = args[3];
            }
            // Blocks list
            TransformBlock <string, ArrayList> fileListBuilder = new TransformBlock <string, ArrayList>
                                                                     (domainFolderName => (new FileListBuilder()).ParseDirectory(domainFolderName, wildcard));
            TransformBlock <ArrayList, ChangeModel> guardAnalyzer = new TransformBlock <ArrayList, ChangeModel>
                                                                        (fileList => (new GuardAnalyzer()).Prepare(fileList));
            TransformBlock <string, ChangeModel> loader = new TransformBlock <string, ChangeModel>
                                                              (fileName => ChangeModel.Load(fileName));
            TransformBlock <ChangeModel, ChangeModel> printer = new TransformBlock <ChangeModel, ChangeModel>
                                                                    (model => model.Print());
            ActionBlock <ChangeModel> writer   = new ActionBlock <ChangeModel>(tempModel => tempModel.Save(path));
            ActionBlock <ChangeModel> commiter = new ActionBlock <ChangeModel>(tempModel => (new GuardAnalyzer()).Commit(tempModel));

            //Start
            // Preview - fileListBuilder->guardAnalyzer->printer->writer
            // Commit  - loader->printer->commiter
            // Forse   - fileListBuilder->guardAnalyzer->printer->commiter
            Logger.Log("Preview : ");
            if (mode == WorkMode.Preview || mode == WorkMode.Force)
            {
                fileListBuilder.LinkTo(guardAnalyzer);
                fileListBuilder.Completion.ContinueWith(task => guardAnalyzer.Complete());

                guardAnalyzer.LinkTo(printer);
                guardAnalyzer.Completion.ContinueWith(task => printer.Complete());
                if (mode != WorkMode.Force)
                {
                    printer.LinkTo(writer);
                    printer.Completion.ContinueWith(task => writer.Complete());
                    fileListBuilder.Post(pathToStartFolder);
                    fileListBuilder.Complete();
                    writer.Completion.Wait();
                }
            }
            if (mode == WorkMode.Commit || mode == WorkMode.Force)
            {
                if (mode != WorkMode.Force)
                {
                    loader.LinkTo(printer);
                    loader.Completion.ContinueWith(task => printer.Complete());
                }
                printer.LinkTo(commiter);
                printer.Completion.ContinueWith(task => commiter.Complete());
                if (mode != WorkMode.Force)
                {
                    loader.Post(path);
                    loader.Complete();
                }
                else
                {
                    fileListBuilder.Post(pathToStartFolder);
                    fileListBuilder.Complete();
                }
                commiter.Completion.Wait();
            }

            Console.ReadKey();
        }