Make Websites With Swift, Publish, & Xcode
Swift is an open-source programming language primarily used to create iOS, iPadOS, WatchOS, and MacOS apps using software development kits provided by Apple. Although Swift was created by Apple and primarily used to develop apps for their platforms, it's an excellent, general-purpose language with a large community creating new tools and use cases with it.
Swift Web Development
There was interest in using Swift to build websites between 2016 and 2018, which even saw IBM introduce the web templating framework Kitura for that very purpose. To my shock and dismay, IBM later halted the development of Kitura, and I hadn't found any other frameworks I liked.
Until now!
Enter: Publish
Publish is a Swift framework created by John Sundell, that allows us to create entire websites–frontend to backend–using only Swift and without the need for templating frameworks, such as Kitura. In fact, the Kitura team may be migrating to Publish, as mentioned toward the bottom of their updates page.
Objective
Using John Sundell's Publish framework, GitHub user jbird84 created SwiftSite, a simple, Swift blog template. Our objective is to use that template to get a basic starter site up and running by the end of this post.
Tutorial
1. Search For Terminal app and open it.
You will see a window like the one below, with the name of your home folder displayed.
2. Set which folder you want to install Publish into.
To install it on the desktop, paste in the command below. Then, press return/enter.
cd Desktop
To install it somewhere else, change "Desktop" to the name of the folder you want to install it in. For example, the command below would install Publish into a folder named "MyFolderOfChoice".
cd MyFolderOfChoice
3. Download a clone of Publish from GitHub.
Copy and paste the command below. Then, press return/enter.
git clone <https://github.com/JohnSundell/Publish.git>
This will download a copy (clone) of the project from GitHub into a new folder named Publish.
4. Move from the Desktop folder into the newly created Publish folder.
Copy and paste the command below. Then, press return/enter.
cd Publish
5. Create a Publish project.
Copy and paste the command below. Then, press return/enter.
make
6. Make sure Publish is properly installed.
Copy and paste the command below. Then, press return/enter.
publish --help
This should list the 'Available commands':
If you don't see the list of available commands, make sure you check the above output for any errors. I received the 'Permission denied’ error:
install: /usr/local/bin/publish: Permission denied make: *** [install] Error 71
That error means Publish was not properly installed due to some kind of permissions my user account didn't have. If you get this error, copy and paste the command below. Then, press return/enter.
sudo make
You should be prompted to enter your computer password. Enter it and press return/enter. Then, the project should be built without any issues.
If you still don't see the list of available commands, your issue is beyond the scope of this tutorial and you may need to search for solutions.
6. Move back to the Desktop folder.
Copy or paste the command below. Then, press return/enter.
cd ../
7. Create your project folder.
Copy and paste the command below. Name your project by changing "ProjectName" to whatever name you want. Then, press return/enter.
mkdir ProjectName
8. Publish the new project.
Copy and paste the command below. Then, press return/enter.
publish new
This will create an Xcode project with supporting files.
9. Open the new project.
Copy and paste the command below. Then, press return/enter.
open Package.swift
This will open the project in Xcode for editing.
10. Run the project.
Copy and paste the command below. Then, press return/enter.
publish run
This will run the Xcode project, build the website, and host it via the local server on your computer using port 8000. You will use this port to view the website in the next step.
11. View the website.
Open your web browser and enter http://localhost:8000 in the address bar.
Congratulations!
You now have a live website up and running that's coded completely in Swift and editable within Xcode.
Happy building! 🤓