8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / project / files.go
2.9 KB
1package project 2 3import ( 4 "path" 5 "regexp" 6 "sort" 7 "strconv" 8 "strings" 9 10 "xonecas.io/site/internal/repo" 11) 12 13var languages = map[string]string{ 14 ".js": "JavaScript", ".mjs": "JavaScript", ".cjs": "JavaScript", ".jsx": "JavaScript", ".ts": "TypeScript", ".tsx": "TypeScript", 15 ".go": "Go", ".py": "Python", ".rb": "Ruby", ".java": "Java", ".c": "C", ".h": "C", ".cpp": "C++", ".cc": "C++", ".hpp": "C++", 16 ".rs": "Rust", ".zig": "Zig", ".lua": "Lua", ".vim": "Vim script", ".sh": "Shell", ".bash": "Shell", ".zsh": "Shell", 17 ".html": "HTML", ".htm": "HTML", ".css": "CSS", ".scss": "SCSS", ".less": "Less", ".coffee": "CoffeeScript", ".php": "PHP", 18 ".swift": "Swift", ".kt": "Kotlin", ".cs": "C#", ".ex": "Elixir", ".svelte": "Svelte", ".vue": "Vue", ".elm": "Elm", 19} 20 21var ( 22 vendored = regexp.MustCompile(`(^|/)(node_modules|vendor|dist|build|third_party|bower_components)/|\.min\.(js|css)$`) 23 readme = regexp.MustCompile(`(?i)^readme(\.[a-z]+)?$`) 24 front = regexp.MustCompile(`\.(html?|css|scss|sass|less|jsx|tsx|vue|svelte|elm)$`) 25 back = regexp.MustCompile(`(?i)(^|/)(dockerfile|docker-compose\.ya?ml|compose\.ya?ml|procfile|manage\.py|wsgi\.py|server\.(js|ts|go|py)|app\.py|.*\.sql|go\.mod|cargo\.toml)$`) 26) 27 28// Files is what the default branch's tree says about a project. 29type Files struct { 30 Count int 31 Readme string // root README path, "" if none 32 Languages []Language // by bytes, largest first, 5% and up 33 Front bool // has browser files 34 Back bool // has server files 35 Topics []string // see topics.go 36} 37 38// Language is one language's share of a project's code. 39type Language struct { 40 Name string `json:"name"` 41 Percent int `json:"percent"` 42} 43 44// ReadFiles lists the tree of the source's default branch. An empty repo has no files. 45func ReadFiles(s Source) (Files, error) { 46 var f Files 47 if s.Commits == 0 { 48 return f, nil 49 } 50 out, err := repo.Git(s.Dir, "ls-tree", "-r", "-l", "HEAD") 51 if err != nil || out == "" { 52 return f, err 53 } 54 bytes, total, all, kept := map[string]int{}, 0, []string{}, []string{} 55 for _, line := range strings.Split(out, "\n") { 56 meta, name, _ := strings.Cut(line, "\t") 57 f.Count++ 58 all = append(all, name) 59 if !strings.Contains(name, "/") && readme.MatchString(name) && f.Readme == "" { 60 f.Readme = name 61 } 62 if vendored.MatchString(name) { 63 continue 64 } 65 kept = append(kept, name) 66 f.Front = f.Front || front.MatchString(name) 67 f.Back = f.Back || back.MatchString(name) 68 if lang, ok := languages[strings.ToLower(path.Ext(name))]; ok { 69 fields := strings.Fields(meta) 70 n, _ := strconv.Atoi(fields[len(fields)-1]) 71 bytes[lang] += n 72 total += n 73 } 74 } 75 for name, n := range bytes { 76 if p := n * 100 / total; p >= 5 { 77 f.Languages = append(f.Languages, Language{name, p}) 78 } 79 } 80 f.Topics = findTopics(s.Dir, all, kept, f.Readme) 81 sort.Slice(f.Languages, func(a, b int) bool { return f.Languages[a].Percent > f.Languages[b].Percent }) 82 return f, nil 83}