github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/strategy/createplugin.go (about)

     1  //Copyright Turing Corp. 2018 All Rights Reserved.
     2  //Use of this source code is governed by a BSD-style
     3  //license that can be found in the LICENSE file.
     4  
     5  package strategy
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/turingchain2020/turingchain/util"
    14  
    15  	"github.com/turingchain2020/turingchain/cmd/tools/tasks"
    16  	"github.com/turingchain2020/turingchain/cmd/tools/types"
    17  	"github.com/pkg/errors"
    18  )
    19  
    20  // createPluginStrategy 根據模板配置文件,創建一個完整的項目工程
    21  type createPluginStrategy struct {
    22  	strategyBasic
    23  
    24  	gopath          string
    25  	projName        string // 项目名称
    26  	execName        string // 项目中实现的执行器名称
    27  	execNameFB      string
    28  	className       string
    29  	classTypeName   string
    30  	classActionName string
    31  	outRootPath     string // 项目生成的根目录
    32  	projectPath     string // 生成的项目路径,是绝对路径
    33  
    34  }
    35  
    36  func (c *createPluginStrategy) Run() error {
    37  	fmt.Println("Begin run turingchain create plugin project mode.")
    38  	defer fmt.Println("Run turingchain create plugin project mode finish.")
    39  	if err := c.initMember(); err != nil {
    40  		return err
    41  	}
    42  	return c.rumImpl()
    43  }
    44  
    45  func (c *createPluginStrategy) initMember() error {
    46  	gopath := os.Getenv("GOPATH")
    47  	if len(gopath) <= 0 {
    48  		return errors.New("Can't find GOPATH")
    49  	}
    50  	c.gopath = gopath
    51  	c.outRootPath = filepath.Join(gopath, "/src/github.com/turingchain2020")
    52  	c.projName, _ = c.getParam(types.KeyProjectName)
    53  	c.execName, _ = c.getParam(types.KeyExecutorName)
    54  	c.className, _ = c.getParam(types.KeyClassName)
    55  	c.projectPath = fmt.Sprintf("%s/%s", c.outRootPath, c.projName)
    56  	c.execNameFB, _ = util.MakeStringToUpper(c.execName, 0, 1)
    57  	c.classTypeName = c.execNameFB + "Type"
    58  	c.classActionName = c.execNameFB + "Action"
    59  	return nil
    60  }
    61  
    62  func (c *createPluginStrategy) rumImpl() error {
    63  	var err error
    64  	tasks := c.buildTask()
    65  	for _, task := range tasks {
    66  		err = task.Execute()
    67  		if err != nil {
    68  			mlog.Error("Execute command failed.", "error", err, "taskname", task.GetName())
    69  			break
    70  		}
    71  	}
    72  	return err
    73  }
    74  
    75  func (c *createPluginStrategy) buildTask() []tasks.Task {
    76  	// 获取项目相对于gopath/src中的目录路径
    77  	goprojpath := strings.Replace(c.projectPath, c.gopath+"/src/", "", -1)
    78  	taskSlice := make([]tasks.Task, 0)
    79  	taskSlice = append(taskSlice,
    80  		&tasks.CreateFileFromStrTemplateTask{
    81  			SourceStr:  CpftMainGo,
    82  			OutputFile: fmt.Sprintf("%s/main.go", c.projectPath),
    83  			ReplaceKeyPairs: map[string]string{
    84  				types.TagProjectName: c.projName,
    85  				types.TagProjectPath: goprojpath,
    86  			},
    87  		},
    88  		&tasks.CreateFileFromStrTemplateTask{
    89  			SourceStr:  CpftCfgToml,
    90  			OutputFile: fmt.Sprintf("%s/%s.toml", c.projectPath, c.projName),
    91  			ReplaceKeyPairs: map[string]string{
    92  				types.TagProjectName: c.projName,
    93  			},
    94  		},
    95  		&tasks.CreateFileFromStrTemplateTask{
    96  			SourceStr:     CpftRunMain,
    97  			BlockStrBegin: CpftRunmainBlock + "`",
    98  			BlockStrEnd:   "`",
    99  			OutputFile:    fmt.Sprintf("%s/%s.go", c.projectPath, c.projName),
   100  			ReplaceKeyPairs: map[string]string{
   101  				types.TagProjectName: c.projName,
   102  			},
   103  		},
   104  		&tasks.CreateFileFromStrTemplateTask{
   105  			SourceStr:  CpftMakefile,
   106  			OutputFile: fmt.Sprintf("%s/Makefile", c.projectPath),
   107  			ReplaceKeyPairs: map[string]string{
   108  				types.TagProjectName: c.projName,
   109  				types.TagProjectPath: goprojpath,
   110  				types.TagGoPath:      c.gopath,
   111  				types.TagExecName:    c.execName,
   112  			},
   113  		},
   114  		&tasks.CreateFileFromStrTemplateTask{
   115  			SourceStr:  CpftTravisYml,
   116  			OutputFile: fmt.Sprintf("%s/.travis.yml", c.projectPath),
   117  			ReplaceKeyPairs: map[string]string{
   118  				types.TagProjectName: c.projName,
   119  			},
   120  		},
   121  		&tasks.CreateFileFromStrTemplateTask{
   122  			SourceStr:  CpftPluginToml,
   123  			OutputFile: fmt.Sprintf("%s/plugin/plugin.toml", c.projectPath),
   124  			ReplaceKeyPairs: map[string]string{
   125  				types.TagProjectName: c.projName,
   126  			},
   127  		},
   128  		&tasks.CreateFileFromStrTemplateTask{
   129  			SourceStr:  CpftCliMain,
   130  			OutputFile: fmt.Sprintf("%s/cli/main.go", c.projectPath),
   131  			ReplaceKeyPairs: map[string]string{
   132  				types.TagProjectName: c.projName,
   133  				types.TagProjectPath: goprojpath,
   134  			},
   135  		},
   136  		&tasks.CreateFileFromStrTemplateTask{
   137  			SourceStr:  CpftDappCommands,
   138  			OutputFile: fmt.Sprintf("%s/plugin/dapp/%s/commands/cmd.go", c.projectPath, c.execName),
   139  			ReplaceKeyPairs: map[string]string{
   140  				types.TagProjectName: c.projName,
   141  			},
   142  		},
   143  		&tasks.CreateFileFromStrTemplateTask{
   144  			SourceStr:  CpftDappPlugin,
   145  			OutputFile: fmt.Sprintf("%s/plugin/dapp/%s/plugin.go", c.projectPath, c.projName),
   146  			ReplaceKeyPairs: map[string]string{
   147  				types.TagProjectName: c.projName,
   148  				types.TagExecNameFB:  c.execNameFB,
   149  				types.TagProjectPath: goprojpath,
   150  			},
   151  		},
   152  		&tasks.CreateFileFromStrTemplateTask{
   153  			SourceStr:  CpftDappExec,
   154  			OutputFile: fmt.Sprintf("%s/plugin/dapp/%s/executor/%s.go", c.projectPath, c.projName, c.execName),
   155  			ReplaceKeyPairs: map[string]string{
   156  				types.TagProjectName: c.projName,
   157  				types.TagExecName:    c.execName,
   158  				types.TagClassName:   c.className,
   159  			},
   160  		},
   161  		&tasks.CreateFileFromStrTemplateTask{
   162  			SourceStr:       CpftDappCreatepb,
   163  			OutputFile:      fmt.Sprintf("%s/plugin/dapp/%s/proto/create_protobuf.sh", c.projectPath, c.projName),
   164  			ReplaceKeyPairs: map[string]string{},
   165  		},
   166  		&tasks.CreateFileFromStrTemplateTask{
   167  			SourceStr:       CpftDappMakefile,
   168  			OutputFile:      fmt.Sprintf("%s/plugin/dapp/%s/proto/Makefile", c.projectPath, c.projName),
   169  			ReplaceKeyPairs: map[string]string{},
   170  		},
   171  		&tasks.CreateFileFromStrTemplateTask{
   172  			SourceStr:  CpftDappProto,
   173  			OutputFile: fmt.Sprintf("%s/plugin/dapp/%s/proto/%s.proto", c.projectPath, c.projName, c.execName),
   174  			ReplaceKeyPairs: map[string]string{
   175  				types.TagActionName: c.classActionName,
   176  			},
   177  		},
   178  		&tasks.CreateFileFromStrTemplateTask{
   179  			SourceStr:  CpftDappTypefile,
   180  			OutputFile: fmt.Sprintf("%s/plugin/dapp/%s/types/types.go", c.projectPath, c.projName),
   181  			ReplaceKeyPairs: map[string]string{
   182  				types.TagExecNameFB:    c.execNameFB,
   183  				types.TagExecName:      c.execName,
   184  				types.TagClassTypeName: c.classTypeName,
   185  				types.TagActionName:    c.classActionName,
   186  			},
   187  		},
   188  		// 需要将所有的go文件格式化以下
   189  		&tasks.FormatDappSourceTask{
   190  			OutputFolder: fmt.Sprintf("%s/%s/", c.outRootPath, c.projName),
   191  		},
   192  	)
   193  	return taskSlice
   194  }