PageRenderTime 17ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Mercurial.Net.Tests/Hooks/HookTestUtilities.cs

#
C# | 85 lines | 79 code | 6 blank | 0 comment | 9 complexity | 76ace6d25364583462580f47ca4bbebf MD5 | raw file
 1using System.Collections.Generic;
 2using System.Globalization;
 3using System.IO;
 4using System.Linq;
 5using System.Reflection;
 6using Mercurial.Net.Tests.Hook;
 7
 8namespace Mercurial.Tests.Hooks
 9{
10    public static class HookTestUtilities
11    {
12        public static int Count(this string text, string textToCountOccurancesOf)
13        {
14            int originalLength = text.Length;
15            text = text.Replace(textToCountOccurancesOf, string.Empty);
16            int replacedLength = text.Length;
17
18            return (originalLength - replacedLength) / textToCountOccurancesOf.Length;
19        }
20
21        public static void SetHook(this Repository repository, string hookName, params string[] arguments)
22        {
23            arguments = new string[] { hookName }.Concat(arguments ?? new string[0]).ToArray();
24            string argumentString = string.Join(" ", arguments.Select(s => "\"" + s + "\"").ToArray());
25            string[] lines;
26            string hgrcPath = Path.Combine(Path.Combine(repository.Path, ".hg"), "hgrc");
27            if (File.Exists(hgrcPath))
28                lines = File.ReadAllLines(hgrcPath);
29            else
30                lines = new string[0];
31
32            string hookCommand = string.Format(
33                CultureInfo.InvariantCulture, "{0}=\"{1}\" {2}", 
34                hookName,
35                typeof(Program).Assembly.Location,
36                argumentString);
37            var modifiedLines = new List<string>();
38            bool inHooks = false;
39            bool hookAdded = false;
40            foreach (string line in lines)
41            {
42                if (inHooks)
43                {
44                    if (line.StartsWith("["))
45                    {
46                        if (!hookAdded)
47                        {
48                            modifiedLines.Add(hookCommand);
49                            hookAdded = true;
50                        }
51                        modifiedLines.Add(line);
52                        inHooks = false;
53                    }
54                    else
55                    {
56                        if (line.StartsWith(hookName + "="))
57                        {
58                            modifiedLines.Add(hookCommand);
59                            hookAdded = true;
60                        }
61                        else
62                            modifiedLines.Add(line);
63                    }
64                }
65                else
66                {
67                    if (line == "[hooks]")
68                    {
69                        modifiedLines.Add(line);
70                        inHooks = true;
71                    }
72                }
73            }
74            if (!hookAdded)
75            {
76                if (!inHooks)
77                    modifiedLines.Add("[hooks]");
78
79                modifiedLines.Add(hookCommand);
80            }
81
82            File.WriteAllLines(hgrcPath, modifiedLines.ToArray());
83        }
84    }
85}