#!/usr/bin/env ruby # == Synopsis # # svn_wc_tree: install the svn_wc_tree web application. # # == Usage # # svn_wc_tree [OPTIONS] # # --help, -h: # show help # # --html, -l [html_dir]: # Move all web files to the directory path specified. # A directory called svn_wc_tree will be created at this path. # # --cgi, -c [cgi_dir]: # Move the included CGI script to the path specified. # path/directory must exist and must be able to execute CGI scripts # # --post_to_url, -u [post_to_url]: # a URL that will receive AJAX requests and return the required JSON. # (see the CGI script provided) # # --php, -p [php]: # Run as a PHP application. (use only as last choice) # (Ruby is still required; CGI not needed) # # --conf_location, -u [conf_location]: # location (filesystem path) of your svn_wc.conf file (readable by 'httpd # runs as' owner) # # --move_conf_from, -f [move_conf_from]: # move your conf file (svn_wc_tree.conf) from filesystem path to # (move_conf_to) # # --move_conf_to, -t [move_conf_to]: # move your conf file (svn_wc_tree.conf) to filesystem path from # (move_conf_from) # # # Example: # svn_wc_tree --html /var/htdocs --cgi /var/cgi-bin \ # --post_to_url 'http://example.com/cgi-bin/ \ # --conf_location /opt/conf/svn_conf.yaml # # sudo svn_wc_tree --html /var/htdocs --post_to_url 'cgi-bin/cgi.rb' # # sudo svn_wc_tree --html /var/htdocs --php true \ # --conf_location /opt/svn_wc_tree/svn_conf.yaml # # sudo svn_wc_tree --html /var/www --cgi /usr/lib/cgi-bin \ # --post_to_url 'http://localhost/cgi-bin/' --move_conf_from \ # /tmp/svn_conf.yaml --move_conf_to /usr/local/svn_wc_tree require 'getoptlong' require 'rdoc/usage' require 'fileutils' opts = GetoptLong.new( [ '--help', '-h', GetoptLong::NO_ARGUMENT], [ '--html', '-l', GetoptLong::REQUIRED_ARGUMENT], [ '--post_to_url', '-u', GetoptLong::OPTIONAL_ARGUMENT], [ '--cgi', '-c', GetoptLong::OPTIONAL_ARGUMENT], [ '--php', '-p', GetoptLong::OPTIONAL_ARGUMENT], [ '--conf_location', '-o', GetoptLong::OPTIONAL_ARGUMENT], [ '--move_conf_from', '-f', GetoptLong::OPTIONAL_ARGUMENT], [ '--move_conf_to' , '-t', GetoptLong::OPTIONAL_ARGUMENT] ) html_dir = nil cgi_dir = nil php = nil post_to_url = 'index.php' # assume php mode conf_location = nil move_conf_from = nil move_conf_to = nil opts.each do |opt, arg| case opt when '--help' RDoc::usage when '--html' if arg == '' then puts 'path cannot be empty!' and exit 1 else html_dir = arg end when '--cgi' if arg != '' then cgi_dir = arg end when '--php' if arg != '' then php = true end when '--post_to_url' #if php then raise ArgumentError, 'post_to_url, is not used when php' if arg != '' then post_to_url = arg end when '--conf_location' if arg != '' then conf_location = arg end when '--move_conf_from' if arg != '' then move_conf_from = arg end when '--move_conf_to' if arg != '' then move_conf_to = arg end end end # methods def handle_conf(move_conf_from, move_conf_to, cgi_dir=nil) begin FileUtils.mkdir move_conf_to FileUtils.cp move_conf_from, move_conf_to rescue #puts "Destination conf file exists #{move_conf_to}" end # now edit lib to set conf_file path to_path = File.dirname(move_conf_to) # moved to path to_name = File.basename(move_conf_from) # name of conf file conf_abs_path = File.join(to_path, to_name) set_conf_file_in_cgi(cgi_dir, conf_abs_path) if cgi_dir end def set_conf_file_in_cgi(cgi_dir, conf_location, php=nil) cgi_file = File.join(cgi_dir, 'svn_wc_broker.cgi') File.open(cgi_file, 'r+') do |cf| lines = cf.readlines lines.each do |el| el.gsub! /CONF_FILE = nil/, "CONF_FILE = '#{conf_location}'" # dont print header when run under php el.gsub! /print cgi.header/, '#print cgi.header' if php end cf.pos = 0 cf.print lines cf.truncate(cf.pos) end end def set_cgi_path_in_js(html_dir, post_to_url) js_file = File.join(html_dir, 'js', 'swt.js') File.open(js_file, 'r+') do |fst| lines = fst.readlines lines.each do |el| el.gsub! /var POST_URL = '.*;/, "var POST_URL = '#{post_to_url}';" end fst.pos = 0 fst.print lines fst.truncate(fst.pos) end end # actions if not html_dir puts "Required argument missing. try --help" exit 0 end # ok, html_dir should be set swt_gem_ins_dir = File.join(File.dirname(__FILE__), '..') puts 'Exception: cannot find this lib gem install path' and exit 1 \ unless File.directory? swt_gem_ins_dir html_dest = File.join(html_dir, 'svn_wc_tree') begin # copy web app (html related) files FileUtils.rm_rf html_dest FileUtils.mkdir html_dest FileUtils.cp_r(File.join(swt_gem_ins_dir, 'svn_wc_tree/'), html_dir) rescue Exception => e puts "line:#{__LINE__} Exception: #{e.message}" and exit 1 end # cgi script cgi_path = html_dest if php # if set, will overwrite php cgi url cgi_path = cgi_dir if cgi_dir if cgi_path begin # copy web app cgi script to web app specified location FileUtils.cp(File.join(swt_gem_ins_dir, 'cgi', 'svn_wc_broker.cgi'), cgi_path) rescue Exception => e puts "line:#{__LINE__} Exception: #{e.message}" exit 1 end end if conf_location if cgi_path and conf_location begin set_conf_file_in_cgi(cgi_path, conf_location, php) rescue Exception => e puts "line:#{__LINE__} Exception: #{e.message}" exit 1 end end end begin # edit js/swt.js file to full web accessible CGI URL set_cgi_path_in_js(html_dest, post_to_url) rescue Exception => e puts "line:#{__LINE__} Exception: #{e.message}" exit 1 end begin # conf file if move_conf_from or move_conf_to if not (move_conf_from and move_conf_to) puts 'if using "move_", both move_conf_to and move_conf_from must be set' exit 1 end handle_conf(move_conf_from, move_conf_to, cgi_dir) end rescue Exception => e puts "line:#{__LINE__} Exception: #{e.message}" exit 1 end