Introduction
Introduction Statistics Contact Development Disclaimer Help
build.go - staticgit - A git static site generator in GO with HTML output!
git clone git://jay.scot/staticgit
Log
Files
Refs
README
---
build.go (1959B)
---
1 package site
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "sync"
8
9 "staticgit/internal/config"
10 "staticgit/internal/repo"
11 "staticgit/internal/template"
12 )
13
14 func Build(cfg *config.Config) error {
15 dirs, err := os.ReadDir(cfg.RepoDir)
16 if err != nil {
17 return fmt.Errorf("read repos dir: %w", err)
18 }
19
20 var wg sync.WaitGroup
21 repoChan := make(chan *repo.Repo, len(dirs))
22 errChan := make(chan error, len(dirs))
23
24 for _, d := range dirs {
25 if d.IsDir() && !contains(cfg.IgnoreDirs, d.Name()) {
26 wg.Add(1)
27 go func(d os.DirEntry) {
28 defer wg.Done()
29 path := filepath.Join(cfg.RepoDir, d.Nam…
30 r, err := repo.OpenRepo(path)
31 if err != nil {
32 errChan <- fmt.Errorf("open repo…
33 return
34 }
35 repoChan <- r
36
37 out := filepath.Join(cfg.OutDir, d.Name(…
38 if err := os.MkdirAll(out, 0755); err !=…
39 errChan <- fmt.Errorf("create di…
40 return
41 }
42
43 if err := generateRepoPage(r, out, cfg.M…
44 errChan <- fmt.Errorf("process %…
45 }
46 }(d)
47 }
48 }
49
50 go func() {
51 wg.Wait()
52 close(repoChan)
53 close(errChan)
54 }()
55
56 var repos []*repo.Repo
57 for r := range repoChan {
58 repos = append(repos, r)
59 }
60
61 for err := range errChan {
62 fmt.Println(err)
63 }
64
65 return template.GenerateIndex(cfg.OutDir, repos)
66 }
67
68 func generateRepoPage(r *repo.Repo, out string, maxCommits int) error {
69 readme, err := r.GetReadme()
70 if err != nil {
71 return fmt.Errorf("get README: %w", err)
72 }
73
74 commits, err := r.GetCommits(maxCommits)
75 if err != nil {
76 return fmt.Errorf("get commits: %w", err)
77 }
78
79 files, err := r.GetFiles()
80 if err != nil {
81 return fmt.Errorf("get files: %w", err)
82 }
83
84 outPath := filepath.Join(out, "index.html")
85
86 return template.GenerateRepoPage(r.Name, readme, commits, files,…
87 }
88
89 func contains(slice []string, item string) bool {
90 for _, a := range slice {
91 if a == item {
92 return true
93 }
94 }
95 return false
96 }
You are viewing proxied material from jay.scot. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.