Sha256: fa5a19920a6a175808821bfdbad9b0c3a62580245c99f2d01362ef0de93bf6ab

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'conventions'
require 'optparse'
require 'osutil'
require 'net/ftp'

module Commands

  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 '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 [p4d|p4api]

      Downloads one of the following utilities (in lieu of an installer) into
      a local work/ directory.

      * p4d
      * p4api

      Will default to the latest release unless you don't know what that is.

      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_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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
p4util-0.0.3 ./lib/commands/download.rb
p4util-0.0.2 ./lib/commands/download.rb
p4util-0.0.1 ./lib/commands/download.rb