Class: Lolcommits::Platform

Inherits:
Object
  • Object
show all
Defined in:
lib/lolcommits/platform.rb

Class Method Summary collapse

Class Method Details

.can_animate?Boolean

Is the platform capable of capturing animated GIFs from webcam?

Returns:

  • (Boolean)

    Boolean



57
58
59
# File 'lib/lolcommits/platform.rb', line 57

def self.can_animate?
  platform_linux? || platform_mac?
end

.capturer_class(animate = false) ⇒ Object

The capturer class constant to use

Returns:

  • Class



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lolcommits/platform.rb', line 9

def self.capturer_class(animate = false)
  if ENV['LOLCOMMITS_CAPTURER']
    const_get(ENV['LOLCOMMITS_CAPTURER'])
  elsif platform_mac?
    animate ? CaptureMacAnimated : CaptureMac
  elsif platform_linux?
    animate ? CaptureLinuxAnimated : CaptureLinux
  elsif platform_windows?
    CaptureWindows
  elsif platform_cygwin?
    CaptureCygwin
  else
    raise 'Unknown / Unsupported Platform.'
  end
end

.command_which(cmd, only_path = false) ⇒ Object

Cross-platform way of finding an executable in the $PATH.

Idea taken from bit.ly/qDaTbY, if only_path is true, only the path is returned (not the path and command)

Examples:

command_which('ruby') #=> /usr/bin/ruby

Returns:

  • Boolean



90
91
92
93
94
95
96
97
98
99
# File 'lib/lolcommits/platform.rb', line 90

def self.command_which(cmd, only_path = false)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return only_path ? path : exe if File.executable? exe
    end
  end
  nil
end

.device_listObject

Note:

Currently only functions on Mac.

Returns a list of system camera devices in a format suitable to display to the user.

Returns:

  • String



119
120
121
122
123
124
125
# File 'lib/lolcommits/platform.rb', line 119

def self.device_list
  # TODO: handle other platforms here (linux/windows)
  if Platform.platform_mac?
    capturer = Lolcommits::CaptureMacAnimated.new
    `#{capturer.executable_path} -l`
  end
end

.git_config_color_always?Boolean

Is ‘git config color.ui` set to ’always’?

Due to a bug in the ruby-git library, git config for color.ui cannot be set to ‘always’ or it won’t read properly.

This helper method let’s us check for that error condition so we can alert the user in the CLI tool.

Returns:

  • (Boolean)

    Boolean



110
111
112
# File 'lib/lolcommits/platform.rb', line 110

def self.git_config_color_always?
  `git config color.ui`.chomp =~ /always/
end

.host_osObject

return host_os identifier from the RbConfig::CONFIG constant

Returns:

  • String



51
52
53
# File 'lib/lolcommits/platform.rb', line 51

def self.host_os
  ENV['LOLCOMMITS_FAKE_HOST_OS'] || RbConfig::CONFIG['host_os'].downcase
end

.platform_cygwin?Boolean

Are we on a Cygwin platform?

Returns:

  • (Boolean)

    Boolean



45
46
47
# File 'lib/lolcommits/platform.rb', line 45

def self.platform_cygwin?
  host_os.include?('cygwin')
end

.platform_linux?Boolean

Are we on a Linux platform?

Returns:

  • (Boolean)

    Boolean



33
34
35
# File 'lib/lolcommits/platform.rb', line 33

def self.platform_linux?
  host_os.include?('linux')
end

.platform_mac?Boolean

Are we on a Mac platform?

Returns:

  • (Boolean)

    Boolean



27
28
29
# File 'lib/lolcommits/platform.rb', line 27

def self.platform_mac?
  host_os.include?('darwin')
end

.platform_windows?Boolean

Are we on a Windows platform?

Returns:

  • (Boolean)

    Boolean



39
40
41
# File 'lib/lolcommits/platform.rb', line 39

def self.platform_windows?
  !host_os.match(/(win|w)32/).nil?
end

.valid_ffmpeg_installed?Boolean

Note:

For now, this just checks for presence, any version should work.

Is a valid install of ffmpeg present on the system?

Returns:

  • (Boolean)

    Boolean



77
78
79
# File 'lib/lolcommits/platform.rb', line 77

def self.valid_ffmpeg_installed?
  command_which('ffmpeg')
end

.valid_imagemagick_installed?Boolean

Is a valid install of imagemagick present on the system?

Returns:

  • (Boolean)

    Boolean



63
64
65
66
67
68
69
70
71
# File 'lib/lolcommits/platform.rb', line 63

def self.valid_imagemagick_installed?
  return false unless command_which('identify')
  return false unless command_which('mogrify')
  # you'd expect the below to work on its own, but it only handles old versions
  # and will throw an exception if IM is not installed in PATH
  MiniMagick.valid_version_installed?
rescue
  return false
end