{"id":2617,"date":"2016-01-31T09:26:50","date_gmt":"2016-01-31T15:26:50","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=2617"},"modified":"2016-02-04T13:45:48","modified_gmt":"2016-02-04T19:45:48","slug":"creating-new-packages-with-swiftpm","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/creating-new-packages-with-swiftpm\/","title":{"rendered":"Creating New Packages with SwiftPM"},"content":{"rendered":"<p><a href=\"https:\/\/swift.org\/\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-2.2-orange.svg?style=flat\" alt=\"Swift 2.2\" \/><\/a> <a href=\"https:\/\/opensource.org\/licenses\/ISC\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/License-ISC-blue.svg?style=flat\" alt=\"ISC\" \/><\/a><\/p>\n<p>Back in December I opened an enhancement request in the Swift <a href=\"https:\/\/www.atlassian.com\/software\/jira\">JIRA<\/a> site, issue <a href=\"https:\/\/bugs.swift.org\/browse\/SR-353\">SR-353<\/a>, to add an <code>npm init<\/code>-style invocation to <code>swift build<\/code> for creating all of the boilerplate files needed for a complete Swift package.  It wasn\u2019t too long after the request was added that <a href=\"https:\/\/twitter.com\/Memorion\">Bastian Rinsche<\/a> and <a href=\"https:\/\/github.com\/tlandsberg\">Tobias Landsberg<\/a> implemented the feature and had a <a href=\"https:\/\/github.com\/apple\/swift-package-manager\/pull\/117\">pull request accepted<\/a>.  Bastian has written up a <a href=\"http:\/\/blog.bastianrinsche.de\/2016\/01\/31\/contributing-to-swift\/\">great post<\/a> on his experience with opensource Swift and contributing to the community with the implementation of <code>swift build --init<\/code>.<\/p>\n<p>Let&#8217;s look at how it works.  First, create a new directory with <code>mkdir<\/code> for your package or application.  We&#8217;ll use <code>helloworld<\/code> as an example:  <code>mkdir helloworld<\/code>.  Now, <code>cd<\/code> into your <code>helloworld<\/code> directory and run <code>swift build --init<\/code>.<\/p>\n<pre class=\"crayon:false\">\n# cd helloworld\n# swift build --init\nCreating Package.swift\nCreating .gitignore\nCreating Sources\/\nCreating Sources\/main.swift\nCreating Tests\/\n<\/pre>\n<p>Let&#8217;s take a look at what <code>swift build --init<\/code> generates:<\/p>\n<ul>\n<li>`Package.swift` &#8211; the &#8220;manifest&#8221; for this package; `swift build &#8211;init` names your package the same as the directory you invoked it in\n<li>`Sources` &#8211; SwiftPM convention is that source files are included in a directory called `Sources`\n<li>`main.swift` &#8211; Swift applications will have one (and only one) `main.swift` file as the execution entry point\n<li>`Tests` &#8211; a directory to hold testcases for the application; more on this in a moment\n<li>`.gitignore` &#8211; a handy Git <a href=\"https:\/\/git-scm.com\/docs\/gitignore\">gitignore<\/a> starter file that ignores build artifacts such as `.build` and `Packages` (things you don&#8217;t want to check in to a repository)\n<\/ul>\n<p>In our <code>helloworld<\/code> example <code>Package.swift<\/code> looks like this:<\/p>\n<pre class=\"lang:swift\">\nimport PackageDescription\n\nlet package = Package(\n    name: \"helloworld\"\n)\n<\/pre>\n<p>Running <code>swift build<\/code> at this point will create <code>.build\/debug\/helloworld<\/code> which when executed you will see <code>Hello, world!<\/code>.<\/p>\n<p>If you are building a Swift-based library then remove <code>main.swift<\/code> and replace it with your library code.  Without a <code>main.swift<\/code> SwiftPM will create a static library archive.<\/p>\n<h3>Tests<\/h3>\n<p>The <code>Tests<\/code> directory added by <code>swift build --init<\/code> is a nod to the future implementation of <a href=\"https:\/\/bugs.swift.org\/browse\/SR-592\">SR-592<\/a>, which tracks <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0019-package-manager-testing.md\">SE-0019<\/a>, Swift Package Manager support for automated testing.<\/p>\n<h3>Getting the Ubuntu Package<\/h3>\n<p>To get started with <code>swift build --init<\/code> you can grab the latest Ubuntu package for Swift 2.2 (as of this writing, version 2.2-0ubuntu15).  See <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/ubuntu-packages-for-open-source-swift\/\">this post<\/a> for instructions.  For more details about using SwiftPM in general, see our introduction <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/introducing-the-swift-package-manager\/\">here<\/a>.  Happy Swifting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in December I opened an enhancement request in the Swift JIRA site, issue SR-353, to add an npm init-style invocation to swift build for creating all of the boilerplate files needed for a complete Swift package. It wasn\u2019t too long after the request was added that Bastian Rinsche and Tobias Landsberg implemented the feature [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,5],"tags":[],"class_list":["post-2617","post","type-post","status-publish","format-standard","hentry","category-linux","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2617"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=2617"}],"version-history":[{"count":26,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2617\/revisions"}],"predecessor-version":[{"id":2639,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/2617\/revisions\/2639"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=2617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=2617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=2617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}