Skip to content

Instantly share code, notes, and snippets.

@lantins
Created January 11, 2015 01:35

Revisions

  1. lantins created this gist Jan 11, 2015.
    57 changes: 57 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    #
    # Makefile to perform "live code reloading" after changes to .go files.
    #
    # n.b. you must install fswatch (OS X: `brew install fswatch`)
    #
    # To start live reloading run the following command:
    # $ make serve
    #

    # binary name to kill/restart
    PROG = vanilla
    BINDATA_PATHS = tmpl/... assets/...

    # targets not associated with files
    .PHONY: dependencies default build test coverage clean kill restart serve

    # check we have a couple of dependencies
    dependencies:
    @command -v fswatch --version >/dev/null 2>&1 || { printf >&2 "fswatch is not installed, please run: brew install fswatch\n"; exit 1; }
    @command -v go-bindata >/dev/null 2>&1 || { printf >&2 "go-bindata is not installed, please run: go get github.com/jteeuwen/go-bindata/...\n"; exit 1; }

    # default targets to run when only running `make`
    default: dependencies test

    # clean up
    clean:
    go clean

    # run formatting tool and build
    build: dependencies clean
    go-bindata $(BINDATA_PATHS)
    go fmt
    go build

    # run unit tests with code coverage
    test: dependencies
    go test -cover

    # generate code coverage report
    coverage: test
    go test -coverprofile=.coverage.out
    go tool cover -html=.coverage.out

    # attempt to kill running server
    kill:
    -@killall -9 $(PROG) 2>/dev/null || true

    # attempt to build and start server
    restart:
    @make kill
    @make build; (if [ "$$?" -eq 0 ]; then (./${PROG} &); fi)

    # watch .go files for changes then recompile & try to start server
    # will also kill server after ctrl+c
    serve: dependencies
    @make restart
    @fswatch -o --exclude=bindata.go ./*.go tmpl/* | xargs -n1 -I{} make restart || make kill