build.cake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Install modules
  2. #module nuget:?package=Cake.DotNetTool.Module&version=0.3.0
  3. // Install tools
  4. #tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0
  5. // Install .NET Core Global tools.
  6. #tool "dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=5.0.1"
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ARGUMENTS
  9. ///////////////////////////////////////////////////////////////////////////////
  10. var target = Argument("target", "Default");
  11. var configuration = Argument("configuration", "Release");
  12. //////////////////////////////////////////////////////////////////////
  13. // PARAMETERS
  14. //////////////////////////////////////////////////////////////////////
  15. DotNetCoreMSBuildSettings msBuildSettings = null;
  16. string version = null,
  17. semVersion = null,
  18. milestone = null;
  19. FilePath litjsonProjectPath = "./src/LitJson/LitJSON.csproj";
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // SETUP / TEARDOWN
  22. ///////////////////////////////////////////////////////////////////////////////
  23. Setup(ctx =>
  24. {
  25. Information("Calculating Semantic Version");
  26. if (!BuildSystem.IsLocalBuild)
  27. {
  28. GitVersion(new GitVersionSettings{
  29. OutputType = GitVersionOutput.BuildServer
  30. });
  31. }
  32. CopyFile("./src/LitJson/AssemblyInfo.cs.in", "./src/LitJson/AssemblyInfo.cs");
  33. GitVersion assertedVersions = GitVersion(new GitVersionSettings
  34. {
  35. UpdateAssemblyInfoFilePath = "./src/LitJson/AssemblyInfo.cs",
  36. UpdateAssemblyInfo = true,
  37. OutputType = GitVersionOutput.Json,
  38. });
  39. version = assertedVersions.MajorMinorPatch;
  40. semVersion = assertedVersions.LegacySemVerPadded;
  41. milestone = string.Concat("v", version);
  42. Information("Calculated Semantic Version: {0}", semVersion);
  43. msBuildSettings = new DotNetCoreMSBuildSettings()
  44. .WithProperty("Version", semVersion)
  45. .WithProperty("AssemblyVersion", version)
  46. .WithProperty("FileVersion", version);
  47. if(!IsRunningOnWindows())
  48. {
  49. var frameworkPathOverride = ctx.Environment.Runtime.IsCoreClr
  50. ? new []{
  51. new DirectoryPath("/Library/Frameworks/Mono.framework/Versions/Current/lib/mono"),
  52. new DirectoryPath("/usr/lib/mono"),
  53. new DirectoryPath("/usr/local/lib/mono")
  54. }
  55. .Select(directory =>directory.Combine("4.5"))
  56. .FirstOrDefault(directory => ctx.DirectoryExists(directory))
  57. ?.FullPath + "/"
  58. : new FilePath(typeof(object).Assembly.Location).GetDirectory().FullPath + "/";
  59. // Use FrameworkPathOverride when not running on Windows.
  60. Information("Build will use FrameworkPathOverride={0} since not building on Windows.", frameworkPathOverride);
  61. msBuildSettings.WithProperty("FrameworkPathOverride", frameworkPathOverride);
  62. }
  63. // Executed BEFORE the first task.
  64. Information("Running tasks...");
  65. });
  66. Teardown(ctx =>
  67. {
  68. // Executed AFTER the last task.
  69. Information("Finished running tasks.");
  70. });
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // TASKS
  73. ///////////////////////////////////////////////////////////////////////////////
  74. Task("Clean")
  75. .Does(() => {
  76. CleanDirectories(
  77. new[] {
  78. "./src/bin",
  79. "./test/bin",
  80. "./src/obj",
  81. "./test/obj",
  82. "./artifacts/nuget"
  83. }
  84. );
  85. });
  86. Task("Restore")
  87. .IsDependentOn("Clean")
  88. .Does(() => {
  89. DotNetCoreRestore("./LitJSON.sln",
  90. new DotNetCoreRestoreSettings { MSBuildSettings = msBuildSettings }
  91. );
  92. });
  93. Task("Build")
  94. .IsDependentOn("Restore")
  95. .Does(() => {
  96. DotNetCoreBuild("./LitJSON.sln",
  97. new DotNetCoreBuildSettings {
  98. Configuration = configuration,
  99. MSBuildSettings = msBuildSettings,
  100. ArgumentCustomization = args => args.Append("--no-restore")
  101. }
  102. );
  103. });
  104. Task("Test")
  105. .IsDependentOn("Build")
  106. .Does(() => {
  107. DotNetCoreTest("./test/LitJSON.Tests.csproj",
  108. new DotNetCoreTestSettings {
  109. Configuration = configuration,
  110. Framework = "netcoreapp2.0",
  111. NoBuild = true,
  112. ArgumentCustomization = args => args.Append("--no-restore")
  113. }
  114. );
  115. NUnit3("./test/**/bin/" + configuration + "/net45/*.Tests.dll", new NUnit3Settings {
  116. NoResults = true
  117. });
  118. });
  119. Task("Test-SourceLink")
  120. .IsDependentOn("Build")
  121. .WithCriteria(IsRunningOnWindows())
  122. .Does(() => {
  123. foreach(var asssembly in GetFiles("./src/LitJson/bin/" + configuration + "/**/*.dll"))
  124. {
  125. DotNetCoreTool(litjsonProjectPath.FullPath, "sourcelink", $"test {asssembly}");
  126. }
  127. });
  128. Task("Package")
  129. .IsDependentOn("Test")
  130. .IsDependentOn("Test-SourceLink")
  131. .Does(() => {
  132. DotNetCorePack(litjsonProjectPath.FullPath,
  133. new DotNetCorePackSettings {
  134. Configuration = configuration,
  135. NoBuild = true,
  136. IncludeSymbols = true,
  137. OutputDirectory = "./artifacts/nuget",
  138. MSBuildSettings = msBuildSettings,
  139. ArgumentCustomization = args => args.Append("--no-restore")
  140. }
  141. );
  142. });
  143. Task("Upload-AppVeyor-Artifacts")
  144. .IsDependentOn("Package")
  145. .WithCriteria(AppVeyor.IsRunningOnAppVeyor)
  146. .Does(() => {
  147. foreach(var artifact in GetFiles("./artifacts/**/*.*"))
  148. {
  149. AppVeyor.UploadArtifact(artifact);
  150. }
  151. });
  152. Task("Publish-MyGet")
  153. .IsDependentOn("Package")
  154. .WithCriteria((AppVeyor.IsRunningOnAppVeyor && !AppVeyor.Environment.PullRequest.IsPullRequest)
  155. || StringComparer.OrdinalIgnoreCase.Equals(target, "Publish-MyGet"))
  156. .Does(() => {
  157. // Resolve the API key.
  158. var apiKey = EnvironmentVariable("MYGET_API_KEY");
  159. if(string.IsNullOrEmpty(apiKey)) {
  160. throw new InvalidOperationException("Could not resolve MyGet API key.");
  161. }
  162. // Resolve the API url.
  163. var apiUrl = EnvironmentVariable("MYGET_API_URL");
  164. if(string.IsNullOrEmpty(apiUrl)) {
  165. throw new InvalidOperationException("Could not resolve MyGet API url.");
  166. }
  167. foreach(var package in (GetFiles("./artifacts/nuget/*.nupkg") - GetFiles("./artifacts/nuget/*.symbols.nupkg")))
  168. {
  169. DotNetCoreNuGetPush(package.FullPath,
  170. new DotNetCoreNuGetPushSettings {
  171. ApiKey = apiKey,
  172. Source = apiUrl
  173. }
  174. );
  175. }
  176. });
  177. Task("Publish-NuGet")
  178. .IsDependentOn("Package")
  179. .WithCriteria((AppVeyor.IsRunningOnAppVeyor && AppVeyor.Environment.Repository.Tag.IsTag && !AppVeyor.Environment.PullRequest.IsPullRequest)
  180. || StringComparer.OrdinalIgnoreCase.Equals(target, "Publish-NuGet"))
  181. .Does(() => {
  182. // Resolve the API key.
  183. var apiKey = EnvironmentVariable("NUGET_API_KEY");
  184. if(string.IsNullOrEmpty(apiKey)) {
  185. throw new InvalidOperationException("Could not resolve MyGet API key.");
  186. }
  187. // Resolve the API url.
  188. var apiUrl = EnvironmentVariable("NUGET_API_URL");
  189. if(string.IsNullOrEmpty(apiUrl)) {
  190. throw new InvalidOperationException("Could not resolve MyGet API url.");
  191. }
  192. foreach(var package in (GetFiles("./artifacts/nuget/*.nupkg") - GetFiles("./artifacts/nuget/*.symbols.nupkg")))
  193. {
  194. DotNetCoreNuGetPush(package.FullPath,
  195. new DotNetCoreNuGetPushSettings {
  196. ApiKey = apiKey,
  197. Source = apiUrl
  198. }
  199. );
  200. }
  201. });
  202. Task("AppVeyor")
  203. .IsDependentOn("Upload-AppVeyor-Artifacts")
  204. .IsDependentOn("Publish-MyGet")
  205. .IsDependentOn("Publish-NuGet");
  206. Task("Default")
  207. .IsDependentOn("Package");
  208. RunTarget(target);