github.com/oam-dev/kubevela@v1.9.11/hack/docgen/def/mods/trait.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  	// TraitDefRefPath is the target path for kubevela.io trait ref docs
    35  	TraitDefRefPath = "../kubevela.io/docs/end-user/traits/references.md"
    36  	// TraitDefRefPathZh is the target path for kubevela.io trait ref docs in Chinese
    37  	TraitDefRefPathZh = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/traits/references.md"
    38  )
    39  
    40  // TraitDefDirs store inner CUE definition
    41  var TraitDefDirs = []string{"./vela-templates/definitions/internal/trait/"}
    42  
    43  // CustomTraitHeaderEN .
    44  var CustomTraitHeaderEN = `---
    45  title: Built-in Trait Type
    46  ---
    47  
    48  This documentation will walk through all the built-in trait 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  // CustomTraitHeaderZH .
    53  var CustomTraitHeaderZH = `---
    54  title: 内置运维特征列表
    55  ---
    56  
    57  本文档将**按字典序**展示所有内置运维特征的参数列表。
    58  
    59  ` + fmt.Sprintf("> 本文档由[脚本](../../contributor/cli-ref-doc)自动生成,请勿手动修改,上次更新于 %s。\n\n", time.Now().Format(time.RFC3339))
    60  
    61  // TraitDef generate trait def reference doc
    62  func TraitDef(ctx context.Context, c common.Args, opt Options) {
    63  	if len(opt.DefDirs) == 0 {
    64  		opt.DefDirs = TraitDefDirs
    65  	}
    66  	ref := &docgen.MarkdownReference{
    67  		AllInOne:     true,
    68  		ForceExample: opt.ForceExamples,
    69  		Filter: func(capability types.Capability) bool {
    70  			if capability.Type != types.TypeTrait || capability.Category != types.CUECategory {
    71  				return false
    72  			}
    73  			if capability.Labels != nil && (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: CustomTraitHeaderEN,
    92  	}
    93  	ref.Local = &docgen.FromLocal{Paths: TraitDefDirs}
    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 = CustomTraitHeaderZH
   101  		}
   102  		if err := ref.GenerateReferenceDocs(ctx, c, opt.Path); err != nil {
   103  			fmt.Println(err)
   104  			os.Exit(1)
   105  		}
   106  		fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), opt.Path)
   107  	} else {
   108  		// Generate to default path depends on language
   109  		if opt.Location == "" || opt.Location == "en" {
   110  			ref.I18N = &docgen.En
   111  			if err := ref.GenerateReferenceDocs(ctx, c, TraitDefRefPath); err != nil {
   112  				fmt.Println(err)
   113  				os.Exit(1)
   114  			}
   115  			fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), TraitDefRefPath)
   116  		}
   117  		if opt.Location == "" || opt.Location == "zh" {
   118  			ref.I18N = &docgen.Zh
   119  			ref.CustomDocHeader = CustomTraitHeaderZH
   120  			if err := ref.GenerateReferenceDocs(ctx, c, TraitDefRefPathZh); err != nil {
   121  				fmt.Println(err)
   122  				os.Exit(1)
   123  			}
   124  			fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), TraitDefRefPathZh)
   125  		}
   126  	}
   127  }