github.com/oam-dev/kubevela@v1.9.11/hack/docgen/def/mods/workflow.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  	// WorkflowDefRefPath is the target path for kubevela.io workflow ref docs
    35  	WorkflowDefRefPath = "../kubevela.io/docs/end-user/workflow/built-in-workflow-defs.md"
    36  	// WorkflowDefRefPathZh is the target path for kubevela.io workflow ref docs in Chinese
    37  	WorkflowDefRefPathZh = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/workflow/built-in-workflow-defs.md"
    38  )
    39  
    40  // WorkflowDefDirs store inner CUE definition
    41  var WorkflowDefDirs = []string{"./vela-templates/definitions/internal/workflowstep/", "../catalog/addons/vela-workflow/definitions"}
    42  
    43  // CustomWorkflowHeaderEN .
    44  var CustomWorkflowHeaderEN = `---
    45  title: Built-in WorkflowStep Type
    46  ---
    47  
    48  This documentation will walk through all the built-in workflow step 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  // CustomWorkflowHeaderZH .
    53  var CustomWorkflowHeaderZH = `---
    54  title: 内置工作流步骤列表
    55  ---
    56  
    57  本文档将**按字典序**展示所有内置工作流步骤的参数列表。
    58  
    59  ` + fmt.Sprintf("> 本文档由[脚本](../../contributor/cli-ref-doc)自动生成,请勿手动修改,上次更新于 %s。\n\n", time.Now().Format(time.RFC3339))
    60  
    61  // WorkflowDef generate workflow def reference doc
    62  func WorkflowDef(ctx context.Context, c common.Args, opt Options) {
    63  	if len(opt.DefDirs) == 0 {
    64  		opt.DefDirs = WorkflowDefDirs
    65  	}
    66  	ref := &docgen.MarkdownReference{
    67  		AllInOne:     true,
    68  		ForceExample: opt.ForceExamples,
    69  		Filter: func(capability types.Capability) bool {
    70  
    71  			if capability.Type != types.TypeWorkflowStep || capability.Category != types.CUECategory {
    72  				return false
    73  			}
    74  
    75  			if capability.Labels != nil && capability.Labels[types.LabelDefinitionDeprecated] == "true" {
    76  				return false
    77  			}
    78  			// only print capability which contained in cue def
    79  			for _, dir := range opt.DefDirs {
    80  				files, err := os.ReadDir(dir)
    81  				if err != nil {
    82  					fmt.Println("read dir err", opt.DefDirs, err)
    83  					return false
    84  				}
    85  				for _, f := range files {
    86  					if strings.Contains(f.Name(), capability.Name) {
    87  						return true
    88  					}
    89  				}
    90  			}
    91  			return false
    92  		},
    93  		CustomDocHeader: CustomWorkflowHeaderEN,
    94  	}
    95  	ref.Local = &docgen.FromLocal{Paths: opt.DefDirs}
    96  	ref.Client = singleton.KubeClient.Get()
    97  
    98  	if opt.Path != "" {
    99  		ref.I18N = &docgen.En
   100  		if strings.Contains(opt.Location, "zh") || strings.Contains(opt.Location, "chinese") {
   101  			ref.I18N = &docgen.Zh
   102  			ref.CustomDocHeader = CustomWorkflowHeaderZH
   103  		}
   104  		if err := ref.GenerateReferenceDocs(ctx, c, opt.Path); err != nil {
   105  			fmt.Println(err)
   106  			os.Exit(1)
   107  		}
   108  		fmt.Printf("workflow reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), opt.Path)
   109  		return
   110  	}
   111  	if opt.Location == "" || opt.Location == "en" {
   112  		ref.I18N = &docgen.En
   113  		if err := ref.GenerateReferenceDocs(ctx, c, WorkflowDefRefPath); err != nil {
   114  			fmt.Println(err)
   115  			os.Exit(1)
   116  		}
   117  		fmt.Printf("workflow reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), WorkflowDefRefPath)
   118  	}
   119  	if opt.Location == "" || opt.Location == "zh" {
   120  		ref.I18N = &docgen.Zh
   121  		ref.CustomDocHeader = CustomWorkflowHeaderZH
   122  		if err := ref.GenerateReferenceDocs(ctx, c, WorkflowDefRefPathZh); err != nil {
   123  			fmt.Println(err)
   124  			os.Exit(1)
   125  		}
   126  		fmt.Printf("workflow reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), WorkflowDefRefPathZh)
   127  	}
   128  }