Module: JekyllImgFlow::Tasks

Extended by:
Rake::DSL
Defined in:
lib/jekyll-imgflow/tasks.rb

Overview

Rake tasks for listing and installing built-in presets.

Tasks (defined in the host project's Rakefile):

imgflow:presets       — list built-in presets and their install status
imgflow:install_presets — copy built-in presets into the site's
_data/imgflow/presets/ directory (use overwrite=true to replace)

Class Method Summary collapse

Class Method Details

.builtin_preset_namesArray<String>

Names of built-in presets (without .yml extension)

Returns:

  • (Array<String>)


26
27
28
29
30
31
32
# File 'lib/jekyll-imgflow/tasks.rb', line 26

def builtin_preset_names
  return [] unless Dir.exist?(builtin_presets_dir)

  Dir.glob(File.join(builtin_presets_dir, "*.yml")).map do |f|
    File.basename(f, ".yml")
  end.sort
end

.builtin_presets_dirString

Path to built-in presets shipped with the gem

Returns:

  • (String)


20
21
22
# File 'lib/jekyll-imgflow/tasks.rb', line 20

def builtin_presets_dir
  File.expand_path("presets", __dir__)
end

.install_presets(overwrite: false) ⇒ Hash

Copy built-in presets into the site's _data/imgflow/presets/ directory

Parameters:

  • overwrite (Boolean) (defaults to: false)

    If true, replace modified copies

Returns:

  • (Hash)

    Summary with :installed and :skipped arrays



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jekyll-imgflow/tasks.rb', line 70

def install_presets(overwrite: false)
  installed = []
  skipped = []

  FileUtils.mkdir_p(site_presets_dir)

  builtin_preset_names.each do |name|
    gem_path = File.join(builtin_presets_dir, "#{name}.yml")
    site_path = File.join(site_presets_dir, "#{name}.yml")

    if File.exist?(site_path) && !overwrite
      if same_content?(gem_path, site_path)
        puts "  #{name}.yml: already up to date (skip)"
      else
        puts "  #{name}.yml: exists and differs (skip — use overwrite: true to replace)"
        skipped << name
      end
    else
      existed = File.exist?(site_path)
      FileUtils.cp(gem_path, site_path)
      action = existed ? "updated" : "installed"
      puts "  #{name}.yml: #{action}"
      installed << name
    end
  end

  { installed: installed, skipped: skipped }
end

.install_rake_tasksObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jekyll-imgflow/tasks.rb', line 109

def install_rake_tasks
  return if Rake::Task.task_defined?("imgflow:presets")

  namespace :imgflow do
    desc "List built-in ImgFlow presets and their install status"
    task :presets do
      JekyllImgFlow::Tasks.list_presets
    end

    desc "Copy built-in ImgFlow presets into _data/imgflow/presets/"
    task :install_presets, [:overwrite] do |_task, args|
      overwrite = args[:overwrite] == "true"
      JekyllImgFlow::Tasks.install_presets(overwrite: overwrite)
    end
  end
end

.list_presetsObject

List built-in presets and show their install status in the current site



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jekyll-imgflow/tasks.rb', line 41

def list_presets
  puts "ImgFlow built-in presets:"
  puts
  builtin_preset_names.each do |name|
    gem_path = File.join(builtin_presets_dir, "#{name}.yml")
    site_path = File.join(site_presets_dir, "#{name}.yml")
    site_exists = File.exist?(site_path)

    status = if !site_exists
               "not installed"
             elsif same_content?(gem_path, site_path)
               "up to date"
             else
               "modified or outdated"
             end

    puts "  #{name}"
    puts "    gem:    #{gem_path}"
    puts "    site:   #{site_path}"
    puts "    status: #{status}"
    puts
  end
  puts "To install: bundle exec rake imgflow:install_presets"
  puts "To overwrite modified copies: bundle exec rake 'imgflow:install_presets[true]'"
end

.same_content?(path_a, path_b) ⇒ Boolean

Check if two files have identical content

Parameters:

  • path_a (String)
  • path_b (String)

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/jekyll-imgflow/tasks.rb', line 103

def same_content?(path_a, path_b)
  File.read(path_a) == File.read(path_b)
rescue Errno::ENOENT
  false
end

.site_presets_dirString

Path where presets are installed in the consuming site

Returns:

  • (String)


36
37
38
# File 'lib/jekyll-imgflow/tasks.rb', line 36

def site_presets_dir
  File.join(Dir.pwd, "_data", "imgflow", "presets")
end