Skip to content

Commit

Permalink
Added a description to ServerItem.
Browse files Browse the repository at this point in the history
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
csut017 committed Feb 28, 2011
1 parent 2704265 commit 2d22423
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Playground/CruiseControl.Command/Command.cs
@@ -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
}
}
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Command.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
Expand Down
111 changes: 110 additions & 1 deletion Playground/CruiseControl.Command/Program.cs
@@ -1,13 +1,122 @@
namespace CruiseControl.Command namespace CruiseControl.Command
{ {
using System;
using System.Diagnostics;
using System.Linq;
using CruiseControl.Common; using CruiseControl.Common;


class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
var connection = new ServerConnection(args[0]); 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);
}
} }
} }
} }
2 changes: 2 additions & 0 deletions Playground/CruiseControl.Common/CruiseControl.Common.csproj
Expand Up @@ -43,6 +43,8 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Messages\ServerItemList.cs" />
<Compile Include="Messages\ServerItem.cs" />
<Compile Include="RemoteServerException.cs" /> <Compile Include="RemoteServerException.cs" />
<Compile Include="DataDefinitions.cs" /> <Compile Include="DataDefinitions.cs" />
<Compile Include="ICommunicationsChannel.cs" /> <Compile Include="ICommunicationsChannel.cs" />
Expand Down
40 changes: 40 additions & 0 deletions Playground/CruiseControl.Common/Messages/ServerItem.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
}
}
20 changes: 20 additions & 0 deletions Playground/CruiseControl.Common/Messages/ServerItemList.cs
@@ -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
}
}
4 changes: 4 additions & 0 deletions Playground/CruiseControl.Console/CruiseControl.Console.csproj
Expand Up @@ -50,6 +50,10 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<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"> <ProjectReference Include="..\CruiseControl.Core\CruiseControl.Core.csproj">
<Project>{74405CD5-BD3D-4912-A465-F929FA20220B}</Project> <Project>{74405CD5-BD3D-4912-A465-F929FA20220B}</Project>
<Name>CruiseControl.Core</Name> <Name>CruiseControl.Core</Name>
Expand Down
11 changes: 11 additions & 0 deletions Playground/CruiseControl.Core.Tests/ServerTests.cs
Expand Up @@ -188,6 +188,17 @@ public void CloseCommunicationsCleansUpEachChannel()
server.CloseCommunications(); server.CloseCommunications();
Assert.IsTrue(cleaned); 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 #endregion
} }
} }
29 changes: 29 additions & 0 deletions Playground/CruiseControl.Core/Server.cs
Expand Up @@ -11,6 +11,7 @@
using CruiseControl.Core.Utilities; using CruiseControl.Core.Utilities;
using Ninject; using Ninject;
using NLog; using NLog;
using Messages = CruiseControl.Common.Messages;


/// <summary> /// <summary>
/// The root configuration node. /// The root configuration node.
Expand Down Expand Up @@ -235,6 +236,34 @@ public virtual void Validate(IValidationLog validationLog)
#endregion #endregion
#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 Private methods
#region LocateInChildren() #region LocateInChildren()
/// <summary> /// <summary>
Expand Down
8 changes: 8 additions & 0 deletions Playground/CruiseControl.Core/ServerItem.cs
Expand Up @@ -42,6 +42,14 @@ protected ServerItem(string name)
public string Name { get; set; } public string Name { get; set; }
#endregion #endregion


#region Description
/// <summary>
/// Gets the description.
/// </summary>
[DefaultValue(null)]
public string Description { get; set; }
#endregion

#region ItemType #region ItemType
/// <summary> /// <summary>
/// Gets the type of the item. /// Gets the type of the item.
Expand Down

0 comments on commit 2d22423

Please sign in to comment.