Example #1
0
func TestGoTpl(ot *testing.T) {
	MustBuildTplInDir("testFile")
	files := kmgFile.MustGetAllFiles("testFile")
	AllCurrect := true
	ErrorMsg := ""
	for _, file := range files {
		if filepath.Ext(file) != ".gotplhtml" {
			continue
		}
		generated := kmgFile.MustReadFile(filepath.Join(filepath.Dir(file), kmgFile.GetFileBaseWithoutExt(file)+".go"))
		correct, err := kmgFile.ReadFile(filepath.Join(filepath.Dir(file), kmgFile.GetFileBaseWithoutExt(file)+".go.good"))
		if err != nil {
			ErrorMsg += fmt.Sprintf("%s read good file fail err [%s]\n", file, err)
			AllCurrect = false
			continue
		}
		if !bytes.Equal(generated, correct) {
			ErrorMsg += fmt.Sprintf("%s generated not equal correct\n", file)
			AllCurrect = false
			continue
		}
	}
	if !AllCurrect {
		panic(ErrorMsg)
	}
}
Example #2
0
func (command *GoCrossCompile) Execute(context *console.Context) (err error) {
	if len(context.Args) <= 2 {
		return fmt.Errorf("need gofile parameter")
	}
	targetFile := context.FlagSet().Arg(0)
	kmgc, err := kmgConfig.LoadEnvFromWd()
	if err != nil {
		return
	}
	targetName := kmgFile.GetFileBaseWithoutExt(targetFile)
	if command.outputPath == "" {
		command.outputPath = filepath.Join(kmgc.ProjectPath, "bin")
	}
	for _, target := range kmgc.CrossCompileTarget {
		fileName := targetName + "_" + target.GetGOOS() + "_" + target.GetGOARCH()
		if target.GetGOOS() == "windows" {
			fileName = fileName + ".exe"
		}
		outputFilePath := filepath.Join(command.outputPath, fileName)
		cmd := kmgCmd.NewStdioCmd(context, "go", "build", "-o", outputFilePath, targetFile)
		kmgCmd.SetCmdEnv(cmd, "GOOS", target.GetGOOS())
		kmgCmd.SetCmdEnv(cmd, "GOARCH", target.GetGOARCH())
		kmgCmd.SetCmdEnv(cmd, "GOPATH", kmgc.GOPATHToString())
		err = cmd.Run()
		if err != nil {
			return err
		}
	}
	return
}
Example #3
0
func setCurrentAsCorrect() {
	files := kmgFile.MustGetAllFiles("testFile")
	for _, file := range files {
		if filepath.Ext(file) != ".gotplhtml" {
			continue
		}
		kmgFile.MustCopyFile(filepath.Join(filepath.Dir(file), kmgFile.GetFileBaseWithoutExt(file)+".go"), filepath.Join(filepath.Dir(file), kmgFile.GetFileBaseWithoutExt(file)+".go.good"))
	}
}
Example #4
0
/*
GoCrossComplie [gofile]
the output file will put into $project_root/bin/name_GOOS_GOARCH[.exe]
*/
func runGoCrossCompile() {
	command := GoCrossCompile{}
	flag.StringVar(&command.outputPath, "o", "", "output file dir(file name come from source file name),default to $project_root/bin")
	flag.StringVar(&command.version, "v", "", "version string in output file name")
	flag.StringVar(&command.platform, "platform", "", "platform(default use .kmg.yml config)")
	flag.Parse()

	if len(os.Args) <= 1 {
		kmgConsole.ExitOnErr(fmt.Errorf("need gofile parameter"))
		return
	}
	targetFile := flag.Arg(0)
	kmgc, err := kmgConfig.LoadEnvFromWd()
	kmgConsole.ExitOnErr(err)
	targetName := kmgFile.GetFileBaseWithoutExt(targetFile)
	if command.outputPath == "" {
		command.outputPath = filepath.Join(kmgc.ProjectPath, "bin")
	}
	targetList := []kmgConfig.CompileTarget{}
	if command.platform == "" {
		targetList = kmgc.CrossCompileTarget
	} else {
		targetList = []kmgConfig.CompileTarget{kmgConfig.CompileTarget(command.platform)}
	}
	for _, target := range targetList {
		fileName := ""
		if command.version == "" {
			fileName = targetName + "_" + target.GetGOOS() + "_" + target.GetGOARCH()
		} else {
			fileName = targetName + "_" + command.version + "_" + target.GetGOOS() + "_" + target.GetGOARCH()
		}

		if target.GetGOOS() == "windows" {
			fileName = fileName + ".exe"
		}
		outputFilePath := filepath.Join(command.outputPath, fileName)
		err := kmgCmd.CmdSlice([]string{"go", "build", "-i", "-o", outputFilePath, targetFile}).
			MustSetEnv("GOOS", target.GetGOOS()).
			MustSetEnv("GOARCH", target.GetGOARCH()).
			MustSetEnv("GOPATH", kmgc.GOPATHToString()).
			Run()
		kmgConsole.ExitOnErr(err)
	}
	return
}