Skip to content

Commit

Permalink
Compiler fixes (#4215)
Browse files Browse the repository at this point in the history
* use GlobalString to get linguist-path

* don't convert if there's no scope name

* write version file out in JSON output

* new source for version info

It's actually 6.4.0, but I'm making a follow-up commit to bump "back" up to
6.3.9 so we have a single commit bumping the new version file usable as example
in CONTRIBUTING.md.  It makes sense, I swear.

* bump to 6.4.0

* use new commit as example in CONTRIBUTING.md

* fetch version from new VERSION file
  • Loading branch information
Ashe Connor committed Aug 9, 2018
1 parent ed44c0b commit b9c934c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ If you are the current maintainer of this gem:
1. Bump the Gemfile and Gemfile.lock versions for an app which relies on this gem
1. Install the new gem locally
1. Test behavior locally, branch deploy, whatever needs to happen
1. Bump gem version in `lib/linguist/version.rb`, [like this](https://github.com/github/linguist/commit/8d2ea90a5ba3b2fe6e1508b7155aa4632eea2985).
1. Bump gem version in `lib/linguist/VERSION`, [like this](https://github.com/github/linguist/commit/3212355400974ce5f7873a71eb8b85b1c5f4a6d2).
1. Make a PR to github/linguist, [like this](https://github.com/github/linguist/pull/1238).
1. Build a local gem: `bundle exec rake build_gem`
1. Merge github/linguist PR
Expand Down
1 change: 1 addition & 0 deletions lib/linguist/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.4.0
2 changes: 1 addition & 1 deletion lib/linguist/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Linguist
VERSION = "6.4.0"
VERSION = File.read(File.expand_path("../VERSION", __FILE__)).strip
end
6 changes: 3 additions & 3 deletions tools/grammars/cmd/grammar-compiler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
},
},
Action: func(c *cli.Context) error {
conv, err := compiler.NewConverter(c.String("linguist-path"))
conv, err := compiler.NewConverter(c.GlobalString("linguist-path"))
if err != nil {
return wrap(err)
}
Expand All @@ -65,7 +65,7 @@ func main() {
},
},
Action: func(c *cli.Context) error {
conv, err := compiler.NewConverter(c.String("linguist-path"))
conv, err := compiler.NewConverter(c.GlobalString("linguist-path"))
if err != nil {
return wrap(err)
}
Expand All @@ -91,7 +91,7 @@ func main() {
cli.StringFlag{Name: "out, o"},
},
Action: func(c *cli.Context) error {
conv, err := compiler.NewConverter(c.String("linguist-path"))
conv, err := compiler.NewConverter(c.GlobalString("linguist-path"))
if err != nil {
return cli.NewExitError(err, 1)
}
Expand Down
17 changes: 15 additions & 2 deletions tools/grammars/compiler/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type Converter struct {
root string
root string
version string

modified bool
grammars map[string][]string
Expand Down Expand Up @@ -194,6 +195,13 @@ func (conv *Converter) WriteJSON(rulePath string) error {
return err
}

f, err := os.Create(path.Join(rulePath, "version"))
if err != nil {
return err
}
f.Write([]byte(conv.version))
f.Close()

for _, repo := range conv.Loaded {
for scope, file := range repo.Files {
p := path.Join(rulePath, scope+".json")
Expand Down Expand Up @@ -249,12 +257,17 @@ func (conv *Converter) Report() error {
}

func NewConverter(root string) (*Converter, error) {
ver, err := ioutil.ReadFile(path.Join(root, "lib", "linguist", "VERSION"))
if err != nil {
return nil, err
}

yml, err := ioutil.ReadFile(path.Join(root, "grammars.yml"))
if err != nil {
return nil, err
}

conv := &Converter{root: root}
conv := &Converter{root: root, version: strings.TrimSpace(string(ver))}

if err := yaml.Unmarshal(yml, &conv.grammars); err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions tools/grammars/compiler/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ func (err *InvalidRegexError) Error() string {
"Invalid regex in grammar: %s contains a malformed regex (%s)",
err.File, err.Err)
}

type UndeclaredScopeError struct {
Path string
}

func (err *UndeclaredScopeError) Error() string {
return fmt.Sprintf(
"Undeclared scope in grammar: `%s` has no scope name",
err.Path)
}
2 changes: 1 addition & 1 deletion tools/grammars/compiler/loader_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (l *fsLoader) load() {
path = rel
}

rule, unknown, err := ConvertProto(filepath.Ext(path), data)
rule, unknown, err := ConvertProto(path, filepath.Ext(path), data)
if err != nil {
l.Fail(&ConversionError{path, err})
continue
Expand Down
4 changes: 2 additions & 2 deletions tools/grammars/compiler/loader_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (l *urlLoader) loadTarball(r io.Reader) {
}

ext := filepath.Ext(header.Name)
rule, unknown, err := ConvertProto(ext, data)
rule, unknown, err := ConvertProto(header.Name, ext, data)
if err != nil {
l.Fail(&ConversionError{header.Name, err})
continue
Expand Down Expand Up @@ -77,7 +77,7 @@ func (l *urlLoader) load() {

ext := filepath.Ext(l.Source)
filename := filepath.Base(l.Source)
rule, unknown, err := ConvertProto(ext, data)
rule, unknown, err := ConvertProto(l.Source, ext, data)
if err != nil {
l.Fail(&ConversionError{filename, err})
return
Expand Down
6 changes: 5 additions & 1 deletion tools/grammars/compiler/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func filterUnusedKeys(keys []string) (out []string) {
return
}

func ConvertProto(ext string, data []byte) (*grammar.Rule, []string, error) {
func ConvertProto(path, ext string, data []byte) (*grammar.Rule, []string, error) {
var (
raw map[string]interface{}
out grammar.Rule
Expand Down Expand Up @@ -92,5 +92,9 @@ func ConvertProto(ext string, data []byte) (*grammar.Rule, []string, error) {
return nil, nil, err
}

if out.ScopeName == "" {
return nil, nil, &UndeclaredScopeError{path}
}

return &out, filterUnusedKeys(md.Unused), nil
}

0 comments on commit b9c934c

Please sign in to comment.