LoginSignup
2

More than 3 years have passed since last update.

Windows10のUbuntu18.04でSwift 5.2.1とVapor 4.0.0へアップデート

Posted at

環境をアップデートします。

以前の環境はノートPCと共にお亡くなりになりましたが、再度同環境を構築してVaporの動作を確認していました。しかしながらテンプレートがどうも上手く動かないようで、どうせ調べるなら最新がいいなぁと思いVapor4系に更新することにしました。

Swift 5.2.1

Vapor4系はSwift5.2以上が必要なので、まずSwiftを更新します。
タイムリーなことに、2020年3月30日に5.2.1がリリースされていました。

Ubuntu 18.04用をダウンロードして展開します。

5.1.5の時と同じように/usr/local下へコピーしてパスを通します。

.bashrcを編集

#export PATH=/usr/local/swift-5.1.5-RELEASE-ubuntu18.04/usr/bin:$PATH
export PATH=/usr/local/swift-5.2.1-RELEASE-ubuntu18.04/usr/bin:$PATH

ダメだった時に備えて、Swift5.1.5も残しておきます。

source .bashrc
swift --version

swift5.2.1.PNG

Swift5.2.1が使えるようになりました。

Vapor4

Vapor4.0のヘルプを見ながらVapor Toolboxをインストールします。

githubから取ってきて、ビルドします。

git clone https://github.com/vapor/toolbox.git
cd toolbox
git checkout は最新を使うのでやらない
swift build -c release --disable-sandbox

エラー

Fetching https://github.com/tanner0101/mustache.git
Fetching https://github.com/apple/swift-log.git
Fetching https://github.com/vapor/console-kit.git
Fetching https://github.com/apple/swift-nio.git
Fetching https://github.com/jpsim/Yams.git
Cloning https://github.com/jpsim/Yams.git
Resolving https://github.com/jpsim/Yams.git at 2.0.0
Cloning https://github.com/vapor/console-kit.git
Resolving https://github.com/vapor/console-kit.git at 4.0.0-rc.1
Cloning https://github.com/apple/swift-nio.git
Resolving https://github.com/apple/swift-nio.git at 2.15.0
Cloning https://github.com/apple/swift-log.git
Resolving https://github.com/apple/swift-log.git at 1.2.0
Cloning https://github.com/tanner0101/mustache.git
Resolving https://github.com/tanner0101/mustache.git at 0.1.0
error: missing LinuxMain.swift file in the Tests directory

空でもいいという噂もありましたが、LinuxMain.swift をTests下に以下な感じで作ってみます。

import XCTest
@testable import MonoGeneratorTests

XCTMain([
     testCase(GeneratorTests.allTests),
])

もう一回ビルドすると、なにやらWarningが出てますが、とりあえずvaporは出来ています。

/home/arimitsu/toolbox/.build/checkouts/Yams/Sources/Yams/Emitter.swift:338:32: warning: initialization of 'UnsafeMutablePointer<yaml_version_directive_t>' (aka 'UnsafeMutablePointer<yaml_version_directive_s>') results in a dangling pointer
            versionDirective = UnsafeMutablePointer(&versionDirectiveValue)
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/arimitsu/toolbox/.build/checkouts/Yams/Sources/Yams/Emitter.swift:338:53: note: implicit argument conversion from 'yaml_version_directive_t' (aka 'yaml_version_directive_s') to 'UnsafeMutablePointer<yaml_version_directive_t>' (aka 'UnsafeMutablePointer<yaml_version_directive_s>') produces a pointer valid only for the duration of the call to 'init(_:)'
            versionDirective = UnsafeMutablePointer(&versionDirectiveValue)
                                                    ^~~~~~~~~~~~~~~~~~~~~~
/home/arimitsu/toolbox/.build/checkouts/Yams/Sources/Yams/Emitter.swift:338:53: note: use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope
            versionDirective = UnsafeMutablePointer(&versionDirectiveValue)
                                                    ^
[22/22] Linking vapor

./.build/release下に出力された vapor を/usr/local/binの下へコピーしてヘルプを表示して確認する。

vapor --help

vapor-help.PNG
バージョンの確認はできなくなったらしい。

vapor --version これは無し

プロジェクト作成

新規にプロジェクトを作ってみます。

vapor-beta new hello -n

ん?
vapor-betaは無いだろうなぁ
-nはいらないかな

Vapor4.0.0なのでhello「4」の以下でいきます。

vapor new hello4

Fluentはとりあえず使いませんので「n」で。

Cloning template...
name: hello4
Would you like to use Fluent?
y/n> n
fluent: No
Generating project files
+ Package.swift
+ main.swift
+ configure.swift
+ routes.swift
+ .gitkeep
+ AppTests.swift
+ Dockerfile
+ docker-compose.yml
+ .gitignore
+ .dockerignore
Creating git repository
Adding first commit

ここでGitのエラーが出るかもしれません。その前にGitのユーザー名とメールアドレスを登録しておきましょう。

git config --global user.name [名前]
git config --global user.email [メールアドレス]

hello4.PNG
hello4へ移動してbuildします。

cd hello4
vapor build

core dumped !
coredump.PNG
意味不明ですね。

ネットで調べたところ、同じようにコアダンプしている人が質問してました。

  • 質問者 「Vaporでビルドするとコアダンプするんだけど、どうしたらいい?」
  • 回答者 「Swift試してみた?」
  • 質問者 「動いた!」

以上終わり?swiftでいいのかね?

swift build

hello4build.PNG
またか・・・
上でつくったLinuxMain.swift をTests下にコピーしてやります。

swift build

linkingrun.PNG

できた!

swift run

swiftrun.PNG

ブラウザからも動作を確認できました。

ブラウザ.PNG

途中でいろいろやったこと

ネットで調べてやってみましたがが、とりあえず必要なかったです。

Package.swift

プラットフォーム指定がmacOSだからダメなんじゃないの?でコメントアウト
platform.PNG
dependenciesで指定しているパッケージのfromが最新のものを固定で指定してみる
package.PNG

toolbox

toolboxのPackage.swiftでも、同様にやってみる。

toolboxのビルドで-Xswiftcをつけてみる、-g(何これ?)をつけてみる

最後に

ビルドのWarningが気になりますがerrorではないので致命的ではないし、とりあえず動くようにはなりましたので、テンプレートの確認に戻ります。
動くのかな?

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2