Permalink
Browse files

Added a description to ServerItem.

Added an action to Server to list all the projects (ListProjects).
Modified the command line app so it can be an interaction console for executing commands against a server.
  • Loading branch information...
1 parent 2704265 commit 2d22423a5289b38cf61168303ca1e679b600a741 @csut017 csut017 committed Feb 28, 2011
@@ -0,0 +1,15 @@
+namespace CruiseControl.Command
+{
+ public class Command
+ {
+ #region Public properties
+ #region Name
+ public string Name { get; set; }
+ #endregion
+
+ #region Arguments
+ public string[] Arguments { get; set; }
+ #endregion
+ #endregion
+ }
+}
@@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Command.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -1,13 +1,122 @@
namespace CruiseControl.Command
{
+ using System;
+ using System.Diagnostics;
+ using System.Linq;
using CruiseControl.Common;
class Program
{
static void Main(string[] args)
{
var connection = new ServerConnection(args[0]);
- connection.Ping();
+ var exit = false;
+ WriteToConsole(ConsoleColor.White, "CruiseControl.NET: Interactive Console");
+ WriteToConsole(ConsoleColor.White, new string('=', 40));
+ WriteToConsole(ConsoleColor.Yellow, "Retrieving server name...");
+ var fullUrn = connection.RetrieveServerName();
+ WriteToConsole(ConsoleColor.Yellow, "...server name is '{0}'", fullUrn);
+ var shortUrn = fullUrn.Substring(10);
+ while (!exit)
+ {
+ Console.Write(shortUrn + ">");
+ var command = ReadFromConsole();
+ switch (command.Name)
+ {
+ case "exit":
+ exit = true;
+ break;
+
+ case "ping":
+ RunPingCommand(connection);
+ break;
+
+ case "query":
+ RunQueryCommand(connection, fullUrn);
+ break;
+
+ case "invoke":
+ RunInvokeCommand(connection, fullUrn, command.Arguments);
+ break;
+ }
+ }
+ }
+
+ private static Command ReadFromConsole()
+ {
+ var input = Console.ReadLine();
+ var parts = input.Split(' ');
+ var command = new Command();
+ if (parts.Length > 0)
+ {
+ command.Name = parts[0].ToLowerInvariant();
+ command.Arguments = parts.Skip(1).ToArray();
+ }
+
+ return command;
+ }
+
+ private static void WriteToConsole(ConsoleColor colour, string message, params object[] args)
+ {
+ var oldColour = Console.ForegroundColor;
+ try
+ {
+ Console.ForegroundColor = colour;
+ Console.WriteLine(message, args);
+ }
+ finally
+ {
+ Console.ForegroundColor = oldColour;
+ }
+ }
+
+ private static void RunPingCommand(ServerConnection connection)
+ {
+ WriteToConsole(ConsoleColor.Yellow, "Sending ping...");
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+ var result = connection.Ping();
+ stopwatch.Stop();
+ if (result)
+ {
+ WriteToConsole(ConsoleColor.Yellow, "...ping received in {0}ms", stopwatch.ElapsedMilliseconds);
+ }
+ else
+ {
+ WriteToConsole(ConsoleColor.Red, "...ping failed!", stopwatch.ElapsedMilliseconds);
+ }
+ }
+
+ private static void RunQueryCommand(ServerConnection connection, string fullUrn)
+ {
+ WriteToConsole(ConsoleColor.Yellow, "Querying '{0}'...", fullUrn);
+ try
+ {
+ var result = connection.Query(fullUrn);
+ WriteToConsole(ConsoleColor.Yellow, "...{0} action(s) retrieved", result.Length);
+ foreach (var action in result)
+ {
+ WriteToConsole(ConsoleColor.White, "\t{0}", action.Name);
+ }
+ }
+ catch (Exception error)
+ {
+ WriteToConsole(ConsoleColor.Red, "...failed: " + error.Message);
+ }
+ }
+
+ private static void RunInvokeCommand(ServerConnection connection, string fullUrn, string[] arguments)
+ {
+ WriteToConsole(ConsoleColor.Yellow, "Invoking '{1}' on '{0}'...", fullUrn, arguments[0]);
+ try
+ {
+ var result = connection.Invoke(fullUrn, arguments[0], arguments.Skip(1).ToArray());
+ WriteToConsole(ConsoleColor.Yellow, "...completed");
+ }
+ catch (Exception error)
+ {
+ WriteToConsole(ConsoleColor.Red, "...failed: " + error.Message);
+ }
}
}
}
@@ -43,6 +43,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Messages\ServerItemList.cs" />
+ <Compile Include="Messages\ServerItem.cs" />
<Compile Include="RemoteServerException.cs" />
<Compile Include="DataDefinitions.cs" />
<Compile Include="ICommunicationsChannel.cs" />
@@ -0,0 +1,40 @@
+namespace CruiseControl.Common.Messages
+{
+ /// <summary>
+ /// An item on the server.
+ /// </summary>
+ public class ServerItem
+ {
+ #region Public properties
+ #region DisplayName
+ /// <summary>
+ /// Gets or sets the display name.
+ /// </summary>
+ /// <value>
+ /// The display name.
+ /// </value>
+ public string DisplayName { get; set; }
+ #endregion
+
+ #region Urn
+ /// <summary>
+ /// Gets or sets the URN.
+ /// </summary>
+ /// <value>
+ /// The URN of the item.
+ /// </value>
+ public string Urn { get; set; }
+ #endregion
+
+ #region Description
+ /// <summary>
+ /// Gets or sets the description.
+ /// </summary>
+ /// <value>
+ /// The description.
+ /// </value>
+ public string Description { get; set; }
+ #endregion
+ #endregion
+ }
+}
@@ -0,0 +1,20 @@
+namespace CruiseControl.Common.Messages
+{
+ /// <summary>
+ /// A blank message.
+ /// </summary>
+ public class ServerItemList
+ {
+ #region Public properties
+ #region Children
+ /// <summary>
+ /// Gets or sets the children.
+ /// </summary>
+ /// <value>
+ /// The children.
+ /// </value>
+ public ServerItem[] Children { get; set; }
+ #endregion
+ #endregion
+ }
+}
@@ -50,6 +50,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\CruiseControl.Common\CruiseControl.Common.csproj">
+ <Project>{166D519E-854D-4405-9FAE-C6825CF7BBE6}</Project>
+ <Name>CruiseControl.Common</Name>
+ </ProjectReference>
<ProjectReference Include="..\CruiseControl.Core\CruiseControl.Core.csproj">
<Project>{74405CD5-BD3D-4912-A465-F929FA20220B}</Project>
<Name>CruiseControl.Core</Name>
@@ -188,6 +188,17 @@ public void CloseCommunicationsCleansUpEachChannel()
server.CloseCommunications();
Assert.IsTrue(cleaned);
}
+
+ [Test]
+ public void ListProjectsIncludesAllProjects()
+ {
+ var server = new Server(
+ "test",
+ new Project("project1"),
+ new Project("project2"));
+ var result = server.ListProjects(null);
+ Assert.AreEqual(2, result.Children.Length);
+ }
#endregion
}
}
@@ -11,6 +11,7 @@
using CruiseControl.Core.Utilities;
using Ninject;
using NLog;
+ using Messages = CruiseControl.Common.Messages;
/// <summary>
/// The root configuration node.
@@ -235,6 +236,34 @@ public virtual void Validate(IValidationLog validationLog)
#endregion
#endregion
+ #region Actions
+ #region ListProjects()
+ /// <summary>
+ /// Lists the projects on the server.
+ /// </summary>
+ /// <param name="request">The request containing the details.</param>
+ /// <returns>A message containing the response details.</returns>
+ [RemoteAction]
+ [Description("Lists the projects on the server.")]
+ public virtual Messages.ServerItemList ListProjects(Messages.Blank request)
+ {
+ var projects = this.Children.SelectMany(c => c.ListProjects());
+ var response = new Messages.ServerItemList
+ {
+ Children = projects
+ .Select(p => new Messages.ServerItem
+ {
+ Description = p.Description,
+ DisplayName = p.Name,
+ Urn = p.UniversalName
+ })
+ .ToArray()
+ };
+ return response;
+ }
+ #endregion
+ #endregion
+
#region Private methods
#region LocateInChildren()
/// <summary>
@@ -42,6 +42,14 @@ protected ServerItem(string name)
public string Name { get; set; }
#endregion
+ #region Description
+ /// <summary>
+ /// Gets the description.
+ /// </summary>
+ [DefaultValue(null)]
+ public string Description { get; set; }
+ #endregion
+
#region ItemType
/// <summary>
/// Gets the type of the item.

0 comments on commit 2d22423

Please sign in to comment.