Class: Shellfie::DependencyChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/dependency_checker.rb

Constant Summary collapse

IMAGE_MAGICK_COMMANDS =
%w[magick convert].freeze

Class Method Summary collapse

Class Method Details

.configure_mini_magick!(timeout: 30) ⇒ Object



50
51
52
53
54
# File 'lib/shellfie/dependency_checker.rb', line 50

def configure_mini_magick!(timeout: 30)
  return unless defined?(MiniMagick) && MiniMagick.respond_to?(:timeout=)

  MiniMagick.timeout = timeout
end

.doctor(output_dir: Dir.pwd) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shellfie/dependency_checker.rb', line 56

def doctor(output_dir: Dir.pwd)
  details = imagemagick_details
  video_version = ffmpeg_version
  [
    check("Ruby", RUBY_VERSION, Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")),
    check("ImageMagick", details[:version] || "not found", imagemagick_available?),
    check("Image formats", details[:formats].empty? ? "unavailable" : details[:formats].join(", "),
          (required_formats - details[:formats]).empty?),
    check("Image render", details[:render_ok] ? "1x1 PNG generated" : "failed", details[:render_ok]),
    check("Fonts", details[:font_count].to_s, details[:font_count].positive?),
    check("Security policy", details[:policy] || "unavailable", !details[:policy].nil?),
    check("ffmpeg (optional)", video_version || "not found; required only for APNG/MP4/WebM", true),
    check("Writable output", output_dir, File.writable?(output_dir)),
    check("Encoding", Encoding.default_external.name, true)
  ]
end

.ensure_ffmpeg!Object

Raises:



25
26
27
28
29
# File 'lib/shellfie/dependency_checker.rb', line 25

def ensure_ffmpeg!
  return if ffmpeg_path

  raise DependencyError, "ffmpeg not found; install ffmpeg to generate APNG, MP4, or WebM"
end

.ensure_imagemagick!Object

Raises:



40
41
42
43
44
45
46
47
48
# File 'lib/shellfie/dependency_checker.rb', line 40

def ensure_imagemagick!
  return if imagemagick_available?

  raise DependencyError, <<~MSG
    ImageMagick not found
      → Please install ImageMagick: brew install imagemagick
      → Or visit: https://imagemagick.org/script/download.php
  MSG
end

.ffmpeg_pathObject



21
22
23
# File 'lib/shellfie/dependency_checker.rb', line 21

def ffmpeg_path
  @ffmpeg_path ||= find_executable(%w[ffmpeg], verify_imagemagick: false)
end

.ffmpeg_versionObject



31
32
33
34
35
36
37
38
# File 'lib/shellfie/dependency_checker.rb', line 31

def ffmpeg_version
  return unless ffmpeg_path

  output, = Open3.capture3(ffmpeg_path, "-version")
  output.lines.first&.strip
rescue SystemCallError
  nil
end

.imagemagick_available?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/shellfie/dependency_checker.rb', line 17

def imagemagick_available?
  !imagemagick_path.to_s.empty?
end

.imagemagick_detailsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shellfie/dependency_checker.rb', line 73

def imagemagick_details
  return { version: nil, formats: [], font_count: 0, render_ok: false, policy: nil } unless imagemagick_available?

  version_output, _error, version_status = Open3.capture3(imagemagick_path, "-version")
  formats_output, _error, formats_status = Open3.capture3(imagemagick_path, "-list", "format")
  fonts_output, _error, fonts_status = Open3.capture3(imagemagick_path, "-list", "font")
  policy_output, _error, policy_status = Open3.capture3(imagemagick_path, "-list", "policy")
  {
    version: version_status.success? ? version_output.lines.first&.strip : nil,
    formats: formats_status.success? ? required_formats.select { |format| formats_output.match?(/^\s*#{format}\*?\s/im) } : [],
    font_count: (fonts_status.success? ? fonts_output.scan(/^\s*Font:/).size : 0) +
      FontResolver::FONT_FILES.values.uniq.count { |path| File.file?(path) },
    render_ok: render_smoke_test,
    policy: policy_status.success? ? "#{policy_output.scan(/^\s*Policy:/).size} rules loaded" : nil
  }
rescue SystemCallError
  { version: nil, formats: [], font_count: 0, render_ok: false, policy: nil }
end

.imagemagick_pathObject



13
14
15
# File 'lib/shellfie/dependency_checker.rb', line 13

def imagemagick_path
  @imagemagick_path ||= find_executable(IMAGE_MAGICK_COMMANDS)
end