github.com/oam-dev/kubevela@v1.9.11/hack/docgen/def/mods/component.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mods 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 "strings" 24 "time" 25 26 "github.com/kubevela/pkg/util/singleton" 27 28 "github.com/oam-dev/kubevela/apis/types" 29 "github.com/oam-dev/kubevela/pkg/utils/common" 30 "github.com/oam-dev/kubevela/references/docgen" 31 ) 32 33 const ( 34 // ComponentDefRefPath is the target path for kubevela.io component ref docs 35 ComponentDefRefPath = "../kubevela.io/docs/end-user/components/references.md" 36 // ComponentDefRefPathZh is the target path for kubevela.io component ref docs in Chinese 37 ComponentDefRefPathZh = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/components/references.md" 38 ) 39 40 // ComponentDefDirs store inner CUE definition 41 var ComponentDefDirs = []string{"./vela-templates/definitions/internal/component/"} 42 43 // CustomComponentHeaderEN . 44 var CustomComponentHeaderEN = `--- 45 title: Built-in ParsedComponents Type 46 --- 47 48 This documentation will walk through all the built-in component types sorted alphabetically. 49 50 ` + fmt.Sprintf("> It was generated automatically by [scripts](../../contributor/cli-ref-doc), please don't update manually, last updated at %s.\n\n", time.Now().Format(time.RFC3339)) 51 52 // CustomComponentHeaderZH . 53 var CustomComponentHeaderZH = `--- 54 title: 内置组件列表 55 --- 56 57 本文档将**按字典序**展示所有内置组件的参数列表。 58 59 ` + fmt.Sprintf("> 本文档由[脚本](../../contributor/cli-ref-doc)自动生成,请勿手动修改,上次更新于 %s。\n\n", time.Now().Format(time.RFC3339)) 60 61 // ComponentDef generate component def reference doc 62 func ComponentDef(ctx context.Context, c common.Args, opt Options) { 63 if len(opt.DefDirs) == 0 { 64 opt.DefDirs = ComponentDefDirs 65 } 66 ref := &docgen.MarkdownReference{ 67 AllInOne: true, 68 ForceExample: opt.ForceExamples, 69 Filter: func(capability types.Capability) bool { 70 if capability.Type != types.TypeComponentDefinition || capability.Category != types.CUECategory { 71 return false 72 } 73 if capability.Labels != nil && (capability.Labels[types.LabelDefinitionHidden] == "true" || capability.Labels[types.LabelDefinitionDeprecated] == "true") { 74 return false 75 } 76 // only print capability which contained in cue def 77 for _, dir := range opt.DefDirs { 78 files, err := os.ReadDir(dir) 79 if err != nil { 80 fmt.Println("read dir err", opt.DefDirs, err) 81 return false 82 } 83 for _, f := range files { 84 if strings.Contains(f.Name(), capability.Name) { 85 return true 86 } 87 } 88 } 89 return false 90 }, 91 CustomDocHeader: CustomComponentHeaderEN, 92 } 93 ref.Local = &docgen.FromLocal{Paths: ComponentDefDirs} 94 ref.Client = singleton.KubeClient.Get() 95 96 if opt.Path != "" { 97 ref.I18N = &docgen.En 98 if strings.Contains(opt.Location, "zh") || strings.Contains(opt.Location, "chinese") { 99 ref.I18N = &docgen.Zh 100 ref.CustomDocHeader = CustomComponentHeaderZH 101 } 102 if err := ref.GenerateReferenceDocs(ctx, c, opt.Path); err != nil { 103 fmt.Println(err) 104 os.Exit(1) 105 } 106 fmt.Printf("component reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), opt.Path) 107 return 108 } 109 if opt.Location == "" || opt.Location == "en" { 110 ref.I18N = &docgen.En 111 if err := ref.GenerateReferenceDocs(ctx, c, ComponentDefRefPath); err != nil { 112 fmt.Println(err) 113 os.Exit(1) 114 } 115 fmt.Printf("component reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), ComponentDefRefPath) 116 } 117 if opt.Location == "" || opt.Location == "zh" { 118 ref.I18N = &docgen.Zh 119 ref.CustomDocHeader = CustomComponentHeaderZH 120 if err := ref.GenerateReferenceDocs(ctx, c, ComponentDefRefPathZh); err != nil { 121 fmt.Println(err) 122 os.Exit(1) 123 } 124 fmt.Printf("component reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), ComponentDefRefPathZh) 125 } 126 }