require 'conventions' require 'optparse' require 'osutil' require 'net/ftp' module Commands # TODO the p4ruby extconf.rb file mechanism has some logic to search the ftp # site for things. We might also want to use HTTP def Commands.download(options=nil) version = 'r14.2' binary = 'p4d' if options and !options.params.empty? op = OptionParser.new do |op| op.on('-v VERSION', '--version VERSION', 'ftp.perforce.com version') do |v| version = v end end op.parse!(options.params) if !options.params.empty? binary = options.params.first end end case binary when 'p4' download_p4_via_ftp(version) when 'p4d' download_p4d_via_ftp(version) when 'p4api' download_p4api_via_ftp(version) else raise "Don't know how to download #{binary}, check 'p4util help download'" end end def Commands.print_download_help puts <<-END.gsub(/^ {6}/, '') p4util download [p4|p4d|p4api] Downloads one of the following utilities (in lieu of an installer) into a local work/ directory. * p4 * p4d * p4api Will default to the r14.2 release. Options: --version X where X looks like the version in the ftp.perforce.com site, e.g., 'r14.2' instead of '2014.2' END end private def Commands.download_p4_via_ftp(version) download_via_ftp(version, OsUtil.p4_executable, OsUtil.p4_path) if !File.executable?(OsUtil.p4_path) File.chmod(0755, OsUtil.p4_path) end end def Commands.download_p4d_via_ftp(version) download_via_ftp(version, OsUtil.p4d_executable, OsUtil.p4d_path) if !File.executable?(OsUtil.p4d_path) File.chmod(0755, OsUtil.p4d_path) end end def Commands.download_p4api_via_ftp(version) download_via_ftp(version, OsUtil.p4api_file, OsUtil.p4api_path) # um, probably should expand this guy out end private def Commands.download_via_ftp(version, filename, output_path) ftp = Net::FTP.new('ftp.perforce.com') ftp.login dir = OsUtil.ftp_download_dir(version) ftp.chdir(dir) ftp.passive=true Conventions.init_working_dir ftp.getbinaryfile(filename, output_path) ensure ftp.close if ftp and !ftp.closed? end end