Skip to content

Commit 3837160

Browse files
committed
Throw if no assembly is provided
1 parent f01e739 commit 3837160

File tree

8 files changed

+72
-159
lines changed

8 files changed

+72
-159
lines changed

GitVersion.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 XerProjects
3+
Copyright (c) 2018 Joel Jeremy Marquez
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Src/Xer.Cqrs.Extensions.Microsoft.DependencyInjection/CqrsBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ICqrsBuilder AddCommandHandlers(Action<ICqrsCommandHandlerSelector> selec
4646
}
4747

4848
// Empty delegator.
49-
return new CommandDelegator(new SingleMessageHandlerRegistration().BuildMessageHandlerResolver());
49+
return new CommandDelegator(NullMessageHandlerDelegateResolver.Instance);
5050
});
5151

5252
return this;
@@ -75,7 +75,7 @@ public ICqrsBuilder AddEventHandlers(Action<ICqrsEventHandlerSelector> selector)
7575
}
7676

7777
// Empty delegator.
78-
return new EventDelegator(new MultiMessageHandlerRegistration().BuildMessageHandlerResolver());
78+
return new EventDelegator(NullMessageHandlerDelegateResolver.Instance);
7979
});
8080

8181
return this;

Src/Xer.Cqrs.Extensions.Microsoft.DependencyInjection/CqrsCommandHandlerSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ICqrsCommandHandlerSelector ByInterface(ServiceLifetime lifetime, params
3333

3434
if (assemblies.Length == 0)
3535
{
36-
throw new ArgumentException("No assemblies were provided.", nameof(assemblies));
36+
throw new ArgumentException("No command handler assemblies were provided.", nameof(assemblies));
3737
}
3838

3939
_serviceCollection.Scan(scan => scan
@@ -69,7 +69,7 @@ public ICqrsCommandHandlerSelector ByAttribute(ServiceLifetime lifetime, params
6969

7070
if (assemblies.Length == 0)
7171
{
72-
throw new ArgumentException("No assemblies were provided.", nameof(assemblies));
72+
throw new ArgumentException("No command handler assemblies were provided.", nameof(assemblies));
7373
}
7474

7575
_serviceCollection.Scan(scan => scan

Src/Xer.Cqrs.Extensions.Microsoft.DependencyInjection/CqrsEventHandlerSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ICqrsEventHandlerSelector ByInterface(ServiceLifetime lifetime, params As
3232

3333
if (assemblies.Length == 0)
3434
{
35-
throw new ArgumentException("No assemblies were provided.", nameof(assemblies));
35+
throw new ArgumentException("No event handler assemblies were provided.", nameof(assemblies));
3636
}
3737

3838
_serviceCollection.Scan(scan => scan
@@ -65,7 +65,7 @@ public ICqrsEventHandlerSelector ByAttribute(ServiceLifetime lifetime, params As
6565

6666
if (assemblies.Length == 0)
6767
{
68-
throw new ArgumentException("No assemblies were provided.", nameof(assemblies));
68+
throw new ArgumentException("No event handler assemblies were provided.", nameof(assemblies));
6969
}
7070

7171
_serviceCollection.Scan(scan => scan
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Xer.Delegator;
3+
4+
namespace Xer.Cqrs.Extensions.Microsoft.DependencyInjection
5+
{
6+
internal class NullMessageHandlerDelegateResolver : IMessageHandlerResolver
7+
{
8+
private static readonly Lazy<NullMessageHandlerDelegateResolver> _singleton = new Lazy<NullMessageHandlerDelegateResolver>(() => new NullMessageHandlerDelegateResolver());
9+
public static readonly NullMessageHandlerDelegateResolver Instance = _singleton.Value;
10+
11+
private NullMessageHandlerDelegateResolver() { }
12+
13+
public MessageHandlerDelegate ResolveMessageHandler(Type messageType) => NullMessageHandlerDelegate.Instance;
14+
}
15+
}

Src/Xer.Cqrs.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using Xer.Cqrs.Extensions.Microsoft.DependencyInjection;
34

45
namespace Microsoft.Extensions.DependencyInjection
@@ -7,6 +8,21 @@ public static class ServiceCollectionExtensions
78
{
89
public static IServiceCollection AddCqrs(this IServiceCollection serviceCollection, params Assembly[] assemblies)
910
{
11+
if (serviceCollection == null)
12+
{
13+
throw new ArgumentNullException(nameof(serviceCollection));
14+
}
15+
16+
if (assemblies == null)
17+
{
18+
throw new ArgumentNullException(nameof(assemblies));
19+
}
20+
21+
if (assemblies.Length == 0)
22+
{
23+
throw new ArgumentException("No assemblies were provided.", nameof(assemblies));
24+
}
25+
1026
AddCqrsCore(serviceCollection)
1127
.AddCommandHandlers(select =>
1228
select.ByInterface(assemblies)

build.cake

Lines changed: 33 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,31 @@ var configuration = Argument<string>("configuration", "Release");
1717

1818
var solutions = GetFiles("./**/*.sln");
1919
var projects = GetFiles("./**/*.csproj").Select(x => x.GetDirectory());
20-
string buildArtifactsDirectory = "./BuildArtifacts";
21-
22-
GitVersion gitVersion;
20+
BuildParameters buildParameters;
2321

2422
///////////////////////////////////////////////////////////////////////////////
2523
// SETUP / TEARDOWN
2624
///////////////////////////////////////////////////////////////////////////////
2725

2826
Setup(context =>
2927
{
30-
gitVersion = GitVersion(new GitVersionSettings {
31-
UpdateAssemblyInfo = true
32-
});
33-
34-
BuildParameters.Initialize(Context);
28+
buildParameters = new BuildParameters(Context);
3529

3630
// Executed BEFORE the first task.
3731
Information("Xer.Cqrs.Extensions.Microsoft.DependencyInjection");
38-
Information("Parameters");
39-
Information("///////////////////////////////////////////////////////////////////////////////");
40-
Information("Branch: {0}", BuildParameters.Instance.BranchName);
41-
Information("Version semver: {0}", gitVersion.LegacySemVerPadded);
42-
Information("Version assembly: {0}", gitVersion.MajorMinorPatch);
43-
Information("Version informational: {0}", gitVersion.InformationalVersion);
44-
Information("Master branch: {0}", BuildParameters.Instance.IsMasterBranch);
45-
Information("Dev branch: {0}", BuildParameters.Instance.IsDevBranch);
46-
Information("Hotfix branch: {0}", BuildParameters.Instance.IsHotFixBranch);
47-
Information("Publish to myget: {0}", BuildParameters.Instance.ShouldPublishMyGet);
48-
Information("Publish to nuget: {0}", BuildParameters.Instance.ShouldPublishNuGet);
49-
Information("///////////////////////////////////////////////////////////////////////////////");
50-
51-
if (DirectoryExists(buildArtifactsDirectory))
32+
Information("===========================================================================================");
33+
Information("Git Version");
34+
Information("Semver: {0}", buildParameters.GitVersion.LegacySemVerPadded);
35+
Information("Major minor patch: {0}", buildParameters.GitVersion.MajorMinorPatch);
36+
Information("Assembly: {0}", buildParameters.GitVersion.AssemblySemVer);
37+
Information("Informational: {0}", buildParameters.GitVersion.InformationalVersion);
38+
if (DirectoryExists(buildParameters.BuildArtifactsDirectory))
5239
{
5340
// Cleanup build artifacts.
54-
Information($"Cleaning up {buildArtifactsDirectory} directory.");
55-
DeleteDirectory(buildArtifactsDirectory, new DeleteDirectorySettings { Recursive = true });
41+
Information($"Cleaning up {buildParameters.BuildArtifactsDirectory} directory.");
42+
DeleteDirectory(buildParameters.BuildArtifactsDirectory, new DeleteDirectorySettings { Recursive = true });
5643
}
44+
Information("===========================================================================================");
5745
});
5846

5947
Teardown(context =>
@@ -96,11 +84,7 @@ Task("Restore")
9684

9785
var settings = new DotNetCoreRestoreSettings
9886
{
99-
ArgumentCustomization = args => args
100-
.Append("/p:Version={0}", gitVersion.LegacySemVerPadded)
101-
.Append("/p:AssemblyVersion={0}", gitVersion.MajorMinorPatch)
102-
.Append("/p:FileVersion={0}", gitVersion.MajorMinorPatch)
103-
.Append("/p:AssemblyInformationalVersion={0}", gitVersion.InformationalVersion)
87+
ArgumentCustomization = args => buildParameters.AppendVersionArguments(args)
10488
};
10589

10690
// Restore all NuGet packages.
@@ -127,11 +111,7 @@ Task("Build")
127111
var settings = new DotNetCoreBuildSettings
128112
{
129113
Configuration = configuration,
130-
ArgumentCustomization = args => args
131-
.Append("/p:Version={0}", gitVersion.LegacySemVerPadded)
132-
.Append("/p:AssemblyVersion={0}", gitVersion.MajorMinorPatch)
133-
.Append("/p:FileVersion={0}", gitVersion.MajorMinorPatch)
134-
.Append("/p:AssemblyInformationalVersion={0}", gitVersion.InformationalVersion)
114+
ArgumentCustomization = args => buildParameters.AppendVersionArguments(args)
135115
};
136116

137117
// Build all solutions.
@@ -169,6 +149,7 @@ Task("Test")
169149
});
170150

171151
Task("Pack")
152+
.Description("Create NuGet packages.")
172153
.IsDependentOn("Test")
173154
.Does(() =>
174155
{
@@ -182,14 +163,10 @@ Task("Pack")
182163

183164
var settings = new DotNetCorePackSettings
184165
{
185-
OutputDirectory = buildArtifactsDirectory,
166+
OutputDirectory = buildParameters.BuildArtifactsDirectory,
186167
Configuration = configuration,
187168
NoBuild = true,
188-
ArgumentCustomization = (args) => args
189-
.Append("/p:Version={0}", gitVersion.LegacySemVerPadded)
190-
.Append("/p:AssemblyVersion={0}", gitVersion.MajorMinorPatch)
191-
.Append("/p:FileVersion={0}", gitVersion.MajorMinorPatch)
192-
.Append("/p:AssemblyInformationalVersion={0}", gitVersion.InformationalVersion)
169+
ArgumentCustomization = args => buildParameters.AppendVersionArguments(args)
193170
};
194171

195172
foreach (var project in projects)
@@ -198,66 +175,17 @@ Task("Pack")
198175
}
199176
});
200177

201-
Task("PublishMyGet")
202-
.WithCriteria(() => BuildParameters.Instance.ShouldPublishMyGet)
203-
.IsDependentOn("Pack")
204-
.Does(() =>
205-
{
206-
// Nupkgs in BuildArtifacts folder.
207-
var nupkgs = GetFiles(buildArtifactsDirectory + "/*.nupkg");
208-
209-
if (nupkgs.Count() == 0)
210-
{
211-
Information("No nupkgs found.");
212-
return;
213-
}
214-
215-
foreach (var nupkgFile in nupkgs)
216-
{
217-
Information("Pulishing to myget {0}", nupkgFile);
218-
219-
NuGetPush(nupkgFile, new NuGetPushSettings
220-
{
221-
Source = BuildParameters.Instance.MyGetFeed,
222-
ApiKey = BuildParameters.Instance.MyGetApiKey
223-
});
224-
}
225-
});
226-
227-
Task("PublishNuGet")
228-
.WithCriteria(() => BuildParameters.Instance.ShouldPublishNuGet)
229-
.IsDependentOn("Pack")
230-
.Does(() =>
231-
{
232-
// Nupkgs in BuildArtifacts folder.
233-
var nupkgs = GetFiles(buildArtifactsDirectory + "/*.nupkg");
234-
235-
if (nupkgs.Count() == 0)
236-
{
237-
Information("No nupkgs found.");
238-
return;
239-
}
240-
241-
foreach (var nupkgFile in nupkgs)
242-
{
243-
Information("Pulishing to nuget {0}", nupkgFile);
244-
NuGetPush(nupkgFile, new NuGetPushSettings
245-
{
246-
Source = BuildParameters.Instance.NuGetFeed,
247-
ApiKey = BuildParameters.Instance.NuGetApiKey
248-
});
249-
}
250-
});
251-
252-
253178
///////////////////////////////////////////////////////////////////////////////
254179
// TARGETS
255180
///////////////////////////////////////////////////////////////////////////////
256181

257182
Task("Default")
258183
.Description("This is the default task which will be ran if no specific target is passed in.")
259-
.IsDependentOn("PublishNuGet")
260-
.IsDependentOn("PublishMyGet");
184+
.IsDependentOn("Pack")
185+
.IsDependentOn("Test")
186+
.IsDependentOn("Build")
187+
.IsDependentOn("Restore")
188+
.IsDependentOn("Clean");
261189

262190
///////////////////////////////////////////////////////////////////////////////
263191
// EXECUTION
@@ -266,64 +194,23 @@ Task("Default")
266194
RunTarget(target);
267195

268196
public class BuildParameters
269-
{
270-
private static BuildParameters _buildParameters;
271-
272-
public static BuildParameters Instance => _buildParameters;
273-
197+
{
274198
private ICakeContext _context;
199+
private GitVersion _gitVersion;
275200

276-
private BuildParameters(ICakeContext context)
201+
public BuildParameters(ICakeContext context)
277202
{
278203
_context = context;
204+
_gitVersion = context.GitVersion();
279205
}
280206

281-
public static void Initialize(ICakeContext context)
282-
{
283-
if(_buildParameters != null)
284-
{
285-
return;
286-
}
287-
288-
_buildParameters = new BuildParameters(context);
289-
}
290-
291-
public bool IsAppVeyorBuild => _context.BuildSystem().AppVeyor.IsRunningOnAppVeyor;
292-
293-
public bool IsLocalBuild => _context.BuildSystem().IsLocalBuild;
294-
295-
public bool IsPullRequest => _context.BuildSystem().AppVeyor.Environment.PullRequest.IsPullRequest;
296-
297-
public string BranchName
298-
{
299-
get
300-
{
301-
return IsLocalBuild
302-
? _context.GitBranchCurrent(".").FriendlyName
303-
: _context.BuildSystem().AppVeyor.Environment.Repository.Branch;
304-
}
305-
}
306-
307-
public string MyGetFeed => _context.EnvironmentVariable("MYGET_SOURCE");
308-
309-
public string MyGetApiKey => _context.EnvironmentVariable("MYGET_API_KEY");
310-
311-
public string NuGetFeed => _context.EnvironmentVariable("NUGET_SOURCE");
312-
313-
public string NuGetApiKey => _context.EnvironmentVariable("NUGET_API_KEY");
314-
315-
public bool IsMasterBranch => StringComparer.OrdinalIgnoreCase.Equals("master", BranchName);
316-
317-
public bool IsDevBranch => StringComparer.OrdinalIgnoreCase.Equals("dev", BranchName);
318-
319-
public bool IsReleaseBranch => BranchName.StartsWith("release", StringComparison.OrdinalIgnoreCase);
320-
321-
public bool IsHotFixBranch => BranchName.StartsWith("hotfix", StringComparison.OrdinalIgnoreCase);
207+
public GitVersion GitVersion => _gitVersion;
322208

323-
public bool ShouldPublishMyGet => !string.IsNullOrWhiteSpace(MyGetApiKey) && !string.IsNullOrWhiteSpace(MyGetFeed);
209+
public string BuildArtifactsDirectory => "./BuildArtifacts";
324210

325-
public bool ShouldPublishNuGet => !string.IsNullOrWhiteSpace(NuGetApiKey)
326-
&& !string.IsNullOrWhiteSpace(NuGetFeed)
327-
&& (IsMasterBranch || IsHotFixBranch)
328-
&& !IsPullRequest;
211+
public ProcessArgumentBuilder AppendVersionArguments(ProcessArgumentBuilder args) => args
212+
.Append("/p:Version={0}", GitVersion.LegacySemVerPadded)
213+
.Append("/p:AssemblyVersion={0}", GitVersion.MajorMinorPatch)
214+
.Append("/p:FileVersion={0}", GitVersion.MajorMinorPatch)
215+
.Append("/p:AssemblyInformationalVersion={0}", GitVersion.InformationalVersion);
329216
}

0 commit comments

Comments
 (0)