Created
June 12, 2018 18:24
-
-
Save SjB/b75d5b4093468d11e1c111c4736184a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- csharp -*- -- protobuild.cake : short-description -- | |
// Copyright (c) 2018 National Centre for Audiology. All right reserved. | |
using Cake.Core; | |
using Cake.Core.Polyfill; | |
using Cake.Core.IO.Arguments; | |
public | |
class Protobuild { | |
private | |
static readonly string protobuildUrl = | |
"https://github.com/hach-que/Protobuild/raw/master/Protobuild.exe"; | |
private | |
static readonly FilePath protobuild = new FilePath("Protobuild.exe"); | |
public | |
static int Generate(ICakeContext context, string platform = null) { | |
if (string.IsNullOrEmpty(platform)) | |
platform = Platform(context); | |
var pArgs = new ProcessArgumentBuilder(); | |
pArgs.Append("-generate"); | |
pArgs.Append(platform); | |
var err = Run(context, pArgs); | |
if (err == 0) { | |
var sln = GetSolution(context, platform); | |
context.NuGetRestore(sln); | |
} | |
return err; | |
} | |
public | |
static bool IsConfigured(ICakeContext context, string platform = null) { | |
if (string.IsNullOrEmpty(platform)) | |
platform = Platform(context); | |
var solutions = context.GetFiles("./*." + platform + ".sln"); | |
return (solutions.Count == 0); | |
} | |
public | |
static int Clean(ICakeContext context, string platform = null) { | |
if (string.IsNullOrEmpty(platform)) | |
platform = Platform(context); | |
var pArgs = new ProcessArgumentBuilder(); | |
pArgs.Append("-clean"); | |
pArgs.Append(platform); | |
return Run(context, pArgs); | |
} | |
public | |
static int Build(ICakeContext context, string platform = null, | |
IList<string> arguments = null) { | |
if (string.IsNullOrEmpty(platform)) | |
platform = Platform(context); | |
var err = Generate(context, platform); | |
if (err != 0) | |
return err; | |
var pArgs = BuildArgumentFromList(arguments); | |
pArgs.Append("-build"); | |
pArgs.Append(platform); | |
return Run(context, pArgs); | |
} | |
private | |
static ProcessArgumentBuilder BuildArgumentFromList(IList<string> arguments) { | |
var pArgs = new ProcessArgumentBuilder(); | |
if (arguments != null) { | |
foreach (var a in arguments) { pArgs.Append(a); } | |
} | |
return pArgs; | |
} | |
public | |
static int Pack(ICakeContext context, FilePath filename, | |
string platform = null, IList<string> filter = null) { | |
if (string.IsNullOrEmpty(platform)) | |
platform = Platform(context); | |
var outfile = context.MakeAbsolute(filename.AppendExtension("tar.gz")); | |
var pArgs = new ProcessArgumentBuilder(); | |
pArgs.Append("-pack"); | |
pArgs.Append("."); | |
pArgs.Append(outfile.FullPath); | |
pArgs.Append(platform); | |
if (filter != null) { | |
foreach (var f in filter) { pArgs.Append(f); } | |
} | |
return Run(context, pArgs); | |
} | |
public | |
static void PurgeTools(ICakeContext context) { | |
if (context.FileExists(protobuild)) { | |
context.DeleteFile(protobuild); | |
} | |
} | |
public | |
static string HostPlatform() { | |
var platform = new CakePlatform(); | |
switch (platform.Family) { | |
case PlatformFamily.OSX: | |
return "Macos"; | |
case PlatformFamily.Linux: | |
return "Linux"; | |
case PlatformFamily.Windows: | |
return "Windows"; | |
default: | |
throw new InvalidPlatformException( | |
"Protobuild tool does not support your platform."); | |
} | |
} | |
private | |
static FilePath GetSolution(ICakeContext context, string platform) { | |
var solutions = context.GetFiles("./*." + platform + ".sln"); | |
if (solutions.Count == 0) | |
throw new ProtobuildException("Can't find solution file for platform " + | |
platform); | |
return solutions.First(); | |
} | |
private | |
static string Platform(ICakeContext context) { | |
var platform = context.Environment.GetEnvironmentVariable("PLATFORM"); | |
if (String.IsNullOrEmpty(platform)) { | |
return HostPlatform(); | |
} | |
return platform; | |
} | |
private | |
static int Run(ICakeContext context, ProcessArgumentBuilder procArgs) { | |
var exe = GetBinary(context); | |
if (!context.IsRunningOnWindows()) { | |
procArgs.Prepend(new TextArgument(exe.ToString())); | |
exe = new FilePath("mono"); | |
} | |
context.Information(procArgs.Render()); | |
var proc = context.StartAndReturnProcess( | |
exe, new ProcessSettings{Arguments = procArgs}); | |
proc.WaitForExit(); | |
return proc.GetExitCode(); | |
} | |
private | |
static FilePath GetBinary(ICakeContext context) { | |
if (!context.FileExists(protobuild)) { | |
context.DownloadFile(protobuildUrl, protobuild); | |
} | |
return protobuild; | |
} | |
} | |
public class InvalidPlatformException : Exception { | |
public | |
InvalidPlatformException(string message) : base(message) {} | |
} | |
public class ProtobuildException : Exception { | |
public | |
ProtobuildException(string message) : base(message) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment