Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed May 26, 2015
0 parents commit aefe9ec
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
_rel
_deps
_plugins
_tdeps
logs
_build
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (c) 2015, Tristan Sloughter <t@crashfast.com>.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rebar3_cuttlefish
=====

A rebar plugin

Build
-----

$ rebar3 compile

Use
---

Add the plugin to your rebar config:

{plugins, [
{ rebar3_cuttlefish, ".*", {git, "git@host:user/rebar3_cuttlefish.git", {tag, "0.1.0"}}}
]}.

Then just call your plugin directly in an existing application:


$ rebar3 rebar3_cuttlefish
===> Fetching rebar3_cuttlefish
===> Compiling rebar3_cuttlefish
<Plugin Output>
3 changes: 3 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{erl_opts, [debug_info]}.
{deps, [{providers, "1.3.1"}
,{cuttlefish, {git, "git://github.com/tsloughter/cuttlefish", {branch, "develop"}}}]}.
21 changes: 21 additions & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[{<<"providers">>,{pkg,<<"providers">>,<<"1.3.1">>},0},
{<<"goldrush">>,
{git,"git://github.com/DeadZen/goldrush.git",
{ref,"71e63212f12c25827e0c1b4198d37d5d018a7fec"}},
2},
{<<"neotoma">>,
{git,"git://github.com/seancribbs/neotoma.git",
{ref,"760928ec8870da02eb11bccb501e2700925d06c6"}},
1},
{<<"lager">>,
{git,"git://github.com/basho/lager.git",
{ref,"b6b6cebcb27ccff8acc59ae775acebc2f52e4926"}},
1},
{<<"getopt">>,
{git,"git://github.com/jcomellas/getopt.git",
{ref,"659a28f4145bc9843598972854299dc4ea77e4cb"}},
1},
{<<"cuttlefish">>,
{git,"git://github.com/tsloughter/cuttlefish",
{ref,"bf99e43b68badc8d6e706b3c72663c200d2e1e41"}},
0}].
13 changes: 13 additions & 0 deletions src/rebar3_cuttlefish.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{application, 'rebar3_cuttlefish',
[{description, "A rebar plugin"},
{vsn, "0.1.0"},
{registered, []},
{applications,
[kernel,
stdlib,
providers,
cuttlefish
]},
{env,[]},
{modules, []}
]}.
74 changes: 74 additions & 0 deletions src/rebar3_cuttlefish.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-module(rebar3_cuttlefish).
-behaviour(provider).

-export([init/1
,do/1
,format_error/1]).

-include_lib("providers/include/providers.hrl").

-define(PROVIDER, cuttlefish).
-define(DEPS, [app_discovery]).

%% ===================================================================
%% Public API
%% ===================================================================
-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
Provider = providers:create([
{name, ?PROVIDER}, % The 'user friendly' name of the task
{module, ?MODULE}, % The module implementation of the task
{bare, true}, % The task can be run by the user, always true
{deps, ?DEPS}, % The list of dependencies
{example, "rebar3 cuttlefish"}, % How to use the plugin
{opts, []}, % list of options understood by the plugin
{short_desc, "Rebar3 cuttlefis plugin"},
{desc, ""}
]),
{ok, rebar_state:add_provider(State, Provider)}.


-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
Relx = rebar_state:get(State, relx, []),
ReleaseDir = filename:join(rebar_dir:base_dir(State), "rel"),

{release, {Name, _Vsn}, _} = lists:keyfind(release, 1, Relx),
TargetDir = filename:join([ReleaseDir, Name]),
case lists:keyfind(overlay, 1, Relx) of
{overlay, Overlays} when is_list(Overlays) ->
Schemas = schemas(Overlays, TargetDir),

case cuttlefish_schema:files(Schemas) of
{errorlist, _Es} ->
%% These errors were already printed
error;
{_Translations, Mappings, _Validators} ->
make_default_file(State, Name, TargetDir, Mappings)
end,
{ok, State};
false ->
{ok, State};
_ ->
?PRV_ERROR({bad_overlay_entry})

end.

-spec format_error(any()) -> iolist().
format_error({bad_overlay_entry}) ->
io_lib:format("{overlay, [...]} entry in relx config must be a list.~n", []).

make_default_file(State, Name, TargetDir, Mappings) ->
File = rebar_state:get(State, cuttlefish_filename, io_lib:format("~s.conf", [Name])),
Filename = filename:join([TargetDir, "etc", File]),
cuttlefish_conf:generate_file(Mappings, Filename).

schemas(Overlays, TargetDir) ->
SchemaOverlays =
lists:filter(fun(Overlay) ->
element(1, Overlay) =:= template
andalso filename:extension(element(3, Overlay)) =:= ".schema"
end, Overlays),

lists:sort([lists:flatten(filename:join(TargetDir, element(3, Schema)))
|| Schema <- SchemaOverlays]).

0 comments on commit aefe9ec

Please sign in to comment.