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