Swift をコマンドラインから使う

何度目かのゲーム作りをしている。

UI ではなく、戦闘 AI やマップ自動生成などは Xcode ではなく、コマンドライン上で println & dump で確認しながら開発したい。

Swift のインストール

Mavericks に入っている Xcode 6.0 では Swift が動かない。

なので Apple Developer から Xcode 6.1 GM 以降 をダウンロード。

Vim

XcodeVim キーバインドである JugglerShu/XVim も素晴らしいけど、Vimtoyamarinyon/vim-swift を利用。

NeoBundle 'thinca/vim-quickrun'
NeoBundle 'toyamarinyon/vim-swift'
autocmd BufRead,BufNewFile *.swift set filetype=swift
let g:quickrun_config['swift'] = {
\ 'command': 'xcrun',
\ 'cmdopt': 'swift',
\ 'exec': '%c %o %s',
\}

これで ¥r で quickrun できる。

REPL

Xcode にリッチな Playground があるけど、コマンドラインの REPL がお手軽。

$ xcrun swift

alias に swift で割り当てた。

コンパイル

コマンドラインからコンパイルする予定はないけど、念のため alias に swiftc で割り当てた。

$ SDKPATH=$(/usr/bin/env xcrun --show-sdk-path --sdk macosx)
$ xcrun swiftc foo.swift
$ file foo
foo: Mach-O 64-bit executable x86_64

ユニットテスト

xcodebuild

Xcode でプロジェクトを作ってテストファイルを作成 ( Xcode を使わずコマンドラインからする方法があれば教えてください )。

Xcode 標準の xcodebuild のやり方。

$ xcodebuild test -scheme foo -destination 'platform=iOS Simulator,name=iPad'

情報量が多すぎる。xcpretty があると見やすくなる。

$ gem install xcpretty
$ xcodebuild test -scheme foo  -destination 'platform=iOS Simulator,name=iPad' |xcpretty -c

xctool

xcodebuild はテスト速度が遅すぎる。そこで Facebook 謹製の xctool

$ brew install xctool
$ xctool -scheme foo run-tests -parallelize -sdk iphonesimulator

倍くらい速い。xcodebuild よりバグった場合に表示されるコード行が多いので見やすい。それに Mac の通知で完了を教えてくれる。

Quick

RSpec 風に書ける。

最近、3 つの Rails アプリの RSpec を 3.x に書き換えたばかりで軽く消耗した身としては RSpec をやめて Test::Unit に戻る に共感中。

この QuickRSpec 以上に変化&進化中。RSpec 風の記述は好きなので、少し落ち着いたら見てみる。

Guard::Shell

ファイルを更新する毎に swift とテストの実行をするために Guard::Shell 導入。

$ bundle init
$ echo "gem 'guard-shell'" >> Gemfile
$ bundle
$ guard init shell

Guardfile を編集。

guard :shell do
  watch(/(.*)¥.swift/) do |m|
    `xcrun swift #{m[0]}`
    `xctool -scheme foo run-tests -parallelize -sdk iphonesimulator` 
  end
end

実行は下記。

$ bundle exec guard

CI

Travis CI が Swift に対応。書き方はこちらで。

パッケージ管理

CocoaPods

Objective-C 系のライブラリをパッケージ管理。

Alcatraz

Xcodeプラグインのパッケージ管理。

Swift で使えそうなプラグインがピックアップしてくれてる。

VAIO で Vagrant を使う

この内容は古いです。Vagrant 1.6.x を書きました。

結論

BIOS で VT-x を有効にする。

半年前にハマったのに、また忘れていたので今後のためにメモ。

環境

BIOS の設定

SVZ1311AJ は何故か VT-x の設定が Disabled になっている。

  • 起動時に F2 を押して BIOS を立ち上げる
  • Advanced タブの Intel(R) Virtualization Technology を Disable から Enabled に変更
  • 保存して起動

そうしないと VirtualBox

VT-x features locked for or unavailable in MSR. (VERR_VMX_MSR_LOCKED_OR_DISABLED)

のエラーを出す。

Vagrant のインストール

VagrantVirtualBoxインストーラで一発。

ネットワークの設定

Vagrantfile に下記を追加しておく。

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
#config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 3000, host: 8080

これで FreeBSD 側で :3000 で立ち上がったサーバが、ホスト側で http://localhost:8080/ で見られる。

もし Vagrant が起動済みなら設定の再読み込みを忘れずに。

$ vagrant reload

FreeBSD のインストール

http://www.vagrantbox.es/ を見ると「FreeBSD 9.2 x86_64 Minimal (VirtualBox, ZFS)」があるので使わせてもらう。

$ mkdir freebsd9
$ cd freebsd9
$ vagrant init freebsd9 https://wunki.org/files/freebsd-9.2-amd64-wunki.box
$ vagrant up
$ vagrant ssh

環境設定

あとは開発環境を設定。

FreeBSD9 に Perl 環境をインストール

FreeBSD9 に perlbrewcpanmCarton、 そして Vim と、そのプラグインをインストールする方法。

perlbrew のインストール

$ whereis perlbrew
# ないので find で検索
$ find /usr/ports/ | grep perlbrew
( 中略 )
/usr/ports/devel/p5-perlbrew/pkg-plist
$ cd /usr/ports/devel/p5-perlbrew/
$ sudo make install

perlbrew の設定

perlbrew init で初期化して .bash_profile に指示通りの設定を書く。

$ perlbrew init
$ cat ~/.bash_profile
source ~/perl5/perlbrew/etc/bashrc
$ source ~/.bash_profile

perlbrew で perl のインストール

使用可能な perl のバージョン。

$ perlbrew available
  perl-5.18.1
  perl-5.16.3
  perl-5.14.4
  perl-5.12.5
  perl-5.10.1
  perl-5.8.9
  perl-5.6.2
  perl5.005_04
  perl5.004_05
  perl5.003_07

やんごとなき理由で perl-5.8.9 を選択。

$ perlbrew install --notest perl-5.8.9
$ perlbrew switch perl-5.8.9
$ perl -v
This is perl, v5.8.9 built for amd64-freebsd

cpanm のインストール

CPAN モジュールのインストールが楽になる cpanm のインストール。

$ curl -L http://cpanmin.us | perl - App::cpanminus

本当は perlbrew を使って

$ pelbrew install-cpanm

としたかったけど、

ERROR: Failed to retrieve cpanm executable.

が出るので。

あとは CPAN を活用しまくる。

carton のインストール

インストールした CPAN の管理などが楽になる carton のインストール。

$ cpanm Carton

Carton覚え書き - sakurako_sの早起き日記

Ports を準備して Vim のインストール

初回なら Ports の準備。最初だけの設定。

$ sudo portsnap fetch
$ sudo portsnap extract

vim を探してインストール。

$ whereis vim
/usr/ports/editors/vim
$ cd /usr/ports/editors/vim
$ sudo make install clean

パッチが 1000 個くらいあたるんですかね。何十分か待ちました。

Ports はたまにアップデートしておく。

$ sudo portsnap fetch
$ sudo portsnap update

Vim のプラグイン

まずは NeoBundle を入れて、プラグインを入れまくる。

dotfiles 管理

.bash_profile や .vimrc を管理。

各種コマンド

  • ctags ( exctags になる )
  • curl
  • git
  • mosh
  • nkf
  • portaudit
  • screen or tmux
  • source-highlight
  • the_silver_searcher
  • tig
  • tree
  • wget

apt-cyg の x86_64 対応

Cygwin に setup-x86_64.exe が出てた。早速インストール。

http://www.cygwin.com/

引き続き apt-cyg もインストール。

https://code.google.com/p/apt-cyg/

いつものように apt-cyg のミラー設定を ftp://ftp.iij.ad.jp/pub/cygwin にしたところ、setup.bz2 が見つからないと出る。

$ apt-cyg -m ftp://ftp.iij.ad.jp/pub/cygwin update
Working directory is /setup
Mirror is ftp://ftp.iij.ad.jp/pub/cygwin
--2013-08-20 17:22:49--  ftp://ftp.iij.ad.jp/pub/cygwin/setup.bz2
           => `.listing'
ftp.iij.ad.jp をDNSに問いあわせています... 202.232.140.144, 202.232.140.143, 2001:240:bb8f::f:300, ...
ftp.iij.ad.jp|202.232.140.144|:21 に接続しています... 接続しました。
anonymous としてログインしています... ログインしました!
==> SYST ... 完了しました。    ==> PWD ... 完了しました。
==> TYPE I ... 完了しました。  ==> CWD (1) /pub/cygwin ... 完了しました。
==> PASV ... 完了しました。    ==> LIST ... 完了しました。

    [ <=>                                                                            ] 444         --.-K/s 時間 0.001s

2013-08-20 17:22:49 (796 KB/s) - `.listing' へ保存終了 [444]

`.listing' を削除しました。
--2013-08-20 17:22:49--  ftp://ftp.iij.ad.jp/pub/cygwin/setup.bz2
           => `setup.bz2'
==> CWD は必要ありません。
==> SIZE setup.bz2 ... 完了しました。
==> PASV ... 完了しました。    ==> RETR setup.bz2 ...
`setup.bz2' というファイルはありません。

リポジトリx86x86_64 で分かれてた。

なので

$ apt-cyg -m ftp://ftp.iij.ad.jp/pub/cygwin/x86_64 update
$ apt-cyg find gcc-core

としたけど、取得先が

ftp://ftp.iij.ad.jp/pub/cygwin/x86_64/x86_64/release/gcc/gcc-core/gcc-core-4.8.1-3.tar.bz2

のように /x86_64/x86_64 とダブる。

cygwinで「`setup.ini' というファイルはありません。 Error updating setup.ini, reverting」の対処法 - Qiita [キータ]

にあるように、apt-cyg を書き換えることで動くようになった。

Amazonでオライリー以外の洋書もKindle版の一部が0円で販売中...!!

Amazonでオライリー洋書のKindle版の一部が0円で販売中...!! なことを知った。

見ると、オライリー以外の洋書でも ¥0 があったのでご紹介。

オライリー本で頂戴した本もついでに紹介。

Code Complete

Code Complete

分厚い本なので Kindle は助かる。

Agile Project Management with Scrum (Microsoft Professional)

Agile Project Management with Scrum (Microsoft Professional)

Software Requirements

Software Requirements

The Mythical Man-Month: Essays on Software Engineering, Anniversary Edition (2nd Edition)

The Mythical Man-Month: Essays on Software Engineering, Anniversary Edition (2nd Edition)

人月の神話。

Hackers: Heroes of the Computer Revolution - 25th Anniversary Edition

Hackers: Heroes of the Computer Revolution - 25th Anniversary Edition

Hackers & Painters: Big Ideas from the Computer Age

Hackers & Painters: Big Ideas from the Computer Age

JavaScript: The Definitive Guide (Definitive Guides)

JavaScript: The Definitive Guide (Definitive Guides)

JavaScript: The Good Parts: Working with the Shallow Grain of JavaScript

JavaScript: The Good Parts: Working with the Shallow Grain of JavaScript

Scaling MongoDB

Scaling MongoDB

Learning Perl

Learning Perl

Ruby Pocket Reference (Pocket Reference (O'Reilly))

Ruby Pocket Reference (Pocket Reference (O'Reilly))

Writing GNU Emacs Extensions: Editor Customizations and Creations with Lisp

Writing GNU Emacs Extensions: Editor Customizations and Creations with Lisp

vi 本は見当たらず。

sed & awk (Nutshell Handbooks)

sed & awk (Nutshell Handbooks)

Beginning Lua Programming

Beginning Lua Programming

Getting Started with Arduino

Getting Started with Arduino

10 Terminal Tricks for Mac OS X

10 Terminal Tricks for Mac OS x が勉強になった。

  • open . でカレントフォルダを Finder で開く
  • C-r で history の検索
    • C-k & C-y でカーソル位置から行末までカット & ペーストもよく使う
    • C-a & C-e で文頭、文末も便利。なので screen の bind を C-b にしている
  • pbcopy & pbpaste でクリップボードのコピー & ペースト
  • mdfind file.txt で spotlight から検索
  • !! でコマンド再実行。sudo !! が便利
  • C-x C-e でコマンドラインをエディタで編集
  • say "Peter Piper picked a peck of pickled peppers." でしゃべらせる
  • option キーを押しながらクリックでカーソル移動。これ知らなかった。mysqlSQL が長い時に便利
  • pdfman() { man $1 -t | open -f -a Preview; }; で man を Preview で表示

あと隣のエンジニア改め下の階のエンジニアが言っていた iTerm 2 の command + ↑↓ でスクロールも便利。

それと Terminal エミュレータFinal Term が気になる。

新宿Scala座 第二回で知ったこと

Scala の知識はゼロで 新宿Scala座 第2回 を見学してきた。

まとめは 主催者の numa08 さんが、そのうち公開 してくれるでしょう。

ゆるふわで雑談ベースで楽しかったです。そのメモ。

Scala とは

  • Java VM 上で動作
  • Java のライブラリが使える
  • Java に疲れた人に心地よいらしい

インストール

Mac なら簡単。

$ brew install scala

Hello, world!

A Scala Tutorial for Java programmers より。

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

通称コップ本。744 ページもある。

Scalaスケーラブルプログラミング第2版

Scalaスケーラブルプログラミング第2版

プレゼン・ツール

Markdown で書ける Scala 製。

softprops/picture-show

勉強会で話題になったもう一つのプレゼン・ツール。

JavaScript 製で Markdown で書けて、何より Mou のようにプレビューしながら書ける。

フレームワーク

Play は Rails 以上にガンガン仕様が変わっているようだった。

Finagle のガイドにある絵は調速機。 なぜ調速機なのかは不明。

f:id:oooooooo:20130707221103p:plain

ツール

  • sbt ... maven や rake のようなもの
  • conscript ... gem のようなもの

conscript は「徴集兵」の意味。

f:id:oooooooo:20130707221233p:plain

エディタ

vimemacs もあるけど、Java VM 上の言語らしく、EclipseIntelliJ も使われているらしい。

ctags より便利ということで IntelliJ IDEA ベースの Scala IDE使っている方 がいて、

  • $199 で高いよねー
  • オープンソース・プロジェクトは無料らしいけど基準は何だろうねー
  • 2 年に一回くらい安くなるよねー

と話題になりました。

IntelliJ のライセンスはメジャー・バージョン毎なので、メジャー・バージョンが変わるとアップデートには再度支払いが必要とのこと。