Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Apr 29, 2022
1 parent 8a5d87f commit 783f865
Showing 1 changed file with 137 additions and 12 deletions.
149 changes: 137 additions & 12 deletions build/generate-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,107 @@
package main

import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"

"github.com/hashicorp/go-version"
)

func dlThemeFile(dir string) error {
resp, err := http.Get("https://dl.gitea.io/theme/master.tar.gz")
if err != nil {
return err
}
defer resp.Body.Close()
themePath := filepath.Join(dir, "theme.tar.gz")
f, err := os.Create(themePath)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(f, resp.Body); err != nil {
return err
}
f.Close()

f2, err := os.Open(themePath)
if err != nil {
return err
}
defer f2.Close()

dstPath := filepath.Join(dir, "gitea")

// gzip read
gr, err := gzip.NewReader(f2)
if err != nil {
return err
}
defer gr.Close()

// tar read
tr := tar.NewReader(gr)

// 读取文件
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if h.FileInfo().IsDir() {
continue
}
if err := os.MkdirAll(filepath.Join(dstPath, path.Dir(h.Name)), os.ModePerm); err != nil {
return err
}

// 打开文件
fw, err := os.Create(filepath.Join(dstPath, h.Name))
if err != nil {
return err
}
defer fw.Close()

// 写文件
_, err = io.Copy(fw, tr)
if err != nil {
return err
}
fw.Close()
}
return nil
}

func main() {
curDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

fmt.Println("Current Directory is", curDir)
fmt.Println("Current directory is", curDir)
tmpDir := os.TempDir()
distDir, err := os.MkdirTemp(tmpDir, "gitea-docs")
distDir, err := os.MkdirTemp(tmpDir, "gitea-docs-dist")
if err != nil {
log.Fatal(err)
}
fmt.Println("Dist directory is", distDir)

fmt.Println("Fetch origin branches")
fmt.Println("Fetching origin branches")
cmd := exec.Command("git", "fetch", "origin")
cmd.Dir = curDir
if err := cmd.Run(); err != nil {
Expand All @@ -45,14 +122,24 @@ func main() {
log.Fatal(err)
}
fmt.Printf("Found %d branches\n", len(releaseBranches))
workspaceDir, err := os.MkdirTemp(tmpDir, "gitea-docs-workspace")
if err != nil {
log.Fatal(err)
}

if err = dlThemeFile(workspaceDir); err != nil {
log.Fatal(err)
}

for _, releaseVersion := range releaseBranches {
if err := genearteOneVersion(curDir, distDir, releaseVersion); err != nil {
if err := genearteOneVersion(workspaceDir, curDir, distDir, releaseVersion); err != nil {
log.Fatal(err)
}
}
fmt.Println("----", distDir)
}

var minDocVersion, _ = version.NewVersion("v1.16")

func fetchAllReleasesBranches(curDir string) ([]string, error) {
var output bytes.Buffer
var stderr strings.Builder
Expand All @@ -68,26 +155,64 @@ func fetchAllReleasesBranches(curDir string) ([]string, error) {

for scanner.Scan() {
branch := strings.TrimPrefix(strings.TrimSpace(scanner.Text()), "origin/release/")
ver, _ := version.NewVersion(branch)
if ver.LessThan(minDocVersion) {
continue
}

branches = append(branches, branch)
}

sort.Slice(branches, func(i, j int) bool {
a, _ := version.NewVersion(branches[i])
b, _ := version.NewVersion(branches[j])
return a.LessThan(b)
})

return branches, nil
}

func genearteOneVersion(curDir, distDir, releaseVersion string) error {
fmt.Println("Genera branch", releaseVersion)
func genearteOneVersion(workspaceDir, gitDir, distDir, releaseVersion string) error {
curVerDir := filepath.Join(workspaceDir, releaseVersion)
fmt.Printf("Genera branch %s in %s\n", releaseVersion, curVerDir)

distSubDir := filepath.Join(distDir, releaseVersion)

var stderr strings.Builder
cmd := exec.Command("git", "switch", "release/"+releaseVersion)
cmd.Dir = curDir
cmd := exec.Command("git", "clone", "-b", "release/"+releaseVersion, gitDir, releaseVersion)
cmd.Dir = workspaceDir
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v - %v", err, stderr.String())
}

stderr.Reset()
// hugo $(PUBLIC)
cmd = exec.Command("hugo", "--cleanDestinationDir", "--destination="+distSubDir)
cmd.Dir = curDir
return cmd.Run()
cmd = exec.Command("make", `clean`, "trans-copy")
cmd.Dir = filepath.Join(curVerDir, "docs")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v - %v", err, stderr.String())
}

if err := os.MkdirAll(filepath.Join(curVerDir, "docs", "themes", "gitea"), os.ModePerm); err != nil {
return err
}

stderr.Reset()
cmd = exec.Command("cp", `-r`, filepath.Join(workspaceDir, "gitea"), filepath.Join(curVerDir, "docs", "themes", "gitea"))
cmd.Dir = filepath.Join(curVerDir, "docs")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v - %v", err, stderr.String())
}

stderr.Reset()
cmd = exec.Command("hugo", `--baseURL="/"`, "--cleanDestinationDir", "--destination="+distSubDir)
cmd.Dir = filepath.Join(curVerDir, "docs")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v - %v", err, stderr.String())
}
return nil
}

0 comments on commit 783f865

Please sign in to comment.