class UnisonCommand

Constants

UNISON_DEFAULT_COMMAND
UNISON_OPTION_SPEC
VERSION

Attributes

command[RW]
option[RW]
profile[RW]
root1[RW]
root2[RW]
status[R]

Public Class Methods

new(*args) click to toggle source

args accepts the following three pattern:

  • root1, root2, opts = {}

  • profilename, opts = {}

  • profilename, root1, root2, opts = {}

We set option of unison command to optinal hash. The keys are symbols made from hypen-removed options of unison command and the values are booleans (true or false), strings, and arrays of strings corresponding to unison's options. Note that to set boolean option we do not use a string 'true' or 'false', but we use TrueClass, FalseClass, or NilClass.

# File lib/unison.rb, line 97
def initialize(*args)
  if Hash === args[-1]
    @option = args[-1]
    args = args[0...-1]
  else
    @option = {}
  end
  case args.size
  when 1
    @profile = args[0]
    @root1 = nil
    @root2 = nil
  when 2
    @profile = nil
    @root1 = args[0]
    @root2 = args[1]
  when 3
    @profile = args[0]
    @root1 = args[1]
    @root2 = args[2]
  else
    raise ArgumentError, "Invalid"
  end
  @command = UNISON_DEFAULT_COMMAND
  @status = nil
end

Public Instance Methods

execute(dry_run = false) click to toggle source

The method returns :success when all files are synchronized, :skipped when some files are skipped, :non_fatal_error when non fatal error occurs, and :fatal_error when fatal error occurs or process is interrupted. If dry_run is true, the method returns an array of a unison command to execute.

# File lib/unison.rb, line 190
def execute(dry_run = false)
  cmd = get_command
  if dry_run
    @status = nil
    return cmd
  end
  # Search exit code of unison command.
  Kernel.system(*cmd)
  case get_exit_status
  when 0
    :success
  when 1
    :skipped
  when 2
    :non_fatal_error
  when 3
    :fatal_error
  else
    raise StandardError, "Invalid exit code of unison: #{status.inspect.strip}."
  end
end
version() click to toggle source
# File lib/unison.rb, line 212
def version
  `#{@command} -version`.split[2]
end

Private Instance Methods

get_command() click to toggle source
# File lib/unison.rb, line 124
def get_command
  cmd = [@command]
  cmd << @profile << @root1 << @root2
  @option.each do |key, val|
    if spec = UNISON_OPTION_SPEC[key.intern]
      case spec
      when :bool
        case val
        when TrueClass, FalseClass, NilClass
          if val
            cmd << "-#{key}"
          else
            cmd << "-#{key}=false"
          end
        else
          raise UnisonCommand::InvalidOption, "We use TrueClass, FalseClass, or NilClass for #{key}."
        end
      when :array
        k = "-#{key}"
        if val.respond_to?(:each)
          val.each do |v|
            cmd << k << v.to_s
          end
        else
          cmd << k << val.to_s
        end
      when :string
        cmd << "-#{key}" << val.to_s
      when Array
        v = val.to_s
        unless spec.include?(v)
          raise UnisonCommand::InvalidOption, "Invalid unison option for #{key}: #{v}"
        end
        cmd << "-#{key}" << v
      end
    else
      raise UnisonCommand::NonExistentOption, "Nonexistent unison option: #{key.to_s}"
    end
  end
  cmd.compact!
  cmd
end
get_execute_result() click to toggle source
# File lib/unison.rb, line 168
def get_execute_result
  $?
end
get_exit_status() click to toggle source
# File lib/unison.rb, line 173
def get_exit_status
  @status = get_execute_result

  if @status.respond_to?(:exitstatus)
    @status.exitstatus
  else
    @status
  end
end