/
docs.go
147 lines (135 loc) · 4.6 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
136
137
138
139
140
141
142
143
144
145
146
147
package generator
import (
"os"
"path/filepath"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx-api/cmd/codegen/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.
func InstallGenAPIDocs(version string, gopath string) error {
log.Logger().Infof("installing %s in version %s via 'go get'", genAPIDocsRepo, version)
err := util.GoGet(genAPIDocsRepo, version, gopath, true, false, true)
if err != nil {
return err
}
log.Logger().Infof("installing %s in version %s via 'go get'", genAPIDocsBin, version)
err = util.GoGet(genAPIDocsBin, version, gopath, true, false, true)
if err != nil {
return err
}
return nil
}
// DetermineSourceLocation determines the source location for the installed kubernetes-incubator/reference-docs/
// The location is based on GOPATH/pkd/mod and the current version.
func DetermineSourceLocation(moduleDir string, gopath string) (string, error) {
moduleDir, err := util.GetModuleDir(moduleDir, genAPIDocsRepo, gopath)
if err != nil {
return "", errors.Wrapf(err, "Unable to determine source directory for %s", genAPIDocsRepo)
}
return moduleDir, 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, gopath string) error {
includesDir := filepath.Join(configDir, "includes")
err := util.DeleteDirContents(includesDir)
if err != nil {
return errors.Wrapf(err, "deleting contents of %s", includesDir)
}
buildDir := filepath.Join(configDir, "build")
err = util.DeleteDirContents(buildDir)
if err != nil {
return errors.Wrapf(err, "deleting contents of %s", buildDir)
}
cmd := util.Command{
Dir: configDir,
Name: filepath.Join(util.GoPathBin(gopath), "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)
}
log.Logger().Debugf("running %s\n", cmd.String())
log.Logger().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")
log.Logger().Infof("copying static files from %s to %s\n", srcDir, outDir)
err := util.CopyDirPreserve(srcDir, outDir)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", srcDir, outDir)
}
err = util.DownloadFile(filepath.Join(outDir, bootstrapJsFileName), bootstrapJsUrl)
if err != nil {
return err
}
err = util.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 := util.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 := util.CopyFile(srcPath, dstPath)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", srcPath, dstPath)
}
}
return nil
}