Skip to content

Commit

Permalink
DedAzaMarks/generate-rpc-is-an-optional (#23)
Browse files Browse the repository at this point in the history
* add version print

* change default value of GenerateRPCCode flag to false

* * remote deprecated flags
* ask for BasicPRCPath flag only go code generation

* rearrange flags
  • Loading branch information
DedAzaMarks authored May 24, 2024
1 parent 74b20e9 commit cb8b0cc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 95 deletions.
43 changes: 12 additions & 31 deletions cmd/tlgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,25 @@
package main

import (
"flag"
"fmt"
"log"
"runtime/debug"

"github.com/vkcom/tl/internal/tlcodegen"
)

type arguments struct {
tlcodegen.Gen2Options

SchemaFileName string // not recommended
TLOPath string
CanonicalFormPath string // combinators in canonical form, with comment of source schema file path
Outdir string
IgnoreGeneratedCode bool // ignores generated code. TODO - remove, use language="" instead
}

func commonFlags(argv *arguments) {
flag.StringVar(&argv.Outdir, "outdir", "",
`where to write generated files`)
flag.StringVar(&argv.SchemaFileName, "schema", "",
"input TL schema in binary format")
flag.StringVar(&argv.TLPackageNameFull, "pkgPath", "",
"package path to be used inside generated code")
flag.StringVar(&argv.BytesVersions, "generateByteVersions", "",
"comma-separated list of fully-qualified top-level types or namespaces (if have trailing '.'), to generate byte versions for. Empty means 'none'.")
flag.BoolVar(&argv.Verbose, "v", false,
"verbose mode that prints debug info")
}

func main() {
log.SetFlags(0)
bi, ok := debug.ReadBuildInfo()
if !ok {
log.Printf("Failed to read build info")
return
}
fmt.Printf("tlgen version: %+v\n", bi.Main.Version)

var argv arguments

commonFlags(&argv)
addFlags(&argv)
log.SetFlags(0)

flag.Parse()
var options tlcodegen.Gen2Options

run(argv)
parseFlags(&options)
run(options)
}
114 changes: 60 additions & 54 deletions cmd/tlgen/main2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,77 +26,83 @@ import (

const tlExt = ".tl"

func addFlags(argv *arguments) {
flag.StringVar(&argv.Language, "language", "go",
func parseFlags(opt *tlcodegen.Gen2Options) {
// General
flag.StringVar(&opt.Language, "language", "go",
`generation target language`)
flag.StringVar(&argv.RootCPPNamespace, "cpp-namespace", "",
`c++ root namespace, separated by '::' if more than 1 element`)
flag.StringVar(&argv.BasicPackageNameFull, "basicPkgPath", "",
flag.StringVar(&opt.Outdir, "outdir", "",
`where to write generated files`)
flag.StringVar(&opt.CopyrightFilePath, "copyrightPath", "",
"path to file with copyright text")
flag.BoolVar(&opt.WarningsAreErrors, "Werror", false,
"treat all warnings as errors")
flag.BoolVar(&opt.Verbose, "v", false,
"verbose mode that prints debug info")

// Go
flag.StringVar(&opt.BasicPackageNameFull, "basicPkgPath", "",
"if empty, 'basictl' package will be generated in output dir, otherwise imports will be generated")
flag.BoolVar(&argv.GenerateRandomCode, "generateRandomCode", false,
"whether to generate methods for random filling structs")
flag.BoolVar(&argv.GenerateLegacyJsonRead, "generateLegacyJsonRead", false,
"whether to generate methods to read json in old way")
flag.BoolVar(&argv.GenerateRPCCode, "generateRPCCode", true,
flag.StringVar(&opt.TLPackageNameFull, "pkgPath", "",
"package path to be used inside generated code")
flag.BoolVar(&opt.GenerateRPCCode, "generateRPCCode", false,
"whether to generate *_server.go files")
flag.StringVar(&argv.BasicRPCPath, "basicRPCPath", "",
flag.StringVar(&opt.BasicRPCPath, "basicRPCPath", "",
"path to rpc package")
flag.StringVar(&argv.TLOPath, "tloPath", "",
"whether to serialize TL schema in binary form")
flag.StringVar(&argv.CanonicalFormPath, "canonicalFormPath", "",
"generate file with combinators in canonical form")
flag.BoolVar(&argv.SchemaDocumentation, "generateSchemaDocumentation", false,
flag.StringVar(&opt.BytesVersions, "generateByteVersions", "",
"comma-separated list of fully-qualified top-level types or namespaces (if have trailing '.'), to generate byte versions for. Empty means 'none'.")
flag.StringVar(&opt.TypesWhileList, "typesWhiteList", "",
"comma-separated list of fully-qualified top-level types or namespaces (if have trailing '.'), to generate code. Empty means 'all'")
flag.BoolVar(&opt.SplitInternal, "split-internal", false,
"generated code will be split into independent packages (in a simple word: speeds up compilation)")
flag.BoolVar(&opt.GenerateRandomCode, "generateRandomCode", false,
"whether to generate methods for random filling structs")
flag.BoolVar(&opt.GenerateLegacyJsonRead, "generateLegacyJsonRead", false,
"whether to generate methods to read json in old way")
flag.BoolVar(&opt.SchemaDocumentation, "generateSchemaDocumentation", false,
"whether to generate .html representation of schema in to tljson.html file")
flag.StringVar(&argv.SchemaURLTemplate, "schemaURLTemplate", "",
flag.StringVar(&opt.SchemaURLTemplate, "schemaURLTemplate", "",
"template for url to current schema if documentation is generated")
flag.BoolVar(&argv.SplitInternal, "split-internal", false,
"generated code will be split into independent packages (in a simple word: speeds up compilation)")
flag.StringVar(&argv.TypesWhileList, "typesWhiteList", "",
"comma-separated list of fully-qualified top-level types or namespaces (if have trailing '.'), to generate code. Empty means 'all'")
flag.StringVar(&argv.CopyrightFilePath, "copyrightPath", "",
"path to file with copyright text")
flag.BoolVar(&argv.WarningsAreErrors, "Werror", false,
"treat all warnings as errors")
flag.BoolVar(&argv.IgnoreGeneratedCode, "ignoreGeneratedCode", false,
"ignores generated code, tlo and documentation will be generated with related flags")

// C++
flag.StringVar(&opt.RootCPPNamespace, "cpp-namespace", "",
`c++ root namespace, separated by '::' if more than 1 element`)

// .tlo
flag.StringVar(&opt.TLOPath, "tloPath", "",
"whether to serialize TL schema in binary form")
flag.StringVar(&opt.CanonicalFormPath, "canonicalFormPath", "",
"generate file with combinators in canonical form")

flag.Parse()
}

func run(argv arguments) {
func run(opt tlcodegen.Gen2Options) {
var commit, version = tlcodegen.TLGenBuildInfo()
log.Printf("tlgen version: %s, commit: %s", version, commit)
if err := runMain(&argv); err != nil {
if err := runMain(&opt); err != nil {
var parseError *tlast.ParseError
if errors.As(err, &parseError) {
parseError.ConsolePrint(argv.ErrorWriter, err, false)
parseError.ConsolePrint(opt.ErrorWriter, err, false)
} else {
log.Println(err.Error())
}
log.Printf("TL Generation Failed")
os.Exit(1)
} else {
if argv.Language == "" {
if opt.Language == "" {
log.Printf("TL Linter Success")
} else {
log.Printf("TL Generation Success")
}
}
}

func runMain(argv *arguments) error {
func runMain(opt *tlcodegen.Gen2Options) error {
var ast tlast.TL
var fullAst tlast.TL
var args []string
if argv.ErrorWriter == nil {
argv.ErrorWriter = os.Stdout
}
if argv.IgnoreGeneratedCode {
argv.Language = ""
}
if argv.SchemaFileName != "" {
return fmt.Errorf("--schema argument is removed, specify 1 or more input TL schema filenames after flags")
}
if argv.GenerateRPCCode && argv.BasicRPCPath == "" {
return fmt.Errorf("--basicRPCPath must be specified or set --generateRPCCode=false if you don't use rpc code")
if opt.ErrorWriter == nil {
opt.ErrorWriter = os.Stdout
}
args = append(args, flag.Args()...)
if len(args) == 0 {
Expand All @@ -118,20 +124,20 @@ func runMain(argv *arguments) error {
ast = append(ast, tl...)
fullAst = append(fullAst, fullTl...)
}
gen, err := tlcodegen.GenerateCode(ast, argv.Gen2Options)
gen, err := tlcodegen.GenerateCode(ast, *opt)
if err != nil {
return err // Do not add excess info to already long parse error
}
if argv.Language != "" {
if argv.Outdir == "" {
if opt.Language != "" {
if opt.Outdir == "" {
return fmt.Errorf("--outdir should not be empty")
}
if err = gen.WriteToDir(argv.Outdir); err != nil {
if err = gen.WriteToDir(opt.Outdir); err != nil {
return err // Context is already in err
}
}
if argv.TLOPath != "" {
if argv.Verbose {
if opt.TLOPath != "" {
if opt.Verbose {
log.Print("generating tlo file")
}
s, err := fullAst.GenerateTLO()
Expand All @@ -142,7 +148,7 @@ func runMain(argv *arguments) error {
if err != nil {
return fmt.Errorf("error writing boxed tlo: %w", err)
}
if err := os.WriteFile(argv.TLOPath, buf, 0644); err != nil {
if err := os.WriteFile(opt.TLOPath, buf, 0644); err != nil {
return fmt.Errorf("error writing tlo file: %w", err)
}
buf, err = s.WriteJSON(nil)
Expand All @@ -153,17 +159,17 @@ func runMain(argv *arguments) error {
if err = json.Indent(&prettyJSON, buf, "", " "); err != nil {
return fmt.Errorf("error pretty printing json tlo: %w", err)
}
if err := os.WriteFile(argv.TLOPath+".json", prettyJSON.Bytes(), 0644); err != nil {
if err := os.WriteFile(opt.TLOPath+".json", prettyJSON.Bytes(), 0644); err != nil {
return fmt.Errorf("error writing tlo json file: %w", err)
}
}
if argv.CanonicalFormPath != "" {
if argv.Verbose {
if opt.CanonicalFormPath != "" {
if opt.Verbose {
log.Print("generating file with combinators in canonical form")
}
var buf bytes.Buffer
fullAst.WriteGenerate2TL(&buf)
if err := os.WriteFile(argv.CanonicalFormPath, buf.Bytes(), 0644); err != nil {
if err := os.WriteFile(opt.CanonicalFormPath, buf.Bytes(), 0644); err != nil {
return err
}
}
Expand Down
33 changes: 23 additions & 10 deletions internal/tlcodegen/tlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,23 +307,33 @@ type Namespace struct {
}

type Gen2Options struct {
TypesWhileList string
BytesVersions string
TLPackageNameFull string
// General
Language string
Outdir string
CopyrightFilePath string
WarningsAreErrors bool
Verbose bool
ErrorWriter io.Writer // all Errors and warnings should be redirected to this io.Writer, by default it is os.Stderr

// Go
BasicPackageNameFull string // if empty, will be created
Verbose bool
TLPackageNameFull string
GenerateRPCCode bool
BasicRPCPath string
BytesVersions string
TypesWhileList string
SplitInternal bool
GenerateRandomCode bool
GenerateLegacyJsonRead bool
SchemaDocumentation bool
SchemaURLTemplate string
SplitInternal bool
Language string
RootCPPNamespace string
CopyrightFilePath string
WarningsAreErrors bool
ErrorWriter io.Writer // all Errors and warnings should be redirected to this io.Writer, by default it is os.Stderr

// C++
RootCPPNamespace string

// .tlo
TLOPath string
CanonicalFormPath string // combinators in canonical form, with comment of source schema file path
}

type Gen2 struct {
Expand Down Expand Up @@ -735,6 +745,9 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
switch options.Language {
case "": // linting
case "go":
if options.GenerateRPCCode && options.BasicRPCPath == "" {
return nil, fmt.Errorf("--basicRPCPath must be specified or set --generateRPCCode=false if you don't use rpc code")
}
options.TLPackageNameFull = strings.TrimSpace(options.TLPackageNameFull)
options.TLPackageNameFull = strings.TrimSuffix(options.TLPackageNameFull, "/")
if options.TLPackageNameFull == "" { // for testing, empty path should be prohibited in main argv parsing
Expand Down

0 comments on commit cb8b0cc

Please sign in to comment.