Skip to content

Commit 9da9179

Browse files
author
Martijn van Put
committed
Publish nuget/myget and improve cake
1 parent d260198 commit 9da9179

File tree

1 file changed

+133
-7
lines changed

1 file changed

+133
-7
lines changed

build.cake

Lines changed: 133 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// ADDINS/TOOLS
33
///////////////////////////////////////////////////////////////////////////////
44
#tool "nuget:?package=GitVersion.CommandLine"
5+
#addin nuget:?package=Cake.Git
56

67
///////////////////////////////////////////////////////////////////////////////
78
// ARGUMENTS
@@ -16,10 +17,6 @@ var configuration = Argument<string>("configuration", "Release");
1617

1718
var solutions = GetFiles("./**/*.sln");
1819
var projects = GetFiles("./**/*.csproj").Select(x => x.GetDirectory());
19-
var myGetFeed = EnvironmentVariable("MYGET_SOURCE");
20-
var myGetApiKey = EnvironmentVariable("MYGET_API_KEY");
21-
var nuGetFeed = EnvironmentVariable("NUGET_SOURCE");
22-
var nuGetApiKey = EnvironmentVariable("NUGET_API_KEY");
2320

2421
GitVersion gitVersion;
2522

@@ -32,15 +29,22 @@ Setup(context =>
3229
gitVersion = GitVersion(new GitVersionSettings {
3330
UpdateAssemblyInfo = true
3431
});
32+
33+
BuildParameters.Initialize(Context);
3534

3635
// Executed BEFORE the first task.
3736
Information("Xer.Delegator");
3837
Information("Parameters");
3938
Information("///////////////////////////////////////////////////////////////////////////////");
40-
Information("Branch: {0}", gitVersion.BranchName);
39+
Information("Branch: {0}", BuildParameters.Instance.BranchName);
4140
Information("Version semver: {0}", gitVersion.LegacySemVerPadded);
4241
Information("Version assembly: {0}", gitVersion.MajorMinorPatch);
4342
Information("Version informational: {0}", gitVersion.InformationalVersion);
43+
Information("Master branch: {0}", BuildParameters.Instance.IsMasterBranch);
44+
Information("Dev branch: {0}", BuildParameters.Instance.IsDevBranch);
45+
Information("Hotfix branch: {0}", BuildParameters.Instance.IsHotFixBranch);
46+
Information("Publish to myget: {0}", BuildParameters.Instance.ShouldPublishMyGet);
47+
Information("Publish to nuget: {0}", BuildParameters.Instance.ShouldPublishNuGet);
4448
Information("///////////////////////////////////////////////////////////////////////////////");
4549
});
4650

@@ -131,16 +135,138 @@ Task("Test")
131135
}
132136
});
133137

138+
Task("Pack")
139+
.IsDependentOn("Test")
140+
.Does(() =>
141+
{
142+
var projects = GetFiles("./src/**/*.csproj");
143+
var settings = new DotNetCorePackSettings
144+
{
145+
NoBuild = true,
146+
Configuration = configuration,
147+
ArgumentCustomization = (args) => args
148+
.Append("/p:Version={0}", gitVersion.LegacySemVerPadded)
149+
.Append("/p:AssemblyVersion={0}", gitVersion.MajorMinorPatch)
150+
.Append("/p:FileVersion={0}", gitVersion.MajorMinorPatch)
151+
.Append("/p:AssemblyInformationalVersion={0}", gitVersion.InformationalVersion)
152+
};
153+
154+
foreach (var project in projects)
155+
{
156+
DotNetCorePack(project.ToString(), settings);
157+
}
158+
});
159+
160+
Task("PublishMyGet")
161+
.WithCriteria(() => BuildParameters.Instance.ShouldPublishMyGet)
162+
.IsDependentOn("Pack")
163+
.Does(() =>
164+
{
165+
var nupkgs = GetFiles("./**/*.nupkg");
166+
167+
foreach(var nupkgFile in nupkgs)
168+
{
169+
Information("Pulishing to myget {0}", nupkgFile);
170+
171+
NuGetPush(nupkgFile, new NuGetPushSettings
172+
{
173+
Source = BuildParameters.Instance.MyGetFeed,
174+
ApiKey = BuildParameters.Instance.MyGetApiKey
175+
});
176+
}
177+
});
178+
179+
Task("PublishNuGet")
180+
.WithCriteria(() => BuildParameters.Instance.ShouldPublishNuGet)
181+
.IsDependentOn("Pack")
182+
.Does(() =>
183+
{
184+
var nupkgs = GetFiles("./**/*.nupkg");
185+
186+
foreach(var nupkgFile in nupkgs)
187+
{
188+
Information("Pulishing to nuget {0}", nupkgFile);
189+
NuGetPush(nupkgFile, new NuGetPushSettings
190+
{
191+
Source = BuildParameters.Instance.NuGetFeed,
192+
ApiKey = BuildParameters.Instance.NuGetApiKey
193+
});
194+
}
195+
});
196+
197+
134198
///////////////////////////////////////////////////////////////////////////////
135199
// TARGETS
136200
///////////////////////////////////////////////////////////////////////////////
137201

138202
Task("Default")
139203
.Description("This is the default task which will be ran if no specific target is passed in.")
140-
.IsDependentOn("Test");
204+
.IsDependentOn("PublishNuGet")
205+
.IsDependentOn("PublishMyGet");
141206

142207
///////////////////////////////////////////////////////////////////////////////
143208
// EXECUTION
144209
///////////////////////////////////////////////////////////////////////////////
145210

146-
RunTarget(target);
211+
RunTarget(target);
212+
213+
public class BuildParameters
214+
{
215+
private static BuildParameters _buildParameters;
216+
217+
public static BuildParameters Instance => _buildParameters;
218+
219+
private ICakeContext _context;
220+
221+
private BuildParameters(ICakeContext context)
222+
{
223+
_context = context;
224+
}
225+
226+
public static void Initialize(ICakeContext context)
227+
{
228+
if(_buildParameters != null)
229+
{
230+
return;
231+
}
232+
233+
_buildParameters = new BuildParameters(context);
234+
}
235+
236+
public bool IsAppVeyorBuild => _context.BuildSystem().AppVeyor.IsRunningOnAppVeyor;
237+
238+
public bool IsLocalBuild => _context.BuildSystem().IsLocalBuild;
239+
240+
public string BranchName
241+
{
242+
get
243+
{
244+
return IsLocalBuild
245+
? _context.GitBranchCurrent(".").FriendlyName
246+
: _context.BuildSystem().AppVeyor.Environment.Repository.Branch;
247+
}
248+
}
249+
250+
public string MyGetFeed => _context.EnvironmentVariable("MYGET_SOURCE");
251+
252+
public string MyGetApiKey => _context.EnvironmentVariable("MYGET_API_KEY");
253+
254+
public string NuGetFeed => _context.EnvironmentVariable("NUGET_SOURCE");
255+
256+
public string NuGetApiKey => _context.EnvironmentVariable("NUGET_API_KEY");
257+
258+
public bool IsMasterBranch => StringComparer.OrdinalIgnoreCase.Equals("master", BranchName);
259+
260+
public bool IsDevBranch => StringComparer.OrdinalIgnoreCase.Equals("dev", BranchName);
261+
262+
public bool IsReleaseBranch => BranchName.StartsWith("release", StringComparison.OrdinalIgnoreCase);
263+
264+
public bool IsHotFixBranch => BranchName.StartsWith("hotfix", StringComparison.OrdinalIgnoreCase);
265+
266+
public bool ShouldPublishMyGet => !string.IsNullOrWhiteSpace(MyGetApiKey) && !string.IsNullOrWhiteSpace(MyGetFeed);
267+
268+
public bool ShouldPublishNuGet => !string.IsNullOrWhiteSpace(NuGetApiKey)
269+
&& !string.IsNullOrWhiteSpace(NuGetFeed)
270+
&& IsMasterBranch
271+
&& IsHotFixBranch;
272+
}

0 commit comments

Comments
 (0)