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