diff options
Diffstat (limited to 'bootstrap')
-rwxr-xr-x | bootstrap | 79 |
1 files changed, 60 insertions, 19 deletions
@@ -4,6 +4,14 @@ main(_Args) -> + application:start(crypto), + application:start(asn1), + application:start(public_key), + application:start(ssl), + inets:start(), + inets:start(httpc, [{profile, rebar}]), + set_httpc_options(), + %% Fetch and build deps required to build rebar3 BaseDeps = [{providers, []} ,{getopt, []} @@ -24,6 +32,7 @@ main(_Args) -> setup_env(), os:putenv("REBAR_PROFILE", "bootstrap"), + rebar3:run(["update"]), {ok, State} = rebar3:run(["compile"]), reset_env(), os:putenv("REBAR_PROFILE", ""), @@ -56,28 +65,58 @@ fetch_and_compile({Name, ErlFirstFiles}, Deps) -> ok = fetch(Repo, Name), compile(Name, ErlFirstFiles). -fetch({git, Url, Source}, App) -> +fetch({pkg, Name, Vsn}, App) -> Dir = filename:join([filename:absname("_build/default/lib/"), App]), - case filelib:is_dir(Dir) of - true -> - true = code:add_path(filename:join(Dir, "ebin")), - ok; - false -> - fetch_source(Dir, Url, Source), - ok + CDN = "https://s3.amazonaws.com/s3.hex.pm/tarballs", + Package = binary_to_list(<<Name/binary, "-", Vsn/binary, ".tar">>), + Url = string:join([CDN, Package], "/"), + case request(Url) of + {ok, Binary} -> + {ok, Contents} = extract(Binary), + ok = erl_tar:extract({binary, Contents}, [{cwd, Dir}, compressed]); + _ -> + io:format("Error: Unable to fetch package ~p ~p~n", [Name, Vsn]) + end. + +extract(Binary) -> + {ok, Files} = erl_tar:extract({binary, Binary}, [memory]), + {"contents.tar.gz", Contents} = lists:keyfind("contents.tar.gz", 1, Files), + {ok, Contents}. + +request(Url) -> + case httpc:request(get, {Url, []}, + [{relaxed, true}], + [{body_format, binary}], + rebar) of + {ok, {{_Version, 200, _Reason}, _Headers, Body}} -> + {ok, Body}; + Error -> + Error end. -fetch_source(Dir, Url, {ref, Ref}) -> - ok = filelib:ensure_dir(Dir), - os:cmd(io_lib:format("git clone ~s ~s", [Url, Dir])), - {ok, Cwd} = file:get_cwd(), - file:set_cwd(Dir), - os:cmd(io_lib:format("git checkout -q ~s", [Ref])), - file:set_cwd(Cwd); -fetch_source(Dir, Url, {_, Branch}) -> - ok = filelib:ensure_dir(Dir), - os:cmd(io_lib:format("git clone ~s ~s -b ~s --single-branch", - [Url, Dir, Branch])). +get_rebar_config() -> + {ok, [[Home]]} = init:get_argument(home), + ConfDir = filename:join(Home, ".config/rebar3"), + case file:consult(filename:join(ConfDir, "rebar.config")) of + {ok, Config} -> + Config; + _ -> + [] + end. + +get_http_vars(Scheme) -> + proplists:get_value(Scheme, get_rebar_config(), []). + +set_httpc_options() -> + set_httpc_options(https_proxy, get_http_vars(https_proxy)), + set_httpc_options(proxy, get_http_vars(http_proxy)). + +set_httpc_options(_, []) -> + ok; + +set_httpc_options(Scheme, Proxy) -> + {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), + httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar). compile(App, FirstFiles) -> Dir = filename:join(filename:absname("_build/default/lib/"), App), @@ -86,6 +125,7 @@ compile(App, FirstFiles) -> FirstFilesPaths = [filename:join([Dir, "src", Module]) || Module <- FirstFiles], Sources = FirstFilesPaths ++ filelib:wildcard(filename:join([Dir, "src", "*.erl"])), [compile_file(X, [{i, filename:join(Dir, "include")} + ,debug_info ,{outdir, filename:join(Dir, "ebin")} ,return | additional_defines()]) || X <- Sources]. @@ -219,6 +259,7 @@ setup_env() -> application:load(rebar), {ok, Providers} = application:get_env(rebar, providers), Providers1 = Providers -- [rebar_prv_release, + rebar_prv_relup, rebar_prv_tar], application:set_env(rebar, providers, Providers1). |