implement nuget list function with service messages support
[teamcity/dotNetPackagesSupport.git] / nuget-extensions / nuget-commands / src / Util / ServiceMessageFormatter.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Linq;\r
4 using System.Text;\r
5 \r
6 namespace JetBrains.TeamCity.NuGet.ExtendedCommands.Util\r
7 {\r
8   public static class ServiceMessageFormatter\r
9   {\r
10     private static readonly Pair[] ESCAPINGS = {\r
11                                                  new Pair('|', "||"), \r
12                                                  new Pair('\'', "|'"), \r
13                                                  new Pair('\n', "|n"), \r
14                                                  new Pair('\r', "|r"), \r
15                                                  new Pair(']', "|]")\r
16                                                };\r
17 \r
18     private const string SERVICE_MESSAGE_HEADER = "##teamcity[";\r
19 \r
20     /// <summary>\r
21     /// Performs TeamCity-format escaping of a string.\r
22     /// </summary>\r
23     /// <returns></returns>\r
24     private static string Escape(string value)\r
25     {\r
26       return ESCAPINGS.Aggregate(value, (current, pair) => current.Replace(pair.What, pair.Replacement));\r
27     }\r
28 \r
29     private struct Pair\r
30     {\r
31       public readonly string What;\r
32       public readonly string Replacement;\r
33 \r
34       private Pair(string what, string replacement)\r
35       {\r
36         What = what;\r
37         Replacement = replacement;\r
38       }\r
39 \r
40       public Pair(char what, string replacement)\r
41         : this(what.ToString(), replacement)\r
42       {\r
43       }\r
44     }\r
45 \r
46     public static string FormatMessage(string sMessageName, string sValue)\r
47     {\r
48       if (string.IsNullOrEmpty(sMessageName))\r
49         throw new ArgumentNullException("sMessageName");\r
50       if (sValue == null)\r
51         throw new ArgumentNullException("sValue");\r
52 \r
53       if (Escape(sMessageName) != sMessageName)\r
54         throw new ArgumentException("The message name contains illegal characters.", "sMessageName");\r
55 \r
56       return string.Format("{2}{0} '{1}']", sMessageName, Escape(sValue), SERVICE_MESSAGE_HEADER);\r
57     }\r
58 \r
59     public static string FormatMessage(string sMessageName, params ServiceMessageProperty[] properties)\r
60     {\r
61       if (string.IsNullOrEmpty(sMessageName))\r
62         throw new ArgumentNullException("sMessageName");\r
63       if (properties == null)\r
64         throw new ArgumentNullException("properties");\r
65 \r
66       if (Escape(sMessageName) != sMessageName)\r
67         throw new ArgumentException("The message name contains illegal characters.", "sMessageName");\r
68 \r
69       var sb = new StringBuilder();\r
70       sb.AppendFormat("{1}{0}", sMessageName, SERVICE_MESSAGE_HEADER);\r
71       foreach (var property in properties)\r
72       {\r
73         if (Escape(property.Key) != property.Key)\r
74           throw new InvalidOperationException(string.Format("The property name \93{0}\94 contains illegal characters.", property.Key));\r
75         sb.AppendFormat(" {0}='{1}'", property.Key, Escape(property.Value));\r
76       }\r
77       sb.Append(']');\r
78 \r
79       return sb.ToString();\r
80     }\r
81   }\r
82 }