قالب وردپرس درنا توس
Home / Apple / Using CLion as an IDE for Server Side Swift Apps on Linux

Using CLion as an IDE for Server Side Swift Apps on Linux



When Swift became an open source, one of the major additions to the community was that it was ported to Linux. Since then, society has made many tools and platforms to enable easy, solid Swift development on Linux.

A question that may come up in your quest to use Swift on Linux: "Which IDE is right?"

While there are several good options on macOS, apart from Xcode, there are not many for Linux. The great team over on JetBrains made CLion a platform IDE that works on macOS and Linux.

In this tutorial, you learn how to set up with Swift on Linux, install CLion to work with Swift, and use Steam to build a Swift server app.

Note : This CLion tutorial assumes that you already have a version of Linux installed on a machine, whether native or virtual. You should also be familiar with using the command line.

Getting Started

If you want to follow this tutorial, you should use Ubuntu 1

6.04. If you use a different version of Linux, the mileage may vary and you may encounter a problem that is not covered here.

To install Swift on other versions of Linux, follow the instructions on the official Swift website.

Installing Swift

To install and other components of this tutorial, open Terminal and run the following command:

  sudo apt-get install clang libicu-dev libcurl4-openssl-dev libssl-dev git docker. io
[19659000] Note : Each time this tutorial refers to Terminal, it means a command prompt on Linux and not MacOS Terminal. 

To download Swift 4.2.1, start by navigating to the downloads ] directory in Terminal:

  cd ~ / Downloads

Next, download Swift 4.2.1 with the following command:

  wget https://swift.org/builds/swift-4.2.1-release/ubuntu1604/swift-4.2.1 -RELEASE / Quick-4.2.1-RELEASE-ubuntu16.04.tar.gz

Download Signature file for Swift:

  wget https://swift.org/builds/swift-4.2.1-release/ubuntu1604/swift-4.2.1-RELEASE/ swift-4.2 .1-RELEASE-ubuntu16.04.tar.gz.sig

If Swift is not already installed on your Linux OS, follow the installation instructions found on the Linux page.

It is important to follow the steps that are posted, as they ensure that you use a signed language download. You are welcome to download and extract Swift to your desired location. Make sure you remember where you will need the path for future steps.

To ensure that you can use Swift even after you restart your Terminal or computer, export the Swift path to .bashrc file. In Terminal, open the file in gedit :

  gedit ~ / .bashrc

Browse to the bottom of the file and paste the following line:

  export PATH = ~ / Downloads / swift-4.2.1-RELEASE-ubuntu16.04 / usr / bin: "$ {PATH}"

Note : If you use Ubuntu 18.04, replace the path with the appropriate folder for the black toolbar you downloaded.

Save the file and exit the gedit. Start at a new terminal. Make sure Swift is set up by running Swift REPL in Terminal.

  quickly

If prompted, enter your password and try to run REPL again.

To exit REPL, type:

: exit

Installing CLion

Open Ubuntu Software Center search for CLion and install. This will install a trial version of the software, which should give you plenty of time to complete this tutorial. Once installed, open CLion . You must click through some dialogs when you open CLion for the first time. If you did not purchase CLion, you can select Consider Free when prompted. This will give you 30 days to try the app.

Configuring Swift Support

To use Swift in CLion, you need to install the Swift plugin. At the bottom of the screen Welcome go to Configure ▸ Settings . Here you can configure the settings globally.

Since Swift support is not installed in CLion by default, you need to install a plugin. In the left column, search for Plugins . Then search for Swift and select Install . When this is done, restart CLion.

Now that the plug-in is installed, you need to configure CLion to know where Swift toolchain is. From the Welcome screen, select Configure stillinger Settings . In the left column you expand Construction, Execution, Distribution . Now, select Swift in the extended list.

Then enter the path where you unzipped your Swift toolchain download. Select Folder icon in the field Swift toolchain path and navigate to the installed directory of Swift. You know when you have selected the correct directory when the OK button becomes active. If you installed according to the instructions above, this folder should be in ~ / Downloads .

Select OK and follow the instructions to restart CLion.

Creating Your First Swift App

Now the CLion setup is complete, you are ready to create your first app!

From the welcome screen, select New Project . Under Swift selects Swift Package . On the right, change the path of the project from the end with untitled to Roar . Then click Layers .

When the project window is open, there are some things you need to familiarize yourself with. You can expand Roar ilder Sources ▸ Roar to see where your project files are. From now on only main.swift .

In the project window you also find Package.swift manifest file for Swift Package Manager which manages project dependencies. If you need to add some addictions to your project, do it here.

In main.swift you will find the actual code for this project. Replace the contents of the file with the following:

  class Lion {
func speak () {
out ("Roar")
}
}

leave application = Lion ()
application.speak ()

Select Run shaped as a Play button, at the top right of the screen.

In Messages at the bottom, you will see Roar printed to the console.

Adding files

Since a project you type will not be included in a file, you need to know how to add and debug files. If you are used to Xcode, things will be a little different, but still easy.

First, expand the project files on the left side of CLion, and then expand Sources . Right click on Roar and select New ▸ Swift Type

In Name enter Lion and select OK .

Replace the contents of Lion.swift with the following:

  import fund

the Lion class
func speak (_ text: string) {
print (text)
}
}

Replace the contents of main.swift with the following:

  leave application = Lion ()
application.speak ("Roar")

Then select Run and you should see the same result as before.

Troubleshooting

Finally, one of the most important things is to know how to troubleshoot the code. CLion provides good troubleshooting tools.

Open Lion.swift and add a break point by clicking in the tube next to print (text) . You should now see a red dot:

Now select Debug at the top right of the project. The icon looks like a mistake.

When your app is running, it breaks or pause, on the line of code from the previous step. This allows you to inspect your app as needed.

At the bottom of the screen, the Troubleshooter window appears, allowing you to see the state of your code and interact with the LLDB.

On the left side of the debugger, see Frames which lets you see the progress of your code and any threads.

On the right side of the debugger you will find a tab. First, you will see the Variables tab. You can tap the arrows next to each variable to expand the information about them.

Then select the category that says Swift LLDB . Here you can actually interact with LLDB using commands you may be familiar with from Xcode. In the console, type:

  po text

You should see it print the value of the text in the console:

Yay - you've made your first Swift project using CLion!

Importing a steam project

To conclude this training, you are going to take a project made with steam and import it to CLion. This gives you the flexibility to write server-side applications and distribute them, all from a Linux environment. The trial project, TO is a web app that creates and lists a list of acronyms and their meanings.

To start, download the project material using Download the materials button at the top or bottom of this tutorial.

Then select File ▸ Import Project ... and select steam-to-master in CLion. Select OK to continue and select Open Existing Project

When CLion asks how to open the project, select New Window .

CLion will recognize this as a Swift project and set up everything you need. After the project is opened, the dependencies found in Package.swift must be synchronized. This may take some time, so leave it ready before continuing.

Note : This process may take several minutes, depending on your network connection. However, the process can get stuck. If this happens, quit CLion and navigate to the project folder. If there is a hidden directory called .build delete it. Start the CLion again and it will work. It may take a few times to do this dance before it works.

Change, edit the configuration of the target. Select Run ▸ Edit Configurations ... . Change Work Register to the root directory of the project: It is the steam-to-master folder you imported earlier. This must be set so that Damp can know where your resources (pictures, leaf paints) will be served from.

This project requires some environmental updates that are easy to set up. To start, select the folder in the text field for Environmental Variables in the Edit Configuration window . Select + and add the following variables:

  • GOOGLE_CALLBACK_URL : localhost: 8080
  • GOOGLE_CLIENT_ID : identifier
  • GOOGLE_CLIENT_SECRET : ]] secret

Note : This project comes from the book Server Side Swift with Damp which goes over several steps required to get this project to run fully with Google OAuth. In this tutorial, you go through enough steps to get the project to work, but not everything contained in the book. This is just to show you the function by setting environmental variables. To understand more about how these variables affect this project, check out the book!

When done, select OK .

Configuring the database

This project requires you to have a PostgreSQL server running to save and retrieve data. To make it easy, Docker can distribute what you need in no time. Open Terminal and type the following command:

  sudo docker run - name postgres -e POSTGRES_DB = steam
-e POSTGRES_USER = vapor -e POSTGRES_PASSWORD = password
-p 5432: 5432 -d postgres

If prompted, enter your computer password.

This command will:

  • Download the dock container for PostgreSQL, if it is not already installed.
  • Start a PostgreSQL database with steam . [19659089] Give the database a username / password for steam / password .
  • Run it on port 5432 and in the background.

If you return to this project in a future time, you will not be able to run this command again. You can restart the container in Terminal using the following command:

  sudo docker start postgres

To stop it manually, run:

  sudo docker stop postgres

Now that you have a valid database running, return to CLion and press Run . As this is the first time, it will take some time to compile all the addictions needed to do a steam project.

This time, see Messages that your app is running at http: // localhost: 8080.

Open a web browser and go to localhost: 8080 . You will see that your app is up!

Navigate to Create an acronym in the navigation bar. Log in with the following credentials:

  • Username : admin
  • Password : password [19659000] When logged in, navigate back to Create an acronym and create everything you want .

    ve set up with Swift, Vapor and CLion to run in a Linux environment! You can add more features to the Damp project and examine how the code works with CLion's troubleshooting tools.

    Where to go from here?

    You can download the finished project with Download the materials button at the top or bottom of this tutorial.

    To learn more, check out these resources:

    We hope you enjoyed this tutorial! If you have any questions or comments, please join the forum discussion below!


Source link