<% # Copyright 2011 The Closure Script Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'rexml/document' require 'thread' # This is an example of a complex Closure Script. # It would be easier to read and more performant if split up # into an includable .rb file and multiple templates. # But speed isn't a problem and the single file format makes # it just a touch easier to share and move around. # Example usage: # render 'git', :git_name => 'Closure Library', :git_path => 'closure-library', # :git_url => 'http://closure-library.googlecode.com/git/trunk/' # # Include as many as you want and they can all be updating at once. # Don't supply any locals if you just want Closure::Git for manual use. class Closure::Git # Repository objects are held in global class variable. # Checking out runs in a background thread. # repo = Closure::Git['lib-folder', 'http://git.example.org/trunk'] @repos ||= {} def self.[](path, url=nil, name=nil) repo = @repos[path] raise "url and name requried for setup" if !repo and (!url or !name) repo = @repos[path] ||= new(path, url) repo.name ||= url repo.name = name if name repo end # Configurable Subversion shell command def self.git; @git ||= 'git'; end def self.git=(git); @git = git; end def initialize(path, url) @semaphore = Mutex.new @running = false @path = path @url = url @name = '' @log = nil end attr_reader :running attr_reader :path attr_reader :url attr_accessor :name attr_accessor :log # Update or checkout a Git repository. # Closure Script is thread-safe and so is this. # Although the locks in Subversion would prevent it # from corrupting (one would hope), we don't want our # global object to be in an indeterminate state with two # threads running because submit was hit twice. def update(version='TODO') @semaphore.synchronize do return if running @running = true Thread.new do @log = "Cloning. Stand by..." @local_revision = 'CLONING' cmd = "#{self.class.git.dump} clone #{url.dump} #{path.dump}" @log = "$ #{cmd}\n" + `#{cmd} 2>&1` @local_revision = nil @running = false end end end def info(location) result = `#{self.class.git.dump} -C #{location.dump} name-rev --name-only HEAD` return result if $? == 0 return nil end def local_revision return @local_revision if @local_revision @local_revision = info(path) end end if defined? git_path and defined? git_url and defined? git_name git = Closure::Git[git_path, git_url, git_name] if defined? git_update git.update git_update end elsif params['git_path'] git = Closure::Git[params['git_path']] if post? git.update params['git_update'] response.redirect params['return_to'] end end if get? and git and render_stack.size > 1 is_running = !!git.running action = expand_src File.join File.dirname(__FILE__), File.basename(__FILE__, File.extname(__FILE__)) -%>
<%=h git.name %> <% if is_running -%> is cloning. <% elsif git.local_revision -%> installed: <%=h git.local_revision %> <% else -%> not installed. <% end -%>    <% if is_running %> <% unless @git_document_reload_on_page @git_document_reload_on_page = true -%> <% end elsif !git.local_revision -%>    <% end -%> <% if is_running -%> Stand by... <% elsif git.log -%> <% unless git.local_revision -%> ERROR: <% end -%> VIEW LOG <% end -%>
<% elsif get? and git -%> Back

<%=h git.name %>

Local folder:<%=h git.path %>
Local revision:<%=h git.local_revision %>
Repository URL:<%=h git.url %>
<% if git.log -%>

Results of last run:

<%=h git.log %>
Back <% git.log = nil %> <% end -%> <% end -%>