/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. function _error() {
  13. echo "!!! $*"
  14. }
  15. function _basename() {
  16. cur_pwd=`pwd`
  17. echo $(basename "$cur_pwd")
  18. }
  19. function deactivate() {
  20. if [ -n "$_PREV_PS1" ]; then
  21. export PS1="$_PREV_PS1"
  22. unset _PREV_PS1
  23. fi
  24. if [ -n "$_PREV_PATH" ]; then
  25. export PATH="$_PREV_PATH"
  26. unset _PREV_PATH
  27. fi
  28. if [ -n "$BASH" ]; then
  29. hash -r
  30. fi
  31. unset GOOS
  32. unset GOARCH
  33. unset GOROOT
  34. ## Deactivate functions
  35. unset _error
  36. unset _basename
  37. unset _makefile
  38. unset _cmdmakefile
  39. unset _pkgmakefile
  40. unset _projmakefile
  41. unset _mainfile
  42. unset startproject
  43. unset addsubpkg
  44. unset goclean
  45. unset goremake
  46. unset deactivate
  47. }
  48. function _makefile() {
  49. targ="$1"
  50. file="$2"
  51. if [ ! "$targ" ]; then
  52. targ=$(_basename)
  53. fi
  54. if [ ! "$file" ]; then
  55. file="Makefile"
  56. fi
  57. cat > $file <<EOF
  58. include \$(GOROOT)/src/Make.inc
  59. TARG=$targ
  60. EOF
  61. }
  62. function _cmdmakefile() {
  63. targ="$1"
  64. file="$2"
  65. if [ ! "$targ" ]; then
  66. targ=$(_basename)
  67. fi
  68. if [ ! "$file" ]; then
  69. file="Makefile"
  70. fi
  71. _makefile "$targ" "$file"
  72. echo "include \$(GOROOT)/src/Make.cmd" >> "$file"
  73. cat >> "$file" <<EOF
  74. GOFILES=\\
  75. main.go \\
  76. include \$(GOROOT)/src/Make.cmd
  77. EOF
  78. }
  79. function _pkgmakefile() {
  80. targ="$1"
  81. file="$2"
  82. if [ ! "$targ" ]; then
  83. targ=$(_basename)
  84. fi
  85. if [ ! "$file" ]; then
  86. file="Makefile"
  87. fi
  88. _makefile "$targ" "$file"
  89. cat >> "$file" <<EOF
  90. GOFILES=\\
  91. $targ.go \\
  92. include \$(GOROOT)/src/Make.pkg
  93. EOF
  94. }
  95. function _projmakefile() {
  96. targ="$1"
  97. pkg="$2"
  98. if [ ! "$targ" ]; then
  99. _error "Project makefile requires a target name"
  100. elif [ ! "$pkg" ]; then
  101. _error "Project makefile requires a package"
  102. else
  103. cat > Makefile <<EOF
  104. TARG=$targ
  105. all:
  106. \$(MAKE) -C $pkg
  107. \$(MAKE) Makefile.\$(TARG)
  108. clean:
  109. \$(MAKE) -C $pkg clean
  110. \$(MAKE) Makefile.\$(TARG) clean
  111. EOF
  112. fi
  113. }
  114. function _mainfile() {
  115. cat > main.go <<EOF
  116. package main
  117. import (
  118. )
  119. func main() {
  120. }
  121. EOF
  122. }
  123. function startproject() {
  124. name="$1"
  125. cur_dir=`pwd`
  126. if [ ! "$name" ]; then
  127. _error "Project name required to start a project"
  128. elif [ -d "$name" ]; then
  129. _error "Project with that name already exists"
  130. else
  131. mkdir -p "$name"
  132. cd "$name"
  133. _cmdmakefile "$name"
  134. cd "$cur_dir"
  135. echo "Project successfully created"
  136. fi
  137. }
  138. function addsubpkg() {
  139. name="$1"
  140. project=$(_basename)
  141. if [ ! "$name" ]; then
  142. _error "Adding a sub pkg requires a name for the pkg"
  143. elif [ ! "$project" ]; then
  144. _error "Some how we can't determine the project name"
  145. elif [ -d "$name" ]; then
  146. _error "Pkg with that name already exists"
  147. elif [ ! -e "Makefile.$project" ]; then
  148. if [ ! -e "Makefile" ]; then
  149. _error "Invalid project"
  150. fi
  151. mv "Makefile" "Makefile.$project"
  152. _projmakefile "$project" "$name"
  153. fi
  154. cur_pwd=`pwd`
  155. mkdir "$name"
  156. cd "$name"
  157. _pkgmakefile "$name"
  158. cd "$cur_pwd"
  159. echo "Sub pkg added successfully"
  160. }
  161. function goclean() {
  162. gomake clean
  163. }
  164. function goremake() {
  165. goclean && gomake
  166. }
  167. if [ "$1" ]; then
  168. GOROOT="$1"
  169. elif [ -d /usr/local/go ]; then
  170. GOROOT=/usr/local/go
  171. elif [ -d ~/go ]; then
  172. GOROOT=~/go
  173. fi
  174. export GOROOT
  175. export _PREV_PATH="$PATH"
  176. export _PREV_PS1="$PS1"
  177. export PATH="$GOROOT/bin:$PATH"
  178. export PS1="[go]$PS1"
  179. # Pull the env settings from Make.inc
  180. eval $($GOROOT/bin/gomake -f $GOROOT/src/Make.inc go-env)