Skip to content

Commit 18c5b52

Browse files
authored
add optional gomodules support (#54)
* add support for go modules * add tests for gomodules * bump version
1 parent a196f01 commit 18c5b52

File tree

11 files changed

+174
-79
lines changed

11 files changed

+174
-79
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.4.10
2+
3+
- Support gomodules using the optional '-m' flag.
4+
15
# 1.4.9
26

37
- Add nullSerializable decorator that always emits a field, even with a null value, instead of sometimes omitting it

lib/go_emit.js

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/main.js

Lines changed: 117 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "avdl-compiler",
3-
"version": "1.4.9",
3+
"version": "1.4.10",
44
"description": "AVDL to Go compiler written in IcedCoffeeScript (for node)",
55
"main": "lib/main.js",
66
"scripts": {
@@ -37,4 +37,4 @@
3737
"lodash": "4.17.15",
3838
"minimist": "^1.2.0"
3939
}
40-
}
40+
}

src/go_emit.iced

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ is_one_way = (d) -> (d.notify? or d.oneway)
1414
#====================================================================
1515

1616
exports.GoEmitter = class GoEmitter extends BaseEmitter
17-
constructor : () ->
17+
constructor : (@gomod_path, @gomod_dir) ->
1818
super
1919
@_tab_char = '\t'
2020
@_pkg = null
@@ -588,9 +588,13 @@ exports.GoEmitter = class GoEmitter extends BaseEmitter
588588
@output '"github.com/keybase/go-framed-msgpack-rpc/rpc"'
589589
@output 'context "golang.org/x/net/context"' if Object.keys(messages).length > 0
590590

591-
prefix = process.env.GOPATH + '/src/'
592-
relative_file = path_lib.resolve(outfile).replace(prefix, "")
593-
relative_dir = path_lib.dirname(relative_file)
591+
if @gomod_path and @gomod_dir
592+
relative_file = path_lib.resolve(outfile).replace(@gomod_dir, "")
593+
relative_dir = @gomod_path + path_lib.dirname(relative_file)
594+
else
595+
prefix = process.env.GOPATH + '/src/'
596+
relative_file = path_lib.resolve(outfile).replace(prefix, "")
597+
relative_dir = path_lib.dirname(relative_file)
594598

595599
for {import_as, path} in imports when path.indexOf('/') >= 0
596600
if path.match /(\.\/|\.\.\/)/

src/go_emit.test.iced

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{GoEmitter} = require("./go_emit");
22
pkg = require '../package.json'
3+
path_lib = require 'path'
34

4-
describe "GoEmitter", () ->
5+
describe.each([
6+
["Go Modules disabled", "", ""],
7+
["Go Modules enabled", "github.com/keybase/node-avdl-compiler", path_lib.resolve()]
8+
]) 'GoEmitter - %s', (_, gomod_path, gomod_dir) ->
59
emitter = null
610
beforeEach () ->
7-
emitter = new GoEmitter
11+
emitter = new GoEmitter gomod_path, gomod_dir
812

913
describe "emit_preface", () ->
1014
it "Should emit a preface", () ->

src/main.iced

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ avdl2json = require 'avdl2json'
77
fs = require 'fs'
88
pathmod = require 'path'
99
{mergeWith} = require 'lodash'
10+
{exec} = require 'child_process'
1011

1112
#================================================
1213

1314
usage = () ->
1415
console.error """AVDL Compiler
1516
16-
#{' '}single file: avdlc -l <lang> [-t] -i <infile> -o <outfile>
17-
#{' '}batch: avdlc -l <lang> [-t] -b -o <outdir> <infiles...>
17+
#{' '}single file: avdlc -l <lang> [-t] [-m] -i <infile> -o <outfile>
18+
#{' '}batch: avdlc -l <lang> [-t] [-m] -b -o <outdir> <infiles...>
1819
1920
avdlc can run in either batch or single-file mode. Specify which language
2021
to output code in. Currently, only "go" is fully supported. TypeScript and Python are partially supported.
2122
2223
Use -t to only print types and ignore function definitions.
24+
Use -m to set gomodules support
2325
"""
2426

2527
#================================================
2628

27-
emit = ( { infiles, outfile, json, lang, types_only }, cb) ->
29+
emit = ( { infiles, outfile, json, lang, types_only, gomod_path, gomod_dir }, cb) ->
2830
emitter = switch lang
29-
when "go" then new GoEmitter()
31+
when "go" then new GoEmitter(gomod_path, gomod_dir)
3032
when "typescript" then new TypescriptEmitter()
3133
when "python" then new PythonEmitter()
3234
else throw new Error "Unrecognized language: #{@lang}"
@@ -66,12 +68,14 @@ exports.Main = class Main
6668
err = new Error "usage: shown!"
6769
else if (@batch = argv.b)
6870
@types_only = argv.t
71+
@gomod_enabled = argv.m
6972
@outdir = argv.o
7073
@infiles = argv._
7174
unless @outdir? and @infiles.length
7275
err = new Error "need an [-o <outdir>] and input files in batch mode"
7376
else
7477
@types_only = argv.t
78+
@gomod_enabled = argv.m
7579
@outfile = argv.o
7680
@infile = argv.i
7781
unless @outfile? and @infile?
@@ -104,14 +108,24 @@ exports.Main = class Main
104108

105109
#---------------
106110

111+
get_gomod : (opts, cb) ->
112+
esc = make_esc cb, "get_gomod"
113+
cwd = if @outdir? then @outdir else pathmod.dirname(@outfile)
114+
await exec 'go list -m -json', {cwd}, esc defer stdout
115+
@gomod_path = JSON.parse(stdout).Path
116+
@gomod_dir = JSON.parse(stdout).Dir
117+
cb null
118+
119+
#---------------
120+
107121
do_file : ({infile, outfile}, cb) ->
108122
esc = make_esc cb, "do_file"
109123
if @clean
110124
await fs.unlink outfile, defer err
111125
console.log "Deleting #{outfile}" unless err?
112126
else
113127
await avdl2json.parse { infile, version : 2 }, esc defer ast
114-
await emit { infiles: [infile], outfile, json : ast.to_json(), @types_only, @lang }, esc defer code
128+
await emit { infiles: [infile], outfile, json : ast.to_json(), @types_only, @lang, @gomod_path, @gomod_dir }, esc defer code
115129
await output { code, outfile }, esc defer()
116130
console.log "Compiling #{infile} -> #{outfile}"
117131
cb null
@@ -127,7 +141,7 @@ exports.Main = class Main
127141
for infile in @infiles
128142
await avdl2json.parse { infile, version: 2 }, esc defer ast
129143
merge_asts json, ast.to_json()
130-
await emit { @infiles, json, outfile, types_only: true, @lang }, esc defer code
144+
await emit { @infiles, json, outfile, types_only: true, @lang, @gomod_path, @gomod_dir }, esc defer code
131145
await output { code, outfile }, esc defer()
132146
console.log "Compiling #{@infiles} -> #{outfile}"
133147

@@ -161,6 +175,8 @@ exports.Main = class Main
161175
main : ({argv}, cb) ->
162176
esc = make_esc cb, "main"
163177
await @parse_argv {argv}, esc defer()
178+
if @gomod_enabled
179+
await @get_gomod {}, esc defer()
164180
if @batch
165181
await @do_batch_mode {}, esc defer()
166182
else

test/files/sample.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Auto-generated to Go types and interfaces using avdl-compiler v1.4.9 (https://github.com/keybase/node-avdl-compiler)
1+
// Auto-generated to Go types and interfaces using avdl-compiler v1.4.10 (https://github.com/keybase/node-avdl-compiler)
22
// Input file: avdl/sample.avdl
33

44
package sample1

test/files/sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""sample.1
22
3-
Auto-generated to Python types by avdl-compiler v1.4.9 (https://github.com/keybase/node-avdl-compiler)
3+
Auto-generated to Python types by avdl-compiler v1.4.10 (https://github.com/keybase/node-avdl-compiler)
44
Input files:
55
- avdl/sample.avdl
66
"""

0 commit comments

Comments
 (0)