class RakeCompilerDock::Starter

def exec(*args)

def exec(*args)
  options = (Hash === args.last) ? args.pop : {}
  mountdir = options.fetch(:mountdir){ ENV['RCD_MOUNTDIR'] || Dir.pwd }
  workdir = options.fetch(:workdir){ ENV['RCD_WORKDIR'] || Dir.pwd }
  case RUBY_PLATFORM
  when /mingw|mswin/
    mountdir = sanitize_windows_path(mountdir)
    workdir = sanitize_windows_path(workdir)
    # Virtualbox shared folders don't care about file permissions, so we use generic ids.
    uid = 1000
    gid = 1000
  when /darwin/
    uid = 1000
    gid = 1000
  else
    # Docker mounted volumes also share file uid/gid and permissions with the host.
    # Therefore we use the same attributes inside and outside the container.
    uid = Process.uid
    gid = Process.gid
  end
  user = options.fetch(:username){ current_user }
  group = options.fetch(:groupname){ current_group }
  platforms(options).split(" ").each do |platform|
    image_name = container_image_name(options.merge(platform: platform))
    check = check_docker(mountdir) if options.fetch(:check_docker){ true }
    docker_opts = options.fetch(:options) do
      opts = ["--rm", "-i"]
      opts << "-t" if $stdin.tty?
      opts
    end
    if verbose_flag(options) && args.size == 3 && args[0] == "bash" && args[1] == "-c"
      $stderr.puts "rake-compiler-dock bash -c #{ args[2].inspect }"
    end
    runargs = args.dup
    runargs.unshift("sigfw") if options.fetch(:sigfw){ true }
    runargs.unshift("runas") if options.fetch(:runas){ true }
    cmd = [check.docker_command, "run",
        "-v", "#{mountdir}:#{make_valid_path(mountdir)}",
        "-e", "UID=#{uid}",
        "-e", "GID=#{gid}",
        "-e", "USER=#{user}",
        "-e", "GROUP=#{group}",
        "-e", "GEM_PRIVATE_KEY_PASSPHRASE",
        "-e", "SOURCE_DATE_EPOCH",
        "-e", "ftp_proxy",
        "-e", "http_proxy",
        "-e", "https_proxy",
        "-e", "RCD_HOST_RUBY_PLATFORM=#{RUBY_PLATFORM}",
        "-e", "RCD_HOST_RUBY_VERSION=#{RUBY_VERSION}",
        "-e", "RCD_IMAGE=#{image_name}",
        "-w", make_valid_path(workdir),
        *docker_opts,
        image_name,
        *runargs]
    cmdline = Shellwords.join(cmd)
    if verbose_flag(options) == true
      $stderr.puts cmdline
    end
    ok = system(*cmd)
    if block_given?
      yield(ok, $?)
    elsif !ok
      fail "Command failed with status (#{$?.exitstatus}): " +
      "[#{cmdline}]"
    end
  end
end