godo

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2014 License: MIT Imports: 20 Imported by: 0

README

godo

godoc

godo is a task runner and file watcher for golang in the spirit of rake, gulp.

To install

go get -u gopkg.in/godo.v1/cmd/godo

Godofile

As an example, create a file tasks/Godofile.go with this content

package main

import (
    . "gopkg.in/godo.v1"
)

func Tasks(p *Project) {
    Env = "GOPATH=.vendor:$GOPATH OTHER_VAR=val"

    p.Task("default", D{"hello", "build"})

    p.Task("hello", func() {
        Bash("echo Hello $USER!")
    })

    p.Task("build", W{"**/*.go"}, func() {
        Run("GOOS=linux GOARCH=amd64 go build", In{"cmd/server"})
    })

    p.Task("views", W{"templates/**/*.go.html"}, func() {
        Run("razor templates")
    })

    p.Task("server", D{"views"}, W{"**/*.go"}, Debounce(3000), func() {
        // Start recompiles and restarts on changes when watching
        Start("main.go", In{"cmd/server"})
    })
}

func main() {
    Godo(Tasks)
}

To run "server" task from parent dir of tasks/

godo server

To rerun "server" and its dependencies whenever any *.go.html or *.go file changes

godo server --watch

To run the "default" task which runs "hello" and "views"

godo

Task names may add a "?" suffix meaning only run once even when watching

// build once regardless of number of dependents
p.Task("build?", func() {})

Task options

D{} or Dependencies{} - dependent tasks which run before task
Debounce              - minimum milliseconds before task can run again
W{} or Watches{}      - array of glob file patterns to watch

    /**/   - match zero or more directories
    {a,b}  - match a or b, no spaces
    *      - match any non-separator char
    ?      - match a single non-separator char
    **/    - match any directory, start of pattern only
    /**    - match any this directory, end of pattern only
    !      - removes files from resultset, start of pattern only

Task handlers

func() {}           - Simple function handler
func(c *Context) {} - Handler which accepts the current context

Exec functions

Bash

Bash functions uses the bash executable and thus may not run on all OS.

Run a bash script string. The script can be multine line with continutation.

Bash(`
    echo -n $USER
    echo some really long \
        command
`)

Run a bash script string and capture its output.

output, err := BashOutput(`echo -n $USER`)
Run

Run go build inside of cmd/app and set environment variables. Notice environment variables are set the same way as in a shell.

Run(`GOOS=linux GOARCH=amd64 go build`)

Run and capture output

output, err := RunOutput("whoami")
Start

Godo tracks the pid of the Start() async function to restart an application gracefully.

Start an async command. If executable has suffix ".go" then it will be "go install"ed then executed. Use this for watching a server task.

Start("main.go", In{"cmd/app"})
Inside

If you need to run many commands in a directory, use Inside instead of the In options.

Inside("somedir", func() {
    Run("...")
    Bash("...")
})

Godofile run-time environment

Tasks often need to run in a known evironment.

To specify whether to inherit from parent's process environment, set InheritParentEnv. This setting defaults to true

InheritParentEnv = false

To specify the base environment for your tasks, set Env. Separate with whitespace or newlines.

Env = `
    GOPATH=.vendor:$GOPATH
    PG_USER="developer"
`

Functions can add or override environment variables as part of the command string. Note that environment variables are set similar to how you would set them in a shell; however, the Run and Start functions do not use a shell.

p.Task("build", func() {
    Run("GOOS=linux GOARCH=amd64 go build" )
})

The effective environment for Run or Start is: parent (if inherited) <- Env <- func overrides

The effective environment for Bash is: parent (if inherited) <- Env

Note: Interpolation of $VARIABLE is always from parent environment even if InheritParentEnv is false.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DebounceMs int64

DebounceMs is the default time (1500 ms) to debounce task events in watch mode.

Env is the default environment to use for all commands. That is, the effective environment for all commands is the merged set of (parent environment, Env, func specified environment). Whitespace or newline separate key value pairs. $VAR interpolation is allowed.

Env = "GOOS=linux GOARCH=amd64" Env = `

GOOS=linux
GOPATH=./vendor:$GOPATH

`

View Source
var InheritParentEnv bool

InheritParentEnv whether to inherit parent's environment

View Source
var Version = "1.1.0"

Version is the current version

Functions

func Bash added in v1.1.0

func Bash(script string, wd ...In) error

Bash executes a bash script (string) with an option to set the working directory.

func BashOutput added in v1.1.0

func BashOutput(script string, wd ...In) (string, error)

BashOutput is the same as Bash and it captures stdout and stderr.

func Glob

func Glob(patterns []string) ([]*FileAsset, []*RegexpInfo, error)

Glob returns files and dirctories that match patterns. Patterns must use slashes, even Windows.

Special chars.

/**/   - match zero or more directories
{a,b}  - match a or b, no spaces
*      - match any non-separator char
?      - match a single non-separator char
**/    - match any directory, start of pattern only
/**    - match any this directory, end of pattern only
!      - removes files from resultset, start of pattern only

func Globexp

func Globexp(glob string) *regexp.Regexp

Globexp builds a regular express from from extended glob pattern and then returns a Regexp object from the pattern.

func Godo added in v1.1.0

func Godo(tasksFunc func(*Project))

Godo runs a project of tasks.

func Inside added in v1.1.0

func Inside(dir string, lambda func()) error

Inside temporarily changes the working directory and restores it when lambda finishes.

func Run

func Run(commandstr string, wd ...In) error

Run runs a command with an an option to set the working directory.

func RunOutput added in v1.1.0

func RunOutput(commandstr string, wd ...In) (string, error)

RunOutput is same as Run and it captures stdout and stderr.

func Start

func Start(commandstr string, wd ...In) error

Start starts an async command. If executable has suffix ".go" then it will be "go install"ed then executed. Use this for watching a server task.

If Start is called with the same command it kills the previous process.

The working directory is optional.

Types

type Context

type Context struct {
	// Task is the currently running task.
	Task *Task

	// FileEvent is an event from the watcher with change details.
	FileEvent *watcher.FileEvent
}

Context is the data passed to a task.

func (*Context) AnyFile

func (context *Context) AnyFile() []string

AnyFile returns either a non-DELETe FileEvent file or the WatchGlob patterns which can be used by goa.Load()

type D

type D Dependencies

D is short for Dependencies option.

type Debounce

type Debounce int64

Debounce is the number of milliseconds before a task can run again.

type Dependencies

type Dependencies []string

Dependencies are tasks which must run before a task.

type FileAsset

type FileAsset struct {
	os.FileInfo
	// Path to asset
	Path string
}

FileAsset contains file information and path from globbing.

type In added in v1.1.0

type In []string

In is used by Bash, Run and Start to set the working directory

type M

type M map[string]interface{}

M is generic string to interface alias

type Project

type Project struct {
	sync.Mutex
	Tasks     map[string]*Task
	Namespace map[string]*Project
	// contains filtered or unexported fields
}

Project is a container for tasks.

func NewProject

func NewProject(tasksFunc func(*Project)) *Project

NewProject creates am empty project ready for tasks.

func (*Project) Define

func (project *Project) Define(fn func(*Project))

Define defines tasks

func (*Project) Run

func (project *Project) Run(name string)

Run runs a task by name.

func (*Project) Task

func (project *Project) Task(name string, args ...interface{}) *Task

Task adds a task to the project.

func (*Project) Usage

func (project *Project) Usage()

Usage prints usage about the app and tasks.

func (*Project) Use

func (project *Project) Use(namespace string, tasksFunc func(*Project))

Use uses another project's task within a namespace.

func (*Project) Watch

func (project *Project) Watch(names []string, isParent bool)

Watch watches the Files of a task and reruns the task on a watch event. Any direct dependency is also watched.

type RegexpInfo

type RegexpInfo struct {
	*regexp.Regexp
	Negate bool
}

RegexpInfo contains additional info about the Regexp created by a glob pattern.

type Task

type Task struct {
	Name           string
	Description    string
	Dependencies   []string
	Handler        func()
	ContextHandler func(*Context)

	// Watches are the files are watched. On change the task is rerun. For example `**/*.less`
	// Usually Watches and Sources are the same.
	WatchFiles   []*FileAsset
	WatchGlobs   []string
	WatchRegexps []*RegexpInfo

	// computed based on dependencies
	EffectiveWatchRegexps []*RegexpInfo
	EffectiveWatchGlobs   []string

	// Complete indicates whether this task has already ran. This flag is
	// ignored in watch mode.
	Complete bool
	Debounce int64
	RunOnce  bool
}

A Task is an operation performed on a user's project directory.

func (*Task) Run

func (task *Task) Run()

Run runs all the dependencies of this task and when they have completed, runs this task.

func (*Task) RunWithEvent

func (task *Task) RunWithEvent(logName string, e *watcher.FileEvent)

RunWithEvent runs this task when triggered from a watch. *e* FileEvent contains information about the file/directory which changed in watch mode.

type W

type W Watch

W is short for Watch option.

type Watch

type Watch []string

Watch are the glob file patterns which are watched and trigger rerunning a task on change.

Directories

Path Synopsis
cmd
Package util contains general purpose utility and logging functions.
Package util contains general purpose utility and logging functions.
Package watcher implements filesystem notification,.
Package watcher implements filesystem notification,.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL