Skip to content

Instantly share code, notes, and snippets.

@patriksvensson
Created June 24, 2015 09:44
Show Gist options
  • Save patriksvensson/b4e7b5afca4cddd3e1b1 to your computer and use it in GitHub Desktop.
Save patriksvensson/b4e7b5afca4cddd3e1b1 to your computer and use it in GitHub Desktop.
Simple console application to embed Cake.
using System;
using System.Collections.Generic;
using Cake.Common.IO;
using Cake.Common.Tools.MSBuild;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.IO.NuGet;
namespace MyBuildScript
{
public class Program
{
public static void Main(string[] args)
{
// Setup the input.
var target = "Default";
var workingDir = new DirectoryPath(System.IO.Path.GetDirectoryName(typeof (Program).Assembly.Location));
var verbosity = Verbosity.Normal;
var arguments = new Dictionary<string, string>();
// Run everything.
EmbeddedCake.Run("Default", workingDir, verbosity, arguments, Script);
}
// This is the "Script"
private static void Script(ICakeEngine cake)
{
var directory = new DirectoryPath("./build");
// Clean directory task.
cake.RegisterTask("Clean").Does(context =>
{
context.CleanDirectory(directory);
});
// Build project.
cake.RegisterTask("Build")
.IsDependentOn("Clean")
.Does(context =>
{
context.MSBuild("./src/Project.sln");
});
cake.RegisterTask("Default")
.IsDependentOn("Build");
}
}
public class EmbeddedCake
{
public static void Run(string target, DirectoryPath workingDirectory, Verbosity verbosity, IDictionary<string, string> arguments, Action<ICakeEngine> script)
{
// Create the engine and setup a new context.
var engine = new CakeEngine(new NullLog());
var context = CreateContext(workingDirectory, verbosity, arguments);
// Execute the script.
script(engine);
// Run the target.
engine.RunTarget(context, new DefaultExecutionStrategy(context.Log), target);
}
private static ICakeContext CreateContext(DirectoryPath workingDirectory, Verbosity verbosity, IDictionary<string, string> args = null)
{
var fileSystem = new FileSystem();
var environment = new CakeEnvironment();
environment.WorkingDirectory = workingDirectory;
var globber = new Globber(fileSystem, environment);
var log = new ConsoleLog(verbosity);
var arguments = new CakeArguments();
arguments.SetArguments(args);
var processRunner = new ProcessRunner(environment, log);
var nugetToolResolver = new NuGetToolResolver(fileSystem, environment, globber);
var registry = new WindowsRegistry();
return new CakeContext(
fileSystem,
environment,
globber,
log,
arguments,
processRunner,
new[] { nugetToolResolver },
registry);
}
}
public class CakeArguments : ICakeArguments
{
private Dictionary<string, string> _arguments;
public CakeArguments()
{
_arguments = new Dictionary<string, string>();
}
public void SetArguments(IDictionary<string, string> arguments)
{
_arguments = new Dictionary<string, string>(arguments ?? new Dictionary<string, string>());
}
public bool HasArgument(string name)
{
return _arguments.ContainsKey(name);
}
public string GetArgument(string name)
{
string result;
_arguments.TryGetValue(name, out result);
return result;
}
}
public class ConsoleLog : ICakeLog
{
private readonly Verbosity _verbosity;
public ConsoleLog(Verbosity verbosity)
{
_verbosity = verbosity;
}
public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args)
{
Console.WriteLine(format, args);
}
public Verbosity Verbosity
{
get { return _verbosity; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment