Class: BooticCli::Themes::FSTheme

Inherits:
Object
  • Object
show all
Defined in:
lib/bootic_cli/themes/fs_theme.rb

Defined Under Namespace

Classes: Template, ThemeAsset

Constant Summary collapse

ASSETS_DIR =
'assets'.freeze
TEMPLATE_PATTERNS =
[
  '*.html', '*.css', '*.js', '*.json', '*.yml',
  'sections/*.html', 'partials/*.html', 'data/*.json', 'data/*.yml'
].freeze
ASSET_PATTERNS =
[File.join(ASSETS_DIR, '*')].freeze
ASSET_PATH_REGEX =
/^assets\/[^\/]+$/.freeze
TEMPLATE_PATH_REGEX =
/^[^\/]+\.(html|css|scss|js|json|yml)$/.freeze
SECTION_PATH_REGEX =
/^(sections|partials)\/[^\/]+\.html$/.freeze
DATA_PATH_REGEX =
/^data\/[^\/]+\.(json|yml)$/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, subdomain: nil) ⇒ FSTheme

Returns a new instance of FSTheme.



78
79
80
81
82
# File 'lib/bootic_cli/themes/fs_theme.rb', line 78

def initialize(dir, subdomain: nil)
  @dir = dir
  @setup = false
  @subdomain = subdomain
end

Class Method Details

.resolve_file(path, workdir) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bootic_cli/themes/fs_theme.rb', line 60

def self.resolve_file(path, workdir)
  unless type = resolve_type(path, workdir)
    return # neither an asset or template
  end

  # initialize a new asset or template as it might be a new file
  file = File.new(path)
  item = if type == :asset
    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) ⇒ Object



44
45
46
# File 'lib/bootic_cli/themes/fs_theme.rb', line 44

def self.resolve_path(path, dir)
  File.expand_path(path).sub(File.expand_path(dir) + '/', '')
end

.resolve_type(path, dir) ⇒ Object

helper to resolve the right type (Template or Asset) from a local path this is not part of the generic Theme interface



50
51
52
53
54
55
56
57
58
# File 'lib/bootic_cli/themes/fs_theme.rb', line 50

def self.resolve_type(path, dir)
  relative_path = resolve_path(path, dir)

  if relative_path[ASSET_PATH_REGEX]
    :asset
  elsif relative_path[TEMPLATE_PATH_REGEX] || relative_path[SECTION_PATH_REGEX] || relative_path[DATA_PATH_REGEX]
    :template
  end
end

Instance Method Details

#add_asset(file_name, file) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/bootic_cli/themes/fs_theme.rb', line 158

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) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bootic_cli/themes/fs_theme.rb', line 131

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

#assetsObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bootic_cli/themes/fs_theme.rb', line 119

def assets
  @assets ||= (
    paths_for(ASSET_PATTERNS).sort.map do |path|
      if File.file?(path)
        fname = File.basename(path)
        file = File.new(path)
        ThemeAsset.new(fname, file, file.mtime.utc)
      end
    end.compact
  )
end

#pathObject



99
100
101
# File 'lib/bootic_cli/themes/fs_theme.rb', line 99

def path
  File.expand_path(dir)
end

#reload!Object

Implement generic Theme interface



104
105
106
107
# File 'lib/bootic_cli/themes/fs_theme.rb', line 104

def reload!
  @templates = nil
  @assets = nil
end

#remove_asset(file_name) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/bootic_cli/themes/fs_theme.rb', line 168

def remove_asset(file_name)
  path = File.join(dir, ASSETS_DIR, file_name)
  return false unless File.exist?(path)

  File.unlink path
  @assets = nil
end

#remove_template(file_name) ⇒ Object



151
152
153
154
155
156
# File 'lib/bootic_cli/themes/fs_theme.rb', line 151

def remove_template(file_name)
  path = File.join(dir, file_name)
  return false unless File.exist?(path)
  File.unlink path
  @templates = nil
end

#reset!Object



94
95
96
97
# File 'lib/bootic_cli/themes/fs_theme.rb', line 94

def reset!
  return false unless @setup
  FileUtils.rm_rf dir
end

#subdomainObject



84
85
86
# File 'lib/bootic_cli/themes/fs_theme.rb', line 84

def subdomain
  @subdomain || read_subdomain
end

#templatesObject



109
110
111
112
113
114
115
116
117
# File 'lib/bootic_cli/themes/fs_theme.rb', line 109

def templates
  @templates ||= (
    paths_for(TEMPLATE_PATTERNS).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_subdomainObject



88
89
90
91
92
# File 'lib/bootic_cli/themes/fs_theme.rb', line 88

def write_subdomain
  store.transaction do
    store['subdomain'] = @subdomain
  end
end