Skip to content

Commit

Permalink
handle .couchappignore. While I'm here, add the possibility to set
Browse files Browse the repository at this point in the history
global patterns to ignore in ~/.couchapp.conf .
  • Loading branch information
benoitc committed Dec 4, 2010
1 parent 3fb099e commit f5fea28
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 270 deletions.
Binary file modified couchapp
Binary file not shown.
2 changes: 2 additions & 0 deletions include/couchapp.hrl
Expand Up @@ -7,6 +7,7 @@
opts,
dbs=[],
hooks=[],
ignore=[],
extensions=[] }).

-record(global_state, { working_dir }).
Expand All @@ -25,6 +26,7 @@
-define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))).

-record(couchapp, {
config,
path,
att_dir,
docid,
Expand Down
51 changes: 38 additions & 13 deletions src/couchapp_config.erl
Expand Up @@ -9,6 +9,7 @@
-include("couchapp.hrl").

-export([new/0, new/1,
update/2,
get_db/2,
get/2, get/3,
set/3,
Expand All @@ -27,13 +28,39 @@ new(Options) ->
false ->
{[]}
end,
{Dbs, Hooks, Extensions} = parse_conf(UserConf),
{Dbs, Hooks, Extensions, Ignore} = parse_conf(UserConf),

#config { dir = couchapp_util:get_cwd(),
opts = Options,
dbs = Dbs,
hooks = Hooks,
extensions = Extensions }.
extensions = Extensions,
ignore = Ignore }.


update(AppDir, #config{dbs=Dbs, hooks=Hooks, extensions=Extensions,
ignore=Ignore}=Config) ->
RcFile = filename:join(AppDir, ".couchapprc"),

%% load .couchapprc
AppConf = case filelib:is_regular(RcFile) of
true ->
{ok, Bin} = file:read_file(RcFile),
couchbeam_util:json_decode(Bin);
false ->
{[]}
end,

%% update conf from .couchapprc
{Dbs1, Hooks1, Extensions1, Ignore1} = parse_conf(AppConf),
Config1 = Config#config { dbs = Dbs ++ Dbs1,
hooks = Hooks ++ Hooks1,
extensions = Extensions ++ Extensions1,
ignore = Ignore ++ Ignore1},

%% get ignore file patterns.
couchapp_ignore:init(AppDir, Config1).


get_db(Config, DbString) ->
proplists:get_value(DbString, Config#config.dbs).
Expand All @@ -59,9 +86,8 @@ set(Config, Key, Value) ->
Opts = proplists:delete(Key, Config#config.opts),
Config#config { opts = [{Key, Value} | Opts] }.


parse_conf({[]}) ->
{[], [], []};
{[], [], [], []};
parse_conf(Conf) ->
Dbs = case couchbeam_doc:get_value(<<"env">>, Conf) of
undefined -> [];
Expand All @@ -74,13 +100,19 @@ parse_conf(Conf) ->
[{couchapp_util:v2a(Mod), couchapp_util:v2a(Command)}
|| {Mod, Command} <- Ext]
end,
Hooks = case couchbeam_doc:get_value(<<"Hooks">>, Conf) of
Hooks = case couchbeam_doc:get_value(<<"hooks">>, Conf) of
undefined ->
[];
H ->
[{list_to_atom(Hook), Scripts} || {Hook, Scripts} <- H]
end,
{Dbs, Hooks, Extensions}.
Ignore = case couchbeam_doc:get_value(<<"ignore">>, Conf) of
undefined ->
[];
I ->
I
end,
{Dbs, Hooks, Extensions, Ignore}.

get_config_dbs([], Dbs) ->
Dbs;
Expand All @@ -101,10 +133,3 @@ get_config_dbs([{Name, Obj}|Rest], Dbs) ->
end,
get_config_dbs(Rest, [{Name, Db1}|Dbs])
end.







4 changes: 0 additions & 4 deletions src/couchapp_core.erl
Expand Up @@ -41,13 +41,11 @@ run(RawArgs) ->

process_commands(Commands, Options).


process_commands([Command|Args], Options) ->
{ok, Modules} = application:get_env(couchapp, modules),
Config = couchapp_config:new(Options),
execute(list_to_atom(Command), Args, Modules, Config).


execute(Command, Args, Modules, Config) ->
case select_modules(Modules, Command, []) of
[] ->
Expand Down Expand Up @@ -109,7 +107,6 @@ parse_args(Args) ->
halt(1)
end.


%%
%% options accepted via getopt
%%
Expand All @@ -130,7 +127,6 @@ option_spec_list() ->

].


%%
%% set global flag based on getopt option boolean value
%%
Expand Down
63 changes: 63 additions & 0 deletions src/couchapp_ignore.erl
@@ -0,0 +1,63 @@
%%% -*- erlang -*-
%%%
%%% This file is part of couchapp released under the Apache 2 license.
%%% See the NOTICE for more information.

-module(couchapp_ignore).

-author('Benoît Chesneau <benoitc@e-engura.org>').

-include("couchapp.hrl").


-export([init/2,
ignore/2]).

%% ====================================================================
%% Public API
%% ====================================================================

init(AppDir, #config{ignore=OldIgnore}=Config) ->
IgnoreFile = filename:join(AppDir, ".couchappignore"),
Ignore = case filelib:is_regular(IgnoreFile) of
true ->
load(IgnoreFile);
_ ->
[]
end,
Config#config{ignore=OldIgnore ++ Ignore}.

ignore(Path, #config{ignore=Ignore}) ->
ignore1(Ignore, Path).

ignore1([], _Path) ->
false;
ignore1([Pattern|Rest], Path) ->
case re:run(Path, Pattern, [global, caseless, unicode, multiline,
{capture, all, binary}]) of
nomatch ->
ignore1(Rest, Path);
_ ->
?DEBUG("File '~p' ignored.~n", [Path]),
true
end.

%% ====================================================================
%% Internal functions
%% ====================================================================

load(File) ->
case file:read_file(File) of
{ok, Bin} ->
Bin1 = remove_comments(Bin),
couchbeam_util:json_decode(Bin1);
Error ->
?ERROR("can't read '~p' [~p]~n", [File, Error]),
[]
end.


remove_comments(Content) ->
P = "(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)",
re:replace(Content, P, "").

0 comments on commit f5fea28

Please sign in to comment.