Permalink
Browse files

Added -b/--browser option that will open browser to show the specific…

…ation in the browser
  • Loading branch information...
yrashk committed Jan 27, 2011
1 parent 5d8cb9d commit 7e7089d3f02c4c510a7657f36b61544e89f8234a
Showing with 78 additions and 8 deletions.
  1. +4 −1 README.md
  2. +2 −0 include/agner.hrl
  3. +1 −0 include/agner_index.hrl
  4. +0 −2 include/agner_spec.hrl
  5. +29 −2 src/agner.erl
  6. +4 −2 src/agner_github.erl
  7. +2 −1 src/agner_index.erl
  8. +25 −0 src/agner_server.erl
  9. +11 −0 src/agner_utils.erl
View
@@ -65,13 +65,16 @@ Will list all agner-packages. With the `-d` or `--descriptions`
option, it will also print out the descriptions of the packages, for
easy grepping to find relevant packages.
- agner spec PACKAGE [-v/--version package_version]
+ agner spec PACKAGE [-v/--version package_version] [-b/--browser]
Will print a specification of a given package on stdout. If the
optional version constraint is given (for example `agner spec gproc -v
@release`) then the output is of that version. By default, the
`@master` flavour is chosen.
+If `-b` or `--browser` is used, it will also open browser with the specification
+file in its respective `.agner` repository.
+
agner fetch PACKAGE [DESTDIR] [-v/--version package_version]
Fetch a given `PACKAGE` to either the current directory or,
View
@@ -15,4 +15,6 @@
-type directory() :: string().
+-type url() :: string().
+
-include_lib("agner_spec.hrl").
View
@@ -2,3 +2,4 @@
-spec tags(agner_repo()) -> list({agner_repo_tag(),sha1()}) | not_found_error().
-spec branches(agner_repo()) -> list({agner_repo_branch(), sha1()}) | not_found_error().
-spec spec(agner_repo(), sha1()) -> agner_spec() | not_found_error().
+-spec spec_url(agner_repo(), sha1()) -> url() | not_found_error().
View
@@ -1,5 +1,3 @@
--type url() :: string().
-
-type git_ref() :: sha1() |
{branch, string()} |
{tag, string()}.
View
@@ -3,7 +3,7 @@
-export([start/0,stop/0]).
-export([main/1]).
%% API
--export([spec/1, spec/2, index/0, fetch/2, fetch/3, versions/1]).
+-export([spec/1, spec/2, spec_url/1, spec_url/2, index/0, fetch/2, fetch/3, versions/1]).
start() ->
inets:start(),
@@ -20,6 +20,7 @@ stop() ->
main(["spec"|Args]) ->
OptSpec = [
{package, undefined, undefined, string, "Package name"},
+ {browser, $b, "browser", boolean, "Show specification in the browser"},
{version, $v, "version", {string, "@master"}, "Version"}
],
start(),
@@ -28,7 +29,15 @@ main(["spec"|Args]) ->
undefined ->
io:format("ERROR: Package name required.~n");
Package ->
- io:format("~p~n",[spec(Package,proplists:get_value(version, Opts))])
+ Version = proplists:get_value(version, Opts),
+ case proplists:get_value(browser, Opts) of
+ true ->
+ agner_utils:launch_browser(spec_url(Package, Version));
+ false ->
+ ignore
+ end,
+
+ io:format("~p~n",[spec(Package,Version)])
end,
stop();
@@ -109,6 +118,24 @@ spec(Name, Version) when is_list(Version) ->
spec(Name, Version) ->
gen_server:call(agner_server, {spec, Name, Version}).
+-spec spec_url(agner_spec_name()) -> url() | not_found_error().
+-spec spec_url(agner_spec_name(), agner_spec_version() | string()) -> url() | not_found_error().
+
+spec_url(Name) ->
+ spec_url(Name, {branch, "master"}).
+
+spec_url(Name, Version) when is_atom(Name) ->
+ spec_url(atom_to_list(Name),Version);
+
+spec_url(Name, Version) when is_list(Version) ->
+ spec_url(Name, agner_spec:list_to_version(Name, Version));
+
+spec_url(Name, Version) ->
+ gen_server:call(agner_server, {spec_url, Name, Version}).
+
+
+
+
-spec index() -> list(agner_spec_name()).
index() ->
View
@@ -7,7 +7,8 @@
repository/1,
tags/1,
branches/1,
- spec/2
+ spec/2,
+ spec_url/2
]).
repositories() ->
@@ -106,7 +107,8 @@ spec_1(Name, SHA1, AtFilename) ->
S
end.
-
+spec_url(Name, SHA1) ->
+ "https://github.com/" ++ proper_repo_name(Name) ++ "/blob/" ++ SHA1 ++ "/agner.config".
%%%
View
@@ -4,7 +4,8 @@
-spec behaviour_info(atom()) -> undefined | list({atom(), arity()}).
behaviour_info(callbacks) ->
- [{repositories,0}, {repository, 1}, {tags,1}, {branches, 1}, {spec, 2}];
+ [{repositories,0}, {repository, 1}, {tags,1}, {branches, 1}, {spec, 2},
+ {spec_url, 2}];
behaviour_info(_Other) ->
undefined.
View
@@ -70,13 +70,15 @@ init([]) ->
%% @end
%%--------------------------------------------------------------------
-type agner_call_spec() :: {spec, agner_spec_name(), agner_spec_version()}.
+-type agner_call_spec_url() :: {spec_url, agner_spec_name(), agner_spec_version()}.
-type agner_call_index() :: index.
-type agner_call_fetch() :: {fetch, agner_spec_name(), agner_spec_version(), directory()}.
-type agner_call_versions() :: {versions, agner_spec_name()}.
-type agner_internal_call_pushed_at_updates() :: {pushed_at_updates, list({agner_spec_name(), string()})}.
-type agner_internal_call_pushed_at() :: {pushed_at, agner_spec_name()}.
-spec handle_call(agner_call_spec(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(agner_spec()|{error, bad_version}) ;
+ (agner_call_spec_url(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(url()|{error, bad_version}) ;
(agner_call_index(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(list(agner_spec_name())) ;
(agner_call_fetch(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(ok | {error, any()}) ;
(agner_call_versions(), gen_server_from(), gen_server_state()) -> gen_server_async_reply(list(agner_spec_version()) | not_found_error());
@@ -90,6 +92,12 @@ handle_call({spec, Name, Version}, From, #state{}=State) ->
end),
{noreply, State};
+handle_call({spec_url, Name, Version}, From, #state{}=State) ->
+ spawn_link(fun () ->
+ handle_spec_url(Name, Version, From, indices())
+ end),
+ {noreply, State};
+
handle_call(index, From, #state{}=State) ->
spawn_link(fun () ->
handle_index(From, [], indices())
@@ -203,6 +211,23 @@ handle_spec(Name, Version, From, [Mod0|Rest]) ->
gen_server:reply(From, {error, bad_version})
end.
+-spec handle_spec_url(agner_spec_name(), agner_spec_version(), gen_server_from(), agner_indices()) -> any().
+handle_spec_url(_,_,From,[]) ->
+ gen_server:reply(From, {error, not_found});
+handle_spec_url(Name, Version, From, [Mod0|Rest]) ->
+ Mod = index_module(Mod0),
+ case sha1(Mod, Name, Version) of
+ SHA1 when is_list(SHA1) ->
+ case Mod:spec_url(Name, SHA1) of
+ {error, not_found} ->
+ handle_spec_url(Name, Version, From, Rest);
+ URL ->
+ gen_server:reply(From, URL)
+ end;
+ _ ->
+ gen_server:reply(From, {error, bad_version})
+ end.
+
-spec handle_index(gen_server_from(), list(agner_spec_name()), list(tuple())) -> any().
handle_index(From, Acc, []) ->
Repos = lists:reverse(Acc),
View
@@ -0,0 +1,11 @@
+-module(agner_utils).
+-export([launch_browser/1]).
+
+launch_browser(URL) ->
+ case os:type() of
+ {unix, darwin} ->
+ os:cmd("open " ++ URL);
+ {unix, linux} ->
+ os:cmd("firefox " ++ URL ++ " &")
+ end.
+

0 comments on commit 7e7089d

Please sign in to comment.