Hot questions for Using Vapor in macos

Question:

When trying to build Vapor 0.13 (and likely other versions) w/ Swift 3 and Xcode

Environment

Vapor: 0.13
Swift: 3
Snapshot: DEVELOPMENT-SNAPSHOT-06-20-A
Xcode: 8

The following error is often the first result:

error: Swift does not support the SDK 'MacOSX10.11.sdk'


Answer:

NOTE: This is a Q&A Question that is being answered by the author.

This is an issue of having Xcode 7 already installed, and the latest snapshot being unable to run properly.

Execute the following steps

  1. Completely open Xcode 8 and install additional components as prompted
  2. Make sure you've downloaded DEVELOPMENT-SNAPSHOT-06-20-A here
  3. Run the following command

    sudo xcode-select -s /Applications/Xcode-beta.app/
    

    or go to Xcode preferences, then to the Locations tab and setting the "Command Line Tools" popup to Xcode 8.0.

    This will tell your system to use the Xcode-beta tools which support latest Swift

    Note: In above example, Xcode-beta is expected to be the name of your beta. If you change the names to something like Xcode-beta2, replace above. In occasional situations, users have had to do -s /Applications/Xcode-beta.app/Contents/Developer

  4. Return to your project and Verify that your project specifies Vapor 0.13, it will look like this:

    .Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0, minor: 13),
    
  5. Build your Xcode project

    swift package generate-xcodeproj
    
  6. Completely quit Xcode 7 if it is running

  7. Open Xcode project with open *.xcodeproj

  8. Verify the project was opened with Xcode 8

  9. Select toolchain Xcode > Toolchains > DEVELOPMENT-SNAPSHOT-06-20-A

  10. Run project 🚀

If you have any more questions, checkout the #help channel in our slack.

Question:

I am trying to get a Swift Vapor project started. Following the guide here, it seems that Homebrew is the only option. I already have MacPorts and prefer it in many ways to Homebrew. Unfortunately there is no port for Vapor, so I went for the SPM installation that Vapor people describe here. I had previous success with Kitura, so I thought why not with Vapor. Well, when you go and build your project, you get

$ swift build

[... build stuff ...]

note: you may be able to install ctls using your system-packager:

    brew install ctls

[... more build stuff ...]

<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "shim.h"
    ^
[... more like that ...]

  /Users/morpheu5/web/vizex/api/.build/checkouts/crypto.git-7980259129511365902/Sources/Crypto/Cipher/Cipher+Method.swift:1:8: error: could not build Objective-C module 'CTLS'
import CTLS
    ^
<unknown>:0: error: build had 1 command failures
error: exit(1):/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/morpheu5/web/vizex/api/.build/debug.yaml

Apparently you really need this ctls package, and the only way of getting it appears to be through Homebrew/Tap.

I really don't want or need Homebrew, so how do I get to the bottom of this? I'd really like to give Vapor a try.


Answer:

Obligatory 1: installing Homebrew is the easiest way. If you then decide you don't want Homebrew, it uninstalls quite neatly.

Obligatory 2: using a Linux VM is the second easiest way.

But to answer your question and manually install CTLS:

  1. Make sure you have the libraries for LibreSSL or OpenSSL installed (using MacPorts, presumably)

  2. Download the latest release of CTLS.

  3. From the release archive, rename macos.pc to ctls.pc and then edit it using a text editor. Change the paths to point to your LibreSSL/OpenSSL installation.

  4. Move the edited ctls.pc into your $PKG_CONFIG_PATH.

I have tested this and it works for me, with the caveat that I installed LibreSSL using Homebrew so I don't know where MacPorts will put it.

Question:

I am using Mac with Sierra OS (10.12.5). I Installed Vapor using home-brew. If I tried to run command 'vapor build' in terminal it shows following error.

No .build folder, fetch may take a while...
Fetching Dependencies [  •                      ]
Error: backgroundExecute(code: 1, error: "swift-package: error: unknown command: --enable-prefetching\n", output: "")

Please help me.


Answer:

If you installed latest xcode and still getting error.

Goto: xcode -> Preferences -> Locations -> Command Line Tool

Change it to latest. Then check swift version. Then try vapor commands.

Question:

I'm new to vapor and I just installed in on MacOS, I'm getting following error when I build it. Does anyone know how to fix it? I've searched but most of the solution mentioned are for iOS rather than MacOS.

While building module 'Darwin' imported from /Users/imac1/ROOT/Vapor/Hello/.build/checkouts/swift-nio/Sources/CNIOHTTPParser/include/c_nio_http_parser.h:35:
In file included from <module-includes>:357:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:
/opt/local/include/unctrl.h:60:63: error: unknown type name 'SCREEN'
NCURSES_EXPORT(NCURSES_CONST char *) NCURSES_SP_NAME(unctrl) (SCREEN*, chtype);
                                                              ^
/opt/local/include/unctrl.h:60:53: error: function cannot return function type 'char *(int *, chtype)' (aka 'char *(int *, unsigned int)')
NCURSES_EXPORT(NCURSES_CONST char *) NCURSES_SP_NAME(unctrl) (SCREEN*, chtype);
                                                    ^
/opt/local/include/unctrl.h:60:54: error: a parameter list without types is only allowed in a function definition
NCURSES_EXPORT(NCURSES_CONST char *) NCURSES_SP_NAME(unctrl) (SCREEN*, chtype);
                                                     ^
In file included from /Users/imac1/ROOT/Vapor/Hello/.build/checkouts/swift-nio/Sources/CNIOHTTPParser/c_nio_http_parser.c:24:
/Users/imac1/ROOT/Vapor/Hello/.build/checkouts/swift-nio/Sources/CNIOHTTPParser/include/c_nio_http_parser.h:35:10: fatal error: could not build module 'Darwin'
#include <stddef.h>
 ~~~~~~~~^
4 errors generated.
[0/20] Compiling CNIOHTTPParser c_nio_http_parser.c
[1/20] Compiling CNIOLinux shim.c
[2/20] Compiling CNIOZlib empty.c
[3/20] Compiling CNIOLinux ifaddrs-android.c
[3/20] Compiling CNIOOpenSSL shims.c
[3/20] Compiling CNIOOpenSSL helpers.c
[3/20] Compiling CNIODarwin shim.c
[3/20] Compiling CNIOSHA1 c_nio_sha1.c

Answer:

Uninstall MacPorts - it installs header files where they shouldn’t be and the compiler uses those header files rather than the ones bundled with Xcode

Question:

I am writing backend using Vapor 3 and macOS 10.15 (Mac Mini). The project is ready and working. Problem is how to protect code. I don't want to store code on server unprotected. Question is: how to protect code from anyone who can access to this server (direct, physical access too: someone, who know password can clone it to flash drive)? I heard that it's impossible to build or compile Vapor app in Xcode like iOS application (Product -> Archive) (Compiling Vapor as Standalone App on MacOS). If it's possible, please let me know. Code hardening (make code hardly readable) is easy to breach (at least, my version of hardening). Maybe, can I store code (some files with .swift extension) in some cloud storage (Github, Google Drive)? Or, is there any other way to protect code?

Thank you for any help or advice!


Answer:

It is possible to launch just binary file.

1) Launch swift build -c release in your project folder, in the end it will show you the way to compiled binary file.

2) copy compiled binary file (Run) to another macOS machine and launch

The only limitation that on another macOS machine you should have installed all needed libs and packages at the same paths.

Question:

I'm starting a new vapor project and one of my own swift packages has a min deployment target of macOS 10.12.

The swift build tool itself accepts specifying the target version as follows:

swift build -Xswiftc -target -Xswiftc x86_64-apple-macosx10.12

Is it possible to specify the macOS target when building with the vapor toolbox?


Answer:

You should be able to pass arbitrary arguments through the vapor toolbox, but to be honest I'd just bypass it and use swift build directory - the toolbox doesn't add anything

Question:

The only way I've found to update a record in Vapor is this:

drop.get("update") { request in

  guard var first = try Acronym.query().first(),
    let long = request.data["long"]?.string else {
    throw Abort.badRequest
  }
  first.long = long
  try first.save()
  return first

}

However it's not a very RESTful way of doing it since it's performing a GET request with a parameter instead of a PUT request.

How does one perform a PUT request in Vapor?


Answer:

As it turns out, performing PUT, as well as other HTTP methods are as simple as changing .get() or .post() to .put() or any other HTTP methods.

As for my question, to create a PUT function in Vapor, just simply add a .put method that takes an Int (Or String, or any data type you'd like), and accept a JSON (Or whatever format you'd like), and simply update like it's a POST request.