github.com/relnod/pegomock@v2.0.1+incompatible/pegomock/filehandling/filehandling.go (about)

     1  package filehandling
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/petergtz/pegomock/mockgen"
    13  	"github.com/petergtz/pegomock/model"
    14  	"github.com/petergtz/pegomock/modelgen/gomock"
    15  	"github.com/petergtz/pegomock/modelgen/loader"
    16  	"github.com/petergtz/pegomock/pegomock/util"
    17  )
    18  
    19  func GenerateMockFileInOutputDir(
    20  	args []string,
    21  	outputDirPath string,
    22  	outputFilePathOverride string,
    23  	packageOut string,
    24  	selfPackage string,
    25  	debugParser bool,
    26  	out io.Writer,
    27  	useExperimentalModelGen bool,
    28  	shouldGenerateMatchers bool,
    29  	matchersDestination string) {
    30  
    31  	// if a file path override is specified
    32  	// ensure all directories in the path are created
    33  	if outputFilePathOverride != "" {
    34  		if err := os.MkdirAll(filepath.Dir(outputFilePathOverride), 0755); err != nil {
    35  			panic(fmt.Errorf("Failed to make output directory, error: %v", err))
    36  		}
    37  	}
    38  
    39  	GenerateMockFile(
    40  		args,
    41  		OutputFilePath(args, outputDirPath, outputFilePathOverride),
    42  		packageOut,
    43  		selfPackage,
    44  		debugParser,
    45  		out,
    46  		useExperimentalModelGen,
    47  		shouldGenerateMatchers,
    48  		matchersDestination)
    49  }
    50  
    51  func OutputFilePath(args []string, outputDirPath string, outputFilePathOverride string) string {
    52  	if outputFilePathOverride != "" {
    53  		return outputFilePathOverride
    54  	} else if util.SourceMode(args) {
    55  		return filepath.Join(outputDirPath, "mock_"+strings.TrimSuffix(args[0], ".go")+"_test.go")
    56  	} else {
    57  		return filepath.Join(outputDirPath, "mock_"+strings.ToLower(args[len(args)-1])+"_test.go")
    58  	}
    59  }
    60  
    61  func GenerateMockFile(args []string, outputFilePath string, packageOut string, selfPackage string, debugParser bool, out io.Writer, useExperimentalModelGen bool, shouldGenerateMatchers bool, matchersDestination string) {
    62  	mockSourceCode, matcherSourceCodes := GenerateMockSourceCode(args, packageOut, selfPackage, debugParser, out, useExperimentalModelGen)
    63  
    64  	err := ioutil.WriteFile(outputFilePath, mockSourceCode, 0664)
    65  	if err != nil {
    66  		panic(fmt.Errorf("Failed writing to destination: %v", err))
    67  	}
    68  
    69  	if shouldGenerateMatchers {
    70  		matchersPath := filepath.Join(filepath.Dir(outputFilePath), "matchers")
    71  		if matchersDestination != "" {
    72  			matchersPath = matchersDestination
    73  		}
    74  		err = os.MkdirAll(matchersPath, 0755)
    75  		if err != nil {
    76  			panic(fmt.Errorf("Failed making dirs \"%v\": %v", matchersPath, err))
    77  		}
    78  		for matcherTypeName, matcherSourceCode := range matcherSourceCodes {
    79  			err := ioutil.WriteFile(filepath.Join(matchersPath, matcherTypeName+".go"), []byte(matcherSourceCode), 0664)
    80  			if err != nil {
    81  				panic(fmt.Errorf("Failed writing to destination: %v", err))
    82  			}
    83  		}
    84  	}
    85  }
    86  
    87  func GenerateMockSourceCode(args []string, packageOut string, selfPackage string, debugParser bool, out io.Writer, useExperimentalModelGen bool) ([]byte, map[string]string) {
    88  	var err error
    89  
    90  	var ast *model.Package
    91  	var src string
    92  	if util.SourceMode(args) {
    93  		ast, err = gomock.ParseFile(args[0])
    94  		src = args[0]
    95  	} else {
    96  		if len(args) != 2 {
    97  			log.Fatal("Expected exactly two arguments, but got " + fmt.Sprint(args))
    98  		}
    99  		if useExperimentalModelGen {
   100  			ast, err = loader.GenerateModel(args[0], args[1])
   101  
   102  		} else {
   103  			ast, err = gomock.Reflect(args[0], strings.Split(args[1], ","))
   104  		}
   105  		src = fmt.Sprintf("%v (interfaces: %v)", args[0], args[1])
   106  	}
   107  	if err != nil {
   108  		panic(fmt.Errorf("Loading input failed: %v", err))
   109  	}
   110  
   111  	if debugParser {
   112  		ast.Print(out)
   113  	}
   114  
   115  	return mockgen.GenerateOutput(ast, src, packageOut, selfPackage)
   116  }