{"id":4632,"date":"2017-12-15T14:03:35","date_gmt":"2017-12-15T14:03:35","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4632"},"modified":"2017-12-15T14:03:35","modified_gmt":"2017-12-15T14:03:35","slug":"getting-started-with-cake","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/getting-started-with-cake\/","title":{"rendered":"Getting started with Cake"},"content":{"rendered":"<p>Before I start getting into <em>cake<\/em> usage, I want to just list some problems I had during the <a href=\"http:\/\/cakebuild.net\/docs\/tutorials\/getting-started\" target=\"_blank\">Getting Started<\/a> process. I&#8217;m not blaming <em>cake<\/em> for these, but just wanting to put these issues out there in case others hit the same.<\/p>\n<ul>\n<li>It does state in the Getting Started page, &#8220;NOTE: If downloading the latest zip file, ensure that it is unblocked otherwise you will run into issues when attempting to execute Cake.&#8221; and I forgot this step initially. If you do not unblock before unzipping, you&#8217;ll end up with all sorts of Powershell security problems.<\/li>\n<li>NuGet.exe needed to be located on your machine (not a big deal) and copied to the Tools folder &#8211; I couldn&#8217;t find this mentioned in the Getting Started, so not quite sure wther this was supposed to be in the example zip or downloaded manually or whatever<\/li>\n<li>When I eventually got cake to run I kept hitting this error &#8220;Error: Failed to install tool &#8216;NUnit.ConsoleRunner&#8217;.&#8221;. The .cake script specifies that NUnit.ConsoleRunner version 3.4.0 is required, but this continually failed to work. I ran <em>NuGet list NUnit.ConsoleRunner<\/em> and found that the package didn&#8217;t exist. When I ran <em>NuGet sources<\/em> I noticed nuget api v2 was disabled, enabling this using <em>NuGet.exe source Enable -Name &#8220;https:\/\/www.nuget.org\/api\/v2\/&#8221;<\/em> solved this. I&#8217;m not sure why cake was unable to use nuget.org (v3) which was enabled, but this solved the problem for me.<\/li>\n<\/ul>\n<p><strong>Make, Rake, Fake, Cake&#8230;<\/strong><\/p>\n<p>So first there was <em>make<\/em>, the good old (and complicated to understand) C\/C++ build tool, then there were others ant\/nant etc. Then along comes <em>fake<\/em>, the F# build tool and now <em>cake<\/em> the C# build tool. When I say F# or C#, I simply mean they use the syntax of F# or C# as opposed to nant&#8217;s (for example) XML, for creating the build etc. process. <\/p>\n<p><strong>The Basics<\/strong><\/p>\n<p><em>I&#8217;m going to solely be discussing running the cake executable against a .cake script here, not a PowerShell ps1 file hence you needn&#8217;t have to use PowerShell to try these commands out.<\/em><\/p>\n<p>By default, if you run cake without supplying a specific .cake file (the extension can be anything you prefer if specifying the cake file name), cake will try to run build.cake.<\/p>\n<p>A .cake file will have one or more Task&#8217;s (these are analogous to Target&#8217;s in nant and strangely cake has the command RunTarget to run the tasks). The task ofcourse can have some meaningful name, such as Build, Clean, Deploy (or whatever your preference). Let&#8217;s look at a simple task from Cake&#8217;s example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTask(&quot;Clean&quot;)\r\n    .Does(() =&gt;\r\n{\r\n    CleanDirectory(buildDir);\r\n});\r\n<\/pre>\n<p>In the above we have a task named Clean and this runs the code CleanDirectory, which is a DSL method to, as I&#8217;m sure you guessed, clean the supplied folder. The buildDir is declared in the example cack file as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar buildDir = Directory(&quot;.\/src\/Example\/bin&quot;) + Directory(configuration);\r\n<\/pre>\n<p>We can also compose multiple tasks using the DSL&#8217;s IsDependentOn method, such as <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nTask(&quot;Restore-NuGet-Packages&quot;)\r\n    .IsDependentOn(&quot;Clean&quot;)\r\n    .Does(() =&gt;\r\n{\r\n    NuGetRestore(&quot;.\/src\/Example.sln&quot;);\r\n});\r\n<\/pre>\n<p>So now, when the Restore-NuGet-Packages task is run cake will first run the Clean task.<\/p>\n<p>You&#8217;ll need to specify some form of command line argument to allow you to run one of these tasks, i.e. to pass a string from the command line into your cake script. This is done by declaring a line like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar target = Argument(&quot;target&quot;, &quot;Default&quot;);\r\n<\/pre>\n<p>in this case we&#8217;ve declared a target command line switch with the default value of Default (which obviously you&#8217;ll need to create a matching task name for). To run this command line using cake, you use a single hyphen such as<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\\tools\\Cake\\Cake.exe --verbosity=Verbose -target=Clean\r\n<\/pre>\n<p><em>Note: the Cake exe command line switch uses &#8211;, whereas a command line to be passed into the cake script, uses a single -.<\/em><\/p>\n<p>I&#8217;m not going to cover the DSL here, for that, go check out the <a href=\"http:\/\/cakebuild.net\/dsl\/\" target=\"_blank\">Cake Reference<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before I start getting into cake usage, I want to just list some problems I had during the Getting Started process. I&#8217;m not blaming cake for these, but just wanting to put these issues out there in case others hit the same. It does state in the Getting Started page, &#8220;NOTE: If downloading the latest [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[158],"tags":[],"class_list":["post-4632","post","type-post","status-publish","format-standard","hentry","category-cake"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4632"}],"version-history":[{"count":13,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4632\/revisions"}],"predecessor-version":[{"id":5667,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4632\/revisions\/5667"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}