github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/tasks/update_initfile_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  	"errors"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"strings"
    14  
    15  	"github.com/turingchain2020/turingchain/cmd/tools/util"
    16  )
    17  
    18  type itemData struct {
    19  	path string
    20  }
    21  
    22  // UpdateInitFileTask 通过扫描本地目录更新init.go文件
    23  type UpdateInitFileTask struct {
    24  	TaskBase
    25  	Folder string
    26  
    27  	initFile  string
    28  	itemDatas []*itemData
    29  }
    30  
    31  //GetName 获取name
    32  func (up *UpdateInitFileTask) GetName() string {
    33  	return "UpdateInitFileTask"
    34  }
    35  
    36  //Execute 执行
    37  func (up *UpdateInitFileTask) Execute() error {
    38  	// 1. 检查目标文件夹是否存在,如果不存在则不扫描
    39  	if !util.CheckPathExisted(up.Folder) {
    40  		mlog.Error("UpdateInitFileTask Execute failed", "folder", up.Folder)
    41  		return errors.New("NotExisted")
    42  	}
    43  	funcs := []func() error{
    44  		up.init,
    45  		up.genInitFile,
    46  		up.formatInitFile,
    47  	}
    48  	for _, fn := range funcs {
    49  		if err := fn(); err != nil {
    50  			return err
    51  		}
    52  	}
    53  	mlog.Info("Success generate init.go", "filename", up.initFile)
    54  	return nil
    55  }
    56  
    57  func (up *UpdateInitFileTask) init() error {
    58  	up.initFile = fmt.Sprintf("%sinit/init.go", up.Folder)
    59  	up.itemDatas = make([]*itemData, 0)
    60  	gopath := os.Getenv("GOPATH")
    61  	if len(gopath) == 0 {
    62  		return errors.New("GOPATH Not Existed")
    63  	}
    64  	// 获取所有文件
    65  	files, _ := ioutil.ReadDir(up.Folder)
    66  	for _, file := range files {
    67  		if file.IsDir() && file.Name() != "init" {
    68  			up.itemDatas = append(up.itemDatas, &itemData{strings.Replace(up.Folder, gopath+"/src/", "", 1) + file.Name()})
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  func (up *UpdateInitFileTask) genInitFile() error {
    75  	if err := util.MakeDir(up.initFile); err != nil {
    76  		return err
    77  	}
    78  	var importStr, content string
    79  	for _, item := range up.itemDatas {
    80  		importStr += fmt.Sprintf("_ \"%s\"\n", item.path)
    81  	}
    82  	content = fmt.Sprintf("package init \n\nimport (\n%s)\n", importStr)
    83  
    84  	util.DeleteFile(up.initFile)
    85  	_, err := util.WriteStringToFile(up.initFile, content)
    86  	return err
    87  }
    88  
    89  func (up *UpdateInitFileTask) formatInitFile() error {
    90  	cmd := exec.Command("gofmt", "-l", "-s", "-w", up.initFile)
    91  	err := cmd.Run()
    92  	if err != nil {
    93  		return fmt.Errorf("Format init.go failed. file %v", up.initFile)
    94  	}
    95  	return nil
    96  }