Upgrade to Pro — share decks privately, control downloads, hide ads and more …

try! swift-sh

try! swift-sh


Swiftでスクリプトを書く場合、サードパーティのライブラリを使う場合にちょっとした面倒臭さを感じます。Package.swiftを書き、依存性を定義し、swift buildし・・。

Homebrew開発者でもあるMax Howell氏がswift-shというプロジェクトを開始しました。

try! Swift Pre Talks



March 19, 2019


  1. try! Swi) Pre Talks 2019 @satoshin21 #tryswi(_pre

  2. INTRODUCE • @satoshin21 • eureka, Inc. • Pairs JP iOS

    Applica<on Engineer #tryswi(_pre
  3. Have you ever wri,en scripts in Swi3? #tryswi(_pre

  4. How to make CLI tool with dependencies basically 1. swift

    init —type executable 2. Edit Package.swift 1. Add dependencies 2. Add dependencies to target 3. swift build —configuration debug (or swift run) 4. Execute ./.build/debug/{package name} #tryswi(_pre
  5. It's boresome a bit • It's easy to write swi.

    script without dependencies. • no needs to create Package.swift file. • But, some:mes, I just want to write simple scripts quickly. • try awesome packages... • write swi. codes as playgrounds... #tryswi(_pre
  6. Let's try! swift-sh ! #tryswi(_pre

  7. What is swi)-sh ? • Define Dependencies and write swi0

    scripts too easily • made by @mxcl (Max Howell) • Author of Homebrew, PromiseKit. #tryswi(_pre
  8. Install brew install mxcl/made/swift-sh or mint install mxcl/swift-sh #tryswi(_pre

  9. How to run script • Demo #tryswi(_pre

  10. How swi'-sh manage dependencies? 1. make build directory each script

    file • mkdir -p ~/Library/Developer/swi6.-sh.cache/{filename} 2. write Package.swi; 3. make symlink script file as main.swi6 4. swift build -Xswiftc -spress-warnings 5. exec ./build/debug/{filename} #tryswi(_pre
  11. swi$ sh run with stdin > echo "import PromiseKit //

    @mxcl\nprint(Promise.value(\"Hello, World!\"))" | swift sh #tryswi(_pre
  12. swi$ sh edit > swift sh edit sample.swift • Generate

    .xcodeproj/ file in swi$-sh cache dir • Not working dir. • cached swi: file is symlinked to working file. #tryswi(_pre
  13. swi$ sh eject > swift sh eject sample.swift . └──

    Sample ├── Package.swift └── Sources └── main.swift • Generate Package.swift file. • ⚠ swi/ file will be moved to sub directory #tryswi(_pre
  14. ⚠ Warnings #tryswi(_pre

  15. Warning 1: too late... // sample.swift import PromiseKit // @mxcl

    ~> 6.5 print(Promise.value("Hi!")) target elapsed +me compiled swi, file 0.01s ! swi,-sh(no cached) 42.95s " swi,-sh(cached) 0.67s # • swi%-sh is build every +me, so affect execu2on 2me. #tryswi(_pre
  16. Warning 2: cache key • cache directory is created with

    swi/ file name. > swift sh sample.swift ~/Library/Developer/swift-sh.cache ├── <stdin> └── sample ├── Package.resolved ├── Package.swift └── main.swift • If same file name is used, cache is shared each script. #tryswi(_pre
  17. ·ͱΊ Write with for target user Swi$PM ɾWant to manage

    targets/ dependencies strictly ɾneeds to write test code. swi$-sh ɾTry awesome Libraries ɾWrite scripts quickly #tryswi(_pre
  18. END #tryswi(_pre