github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/tasks/copy_template_to_output_task.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 tasks
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/turingchain2020/turingchain/cmd/tools/types"
    14  	"github.com/turingchain2020/turingchain/cmd/tools/util"
    15  )
    16  
    17  //CopyTemplateToOutputTask ...
    18  type CopyTemplateToOutputTask struct {
    19  	TaskBase
    20  	TemplatePath string
    21  	OutputPath   string
    22  	ProjectName  string
    23  	ClassName    string
    24  }
    25  
    26  //GetName 获取name
    27  func (c *CopyTemplateToOutputTask) GetName() string {
    28  	return "CopyTemplateToOutputTask"
    29  }
    30  
    31  //Execute 执行
    32  func (c *CopyTemplateToOutputTask) Execute() error {
    33  	mlog.Info("Execute copy template task.")
    34  	err := filepath.Walk(c.TemplatePath, func(path string, info os.FileInfo, err error) error {
    35  		if info == nil {
    36  			return err
    37  		}
    38  		if c.TemplatePath == path {
    39  			return nil
    40  		}
    41  		if info.IsDir() {
    42  			outFolder := fmt.Sprintf("%s/%s/", c.OutputPath, info.Name())
    43  			if err := util.MakeDir(outFolder); err != nil {
    44  				mlog.Error("MakeDir failed", "error", err, "outFolder", outFolder)
    45  				return err
    46  			}
    47  		} else {
    48  			srcFile := path
    49  			path = strings.Replace(path, types.TagClassName, c.ClassName, -1)
    50  			path = strings.Replace(path, ".tmp", "", -1)
    51  			dstFile := strings.Replace(path, c.TemplatePath, c.OutputPath, -1)
    52  			if _, err := util.CopyFile(srcFile, dstFile); err != nil {
    53  				mlog.Error("CopyFile failed", "error", err, "srcFile", srcFile, "dstFile", dstFile)
    54  				return err
    55  			}
    56  		}
    57  		return nil
    58  	})
    59  	return err
    60  }