class BooticCli::Themes::FSTheme

Constants

ASSETS_DIR
ASSET_PATTERNS
TEMPLATE_PATTERNS
Template
ThemeAsset

Attributes

dir[R]

Public Class Methods

new(dir, subdomain: nil) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 62
def initialize(dir, subdomain: nil)
  @dir = dir
  @setup = false
  @subdomain = subdomain
end
resolve_file(path, workdir) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 46
def self.resolve_file(path, workdir)
  file = File.new(path)
  type = resolve_type(path)

  # initialize a new asset or template as it might be a new file
  item = if path =~ /assets\//
    file_name = File.basename(path)
    ThemeAsset.new(file_name, file, file.mtime.utc)
  else
    file_name = resolve_path(path, workdir)
    Template.new(file_name, file.read, file.mtime.utc)
  end

  [item, type]
end
resolve_path(path, dir) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 36
def self.resolve_path(path, dir)
  File.expand_path(path).sub(File.expand_path(dir) + '/', '')
end
resolve_type(path) click to toggle source

 helper to resolve the right type (Template or Asset) from a local path

this is not part of the generic Theme interface
# File lib/bootic_cli/themes/fs_theme.rb, line 42
def self.resolve_type(path)
  path =~ /assets\// ? :asset : :template
end

Public Instance Methods

add_asset(file_name, file) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 140
def add_asset(file_name, file)
  setup
  path = File.join(dir, ASSETS_DIR, file_name)
  File.open(path, 'wb') do |io|
    io.write file.read
  end

  @assets = nil
end
add_template(file_name, body) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 113
def add_template(file_name, body)
  setup
  path = File.join(dir, file_name)

  # remove DOS line endings for new templates
  # or for existing ones that don't have any.
  if !File.exist?(path) or !has_dos_line_endings?(path)
    body = body.gsub(/\r\n?/, "\n")
  end

  dir = File.dirname(path)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)

  File.open(path, 'w') do |io|
    io.write(body)
  end

  @templates = nil
end
assets() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 103
def assets
  @assets ||= (
    paths_for(ASSET_PATTERNS).sort.map do |path|
      fname = File.basename(path)
      file = File.new(path)
      ThemeAsset.new(fname, file, file.mtime.utc)
    end
  )
end
path() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 83
def path
  File.expand_path(dir)
end
reload!() click to toggle source

Implement generic Theme interface

# File lib/bootic_cli/themes/fs_theme.rb, line 88
def reload!
  @templates = nil
  @assets = nil
end
remove_asset(file_name) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 150
def remove_asset(file_name)
  path = File.join(dir, ASSETS_DIR, file_name)
  return false unless File.exists?(path)

  File.unlink path
  @assets = nil
end
remove_template(file_name) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 133
def remove_template(file_name)
  path = File.join(dir, file_name)
  return false unless File.exists?(path)
  File.unlink path
  @templates = nil
end
reset!() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 78
def reset!
  return false unless @setup
  FileUtils.rm_rf dir
end
subdomain() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 68
def subdomain
  @subdomain || read_subdomain
end
templates() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 93
def templates
  @templates ||= (
    paths_for(TEMPLATE_PATTERNS).sort.map do |path|
      name = self.class.resolve_path(path, dir)
      file = File.new(path)
      Template.new(name, file.read, file.mtime.utc)
    end
  )
end
write_subdomain() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 72
def write_subdomain
  store.transaction do
    store['subdomain'] = @subdomain
  end
end

Private Instance Methods

has_dos_line_endings?(path) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 162
def has_dos_line_endings?(path)
  !!IO.read(path)["\r\n"]
end
paths_for(patterns) click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 166
def paths_for(patterns)
  patterns.reduce([]) do |m, pattern|
    m + Dir[File.join(dir, pattern)]
  end
end
read_subdomain() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 187
def read_subdomain
  store.transaction { store['subdomain'] }
end
setup() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 172
def setup
  return self if @setup
  FileUtils.mkdir_p dir
  FileUtils.mkdir_p File.join(dir, ASSETS_DIR)
  @setup = true
  self
end
store() click to toggle source
# File lib/bootic_cli/themes/fs_theme.rb, line 180
def store
  @store ||= (
    setup
    YAML::Store.new(File.join(path, '.state'))
  )
end