PageRenderTime 442ms CodeModel.GetById 32ms RepoModel.GetById 7ms app.codeStats 0ms

/BTSControl/Microsoft.BizTalk.ApplicationDeployment.CommandLine/Command.cs

#
C# | 203 lines | 180 code | 22 blank | 1 comment | 12 complexity | 18cb3e27c99959dbb8217e4edfbe4e34 MD5 | raw file
  1. namespace Microsoft.BizTalk.ApplicationDeployment.CommandLine
  2. {
  3. using Microsoft.BizTalk.ApplicationDeployment;
  4. using Microsoft.BizTalk.Log;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.Specialized;
  8. using System.Diagnostics;
  9. using System.Globalization;
  10. using System.Text;
  11. internal abstract class Command
  12. {
  13. protected CommandResult commandResult;
  14. protected NameValueCollection nameValueArgs;
  15. protected CommandLineArgDescriptorList parameterDescriptors;
  16. protected Command(NameValueCollection nameValueArgs)
  17. {
  18. this.nameValueArgs = nameValueArgs;
  19. }
  20. public virtual void Execute()
  21. {
  22. throw new NotImplementedException();
  23. }
  24. protected abstract CommandLineArgDescriptorList GetParameterDescriptors();
  25. public string GetSummary()
  26. {
  27. return ConsoleHelper.Wrap(string.Format(CultureInfo.InvariantCulture, "{0} {1}", new object[] { this.Name.PadRight(0x12, ' '), this.Description }), 0x15, Console.BufferWidth, -19);
  28. }
  29. protected void OnLog(object sender, LogEventArgs e)
  30. {
  31. if (e != null)
  32. {
  33. using (new ConsoleColorChanger(ConsoleColorManager.GetInstance().GetColor((TraceLevel) e.LogEntry.Type)))
  34. {
  35. Console.WriteLine(e.LogEntry.ToString());
  36. }
  37. }
  38. }
  39. internal void ShowError(Exception ex)
  40. {
  41. Exception innerException = ex;
  42. StringBuilder builder = new StringBuilder();
  43. while (innerException != null)
  44. {
  45. // //Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine(innerException.ToString(), new object[0]);
  46. builder.Append(innerException.Message + Environment.NewLine);
  47. innerException = innerException.InnerException;
  48. }
  49. this.WriteLogEntry(LogEntryType.Error, builder.ToString());
  50. }
  51. public virtual void Validate()
  52. {
  53. throw new NotImplementedException();
  54. }
  55. public virtual List<CommandLineArgumentException> ValidateArgs()
  56. {
  57. List<CommandLineArgumentException> list = new List<CommandLineArgumentException>();
  58. list.AddRange(CommandLineParser.Expand(ref this.nameValueArgs, this.ParameterDescriptors));
  59. list.AddRange(CommandLineParser.Validate(this.nameValueArgs, this.ParameterDescriptors));
  60. string[] values = this.nameValueArgs.GetValues((string) null);
  61. if ((values != null) && (values.Length > 1))
  62. {
  63. string[] strArray2 = new string[values.Length - 1];
  64. for (int i = 0; i < (values.Length - 1); i++)
  65. {
  66. strArray2[i] = values[i + 1];
  67. }
  68. string formattedString = CommandResources.GetFormattedString(CommandResources.ResourceID.ExtraUnnamedArguments, new object[] { string.Join(", ", strArray2) });
  69. list.Add(new CommandLineArgumentException(formattedString, string.Empty, TraceLevel.Error));
  70. }
  71. return list;
  72. }
  73. protected void WriteLogEntry(LogEntryType logEntryType, Exception exception)
  74. {
  75. while (exception != null)
  76. {
  77. LogEntry logEntry = new LogEntry(exception.Message, logEntryType);
  78. this.OnLog(this, new LogEventArgs(logEntry));
  79. exception = exception.InnerException;
  80. }
  81. }
  82. protected void WriteLogEntry(LogEntryType logEntryType, string message)
  83. {
  84. LogEntry logEntry = new LogEntry(message, logEntryType);
  85. this.OnLog(this, new LogEventArgs(logEntry));
  86. }
  87. public virtual void WriteUsage()
  88. {
  89. Console.WriteLine(ConsoleHelper.Wrap(this.Name + ": " + this.Description, 2, Console.BufferWidth, -2));
  90. Console.WriteLine(string.Empty);
  91. Console.WriteLine(this.Usage);
  92. Console.WriteLine(string.Empty);
  93. Console.WriteLine(this.Parameters);
  94. Console.WriteLine(string.Empty);
  95. Console.WriteLine(ConsoleHelper.Wrap(this.Example, 2, Console.BufferWidth, -2));
  96. Console.WriteLine(string.Empty);
  97. Console.WriteLine(ConsoleHelper.Wrap(this.Notes, 2, Console.BufferWidth, -2));
  98. Console.WriteLine(string.Empty);
  99. }
  100. public virtual void WriteUsageHint()
  101. {
  102. Console.WriteLine(CommandResources.GetFormattedString(CommandResources.ResourceID.CommandUsageHint, new object[] { this.Name }));
  103. }
  104. public NameValueCollection Args
  105. {
  106. get
  107. {
  108. return this.nameValueArgs;
  109. }
  110. }
  111. public string Description
  112. {
  113. get
  114. {
  115. return CommandResources.GetString(string.Format(CultureInfo.InvariantCulture, "Command_{0}.Description", new object[] { this.Name }));
  116. }
  117. }
  118. public string Example
  119. {
  120. get
  121. {
  122. string str = CommandResources.GetString("Label_Example");
  123. string str3 = ConsoleHelper.Wrap(CommandResources.GetString(string.Format(CultureInfo.InvariantCulture, "Command_{0}.Example", new object[] { this.Name })), 0, Console.BufferWidth, 2);
  124. return (str + Environment.NewLine + str3);
  125. }
  126. }
  127. public abstract string Name { get; }
  128. public string Notes
  129. {
  130. get
  131. {
  132. string str = CommandResources.GetString("Label_Notes");
  133. string str3 = ConsoleHelper.Wrap(CommandResources.GetString(string.Format(CultureInfo.InvariantCulture, "Command_{0}.Notes", new object[] { this.Name })), 0, Console.BufferWidth, 2);
  134. return (str + Environment.NewLine + str3);
  135. }
  136. }
  137. public CommandLineArgDescriptorList ParameterDescriptors
  138. {
  139. get
  140. {
  141. if (this.parameterDescriptors == null)
  142. {
  143. this.parameterDescriptors = this.GetParameterDescriptors();
  144. }
  145. return this.parameterDescriptors;
  146. }
  147. }
  148. public virtual string Parameters
  149. {
  150. get
  151. {
  152. StringBuilder builder = new StringBuilder();
  153. string str = CommandResources.GetString(CommandResources.ResourceID.Label_Parameters);
  154. builder.Append(str);
  155. foreach (CommandLineArgDescriptor descriptor in this.ParameterDescriptors)
  156. {
  157. string option = descriptor.GetOption();
  158. builder.Append(Environment.NewLine);
  159. builder.Append(option);
  160. }
  161. return builder.ToString();
  162. }
  163. }
  164. public CommandResult Result
  165. {
  166. get
  167. {
  168. return this.commandResult;
  169. }
  170. }
  171. public virtual string Usage
  172. {
  173. get
  174. {
  175. string str = CommandResources.GetString(CommandResources.ResourceID.Label_Usage);
  176. string str2 = ConsoleHelper.Wrap(this.Name + this.ParameterDescriptors.GetUsage(), 0, Console.BufferWidth, 2);
  177. return (str + Environment.NewLine + str2);
  178. }
  179. }
  180. }
  181. }