// HasPkg checks if a package exists. func (h *DirHome) HasPkg(p string) bool { root := h.path fp := filepath.Join(root, p) info, err := os.Stat(fp) if err != nil { return false } if !info.IsDir() { return false } base := filepath.Base(p) if !lex8.IsPkgName(base) { return false } lang := h.Lang(p) files, err := listSrcFiles(p, lang) if err != nil { return false } if len(files) > 0 { h.fileList[p] = files // caching return true } return false }
func isPkgPath(p string) bool { if p == "" { return false } subs := strings.Split(p, "/") for _, sub := range subs { if !lex8.IsPkgName(sub) { return false } } return true }
// Pkgs lists all the packages inside this home folder. func (h *DirHome) Pkgs(prefix string) []string { root := filepath.Join(h.path, "src") start := filepath.Join(root, prefix) var pkgs []string walkFunc := func(p string, info os.FileInfo, e error) error { if e != nil { return e } if !info.IsDir() { return nil } else if !lex8.IsPkgName(info.Name()) { return filepath.SkipDir } if root == p { return nil } path, e := filepath.Rel(root, p) if e != nil { panic(e) } else if path == "." { return nil } lang := h.Lang(path) if lang == nil { panic(path) } files, e := listSrcFiles(p, lang) if e != nil { return e } if len(files) > 0 { h.fileList[path] = files // caching pkgs = append(pkgs, path) } return nil } e := filepath.Walk(start, walkFunc) if e != nil && !h.Quiet { log.Fatal("error", e) } sort.Strings(pkgs) return pkgs }
func isPkgPath(p string) bool { p = strings.TrimPrefix(p, "/") // might have a leading slash if p == "" { return false } subs := strings.Split(p, "/") for _, sub := range subs { if !lex8.IsPkgName(sub) { return false } } return true }
func parseSym(p lex8.Logger, t *lex8.Token) (pack, sym string) { if t.Type != parse.Operand { panic("symbol not an operand") } sym = t.Lit dot := strings.Index(sym, ".") if dot >= 0 { pack, sym = sym[:dot], sym[dot+1:] } if dot >= 0 && !lex8.IsPkgName(pack) { p.Errorf(t.Pos, "invalid package name: %q", pack) } else if !parse.IsIdent(sym) { p.Errorf(t.Pos, "invalid symbol: %q", t.Lit) } return pack, sym }