Ejemplo n.º 1
0
// addWatch 使用fsnotify,监听src目录以及子目录
func addWatch(watcher *fsnotify.Watcher, dir string) {
	watcher.Watch(dir)
	for _, filename := range files.ScanDir(dir) {
		childDir := filepath.Join(dir, filename)
		if files.IsDir(childDir) {
			addWatch(watcher, childDir)
		}
	}
}
Ejemplo n.º 2
0
// New 要求被监听项目必须有src目录(按Go习惯建目录)
func New(name, root, goWay, mainFile string, deamon bool, depends ...string) (*Project, error) {
	if !files.IsDir(root) {
		return nil, PrjRootErr
	}
	if filepath.IsAbs(mainFile) {
		return nil, errors.New("main配置项必须是相对于项目src的相对路径!")
	}
	root, err := filepath.Abs(root)
	if err != nil {
		return nil, err
	}
	binAbsolutePath := filepath.Join(root, "bin")

	options := ""
	switch goWay {
	case "run":
		if mainFile == "" {
			mainFile = name + ".go"
		}
		mainFile = filepath.Join("src", mainFile)
	case "build":
		if mainFile == "" {
			mainFile = name + ".go"
		}
		mainFile = filepath.Join("src", mainFile)
		if !files.Exist(binAbsolutePath) {
			if err = os.Mkdir(binAbsolutePath, 0777); err != nil {
				return nil, err
			}
		}
		output := filepath.Join("bin", name+binanryFileSuffix)
		options = "-o " + output
	case "install":
		fallthrough
	default:
		if mainFile == "" {
			mainFile = name
		} else {
			mainFile = filepath.Dir(mainFile)
		}
	}
	return &Project{
		name:            name,
		Root:            root,
		binAbsolutePath: binAbsolutePath,
		srcAbsolutePath: filepath.Join(root, "src"),
		errAbsolutePath: filepath.Join(root, "_log_"),
		GoWay:           goWay,
		deamon:          deamon,
		MainFile:        mainFile,
		Options:         options,
		Depends:         depends,
	}, nil
}