zmr / 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) ->
    Source = mod_zmr:get_repo_url(RepoId, Context),
    Target = mod_zmr:get_repo_path(RepoId, Context),
    Cmd = mod_zmr:get_scm_cmd(RepoId,
			      [clone, 
			       {"\\$source", Source}, 
			       {"\\$target", Target}
			      ],
			      Context),
    Context1 = update_progress(TargetId, [{message, "Cloning repository..."}], Context),
    Result = os:cmd(Cmd),
    ?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).
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.