Commits

Andreas Stenius committed 47918c3

Clone hg repository implemented.

Comments (0)

Files changed (5)

actions/action_zmr_clone_repo.erl

+%%% @author Andreas Stenius <andreas.stenius@astekk.se>
+%%% @copyright (C) 2011, Andreas Stenius
+%%% @doc
+%%%
+%%% @end
+%%% Created :  5 Feb 2011 by Andreas Stenius <andreas.stenius@astekk.se>
+
+-module(action_zmr_clone_repo).
+-author("Andreas Stenius <andreas.stenius@astekk.se>").
+
+-export([
+	 render_action/4,
+	 event/2,
+	 clone_repo/3
+	]).
+
+-include_lib("zotonic.hrl").
+
+render_action(TriggerId, TargetId, Args, Context) ->
+    RepoId = proplists:get_value(id, Args),
+    {PostbackMsgJS, _PickledPostback} = z_render:make_postback({clone_repo, RepoId}, click, TriggerId, TargetId, ?MODULE, Context),
+    {PostbackMsgJS, Context}.
+
+event({postback, {clone_repo, RepoId}, _TriggerId, TargetId}, Context) ->
+    z_session_page:spawn_link(?MODULE, clone_repo, [TargetId, RepoId, Context], Context),
+    Context.
+
+
+clone_repo(TargetId, RepoId, Context) ->
+    Context1 = update_progress(TargetId, [{message, "Cloning repository..."}], Context),
+    Source = m_rsc:p(RepoId, zmr_repository_path, Context1),
+    Target = mod_zmr:repo_path(RepoId, Context1),
+    ToolId = m_edge:object(RepoId, zmr_scm, 1, Context1),
+    Cmd = z_convert:to_list(m_rsc:p(ToolId, zmr_command, Context1)),
+    Arg = m_rsc:p(ToolId, zmr_arg_checkout, Context1),
+    Arg1 = re:replace(Arg, "\\$source", Source, [{return, list}]),
+    Arg2 = re:replace(Arg1, "\\$target", Target, [{return, list}]),
+    Cmd1 = lists:flatten([Cmd, " ", Arg2]),
+    ?PRINT(Cmd1),
+    Result = os:cmd(Cmd1),
+    ?PRINT(Result),
+    Message = lists:flatten(["Clone Repository Done. <br /><pre>", Result, "</pre>"]),
+    update_progress(TargetId, [{message, Message}, {done, true}], Context1).
+
+
+
+update_progress(TargetId, Vars, Context) ->
+    Context1 = z_render:update(TargetId, 
+			       #render{ template="_zmr_progress.tpl", 
+					vars=Vars
+				      }, 
+			       Context
+			      ),
+    z_session_page:add_script(Context1).
 
 %% interface functions
 -export([
-	 init/1
+	 init/1,
+	 repo_path/2
 %	 handle_call/3,
 %	 handle_cast/2,
 %	 handle_info/2,
 %    {ok, #state{context = z_context:new(Context)}}.
     ok.
 
+
+repo_path(RepoId, Context) ->
+    z_path:files_subdir(filename:join("zmr_repos", z_convert:to_list(RepoId)), Context).
+
+
 datamodel() ->
     [
 
 	[
 	 {title, <<"Mercurial (hg)">>},
 	 {zmr_command, <<"hg">>},
-	 {zmr_arg_checkout, <<"clone $repo $dest">>},
+	 {zmr_arg_checkout, <<"clone --noupdate $source $target">>},
 	 {zmr_arg_history, <<"log">>}
 	]},
 
 	[
 	 {title, <<"Git">>},
 	 {zmr_command, <<"git">>},
-	 {zmr_arg_checkout, <<"clone $repo $dest">>},
+	 {zmr_arg_checkout, <<"clone $source $target">>},
 	 {zmr_arg_history, <<"log">>}
 	]},
 	
     end;
 
 m_find_value(log, #m{value={repo, Id}}=M, _Context) ->
-    M#m{value={repo_log, ["example data", "should retreive this", "from the source repo", "in a suitable format"]}}.
+    M#m{value={repo_log, ["example data", "should retreive this", "from the source repo", "in a suitable format"]}};
 
-%m_find_value(exist, #m{value={repo, Id}}=M, Context) ->
-    
+m_find_value(exist, #m{value={repo, Id}}, Context) ->
+    filelib:is_dir(mod_zmr:repo_path(Id, Context)).
 
 
 

templates/_admin_edit_content.zmr_repository.tpl

 <div class="form-item clearfix">
 <label for="zmr_repository_path">{_ Repository path _}</label>
 <input id="zmr_repository_path" type="text" name="zmr_repository_path" value="{{ r.zmr_repository_path }}" style="width: 80%" />
+
+<div id="repo_status">
+{% if m.zmr.repo[id].exist %} 
+	Repository is cloned.
+{% else %}
+	{% button text="Clone repository" action={clone_repo id=id target="repo_status"} %}
+{% endif %}
 </div>
-
+</div>
 
 </fieldset>
 </div>

templates/_zmr_progress.tpl

+{{message}}
+{% if not done %}
+	{% button text="Cancel" disabled=true action={growl text="Cancel Not Yet Implemented..."} %}
+{% endif %}