github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/service/protobuf.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strconv"
     7  	"strings"
     8  
     9  	commonUtils "github.com/easysoft/zendata/pkg/utils/common"
    10  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    11  	logUtils "github.com/easysoft/zendata/pkg/utils/log"
    12  	shellUtils "github.com/easysoft/zendata/pkg/utils/shell"
    13  	"github.com/easysoft/zendata/pkg/utils/vari"
    14  )
    15  
    16  const (
    17  	outputDir = "out"
    18  	bufFile   = "data.bin"
    19  )
    20  
    21  type ProtobufService struct {
    22  	ResService     *ResService     `inject:""`
    23  	FieldService   *FieldService   `inject:""`
    24  	CombineService *CombineService `inject:""`
    25  	OutputService  *OutputService  `inject:""`
    26  }
    27  
    28  func (s *ProtobufService) GenerateProtobuf(protoFile string) (content, pth string) {
    29  	outputDir := s.generateCls(protoFile)
    30  
    31  	convertFile := s.generateConverter(outputDir)
    32  
    33  	content, pth = s.generateBinData(convertFile)
    34  
    35  	return
    36  }
    37  
    38  func (s *ProtobufService) generateBinData(convertFile string) (content, pth string) {
    39  	dir := filepath.Dir(convertFile)
    40  
    41  	phpExeFile := "php"
    42  	if commonUtils.IsWin() { // use build-in php runtime
    43  		phpExeFile = filepath.Join(vari.WorkDir, "runtime", "php", "php7", "php.exe")
    44  	}
    45  	cmdStr := phpExeFile + " convert.php"
    46  	out, _ := shellUtils.ExecInDir(cmdStr, dir)
    47  	if vari.Verbose {
    48  		logUtils.PrintTo(out)
    49  	}
    50  
    51  	pth = filepath.Join(dir, bufFile)
    52  
    53  	return
    54  }
    55  
    56  func (s *ProtobufService) generateConverter(dir string) (pth string) {
    57  	srcFile := filepath.Join(vari.WorkDir, "runtime", "protobuf", "convert.php")
    58  	pth = filepath.Join(dir, "convert.php")
    59  
    60  	content := fileUtils.ReadFile(srcFile)
    61  	content = strings.ReplaceAll(content, "${cls_name}", vari.ProtoCls)
    62  
    63  	fileUtils.WriteFile(pth, content)
    64  
    65  	return
    66  }
    67  
    68  func (s *ProtobufService) generateCls(protoFile string) (ret string) {
    69  	outputDir := filepath.Join(fileUtils.GetAbsoluteDir(protoFile), outputDir)
    70  	fileUtils.RmFile(outputDir)
    71  	fileUtils.MkDirIfNeeded(outputDir)
    72  
    73  	platform := commonUtils.GetOs()
    74  	execFile := "protoc"
    75  	if commonUtils.IsWin() {
    76  		platform += fmt.Sprintf("%d", strconv.IntSize)
    77  		execFile += ".exe"
    78  	}
    79  
    80  	execFile = filepath.Join(vari.WorkDir, "runtime", "protobuf", "bin", platform, execFile)
    81  
    82  	cmdStr := fmt.Sprintf("%s --php_out=%s %s", execFile, outputDir, protoFile)
    83  	shellUtils.Exec(cmdStr)
    84  
    85  	ret = outputDir
    86  
    87  	return
    88  }