trpc.group/trpc-go/trpc-cmdline@v1.0.9/plugin/cpp_move.go (about)

     1  // Tencent is pleased to support the open source community by making tRPC available.
     2  //
     3  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     4  // All rights reserved.
     5  //
     6  // If you have downloaded a copy of the tRPC source code from Tencent,
     7  // please note that tRPC source code is licensed under the  Apache 2.0 License,
     8  // A copy of the Apache 2.0 License is included in this file.
     9  
    10  package plugin
    11  
    12  import (
    13  	"fmt"
    14  	"os"
    15  	"path/filepath"
    16  
    17  	"trpc.group/trpc-go/trpc-cmdline/descriptor"
    18  	"trpc.group/trpc-go/trpc-cmdline/params"
    19  	"trpc.group/trpc-go/trpc-cmdline/util/fs"
    20  	"trpc.group/trpc-go/trpc-cmdline/util/log"
    21  )
    22  
    23  // CppMove is the plugin for moving proto files to proto directory in generated project.
    24  type CppMove struct {
    25  }
    26  
    27  // Name returns the plugin name.
    28  func (p *CppMove) Name() string {
    29  	return "cpp_move"
    30  }
    31  
    32  // Check only run when `--lang=cpp`
    33  func (p *CppMove) Check(fd *descriptor.FileDescriptor, opt *params.Option) bool {
    34  	return opt.Language == "cpp" && !opt.RPCOnly
    35  }
    36  
    37  // Run runs moving directories action.
    38  func (p *CppMove) Run(fd *descriptor.FileDescriptor, opt *params.Option) error {
    39  	log.Debug("execute plugin for %s in %s", opt.Language, opt.OutputDir)
    40  	pbOutDir := filepath.Join(opt.OutputDir, "proto")
    41  	// moving proto files to proto directory in generated project.
    42  	for pbFile := range fd.Pb2DepsPbs {
    43  		pbAbsPath, err := fs.LocateFile(pbFile, opt.Protodirs)
    44  		if err != nil {
    45  			fmt.Println("fs.LocateFile err: %w", err)
    46  			return nil
    47  		}
    48  		err = fs.Copy(pbAbsPath, filepath.Join(pbOutDir, pbFile))
    49  		if err != nil {
    50  			fmt.Println("file copy err: %w", err)
    51  			return err
    52  		}
    53  	}
    54  	// add executing mode for script
    55  	scriptLs := []string{"build.sh", "clean.sh", "run_client.sh", "run_server.sh"}
    56  	for _, script := range scriptLs {
    57  		err := os.Chmod(filepath.Join(opt.OutputDir, script), 0755)
    58  		if err != nil {
    59  			fmt.Println("chmod failed err: %w", err)
    60  			return err
    61  		}
    62  	}
    63  	return nil
    64  }