Skip to content

Commit

Permalink
schema commit, timestamp, url passed, simplified work with marker file
Browse files Browse the repository at this point in the history
  • Loading branch information
hrissan committed Jun 27, 2024
1 parent 4d149a6 commit d1d5817
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 68 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ goldmaster_nocompile: build
--outdir=./$(GEN_PATH)/goldmaster \
--generateSchemaDocumentation \
--schemaURL="https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl" \
--schemaCommit=abcdefgh \
--schemaTimestamp=301812000 \
--pkgPath=github.com/vkcom/tl/$(GEN_PATH)/goldmaster/tl \
--basicPkgPath=$(BASIC_TL_PATH) \
--generateByteVersions=$(TL_BYTE_VERSIONS) \
Expand All @@ -90,6 +92,8 @@ goldmaster_nocompile: build
--outdir=./$(GEN_PATH)/goldmaster_nosplit \
--generateSchemaDocumentation \
--schemaURL="https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl" \
--schemaCommit=abcdefgh \
--schemaTimestamp=301812000 \
--pkgPath=github.com/vkcom/tl/$(GEN_PATH)/goldmaster_nosplit/tl \
--basicPkgPath=$(BASIC_TL_PATH) \
--generateByteVersions=$(TL_BYTE_VERSIONS) \
Expand Down
9 changes: 1 addition & 8 deletions cmd/tlgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
package main

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

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

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

log.SetFlags(0)

Expand Down
27 changes: 16 additions & 11 deletions cmd/tlgen/main2.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func parseFlags(opt *tlcodegen.Gen2Options) {
flag.BoolVar(&opt.SchemaDocumentation, "generateSchemaDocumentation", false,
"whether to generate .html representation of schema in to tljson.html file")
flag.StringVar(&opt.SchemaURL, "schemaURL", "",
"template for url to current schema if documentation is generated")
"url of schema (for documentation)")
flag.UintVar(&opt.SchemaTimestamp, "schemaTimestamp", 0,
"timestamp of schema (for documentation, TLO version)")
flag.StringVar(&opt.SchemaCommit, "schemaCommit", "",
"commit of schema (for documentation)")

// C++
flag.StringVar(&opt.RootCPPNamespace, "cpp-namespace", "",
Expand All @@ -80,34 +84,35 @@ func parseFlags(opt *tlcodegen.Gen2Options) {
}

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

func runMain(opt *tlcodegen.Gen2Options) error {
var ast tlast.TL
var fullAst tlast.TL
var args []string
if opt.ErrorWriter == nil {
opt.ErrorWriter = os.Stdout
}
args = append(args, flag.Args()...)
args := flag.Args()
if len(args) == 0 {
return fmt.Errorf("specify 1 or more input TL schema filenames after flags")
}
Expand Down Expand Up @@ -143,7 +148,7 @@ func runMain(opt *tlcodegen.Gen2Options) error {
if opt.Verbose {
log.Print("generating tlo file")
}
s, err := fullAst.GenerateTLO()
s, err := fullAst.GenerateTLO(uint32(opt.SchemaTimestamp))
if err != nil {
return fmt.Errorf("error on generating tlo: %w", err)
}
Expand Down
22 changes: 7 additions & 15 deletions internal/tlast/tlgen_tlo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (ps *paramScope) find(f func(e param) bool) (param, bool) {
return param{}, false
}

func (tl TL) GenerateTLO() (tls.SchemaV4, error) {
func (tl TL) GenerateTLO(version uint32) (tls.SchemaV4, error) {
allCombinators := make(map[string]*Combinator, len(tl))
for i, comb := range tl {
allCombinators[comb.Construct.Name.String()] = tl[i]
Expand Down Expand Up @@ -291,9 +291,13 @@ func (tl TL) GenerateTLO() (tls.SchemaV4, error) {
v4B, _ := functions[j].AsV4()
return v4A.Id < v4B.Id
})
date := version
if version == 0 {
date = uint32(time.Now().Unix())
}
return tls.SchemaV4{
Version: 0, // always 0
Date: int32(time.Now().Unix()),
Version: int32(version), // TODO - must be changed to # in tl definition
Date: int32(date), // TODO - must be changed to # in tl definition
TypesNum: uint32(len(types)),
Types: types,
ConstructorNum: uint32(len(constructors)),
Expand All @@ -303,18 +307,6 @@ func (tl TL) GenerateTLO() (tls.SchemaV4, error) {
}, nil
}

func (tl TL) GenerateTLOBytes() ([]byte, error) {
s, err := tl.GenerateTLO()
if err != nil {
return nil, err
}
res, err := s.WriteBoxed(nil)
if err != nil {
return nil, fmt.Errorf("can't write TLO boxed: %w", err)
}
return res, nil
}

func typeRefToTypeExpr(mc paramScope, allCombinators map[string]*Combinator, tlsTypes map[string]*tls.Type, t *TypeRef, bare bool) tls.TypeExpr {
if typeExpr, ok := builtinTypeExprUnions[t.Type.String()]; ok {
return typeExpr
Expand Down
9 changes: 8 additions & 1 deletion internal/tlcodegen/qt_tljson_html.qtpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% package tlcodegen %}

{% import "fmt" %}
{% import "time" %}
{% import "strings" %}

{%- func tlJSON(gen *Gen2, tlgenVersion string) -%}
Expand All @@ -16,7 +17,13 @@
<ul>
<li>tlgen version: {%s tlgenVersion %}</li>
{%- if gen.options.SchemaURL != "" -%}
<li><abbr>TL</abbr> <a href="{%s gen.options.SchemaURL %}">schema</a></li>
<li><abbr>TL</abbr> schema <a href="{%s gen.options.SchemaURL %}">url</a></li>
{%- endif -%}
{%- if gen.options.SchemaCommit != "" -%}
<li><abbr>TL</abbr> schema commit: {%s gen.options.SchemaCommit %}</li>
{%- endif -%}
{%- if gen.options.SchemaTimestamp != 0 -%}
<li><abbr>TL</abbr> schema version: {%v gen.options.SchemaTimestamp %} ({%v time.Unix(int64(gen.options.SchemaTimestamp),0).UTC() %})</li>
{%- endif -%}
<li><abbr>TL</abbr> ⟷ <abbr>JSON</abbr> mapping rules: <a href="https://github.com/VKCOM/tl/blob/master/TLJSON.md">TLJSON.md</a></li>
</ul>
Expand Down
20 changes: 18 additions & 2 deletions internal/tlcodegen/qt_tljson_html.qtpl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion internal/tlcodegen/test/gen/cases/tlgen2_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
tlgen2 version (devel)
tlgen version: (devel)
schema commit:
schema version: 0 (1970-01-01 00:00:00 +0000 UTC)
schema url:
5 changes: 4 additions & 1 deletion internal/tlcodegen/test/gen/goldmaster/tlgen2_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
tlgen2 version (devel)
tlgen version: (devel)
schema commit: abcdefgh
schema version: 301812000 (1979-07-26 04:40:00 +0000 UTC)
schema url: https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl
4 changes: 3 additions & 1 deletion internal/tlcodegen/test/gen/goldmaster/tljson.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<h1>Schema</h1>
<ul>
<li>tlgen version: (devel)</li>
<li><abbr>TL</abbr> <a href="https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl">schema</a></li>
<li><abbr>TL</abbr> schema <a href="https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl">url</a></li>
<li><abbr>TL</abbr> schema version 301812000 (1979-07-26 04:40:00 +0000 UTC)</li>
<li><abbr>TL</abbr> schema commit abcdefgh</li>
<li><abbr>TL</abbr><abbr>JSON</abbr> mapping rules: <a href="https://github.com/VKCOM/tl/blob/master/TLJSON.md">TLJSON.md</a></li>
</ul>
<h1>Functions</h1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
tlgen2 version (devel)
tlgen version: (devel)
schema commit: abcdefgh
schema version: 301812000 (1979-07-26 04:40:00 +0000 UTC)
schema url: https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl
4 changes: 3 additions & 1 deletion internal/tlcodegen/test/gen/goldmaster_nosplit/tljson.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<h1>Schema</h1>
<ul>
<li>tlgen version: (devel)</li>
<li><abbr>TL</abbr> <a href="https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl">schema</a></li>
<li><abbr>TL</abbr> schema <a href="https://github.com/VKCOM/tl/blob/master/internal/tlcodegen/test/tls/goldmaster.tl">url</a></li>
<li><abbr>TL</abbr> schema version 301812000 (1979-07-26 04:40:00 +0000 UTC)</li>
<li><abbr>TL</abbr> schema commit abcdefgh</li>
<li><abbr>TL</abbr><abbr>JSON</abbr> mapping rules: <a href="https://github.com/VKCOM/tl/blob/master/TLJSON.md">TLJSON.md</a></li>
</ul>
<h1>Functions</h1>
Expand Down
46 changes: 20 additions & 26 deletions internal/tlcodegen/tlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sort"
"strings"
"sync/atomic"
"time"

"golang.org/x/exp/slices"

Expand Down Expand Up @@ -46,10 +47,11 @@ const (

const TlJSONHTML = "tljson.html"

// Do not forget to bump version when making changes.
// We do not want repository hash, because it advances automatically each time ANYTHING in repository changes, not only tlgen2.
// And we do not want stable checksum of go files tlgen folder, because checksums are not comparable and there is no idea how old that version is
const buildVersionString = "tlgen2 version "
const buildVersionFormat = `tlgen version: %s
schema url: %s
schema commit: %s
schema version: %d (%v)
`

var (
errSeeHere = fmt.Errorf("see here")
Expand All @@ -66,17 +68,11 @@ type LocalResolveContext struct {
overrideFileName string // used for unions and built-in vectors and tuples, so they are defined in the file of argument
}

func TLGenBuildInfo() (commit string, version string) {
func TLGenVersion() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
commit = setting.Value
break
}
}
version = info.Main.Version
return info.Main.Version
}
return commit, version
return ""
}

// checkArgsCollision checks if passed name is already used in local context.
Expand Down Expand Up @@ -325,6 +321,8 @@ type Gen2Options struct {
GenerateLegacyJsonRead bool
SchemaDocumentation bool
SchemaURL string
SchemaTimestamp uint // for TLO version/date
SchemaCommit string

// C++
RootCPPNamespace string
Expand Down Expand Up @@ -666,7 +664,14 @@ func (gen *Gen2) WriteToDir(outdir string) error {
if len(relativeFiles) != 0 && !relativeFiles[markerFile] {
return fmt.Errorf("outdir %q not empty and has no %q marker file, please clean manually", outdir, markerFile)
}
delete(relativeFiles, markerFile) // special treatment, never delete it
markerContent := fmt.Sprintf(buildVersionFormat,
strings.TrimSpace(TLGenVersion()),
strings.TrimSpace(gen.options.SchemaCommit),
gen.options.SchemaTimestamp, time.Unix(int64(gen.options.SchemaTimestamp), 0).UTC(),
strings.TrimSpace(gen.options.SchemaURL))
if err := gen.addCodeFile(markerFile, markerContent); err != nil {
return err
}
notTouched := 0
written := 0
deleted := 0
Expand All @@ -691,16 +696,6 @@ func (gen *Gen2) WriteToDir(outdir string) error {
return fmt.Errorf("error writing file %q: %w", f, err)
}
}
if len(gen.Code) == 0 || written != 0 || len(relativeFiles) != 0 {
// motivation - do not modify marker file if no code changed. Greatly reduces efforts to refactor tlgen.
f := filepath.Join(outdir, markerFile)
_, version := TLGenBuildInfo()
code := buildVersionString + strings.TrimSpace(version) + "\n" // many editors insist on empty line at the end
written++
if err := os.WriteFile(f, []byte(code), 0644); err != nil {
return fmt.Errorf("error writing file %q: %w", f, err)
}
}
for filepathName := range relativeFiles {
deleted++
f := filepath.Join(outdir, filepathName)
Expand Down Expand Up @@ -1187,8 +1182,7 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
}

if options.SchemaDocumentation {
_, version := TLGenBuildInfo()
if err := gen.addCodeFile(TlJSONHTML, tlJSON(gen, version)); err != nil {
if err := gen.addCodeFile(TlJSONHTML, tlJSON(gen, TLGenVersion())); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit d1d5817

Please sign in to comment.