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

Building CLI with Swift

Building CLI with Swift

- Different Platforms environment
- CLI abc
- CLI in Swift
- Pros & Cons

D94c9a683d3b45cfe42548cd5a1a39b0?s=128

Alexander Voronov

March 19, 2016
Tweet

Transcript

  1. IT Saturday • @aleks_voronov • Stanfy BUILDING CLI WITH SWIFT

    ALEXANDER VORONOV, STANFY @aleks_voronov
  2. IT Saturday • @aleks_voronov • Stanfy REASON WHY?

  3. IT Saturday • @aleks_voronov • Stanfy OTHER PLATFORMS

  4. IT Saturday • @aleks_voronov • Stanfy JAVASCRIPT NODEJS NPM BOWER

    GULP GRUNT
  5. IT Saturday • @aleks_voronov • Stanfy RUBY RACK RAILS SINATRA

    RUBYGEMS RAKE
  6. IT Saturday • @aleks_voronov • Stanfy PYTHON PYPI PIP …

  7. IT Saturday • @aleks_voronov • Stanfy SWIFT COCOAPODS CARTHAGE SPM

    PERFECT COMMANDANT
  8. IT Saturday • @aleks_voronov • Stanfy FULLSTACK MULTIPLATFORM

  9. IT Saturday • @aleks_voronov • Stanfy FULLSTACK MULTIPLATFORM

  10. IT Saturday • @aleks_voronov • Stanfy WHAT IS CLI?

  11. IT Saturday • @aleks_voronov • Stanfy WHAT IS CLI?

  12. IT Saturday • @aleks_voronov • Stanfy CLI ARGUMENTS

  13. IT Saturday • @aleks_voronov • Stanfy POSIX STANDARD SHORT FLAGS

    $ ls -a $ ruby -v $ git commit -m "..."
  14. IT Saturday • @aleks_voronov • Stanfy GNU STANDARD LONG FLAGS

    $ ls --all $ ruby --version $ git commit --message "..."
  15. IT Saturday • @aleks_voronov • Stanfy COMBINED FLAGS $ git

    commit -a -m "..." $ git commit -am "..."
  16. IT Saturday • @aleks_voronov • Stanfy SUBCOMMANDS $ git log

    -2 $ git status $ git commit -am "..."
  17. IT Saturday • @aleks_voronov • Stanfy STANDARD IO 0 -

    STDIN 1 - STDOUT 2 - STDERR
  18. IT Saturday • @aleks_voronov • Stanfy 0 - STDIN 1

    - STDOUT 2 - STDERR $ cat nofile > cat: nofile: No such file or directory
  19. IT Saturday • @aleks_voronov • Stanfy 0 - STDIN 1

    - STDOUT 2 - STDERR $ cat nofile 2>>err.log
  20. IT Saturday • @aleks_voronov • Stanfy 0 - STDIN 1

    - STDOUT 2 - STDERR $ cat nofile 2>&1
  21. IT Saturday • @aleks_voronov • Stanfy 0 - STDIN 1

    - STDOUT 2 - STDERR $ cat nofile 2>>err.log 1>>out.log
  22. IT Saturday • @aleks_voronov • Stanfy 0 - STDIN 1

    - STDOUT 2 - STDERR $ cat nofile 2>/dev/null
  23. IT Saturday • @aleks_voronov • Stanfy 0 - STDIN 1

    - STDOUT 2 - STDERR $ cat nofile 2>&1 | grep kernel
  24. IT Saturday • @aleks_voronov • Stanfy UNIX PIPELINES ⛓ $

    ls -l | grep "Mar" | sort
  25. IT Saturday • @aleks_voronov • Stanfy UNIX PIPELINES ⛓ $

    ps aux | grep Xcode | awk '{print $2}'
  26. IT Saturday • @aleks_voronov • Stanfy UNIX PIPELINES ⛓ $

    git branch | xargs git branch -D
  27. IT Saturday • @aleks_voronov • Stanfy CLI WITH SWIFT

  28. IT Saturday • @aleks_voronov • Stanfy SWIFT REPL (READ-EVALUATION-PRINT-LOOP) $

    swift
  29. IT Saturday • @aleks_voronov • Stanfy IMMEDIATE MODE $ swift

    script.swift
  30. IT Saturday • @aleks_voronov • Stanfy SWIFT COMPILE ⚙ $

    swiftc script.swift $ ./script
  31. IT Saturday • @aleks_voronov • Stanfy SWIFT COMPILE ⚙ $

    swiftc script.swift -o myscript $ ./myscript
  32. IT Saturday • @aleks_voronov • Stanfy EXECUTABLE SCRIPTS

  33. IT Saturday • @aleks_voronov • Stanfy EXECUTABLE SCRIPTS #!/USR/BIN/SWIFT HASHBANG

  34. IT Saturday • @aleks_voronov • Stanfy EXECUTABLE SCRIPTS CHMOD +X

    SCRIPT.SWIFT
  35. IT Saturday • @aleks_voronov • Stanfy SCALING OUR CLI WORKING

    WITH MULTIPLE FILES
  36. IT Saturday • @aleks_voronov • Stanfy MAIN.SWIFT

  37. IT Saturday • @aleks_voronov • Stanfy EXECUTING MULTIPLE FILES $

    swift main.swift script.swift
  38. IT Saturday • @aleks_voronov • Stanfy $ swiftc main.swift script.swift

    -o script EXECUTING MULTIPLE FILES
  39. IT Saturday • @aleks_voronov • Stanfy $ swiftc script.swift main.swift

    -o script EXECUTING MULTIPLE FILES
  40. IT Saturday • @aleks_voronov • Stanfy $ swiftc *.swift -o

    script EXECUTING MULTIPLE FILES
  41. IT Saturday • @aleks_voronov • Stanfy $ find . -iname

    '*.swift' | xargs swiftc -o script EXECUTING MULTIPLE FILES
  42. IT Saturday • @aleks_voronov • Stanfy EXTERNAL RESOURCES

  43. IT Saturday • @aleks_voronov • Stanfy SYSTEM DEPENDENCIES $ swiftc

    script.swift $ xcrun -sdk macosx swiftc script.swift
  44. IT Saturday • @aleks_voronov • Stanfy CUSTOM DEPENDENCIES -F $

    swift -F /Carthage/Build/Mac script.swift
  45. IT Saturday • @aleks_voronov • Stanfy CUSTOM DEPENDENCIES #!/USR/BIN/SWIFT -F

    Carthage/Build/Mac HASHBANG
  46. IT Saturday • @aleks_voronov • Stanfy COMPILING DEPENDENT SCRIPT

  47. IT Saturday • @aleks_voronov • Stanfy $ xcrun \ -sdk

    macosx swiftc \ -F Carthage/Build/Mac script.swift COMPILING DEPENDENT SCRIPT > dyld: Library not loaded: @rpath
  48. IT Saturday • @aleks_voronov • Stanfy $ xcrun \ -sdk

    macosx swiftc \ -F Carthage/Build/Mac script.swift \ -Xlinker -rpath \ -Xlinker @executable_path/Carthage/Build/Mac COMPILING DEPENDENT SCRIPT
  49. IT Saturday • @aleks_voronov • Stanfy DEPENDENCIES MANAGEMENT

  50. IT Saturday • @aleks_voronov • Stanfy COCOAPODS

  51. IT Saturday • @aleks_voronov • Stanfy CARTHAGE

  52. IT Saturday • @aleks_voronov • Stanfy SPM SWIFT PACKAGE MANAGER

  53. IT Saturday • @aleks_voronov • Stanfy PARSING ARGUMENTS

  54. IT Saturday • @aleks_voronov • Stanfy PARSING ARGUMENTS $ ./script

    one two three $ ./script one\ two\ three $ ./script "one two three" 3 args 1 arg 1 arg
  55. IT Saturday • @aleks_voronov • Stanfy PROCESS ENUM Process.arguments ==

    [ "./script.swift", "one", "two", "three" ]
  56. IT Saturday • @aleks_voronov • Stanfy FILESYSTEM

  57. IT Saturday • @aleks_voronov • Stanfy FILESYSTEM NSFILEMANAGER NSFILEHANDLER NSTASK

    NSPIPE
  58. IT Saturday • @aleks_voronov • Stanfy HANDY LIBRARIES
 COMMANDER OPTIONKIT

    COMMANDANT REACTIVETASKS PRETTYCOLORS
  59. IT Saturday • @aleks_voronov • Stanfy REAL WORLD EXAMPLES CARTHAGEKIT


    CARTHAGE CLI
  60. IT Saturday • @aleks_voronov • Stanfy THANK YOU!

  61. IT Saturday • @aleks_voronov • Stanfy Q/A @ALEKS_VORONOV A-VORONOV