Class: Jekyll::Minibundle::BundleFile

Inherits:
Object
  • Object
show all
Includes:
AssetFileProperties
Defined in:
lib/jekyll/minibundle/bundle_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AssetFileProperties

#asset_destination_path, #basename, #data, #defaults, #destination, #destination_rel_dir, #mtime, #name, #path, #placeholders, #relative_path, #to_liquid, #type, #url, #write?

Constructor Details

#initialize(site, config) ⇒ BundleFile

Returns a new instance of BundleFile.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jekyll/minibundle/bundle_file.rb', line 13

def initialize(site, config)
  @site = site
  @type = config.fetch('type')
  asset_source_dir = File.join(@site.source, config.fetch('source_dir'))
  raise ArgumentError, "Bundle source directory does not exist: #{asset_source_dir}" unless File.directory?(asset_source_dir)
  @asset_paths = config.fetch('assets').map do |asset_path|
    path = File.join(asset_source_dir, "#{asset_path}.#{@type}")
    raise ArgumentError, "Bundle asset source file does not exist: #{path}" unless File.file?(path)
    path
  end
  @destination_path = config.fetch('destination_path')
  @asset_destination_dir = File.dirname(@destination_path)
  @asset_destination_filename_prefix = File.basename(@destination_path)
  @minifier_cmd = config.fetch('minifier_cmd')
  @stamped_at = nil
  @is_modified = false
  @_asset_bundle = nil
end

Instance Attribute Details

#asset_destination_dirObject (readonly)

Returns the value of attribute asset_destination_dir.



10
11
12
# File 'lib/jekyll/minibundle/bundle_file.rb', line 10

def asset_destination_dir
  @asset_destination_dir
end

#stamped_atObject (readonly)

Returns the value of attribute stamped_at.



10
11
12
# File 'lib/jekyll/minibundle/bundle_file.rb', line 10

def stamped_at
  @stamped_at
end

Instance Method Details

#asset_destination_filenameObject



59
60
61
# File 'lib/jekyll/minibundle/bundle_file.rb', line 59

def asset_destination_filename
  "#{@asset_destination_filename_prefix}-#{asset_stamp}#{extname}"
end

#asset_source_pathObject



55
56
57
# File 'lib/jekyll/minibundle/bundle_file.rb', line 55

def asset_source_path
  asset_bundle.path
end

#cleanupObject



32
33
34
35
36
# File 'lib/jekyll/minibundle/bundle_file.rb', line 32

def cleanup
  return unless @_asset_bundle
  @_asset_bundle.close
  @_asset_bundle = nil
end

#destination_path_for_markupObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll/minibundle/bundle_file.rb', line 38

def destination_path_for_markup
  # we must rebundle here, if at all, in order to make sure the
  # destination path in the markup and the generated file path have
  # the same fingerprint

  source_mtime = mtime

  if @stamped_at != source_mtime
    @stamped_at = source_mtime
    @is_modified = true
    @_asset_stamp = nil
    asset_bundle.make_bundle
  end

  asset_destination_path
end

#extnameObject



63
64
65
# File 'lib/jekyll/minibundle/bundle_file.rb', line 63

def extname
  ".#{@type}"
end

#modified?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/jekyll/minibundle/bundle_file.rb', line 71

def modified?
  @is_modified
end

#modified_timeObject



67
68
69
# File 'lib/jekyll/minibundle/bundle_file.rb', line 67

def modified_time
  @asset_paths.map { |f| File.stat(f).mtime }.max
end

#write(site_destination_dir) ⇒ Object

allows writing destination only after ‘destination_path_for_markup` has been called



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jekyll/minibundle/bundle_file.rb', line 77

def write(site_destination_dir)
  if modified?
    dst_path = destination(site_destination_dir)
    Files.copy_p(path, dst_path)

    # respect user's umask; Ruby's tempfile has mode 0o600
    File.chmod(0o666 & ~File.umask, dst_path)

    @is_modified = false
    true
  else
    false
  end
end