$30 off During Our Annual Pro Sale. View Details »

Bang.sh - Bash Framework

Bang.sh - Bash Framework

Gustavo Dutra

May 18, 2013
Tweet

More Decks by Gustavo Dutra

Other Decks in Programming

Transcript

  1. Bang.sh

    View Slide

  2. Gustavo Dutra
    twitter.com/gustavotkg
    github.com/bellthoven

    View Slide

  3. View Slide

  4. View Slide

  5. Agenda

    View Slide

  6. Introduction
    Setup
    Organization
    Options
    Arsenal
    Exceptions
    Unit Tests
    What’s next

    View Slide

  7. View Slide

  8. BASH 4

    View Slide

  9. Easy setup

    View Slide

  10. mkdir project/
    cd project/
    git clone \
    https://github.com/bellthoven/bang.sh

    View Slide

  11. #!/bin/bash
    source “bangsh/src/bang.sh”
    echo “Hello World”

    View Slide

  12. Hitting the target
    Organization

    View Slide

  13. Repositories

    View Slide

  14. No conventions

    View Slide

  15. source ./functions.sh

    View Slide

  16. source ./utils.sh

    View Slide

  17. SRC_DIR=$(dirname $0)
    source “$SRC_DIR/funcs.sh”

    View Slide

  18. Modules

    View Slide

  19. modules/.sh

    View Slide

  20. b.module.prepend_lookup_dir
    b.module.append_lookup_dir

    View Slide

  21. (b.).

    View Slide

  22. __

    View Slide

  23. b.module.require
    source ./modules/.sh

    View Slide

  24. b.module.resolve_path
    ./modules/.sh

    View Slide

  25. Options

    View Slide

  26. b.module.require opt

    View Slide

  27. b.opt.add_opt \
    --title “Sets the title”

    View Slide

  28. b.opt.add_flag \
    --debug “Enables debug”

    View Slide

  29. b.opt.add_alias --title -t

    View Slide

  30. b.opts.required_args --title

    View Slide

  31. b.opt.init “$@”

    View Slide

  32. b.opt.check_required_args

    View Slide

  33. b.opt.has_flag? --debug

    View Slide

  34. b.opt.get_opt --title

    View Slide

  35. b.opt.show_usage

    View Slide

  36. Example!

    View Slide

  37. #!/bin/bash
    source “bang.sh”
    function load_options () {
    b.opt.add_flag --stderr "Prints to stderr"
    b.opt.add_opt --text "Specify text to be printed"
    b.opt.add_alias --text -t
    b.opt.required_args --text
    }

    View Slide

  38. #!/bin/bash
    source “bang.sh”
    function load_options () {
    # ...
    }
    function run () {
    load_options
    b.opt.init "$@"
    if b.opt.check_required_args; then
    local text="$(b.opt.get_opt --text)"
    if b.opt.has_flag? --stderr; then
    echo "$text" 1>&2
    else
    echo “$text”
    fi
    fi
    }

    View Slide

  39. #!/bin/bash
    source “bang.sh”
    function load_options () {
    # ...
    }
    function run () {
    # ...
    }
    # Run!
    run “$@”

    View Slide

  40. $ ./samples/parsing_argv.sh \
    --text ‘Hello World’ 2> /dev/null
    Hello World
    $ ./samples/parsing_argv.sh --stderr\
    --text ‘Hello World’ > /dev/null
    Hello World

    View Slide

  41. Arsenal

    View Slide

  42. b.str.length “$foo”
    “${#foo}”

    View Slide

  43. b.str.part “$foo” 2 2
    “${foo:2:2}”
    b.str.part “$foo” -2
    “${foo:(-2)}”

    View Slide

  44. b.path.expand symlink/
    # => /absolute/path/
    (follow symlinks)
    b.path.expand ~/file.txt
    # => /absolute/path/file.txt

    View Slide

  45. if [ -f “$1” ]
    if b.path.file? “$1”

    View Slide

  46. if [ “$1” -ot “$2” ]; then

    View Slide

  47. if b.path.older? “$1” “$2”; then

    View Slide

  48. b.set ‘config.name’ ‘value’
    b.get ‘config.name’

    View Slide

  49. b.unset ‘config’

    View Slide

  50. bangsh.com/api

    View Slide

  51. Exceptions

    View Slide

  52. b.try.do something
    b.catch ExceptionName action
    b.finnaly something
    b.try.end

    View Slide

  53. Example!

    View Slide

  54. #!/bin/bash
    source “bang.sh”
    function load_options () {
    b.opt.add_flag --stderr "Prints to stderr"
    b.opt.add_opt --text "Specify text to be printed"
    b.opt.add_alias --text -t
    b.opt.required_args --text
    }

    View Slide

  55. #!/bin/bash
    source “bang.sh”
    function load_options () {
    # ...
    }
    function run () {
    load_options
    b.opt.init "$@"
    if b.opt.check_required_args; then
    local text="$(b.opt.get_opt --text)"
    if b.opt.has_flag? --stderr; then
    echo "$text" 1>&2
    else
    echo “$text”
    fi
    fi
    }

    View Slide

  56. #!/bin/bash
    source “bang.sh”
    function load_options () {
    # ...
    }
    function run () {
    # ...
    }
    # Run!
    b.try.do run “$@”
    b.catch RequiredOptionNotSet b.opt.show_usage
    b.try.end

    View Slide

  57. $ ./samples/parsing_argv.sh [--text]
    Showing usage:
    --stderr
    Prints to stderr
    --text|-t (Required)
    Specify text to be printed

    View Slide

  58. b.module.require foobar
    The program was aborted due to an
    error:
    Uncautch exception NoModuleFound:
    Module foobar not found

    View Slide

  59. Hitting the target

    View Slide

  60. #!/bin/bash
    source “bang.sh”
    b.module.require unittest

    View Slide

  61. function b.test.trim () {
    b.unittest.assert_equal \
    “ABC” “$(b.str.trim ‘ ABC ‘)”
    }

    View Slide

  62. function b.test.if_creates_dir () {
    mkdir /tmp/foo
    b.path.dir? /tmp/foo
    b.unittest.assert_success $?
    }

    View Slide

  63. function b.test.if_fails () {
    cat not_exists.txt &> /dev/null
    b.unittest.assert_error $?
    }

    View Slide

  64. function b.test.if_raises_error () {
    function foo () {
    b.raise InvalidArgumentsError
    }
    b.unittest.assert_raise \
    foo InvalidArgumentsError
    }

    View Slide

  65. Mocking

    View Slide

  66. function net_up? () {
    ping -c1 8.8.8.8 &> /dev/null
    }
    function print_net_stats () {
    if net_up?; then
    echo "UP"
    else
    echo "DOWN"
    fi
    }

    View Slide

  67. function b.test.mocking_up () {
    function net_up () { return 0; }
    b.unittest.double.do net_up? net_up
    b.unittest.assert_equal \
    "UP" "$(print_net_stats)"
    b.unittest.double.undo net_up?
    }

    View Slide

  68. b.module.require unittest
    function b.test.mocking_up () { ... }
    function b.test.mocking_down () {
    function net_down { return 1; }
    b.unittest.double.do \
    net_up? net_down
    b.unittest.assert_equal \
    "DOWN" "$(print_net_stats)"
    b.unittest.double.undo net_up?
    }

    View Slide

  69. Running Tests

    View Slide

  70. b.unittest.add_test_case \

    b.unittest.run_tests

    View Slide

  71. b.unittest.autorun_tests

    View Slide

  72. Running test case ‘b.test.trim’
    Running test case ‘b.test.if_creates_dir’
    Running test case ‘b.test.if_fails’
    Running test case ‘b.test.if_raises_error’
    Running test case ‘b.test.doubling_calls’
    4 tests executed (Assertions: 5 passed / 0 failed)

    View Slide

  73. FYI

    View Slide

  74. github.com/sstephenson/bats

    View Slide

  75. #!/usr/bin/env bats
    create_version () {
    mkdir -p "${RBENV_ROOT}/versions/$1"
    }
    @test "set by RBENV_VERSION" {
    create_version "1.9.3"
    RBENV_VERSION=1.9.3 run rbenv-version
    assert_success "1.9.3 (set by
    RBENV_VERSION environment variable)"
    }

    View Slide

  76. We’re heading to...

    View Slide

  77. Color module

    View Slide

  78. Better test outputs

    View Slide

  79. Tasks

    View Slide

  80. Executables
    bang new
    bang test

    View Slide

  81. Template module

    View Slide

  82. Repositories

    View Slide

  83. getopt

    View Slide

  84. Try module opt
    to parse function params
    print -msg “$hi” -to STDERR

    View Slide

  85. Thanks!
    twitter.com/gustavotkg
    github.com/bellthoven
    bangsh.com

    View Slide

  86. • http://www.flickr.com/photos/us_mission_canada/8513394054
    • http://www.flickr.com/photos/linznicholson/6095327369
    • http://www.flickr.com/photos/wfryer/3242289990
    • http://www.flickr.com/photos/skyblue2/1459329859
    • /http://commons.wikimedia.org/wiki/File:Saint-L
    %C3%B4_Railway_station_destroyed.jpg
    • http://commons.wikimedia.org/wiki/
    File:Cowboy_Action_Shooting.JPG
    Images reference (in order)

    View Slide