Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
exports_files([
"dot_app_to_json.sh",
"erl_attrs_to_json.sh",
"rebar_config_to_json.sh",
])

go_library(
Expand Down
6 changes: 4 additions & 2 deletions gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ func importRebar(args language.GenerateArgs, erlangApp *ErlangApp) error {
}

if rebarConfig.ErlOpts != nil {
for _, o := range *rebarConfig.ErlOpts {
erlangApp.ErlcOpts.Add("+" + o)
for _, opt := range *rebarConfig.ErlOpts {
if opt.Kind == "erlc" {
erlangApp.ErlcOpts.Add("+" + opt.Value)
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions gazelle/rebar_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,14 @@ func (p *rebarConfigParser) parseRebarConfig(configFilename string) (*rebarConfi
return &metadata, nil
}

type rebarConfigErlOpt struct {
Kind string `json:"kind"`
Value string `json:"value"`
}

type rebarConfig struct {
Deps []map[string]string `json:"deps"`
ErlOpts *[]string `json:"erl_opts"`
Deps []map[string]string `json:"deps"`
ErlOpts *[]rebarConfigErlOpt `json:"erl_opts"`
}

func (p *rebarConfigParser) parseRebarLock(lockFilename string) (*rebarLock, error) {
Expand Down
54 changes: 49 additions & 5 deletions gazelle/rebar_config_to_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

-export([main/1]).

-ifdef(TEST).
-export([parse/1]).
-endif.

main(Args) ->
case io:get_line("") of
eof ->
Expand All @@ -27,21 +31,61 @@ parse_json_string("\"" ++ Tail) ->
mapify_dep(Name) when is_atom(Name) ->
#{name => Name,
kind => hex};
mapify_dep({Name, _, {git = Kind, Remote, Ref}}) ->
mapify_dep({Name, Version}) when is_list(Version) ->
#{name => Name,
kind => hex,
version => Version};
mapify_dep({Name, Version, {git = Kind, Remote, Ref}}) ->
#{name => Name,
kind => Kind,
version => Version,
remote => Remote,
ref => Ref};
mapify_dep({Name, Version}) ->
mapify_dep({Name, {git = Kind, Remote, Ref}}) ->
#{name => Name,
kind => hex,
version => Version}.
kind => Kind,
remote => Remote,
ref => Ref};
mapify_dep({Name, {hg = Kind, Remote, Ref}}) ->
#{name => Name,
kind => Kind,
remote => Remote,
ref => Ref};
%% legacy formats
mapify_dep({Name, {git = Kind, Remote}}) ->
#{name => Name,
kind => Kind,
remote => Remote};
mapify_dep({Name, Version, {git = Kind, Remote}}) ->
#{name => Name,
kind => Kind,
version => Version,
remote => Remote};
mapify_dep({Name, Version, {git = Kind, Remote, Ref}, [raw]}) ->
#{name => Name,
kind => Kind,
version => Version,
remote => Remote,
ref => Ref}.

conformErlOpt(Opt) when is_atom(Opt)->
#{kind => erlc,
value => Opt};
conformErlOpt({i, Include}) ->
#{kind => include,
value => Include};
conformErlOpt({platform_define, _Platform, _Key}) ->
#{kind => platform_define,
value => ignored};
conformErlOpt({platform_define, _Platform, _Key, _Value}) ->
#{kind => platform_define,
value => ignored}.

conformConfig(List) ->
maps:map(
fun
(erl_opts, Opts) ->
Opts;
[conformErlOpt(O) || O <- Opts];
(deps, Deps) ->
[mapify_dep(D) || D <- Deps];
(_, _) ->
Expand Down
5 changes: 1 addition & 4 deletions private/erlang_bytecode2.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ fi
include_args = " ".join(include_args),
pa_args = " ".join(pa_args),
out_dir = out_dir,
erlc_opts = " ".join([
"'{}'".format(opt)
for opt in ctx.attr.erlc_opts[ErlcOptsInfo].values
]),
erlc_opts = " ".join(ctx.attr.erlc_opts[ErlcOptsInfo].values),
)

inputs = depset(
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ erlang_package = use_extension(

erlang_package.hex_package(
name = "thoas",
build_file = "@//dot_app_to_json:BUILD.thoas",
build_file = "@//:BUILD.thoas",
sha256 = "442296847aca11db8d25180693d7ca3073d6d7179f66952f07b16415306513b6",
version = "0.4.0",
)
Expand Down
5 changes: 1 addition & 4 deletions test/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ http_archive(
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

http_archive(
name = "io_buildbuddy_buildbuddy_toolchain",
sha256 = "a2a5cccec251211e2221b1587af2ce43c36d32a42f5d881737db3b546a536510",
Expand Down Expand Up @@ -110,7 +107,7 @@ load("@rules_erlang//:hex_pm.bzl", "hex_pm_erlang_app")

hex_pm_erlang_app(
name = "thoas",
build_file = "@//dot_app_to_json:BUILD.thoas",
build_file = "@//:BUILD.thoas",
sha256 = "442296847aca11db8d25180693d7ca3073d6d7179f66952f07b16415306513b6",
version = "0.4.0",
)
111 changes: 111 additions & 0 deletions test/rebar_config_to_json/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
load(
"@rules_erlang//:app_file.bzl",
"app_file",
)
load(
"@rules_erlang//:erlang_bytecode.bzl",
"erlang_bytecode",
)
load(
"@rules_erlang//:erlang_app_info.bzl",
"erlang_app_info",
)
load(
"@rules_erlang//:erlang_app.bzl",
"DEFAULT_ERLC_OPTS",
"DEFAULT_TEST_ERLC_OPTS",
)
load(
"@rules_erlang//:xref2.bzl",
"xref",
)
load(
"@rules_erlang//:dialyze.bzl",
"DEFAULT_PLT_APPS",
"dialyze",
"plt",
)
load(
"@rules_erlang//:ct.bzl",
"assert_suites2",
"ct_suite",
)

genrule(
name = "src",
srcs = ["@rules_erlang//gazelle:rebar_config_to_json.sh"],
outs = ["src/rebar_config_to_json.erl"],
cmd = """\
echo "-module(rebar_config_to_json)." > $@
tail -n +4 $< >> $@
""",
)

APP_NAME = "rebar_config_to_json"

APP_VERSION = "1.0.0"

erlang_bytecode(
name = "beam_files",
srcs = ["src/rebar_config_to_json.erl"],
dest = "ebin",
erlc_opts = DEFAULT_ERLC_OPTS,
)

erlang_bytecode(
name = "test_beam_files",
testonly = True,
srcs = ["src/rebar_config_to_json.erl"],
dest = "test",
erlc_opts = DEFAULT_TEST_ERLC_OPTS + [
"+nowarn_export_all",
],
)

app_file(
name = "app_file",
app_name = APP_NAME,
app_version = APP_VERSION,
)

erlang_app_info(
name = "erlang_app",
srcs = ["src/rebar_config_to_json.erl"],
app = ":app_file",
app_name = APP_NAME,
beam = [":beam_files"],
)

erlang_app_info(
name = "test_erlang_app",
testonly = True,
srcs = ["src/rebar_config_to_json.erl"],
app = ":app_file",
app_name = APP_NAME,
beam = [":test_beam_files"],
)

xref()

plt(
name = "base_plt",
apps = DEFAULT_PLT_APPS,
)

dialyze(
plt = ":base_plt",
)

ct_suite(
name = "rebar_config_to_json_SUITE",
size = "small",
data = [
# https://github.com/erlang/rebar3/blob/main/rebar.config.sample
"test/rebar.config",
],
runtime_deps = [
"@thoas//:erlang_app",
],
)

assert_suites2()
Loading