Sha256: 417cc2103c0cca79df017a715507d5043436517f1e36ffc8c03879a4c0bee274

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'conventions'
require 'random_util'

module Commands
  module Init
    module P4Helpers

      # Intended to be a block helper that creates a 'temporary client'
      #
      # Your block should take the client name and path.
      #
      # Example:
      #
      #     open_client(p4) do |path, name|
      #       puts "my client #{name}'s root is #{path}"
      #     end
      def open_client(options)
        p4 = options[:p4]

        # Switch user only if specified
        user = nil
        password = nil
        olduser = nil
        oldpass = nil
        if (options[:user])
          user = options[:user]
          password = options[:password] if options.key?(:password)
          olduser = options[:olduser]
          oldpass = options[:oldpass] if options.key?(:oldpass)
          p4.user = user
          p4.password = password
          p4.run_login('-p') if password
        end

        name = RandomUtil.randstr
        dir = File.join(Conventions.client_root_dir, name)

        if !Dir.exist?(dir)
          FileUtils.mkpath(dir)
        end

        spec = p4.fetch_client
        spec._root = dir
        spec._client = name
        spec._description = 'p4util init temp client'
        if options.key?(:view)
          spec._view = options[:view].map { |x| x.gsub("${name}", name) }
        else
          spec._view = ["//depot/... //#{name}/depot/..."]
        end

        p4.save_client(spec)

        p4.client = name

        p4.run_sync('-f', '//...')

        if block_given?
          yield dir, name
        else
          return dir, name
        end
      ensure
        if block_given?
          if (user)
            p4.user = olduser
            p4.password = oldpass
            p4.run_login('-p') if oldpass
          end

          p4.run_client('-d', '-f', name)
          p4.client = 'invalid'
          FileUtils.rmtree(dir)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
p4util-0.4.1 ./lib/commands/init/p4_helpers.rb