Skip to content

Commit

Permalink
Initial stab at using Assembly for service info
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisTheTechie authored and phatboyg committed Jun 24, 2013
1 parent 8a5380f commit a2902d8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/SampleTopshelfService/AssemblyInfo.cs
@@ -0,0 +1,3 @@
using System.Reflection;

[assembly: AssemblyTitle("Sample Topshelf Service")]
4 changes: 3 additions & 1 deletion src/SampleTopshelfService/Program.cs
@@ -1,4 +1,4 @@
// Copyright 2007-2012 Chris Patterson, Dru Sellers, Travis Smith, et. al.
// Copyright 2007-2013 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
Expand All @@ -22,6 +22,8 @@ static int Main()
{
x.UseLog4Net("log4net.config");
x.UseAssemblyInfoForServiceInfo();
bool throwOnStart = false;
bool throwOnStop = false;
bool throwUnhandled = false;
Expand Down
1 change: 1 addition & 0 deletions src/SampleTopshelfService/SampleTopshelfService.csproj
Expand Up @@ -88,6 +88,7 @@
<Compile Include="..\SolutionVersion.cs">
<Link>SolutionVersion.cs</Link>
</Compile>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Program.cs" />
<Compile Include="SampleSansInterfaceService.cs" />
<Compile Include="SampleService.cs" />
Expand Down
67 changes: 67 additions & 0 deletions src/Topshelf/Configuration/HostConfiguratorExtensions.cs
@@ -0,0 +1,67 @@
// Copyright 2007-2013 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace Topshelf
{
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using HostConfigurators;
using HostConfigurators.AssemblyExtensions;

public static class HostConfiguratorExtensions
{
public static void UseAssemblyInfoForServiceInfo(this HostConfigurator hostConfigurator, Assembly assembly)
{
hostConfigurator.SetDisplayName(assembly.GetAttribute<AssemblyTitleAttribute>().TryGetProperty(x => x.Title));
hostConfigurator.SetServiceName(assembly.GetAttribute<AssemblyTitleAttribute>().TryGetProperty(x => x.Title).ToServiceNameSafeString());
hostConfigurator.SetDescription(assembly.GetAttribute<AssemblyDescriptionAttribute>().TryGetProperty(x => x.Description));
}

public static void UseAssemblyInfoForServiceInfo(this HostConfigurator hostConfigurator)
{
hostConfigurator.UseAssemblyInfoForServiceInfo(Assembly.GetEntryAssembly());
}
}

namespace HostConfigurators.AssemblyExtensions
{
public static class AssemblyExtentions
{
public static T GetAttribute<T>(this Assembly assembly)
where T : Attribute
{
return assembly.GetCustomAttributes(typeof (T), false)
.Cast<T>()
.FirstOrDefault();
}

public static Return TryGetProperty<Input, Return>(this Input attribute, Func<Input, Return> accessor)
where Input : Attribute
{
if (attribute == null)
return default(Return);

return accessor(attribute);
}

public static String ToServiceNameSafeString(this String input)
{
if (input == null)
return null;

return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input).Replace(" ", String.Empty);
}
}
}
}
1 change: 1 addition & 0 deletions src/Topshelf/Topshelf.csproj
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Configuration\CommandLineParser\StringCommandLineParser.cs" />
<Compile Include="Configuration\CommandLineParser\SwitchElement.cs" />
<Compile Include="Configuration\CommandLineParser\TokenElement.cs" />
<Compile Include="Configuration\HostConfiguratorExtensions.cs" />
<Compile Include="Configuration\HostConfigurators\CommandLineConfigurator.cs" />
<Compile Include="Configuration\HostConfigurators\CommandLineDefinitionConfigurator.cs" />
<Compile Include="Configuration\HostConfigurators\CommandLineSwitchConfigurator.cs" />
Expand Down

0 comments on commit a2902d8

Please sign in to comment.