go-tdlib/cmd/generate-code.go

85 lines
2.4 KiB
Go
Raw Normal View History

2018-08-30 14:55:42 +00:00
package main
import (
2018-10-23 12:49:10 +00:00
"bufio"
"flag"
"log"
"os"
"path/filepath"
2022-01-26 20:48:37 +00:00
"github.com/c0re100/gotdlib/codegen"
"github.com/c0re100/gotdlib/tlparser"
2018-08-30 14:55:42 +00:00
)
type config struct {
2018-10-23 12:49:10 +00:00
version string
outputDirPath string
packageName string
functionFileName string
typeFileName string
unmarshalerFileName string
2018-08-30 14:55:42 +00:00
}
func main() {
2018-10-23 12:49:10 +00:00
var config config
flag.StringVar(&config.version, "version", "", "TDLib version")
2022-01-27 23:44:54 +00:00
flag.StringVar(&config.outputDirPath, "outputDir", "./client", "output directory")
flag.StringVar(&config.packageName, "package", "client", "package name")
2018-10-23 12:49:10 +00:00
flag.StringVar(&config.functionFileName, "functionFile", "function.go", "functions filename")
flag.StringVar(&config.typeFileName, "typeFile", "type.go", "types filename")
flag.StringVar(&config.unmarshalerFileName, "unmarshalerFile", "unmarshaler.go", "unmarshalers filename")
flag.Parse()
2022-01-26 21:05:22 +00:00
tl, err := os.Open("./data/td_api.tl")
2018-10-23 12:49:10 +00:00
if err != nil {
2022-01-26 21:05:22 +00:00
log.Fatalf(err.Error())
2018-10-23 12:49:10 +00:00
return
}
2022-01-26 21:05:22 +00:00
schema, err := tlparser.Parse(bufio.NewReader(tl))
2018-10-23 12:49:10 +00:00
if err != nil {
log.Fatalf("schema parse error: %s", err)
return
}
err = os.MkdirAll(config.outputDirPath, 0755)
if err != nil {
log.Fatalf("error creating %s: %s", config.outputDirPath, err)
}
functionFilePath := filepath.Join(config.outputDirPath, config.functionFileName)
os.Remove(functionFilePath)
functionFile, err := os.OpenFile(functionFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Fatalf("functionFile open error: %s", err)
}
defer functionFile.Close()
bufio.NewWriter(functionFile).Write(codegen.GenerateFunctions(schema, config.packageName))
typeFilePath := filepath.Join(config.outputDirPath, config.typeFileName)
os.Remove(typeFilePath)
typeFile, err := os.OpenFile(typeFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Fatalf("typeFile open error: %s", err)
}
defer typeFile.Close()
bufio.NewWriter(typeFile).Write(codegen.GenerateTypes(schema, config.packageName))
unmarshalerFilePath := filepath.Join(config.outputDirPath, config.unmarshalerFileName)
os.Remove(unmarshalerFilePath)
unmarshalerFile, err := os.OpenFile(unmarshalerFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Fatalf("unmarshalerFile open error: %s", err)
}
defer unmarshalerFile.Close()
bufio.NewWriter(unmarshalerFile).Write(codegen.GenerateUnmarshalers(schema, config.packageName))
2018-08-30 14:55:42 +00:00
}