2018-08-30 14:55:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"log"
|
2018-10-19 18:06:07 +00:00
|
|
|
"net/http"
|
2018-08-30 14:55:42 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/zelenin/go-tdlib/tlparser"
|
|
|
|
"github.com/zelenin/go-tdlib/codegen"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2018-10-19 18:06:07 +00:00
|
|
|
version string
|
2018-08-30 14:55:42 +00:00
|
|
|
outputDirPath string
|
|
|
|
packageName string
|
|
|
|
functionFileName string
|
|
|
|
typeFileName string
|
|
|
|
unmarshalerFileName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var config config
|
|
|
|
|
2018-10-19 18:06:07 +00:00
|
|
|
flag.StringVar(&config.version, "version", "", "TDLib version")
|
2018-08-30 14:55:42 +00:00
|
|
|
flag.StringVar(&config.outputDirPath, "outputDir", "./tdlib", "output directory")
|
|
|
|
flag.StringVar(&config.packageName, "package", "tdlib", "package name")
|
|
|
|
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()
|
|
|
|
|
2018-10-19 18:06:07 +00:00
|
|
|
resp, err := http.Get("https://raw.githubusercontent.com/tdlib/td/" + config.version + "/td/generate/scheme/td_api.tl")
|
2018-08-30 14:55:42 +00:00
|
|
|
if err != nil {
|
2018-10-19 18:06:07 +00:00
|
|
|
log.Fatalf("http.Get error: %s", err)
|
|
|
|
return
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
2018-10-19 18:06:07 +00:00
|
|
|
defer resp.Body.Close()
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-19 18:06:07 +00:00
|
|
|
schema, err := tlparser.Parse(resp.Body)
|
2018-08-30 14:55:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("schema parse error: %s", err)
|
2018-10-19 18:06:07 +00:00
|
|
|
return
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|