PageRenderTime 144ms CodeModel.GetById 40ms app.highlight 51ms RepoModel.GetById 50ms app.codeStats 0ms

/src/igo/igo.go

https://code.google.com/
Go | 286 lines | 257 code | 26 blank | 3 comment | 75 complexity | 416c7724db710cf01194971d5826849c MD5 | raw file
  1package main
  2
  3import (
  4	"os"
  5	"fmt"
  6	"strings"
  7	"io/ioutil"
  8	"os/exec"
  9	"flag"
 10	"go/build"
 11	"path/filepath"
 12)
 13
 14func ChkErr(err error) bool{
 15	if err != nil {
 16		fmt.Println("Error: ", err.Error())
 17		os.Exit(1)
 18	}
 19	return true
 20}
 21
 22func igoStrSpaceSplit(str string) []string {
 23	var ret []string
 24	var bStart, bSeparator bool
 25
 26	str = strings.TrimSpace(str)
 27	length := len(str)
 28	bStart = true
 29	for i, j:=0, 0; i<length; i++ {
 30		if (i == 0 && string(str[i]) == "'") {
 31			bSeparator = true
 32			j = 1
 33		}
 34		if !bStart {
 35			if string(str[i]) == " " {
 36				bStart = true
 37				j = i + 1
 38				if string(str[i + 1]) == "'" {
 39					bSeparator = true
 40					i ++
 41					j = i + 1
 42				}
 43			}
 44		} else {
 45			//bStart is true
 46			if bSeparator {
 47				if string(str[i]) == "'" {
 48					if (i + 1 < length && string(str[i + 1]) == " ") || (i + 1 >= length) {
 49						bSeparator = false
 50						bStart = false
 51						s := strings.TrimSpace(str[j:i])
 52						if len(s) > 0 {
 53							ret = append(ret, s)
 54						}
 55						j = i + 1
 56					}
 57				}
 58			} else {
 59				if string(str[i]) == " " {
 60					s := strings.TrimSpace(str[j:i])
 61					if len(s) > 0 {
 62						ret = append(ret, s)
 63					}
 64					j = i + 1
 65					if string(str[i + 1]) == "'" {
 66						bSeparator = true
 67						i ++
 68						j = i + 1
 69					}
 70				} else if i == length - 1 {
 71					s := strings.TrimSpace(str[j:i + 1])
 72					if len(s) > 0 {
 73						ret = append(ret, s)
 74					}
 75				}
 76			}
 77		}
 78	}
 79	return ret
 80}
 81
 82func igoListPackages(path string) []string{
 83	var ret []string
 84	s := filepath.Join(path, "src")
 85	fis, err := ioutil.ReadDir(s)
 86	if err != nil {
 87//		fmt.Println("Testing: ", err.Error())
 88		return ret
 89	}
 90	for j:=0; j<len(fis); j++ {
 91		if fis[j].IsDir() {
 92			_, pkgerr := build.ScanDir(filepath.Join(s, fis[j].Name()))
 93			if pkgerr == nil {
 94				ret = append(ret, fis[j].Name())
 95			}
 96		}
 97	}
 98	return ret
 99}
100
101const (
102	CCONFIGFILE = "gopath.cfg"
103)
104
105type TGopath struct {
106	curDir string
107	cfgFile string
108	gopath []string
109	pkgs map[string][]string
110}
111
112func (me *TGopath) Create() {
113	var err error
114	me.curDir, err = os.Getwd()
115	me.pkgs = make(map[string][]string)
116	me.cfgFile, _ = filepath.Abs(CCONFIGFILE)
117	ChkErr(err)
118}
119
120func (me *TGopath) Pwd() string {
121	return me.curDir
122}
123
124func (me *TGopath) GetGopath() []string {
125	return me.gopath
126}
127func (me *TGopath) AddPath(newPath string) {
128	if len(newPath) == 0 {
129		return
130	}
131	switch string(newPath[0]) {
132	case "/":
133		me.gopath = append(me.gopath, newPath)
134	case "~":
135		home := os.Getenv("HOME")
136		me.gopath = append(me.gopath, filepath.Join(home, strings.TrimLeft(newPath, "~")))
137	default:
138		me.gopath = append(me.gopath, strings.TrimRight(me.curDir, "/") + "/" + newPath)
139	}
140
141}
142
143func (me *TGopath) ReadGopathFromConfigFile() bool {
144	byteFileContent, err := ioutil.ReadFile(me.cfgFile)
145	if err != nil {
146		return false
147	}
148	strFileContent := string(byteFileContent)
149	gp := strings.Split(strFileContent, "\n")
150
151	for i:=0; i<len(gp); i++ {
152		s := strings.TrimRight(gp[i], "\r")
153		if len(s) > 0 {
154			me.AddPath(s)
155		}
156	}
157	return true
158}
159
160func (me *TGopath) BuildPackageList() {
161	for i:=0; i<len(me.gopath); i++ {
162		me.pkgs[me.gopath[i]] = igoListPackages(me.gopath[i])
163	}
164}
165
166func (me *TGopath) GetPackageListFromPath(path string) []string{
167	return me.pkgs[path]
168}
169
170func (me *TGopath) AutoBuildPath() {
171	fis, err := ioutil.ReadDir(me.curDir)
172	ChkErr(err)
173	for i:=0; i<len(fis); i++ {
174		if fis[i].IsDir() {
175			me.gopath = append(me.gopath, filepath.Join(me.curDir, fis[i].Name()))
176		}
177	}
178}
179
180type TIgo struct {
181	gopath TGopath
182	originGopath string
183	bManualPath, bManualPkg bool
184	mPkgs []string
185//	cmd exec.Cmd
186	Gotool string
187	Args []string
188}
189
190func (me *TIgo) Create() {
191	me.gopath.Create()
192	me.Gotool = os.Getenv("GOBIN")
193	if len(me.Gotool) == 0 {
194		me.Gotool = os.Getenv("GOROOT")
195		if len(me.Gotool) == 0 {
196			os.Exit(2)
197		}
198		me.Gotool = strings.TrimRight(me.Gotool, "/") + "/bin/go"
199	} else {
200		me.Gotool = strings.TrimRight(me.Gotool, "/") + "/go"
201	}
202	me.originGopath = os.Getenv("GOPATH")
203}
204
205func (me *TIgo) HandleArgs() {
206	var manualGoPath, manualPkgs string
207	var workDir, cfgFile string
208	flag.StringVar(&manualGoPath, "p", "", "assign gopath manually")
209	flag.StringVar(&manualPkgs, "m", "", "point out which package or which path to be build")
210	flag.StringVar(&workDir, "d", "", "set working dir")
211	flag.StringVar(&cfgFile, "f", "", "set gopath config file")
212	flag.Parse()
213	if flag.NArg() == 0 {
214		me.Args = append(me.Args, "install")
215	} else {
216		me.Args = append(me.Args, flag.Args()...)
217	}
218
219	if len(cfgFile) > 0 {
220		abs, err := filepath.Abs(cfgFile)
221		if err == nil {
222			me.gopath.cfgFile = abs
223			me.gopath.curDir = filepath.Dir(abs)
224		}
225	} else if len(workDir) >0 {
226		abs, err := filepath.Abs(workDir)
227		if err == nil {
228			me.gopath.curDir = abs
229		}
230	}
231	var slManualGoPath, slManualPkgs []string
232	if len(manualGoPath) > 0 {
233		me.bManualPath = true
234		slManualGoPath = igoStrSpaceSplit(manualGoPath)
235		for i:=0; i<len(slManualGoPath); i++ {
236			me.gopath.AddPath(slManualGoPath[i])
237		}
238	} else if !me.gopath.ReadGopathFromConfigFile() {
239		me.gopath.AutoBuildPath()
240	}
241
242	me.gopath.BuildPackageList()
243
244	if len(manualPkgs) > 0 {
245		me.bManualPkg = true
246		slManualPkgs = igoStrSpaceSplit(manualPkgs)
247		for i:=0; i<len(slManualPkgs); i++ {
248			givenPath := strings.TrimRight(me.gopath.Pwd(), "/") + "/" + slManualPkgs[i]
249			pkgs := me.gopath.GetPackageListFromPath(givenPath)
250			me.mPkgs = append(me.mPkgs, pkgs...)
251		}
252		for _, v := range me.gopath.pkgs {
253			for i:=0; i<len(v); i++ {
254				for j:=0; j<len(slManualPkgs); j++ {
255					if v[i] == slManualPkgs[j] {
256						me.mPkgs = append(me.mPkgs, v[i])
257					}
258				}
259			}
260		}
261	} else {
262		me.mPkgs = []string{"all"}
263	}
264
265	me.Args = append(me.Args, me.mPkgs...)
266	
267	gpl := me.gopath.GetGopath()
268	for _, s := range gpl {
269		me.originGopath = me.originGopath + ":" + s
270	}
271	me.originGopath = strings.TrimLeft(me.originGopath, ":")
272	os.Setenv("GOPATH", me.originGopath)
273}
274
275func main(){
276	var igo TIgo
277	igo.Create()
278	igo.HandleArgs()
279
280	fmt.Println("GOPATH is :\n\t", os.Getenv("GOPATH"))
281	cmd := exec.Command(igo.Gotool, igo.Args[:]...)
282	cmd.Stdout = os.Stdout
283	cmd.Stderr = os.Stderr
284	err := cmd.Run()
285	ChkErr(err)
286}