RobocopyHistoryParser.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Sourcecontrol » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Sourcecontrol » RobocopyHistoryParser.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

namespace ThoughtWorks.CruiseControl.Core.Sourcecontrol{
  public class RobocopyHistoryParser : IHistoryParser
  {
    public Modification[] Parse(TextReader reader, DateTime from, DateTime to)
    {
            var mods = new List<Modification>();

      string currentLine = reader.ReadLine();

      while (currentLine != null)
      {
        // TODO - do it all with a regex?

        if (currentLine.Contains(DELETED_DIR_TAG))
        {
          mods.Add(ParseDeletedDirectory(currentLine));
        }
        else if (currentLine.Contains(DELETED_FILE_TAG))
        {
          mods.Add(ParseDeletedFile(currentLine));
        }
        else if (currentLine.Contains(ADDED_FILE_TAG))
        {
          mods.Add(ParseAddedFile(currentLine));
        }
        else if (currentLine.Contains(UPDATED_FILE_TAG))
        {
          mods.Add(ParseUpdatedFile(currentLine));
        }

        currentLine = reader.ReadLine();
      }

      return mods.ToArray();
    }

    private static string DELETED_DIR_TAG = "*EXTRA Dir";
    private static string DELETED_FILE_TAG = "*EXTRA File";
    private static string ADDED_FILE_TAG = "New File";
    private static string UPDATED_FILE_TAG = "Newer";

    private static readonly Regex ParseDeletedDirectoryRegex = new Regex(@"\s+\*EXTRA Dir\s+(?'Path'.*)");

    // To match this
    //  *EXTRA Dir    E:\copytest\dst\dir2\

    Modification ParseDeletedDirectory(
      string logLine)
    {
      Match match = ParseDeletedDirectoryRegex.Match(logLine);

      if (match.Success)
      {
        if (match.Groups.Count == 2)
        {
          string path = match.Groups["Path"].Captures[0].ToString();

          Modification mod = new Modification();

          mod.Type = "deleted";

          mod.FolderName = Path.GetDirectoryName(path);

          return mod;
        }
      }

      throw new Exception("Failed to match regex");
    }

    private static readonly Regex ParseDeletedFileRegex = new Regex(@"\s+\*EXTRA File\s+(?'Date'\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})\s+(?'Path'.*)");
    // To match this
    //     *EXTRA File          2008/02/06 09:31:37  E:\copytest\dst\dir2\deleted.txt

    Modification ParseDeletedFile(
      string logLine)
    {
      Match match = ParseDeletedFileRegex.Match(logLine);

      if (match.Success)
      {
        if (match.Groups.Count == 3)
        {
          string path = match.Groups["Path"].Captures[0].ToString();

          Modification mod = new Modification();
            
          mod.Type = "deleted";

          mod.FileName = Path.GetFileName(path);
          mod.FolderName = Path.GetDirectoryName(path);

          return mod;
        }
      }

      throw new Exception("Failed to match regex");
    }

    private static readonly Regex ParseAddedFileRegex = new Regex(@"\s+New File\s+(?'Date'\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})\s+(?'Path'.*)");

    // To match this
    //       New File       2008/02/06 09:16:49  E:\copytest\src\file2.txt

    Modification ParseAddedFile(
      string logLine)
    {
      Match match = ParseAddedFileRegex.Match(logLine);

      if (match.Success)
      {
        if (match.Groups.Count == 3)
        {
          string date = match.Groups["Date"].Captures[0].ToString();
          string path = match.Groups["Path"].Captures[0].ToString();

          Modification mod = new Modification();

          mod.Type = "added";

          mod.FileName = Path.GetFileName(path);
          mod.FolderName = Path.GetDirectoryName(path);

          mod.ModifiedTime = CreateDate(date);

          return mod;
        }
      }

      throw new Exception("Failed to match regex");
    }

    private static readonly Regex ParseUpdatedFileRegex = new Regex(@"\s+Newer\s+(?'Date'\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})\s+(?'Path'.*)");

    // To match this
    //       Newer          2008/02/06 09:35:50  E:\copytest\src\file3.txt

    Modification ParseUpdatedFile(
      string logLine)
    {
      Match match = ParseUpdatedFileRegex.Match(logLine);

      if (match.Success)
      {
        if (match.Groups.Count == 3)
        {
          string date = match.Groups["Date"].Captures[0].ToString();
          string path = match.Groups["Path"].Captures[0].ToString();

          Modification mod = new Modification();

          mod.Type = "modified";

          mod.FileName = Path.GetFileName(path);
          mod.FolderName = Path.GetDirectoryName(path);

          mod.ModifiedTime = CreateDate(date);

          return mod;
        }
      }

      throw new Exception("Failed to match regex");
    }

    private DateTime CreateDate(
      string dateTimeString)
    {
      DateTime date = DateTime.ParseExact(dateTimeString, "yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.GetInstance(CultureInfo.InvariantCulture));

      return date;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.