class Fastlane::RamDisk::Manager

Public Class Methods

new(params) click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 21
def initialize(params)
  @params = params
  UI.user_error!('Sorry, it only works in macOS for now') unless mac?
end
run(params) click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 14
def self.run(params)
  instance = new(params)
  instance.create
  params[:use].call
  instance.remove
end

Public Instance Methods

create() click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 26
def create
  name = @params[:name]
  path = @params[:mount_path]
  size = @params[:size]
  ram_disk_path = File.join(path, name)

  UI.user_error!('the name is empty') if name.to_s.empty?
  UI.user_error!('the disk size is zero or negative') if size.to_i <= 0
  UI.user_error!("mount path existed: #{ram_disk_path}") if File.exist?(ram_disk_path)

  commands = []
  commands << create_ram_disk(name, size)
  commands << spotlight_indexes(ram_disk_path, true)

  commands.each do |shell_command|
    if Helper.test?
      UI.message("$ #{shell_command}")
      next
    end

    Actions.sh(shell_command)
  end

  UI.crash! "fail to create ram disk" unless Dir.exist?(ram_disk_path)

  UI.success "Successful on ram disk: #{ram_disk_path}"
  shared_value(Actions::SharedValues::RAM_DISK_PATH, ram_disk_path)
end
mac?() click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 78
def mac?
  require 'rbconfig'

  platform = RbConfig::CONFIG['host_os']
  platform.include?('darwin')
end
remove() click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 55
def remove
  path = @params[:path]

  UI.user_error!('ram path is empty') if path.to_s.empty?
  UI.user_error!("ram path is not exist: #{path}") unless Dir.exist?(path)
  UI.message("Found ram disk: #{path}")

  commands = []
  commands << spotlight_indexes(path, false)
  commands << remove_ram_disk(path)

  commands.each do |shell_command|
    if Helper.test?
      UI.message("$ #{shell_command}")
      next
    end

    Actions.sh(shell_command)
  end

  UI.crash! "fail to remove ram disk" if Dir.exist?(path)
end

Private Instance Methods

create_ram_disk(name, size) click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 87
def create_ram_disk(name, size)
  attch_ram = "hdiutil attach -nomount ram://$[#{size}*2048]"
  disk_path = (Helper.test? ? "'#{attch_ram}'" : `#{attch_ram}`.chomp)

  "diskutil erasevolume HFS+ #{name} #{disk_path}"
end
remove_ram_disk(path) click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 94
def remove_ram_disk(path)
  "diskutil eject #{path}"
end
shared_value(key, value) click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 103
def shared_value(key, value)
  return Actions.lane_context[key] || ENV[key.to_s] unless value

  Actions.lane_context[key] = value
  ENV[key.to_s] = value
end
spotlight_indexes(path, value) click to toggle source
# File lib/fastlane/plugin/ram_disk/manager.rb, line 98
def spotlight_indexes(path, value)
  value = value ? 'on' : 'off'
  "mdutil -i #{value} #{path}"
end