/
docs.go
135 lines (125 loc) · 4.04 KB
/
docs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package generator
import (
"os"
"path/filepath"
"github.com/jenkins-x/jx/cmd/codegen/util"
jxutil "github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
const (
genAPIDocsRepo = "github.com/kubernetes-incubator/reference-docs"
genAPIDocsBin = genAPIDocsRepo + "/gen-apidocs"
)
// InstallGenAPIDocs installs the gen-apidocs tool from the kubernetes-incubator/reference-docs repository.
// Returns the base directory of reference-docs within the GOPATH.
func InstallGenAPIDocs() (string, error) {
util.AppLogger().Infof("installing %s via 'go get'", genAPIDocsBin)
err := util.GoGet(genAPIDocsBin, "", false)
if err != nil {
return "", err
}
return filepath.Join(util.GoPathSrc(), genAPIDocsRepo), nil
}
// GenerateAPIDocs runs the apidocs-gen tool against configDirectory which includes the openapi-spec dir,
// the config.yaml file, static content and the static_includes
func GenerateAPIDocs(configDir string) error {
includesDir := filepath.Join(configDir, "includes")
err := jxutil.DeleteDirContents(includesDir)
if err != nil {
return errors.Wrapf(err, "deleting contents of %s", includesDir)
}
buildDir := filepath.Join(configDir, "build")
err = jxutil.DeleteDirContents(buildDir)
if err != nil {
return errors.Wrapf(err, "deleting contents of %s", buildDir)
}
if err != nil {
return errors.Wrapf(err, "getting codegen dir")
}
cmd := jxutil.Command{
Dir: configDir,
Name: "gen-apidocs",
Args: []string{
"--config-dir",
configDir,
"--munge-groups",
"false",
},
}
out, err := cmd.RunWithoutRetry()
if err != nil {
return errors.Wrapf(err, "running %s, output %s", cmd.String(), out)
}
util.AppLogger().Debugf("running %s\n", cmd.String())
util.AppLogger().Debug(out)
return nil
}
// AssembleAPIDocsStatic copies the static files from the referenceDocsRepo to the outputDir.
// It also downloads from CDN jquery and bootstrap js
func AssembleAPIDocsStatic(referenceDocsRepo string, outputDir string) error {
srcDir := filepath.Join(referenceDocsRepo, "gen-apidocs", "generators", "static")
outDir := filepath.Join(outputDir, "static")
util.AppLogger().Infof("copying static files from %s to %s\n", srcDir, outDir)
err := jxutil.CopyDirPreserve(srcDir, outDir)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", srcDir, outDir)
}
err = jxutil.DownloadFile(filepath.Join(outDir, bootstrapJsFileName), bootstrapJsUrl)
if err != nil {
return err
}
err = jxutil.DownloadFile(filepath.Join(outDir, jqueryFileName), jqueryUrl)
if err != nil {
return err
}
return nil
}
// AssembleAPIDocs copies the generated html files and the static files from srcDir into outputDir
func AssembleAPIDocs(srcDir string, outputDir string) error {
// Clean the dir
err := jxutil.DeleteDirContents(outputDir)
if err != nil {
return errors.Wrapf(err, "deleting contents of %s", outputDir)
}
// Copy the fonts over
err = copyStaticFiles(filepath.Join(srcDir, "static"), filepath.Join(outputDir, "fonts"), fonts)
if err != nil {
return err
}
// Copy the css over
err = copyStaticFiles(filepath.Join(srcDir, "static"), filepath.Join(outputDir, "css"), css)
if err != nil {
return err
}
// Copy the static jsroot over
err = copyStaticFiles(filepath.Join(srcDir, "static"), filepath.Join(outputDir, ""), jsroot)
if err != nil {
return err
}
// Copy the static js over
err = copyStaticFiles(filepath.Join(srcDir, "static"), filepath.Join(outputDir, "js"), js)
if err != nil {
return err
}
// Copy the generated files over
err = copyStaticFiles(filepath.Join(srcDir, "build"), filepath.Join(outputDir, ""), build)
if err != nil {
return err
}
return nil
}
func copyStaticFiles(srcDir string, outputDir string, resources []string) error {
err := os.MkdirAll(outputDir, 0700)
if err != nil {
return errors.Wrapf(err, "making %s", outputDir)
}
for _, resource := range resources {
srcPath := filepath.Join(srcDir, resource)
dstPath := filepath.Join(outputDir, resource)
err := jxutil.CopyFile(srcPath, dstPath)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", srcPath, dstPath)
}
}
return nil
}