PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 7ms RepoModel.GetById 53ms app.codeStats 0ms

/goenv

http://goenv.googlecode.com/
#! | 211 lines | 180 code | 31 blank | 0 comment | 0 complexity | f3f4b3d3c196501d2257767cfac54451 MD5 | raw file
  1#!/bin/bash
  2#
  3#
  4#   goenv
  5#   Copyright (c)2011   Joshua Bell
  6#   All rights reserved.
  7#
  8#
  9#   This script is to be used in order to set up
 10#   one's environment for Golang development.
 11#
 12
 13
 14function _error() {
 15    echo "!!! $*"
 16}
 17
 18function _basename() {
 19    cur_pwd=`pwd`
 20    echo $(basename "$cur_pwd")
 21}
 22
 23function deactivate() {
 24    if [ -n "$_PREV_PS1" ]; then
 25        export PS1="$_PREV_PS1"
 26        unset _PREV_PS1
 27    fi
 28
 29    if [ -n "$_PREV_PATH" ]; then
 30        export PATH="$_PREV_PATH"
 31        unset _PREV_PATH
 32    fi
 33
 34    if [ -n "$BASH" ]; then
 35        hash -r
 36    fi
 37
 38    unset GOOS
 39    unset GOARCH
 40    unset GOROOT
 41
 42    ##  Deactivate functions
 43    unset _error
 44    unset _basename
 45    unset _makefile
 46    unset _cmdmakefile
 47    unset _pkgmakefile
 48    unset _projmakefile
 49    unset _mainfile
 50    unset startproject
 51    unset addsubpkg
 52    unset goclean
 53    unset goremake
 54    unset deactivate
 55}
 56
 57function _makefile() {
 58    targ="$1"
 59    file="$2"
 60    if [ ! "$targ" ]; then
 61        targ=$(_basename)
 62    fi
 63    if [ ! "$file" ]; then
 64        file="Makefile"
 65    fi
 66cat > $file <<EOF
 67include \$(GOROOT)/src/Make.inc
 68
 69TARG=$targ
 70EOF
 71}
 72
 73function _cmdmakefile() {
 74    targ="$1"
 75    file="$2"
 76    if [ ! "$targ" ]; then
 77        targ=$(_basename)
 78    fi
 79    if [ ! "$file" ]; then
 80        file="Makefile"
 81    fi
 82    _makefile "$targ" "$file"
 83    echo "include \$(GOROOT)/src/Make.cmd" >> "$file"
 84cat >> "$file" <<EOF
 85GOFILES=\\
 86        main.go \\
 87
 88include \$(GOROOT)/src/Make.cmd
 89EOF
 90}
 91
 92function _pkgmakefile() {
 93    targ="$1"
 94    file="$2"
 95    if [ ! "$targ" ]; then
 96        targ=$(_basename)
 97    fi
 98    if [ ! "$file" ]; then
 99        file="Makefile"
100    fi
101    _makefile "$targ" "$file"
102cat >> "$file" <<EOF
103GOFILES=\\
104        $targ.go \\
105
106include \$(GOROOT)/src/Make.pkg
107EOF
108}
109
110function _projmakefile() {
111    targ="$1"
112    pkg="$2"
113    if [ ! "$targ" ]; then
114        _error "Project makefile requires a target name"
115    elif [ ! "$pkg" ]; then
116        _error "Project makefile requires a package"
117    else
118
119cat > Makefile <<EOF
120TARG=$targ
121
122all:
123    \$(MAKE) -C $pkg
124    \$(MAKE) Makefile.\$(TARG)
125
126clean:
127    \$(MAKE) -C $pkg clean
128    \$(MAKE) Makefile.\$(TARG) clean
129EOF
130    fi
131}
132
133function _mainfile() {
134cat > main.go <<EOF
135package main
136
137import (
138)
139
140func main() {
141}
142EOF
143}
144
145function startproject() {
146    name="$1"
147    cur_dir=`pwd`
148    if [ ! "$name" ]; then
149        _error "Project name required to start a project"
150    elif [ -d "$name" ]; then
151        _error "Project with that name already exists"
152    else
153        mkdir -p "$name"
154        cd "$name"
155        _cmdmakefile "$name"
156        cd "$cur_dir"
157        echo "Project successfully created"
158    fi
159}
160
161function addsubpkg() {
162    name="$1"
163    project=$(_basename)
164    if [ ! "$name" ]; then
165        _error "Adding a sub pkg requires a name for the pkg"
166    elif [ ! "$project" ]; then
167        _error "Some how we can't determine the project name"
168    elif [ -d "$name" ]; then
169        _error "Pkg with that name already exists"
170    elif [ ! -e "Makefile.$project" ]; then
171        if [ ! -e "Makefile" ]; then
172            _error "Invalid project"
173        fi
174        mv "Makefile" "Makefile.$project"
175        _projmakefile "$project" "$name"
176    fi
177    cur_pwd=`pwd`
178    mkdir "$name"
179    cd "$name"
180    _pkgmakefile "$name"
181    cd "$cur_pwd"
182    echo "Sub pkg added successfully"
183}
184
185function goclean() {
186    gomake clean
187}
188
189function goremake() {
190    goclean && gomake
191}
192
193if [ "$1" ]; then
194    GOROOT="$1"
195elif [ -d /usr/local/go ]; then
196    GOROOT=/usr/local/go
197elif [ -d ~/go ]; then
198    GOROOT=~/go
199fi
200export GOROOT
201
202
203export _PREV_PATH="$PATH"
204export _PREV_PS1="$PS1"
205
206
207export PATH="$GOROOT/bin:$PATH"
208export PS1="[go]$PS1"
209
210#   Pull the env settings from Make.inc
211eval $($GOROOT/bin/gomake -f $GOROOT/src/Make.inc go-env)