Ejemplo n.º 1
0
Archivo: main.go Proyecto: tormoder/fit
func main() {
	l := log.New(os.Stdout, "fitgen:\t", 0)

	fitSrcDir, err := goPackagePath(fitPkgImportPath)
	if err != nil {
		l.Fatalf("can't find fit package root src directory for %q", fitPkgImportPath)
	}
	l.Println("root src directory:", fitSrcDir)

	var (
		messagesOut    = filepath.Join(fitSrcDir, "messages.go")
		typesOut       = filepath.Join(fitSrcDir, "types.go")
		profileOut     = filepath.Join(fitSrcDir, "profile.go")
		stringerPath   = filepath.Join(fitSrcDir, "cmd/stringer/stringer.go")
		typesStringOut = filepath.Join(fitSrcDir, "types_string.go")
	)

	sdkOverride := flag.String(
		"sdk",
		"",
		"provide or override SDK version printed in generated code",
	)
	switches := flag.Bool(
		"switches",
		false,
		"use switches instead jump tables for profile message and field lookups",
	)
	timestamp := flag.Bool(
		"timestamp",
		true,
		"add generation timestamp to generated code",
	)

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: fitgen [flags] [path to sdk zip, xls or xlsx file]\n")
		flag.PrintDefaults()
	}

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(2)
	}

	var (
		inputData []byte
		input     = flag.Arg(0)
		inputExt  = filepath.Ext(input)
	)

	switch inputExt {
	case ".zip":
		inputData, err = readDataFromZIP(input)
	case ".xls", ".xlsx":
		inputData, err = readDataFromXLSX(input)
	default:
		l.Fatalln("input file must be of type [.zip | .xls | .xlsx], got:", inputExt)
	}
	if err != nil {
		l.Fatal(err)
	}

	var genOptions []profile.GeneratorOption
	genOptions = append(
		genOptions,
		profile.WithGenerationTimestamp(*timestamp),
		profile.WithLogger(l),
	)
	if *sdkOverride != "" {
		genOptions = append(genOptions, profile.WithSDKVersionOverride(*sdkOverride))
	}
	if *switches {
		genOptions = append(genOptions, profile.WithUseSwitches())
	}

	generator, err := profile.NewGenerator(input, inputData, genOptions...)
	if err != nil {
		l.Fatal(err)
	}

	fitProfile, err := generator.GenerateProfile()
	if err != nil {
		l.Fatal(err)
	}

	if err = ioutil.WriteFile(typesOut, fitProfile.TypesSource, 0644); err != nil {
		l.Fatalf("typegen: error writing types output file: %v", err)
	}

	if err = ioutil.WriteFile(messagesOut, fitProfile.MessagesSource, 0644); err != nil {
		l.Fatalf("typegen: error writing messages output file: %v", err)
	}

	if err = ioutil.WriteFile(profileOut, fitProfile.ProfileSource, 0644); err != nil {
		l.Fatalf("typegen: error writing profile output file: %v", err)
	}

	l.Println("running stringer")
	err = runStringerOnTypes(stringerPath, fitSrcDir, typesStringOut, fitProfile.StringerInput)
	if err != nil {
		l.Fatal(err)
	}
	l.Println("stringer: types done")

	err = runAllTests(fitPkgImportPath)
	if err != nil {
		l.Fatal(err)
	}
	l.Println("go test: pass")

	logMesgNumVsMessages(fitProfile.MesgNumsWithoutMessage, l)

	l.Println("done")
}
Ejemplo n.º 2
0
Archivo: main.go Proyecto: Kyperion/fit
func main() {
	sdkOverride := flag.String(
		"sdk",
		"",
		"provide or override SDK version printed in generated code",
	)
	jmptable := flag.Bool(
		"jmptable",
		true,
		"use jump tables for profile message and field lookups, otherwise use switches",
	)

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: fitgen [flags] [path to sdk zip, xls or xlsx file]\n")
		flag.PrintDefaults()
	}

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(2)
	}

	var sdkVersion string
	isZip := false
	input := flag.Arg(0)
	inputExt := filepath.Ext(input)
	switch inputExt {
	case ".zip":
		isZip = true
	case ".xls", ".xlsx":
	default:
		log.Fatalln("input file must be of type [.zip | .xls | .xlsx], got:", inputExt)
	}

	switch {
	case *sdkOverride != "":
		sdkVersion = *sdkOverride
	case isZip:
		sdkVersion = parseSDKVersion(input)
	default:
		sdkVersion = "Unknown"
	}
	log.Println("sdk version:", sdkVersion)

	generator = profile.NewGenerator(sdkVersion)

	typeData, msgData, err := parseProfileWorkbook(input)
	if err != nil {
		log.Fatal(err)
	}

	goTypes, stringerInput, err := generateTypes(typeData, typesGoOut)
	if err != nil {
		log.Fatal(err)
	}

	goMsgs, err := generateMsgs(msgData, msgsGoOut, goTypes)
	if err != nil {
		log.Fatal(err)
	}

	err = generateProfile(goTypes, goMsgs, profileGoOut, *jmptable)
	if err != nil {
		log.Fatal(err)
	}

	err = runStringerOnTypes(stringerPath, typesStringGoOut, stringerInput)
	if err != nil {
		log.Fatal(err)
	}

	err = runAllTests(fitPkgImportPath)
	if err != nil {
		log.Fatal(err)
	}

	err = logMesgNumVsMsgs(goTypes, goMsgs)
	if err != nil {
		log.Fatal(err)
	}

	log.Println("done")
}