Class: Bartender::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/bartender/site.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Load the config, and compile the site



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bartender/site.rb', line 8

def initialize(config)
  Bartender.configure(config)

  @sprockets_env = Sprockets::Environment.new
  @sprockets_env.append_path Bartender::DEFAULTS["assets"]
  add_asset_url
  compile_assets

  Dir.glob(File.join(Bartender::DEFAULTS["pages"], '/**/*')).each do |page|
     compile_page(page) unless page.split('/')[-1].match /^_/ #compile the page unless it is a partial
  end
end

Class Method Details

.create(name) ⇒ Object

Function compile_asset



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bartender/site.rb', line 47

def self.create(name)
  name = File.join(Bartender::DEFAULTS["root"], name)
  if File.directory?(name)
    $stderr.puts "ERROR: Could not create a new site by that name (#{name}), it already exists!"
    exit 0
  else #no conflicts with the site name, create some shit
    Dir.mkdir(name) #make new site dir
    Dir.mkdir(File.join(name, Bartender::DEFAULTS["output"]))
    Bartender::DEFAULTS.values_at("layouts", "pages", "assets").each do |dir| #make default directories
      Dir.mkdir(File.join(name, dir))
    end

    #make asset directories
    ["js", "css", "images"].each do |asset_dir|
      Dir.mkdir(File.join(File.join(name, Bartender::DEFAULTS["assets"]), asset_dir)) #this is where un-comiled assets go
      Dir.mkdir(File.join(File.join(name, Bartender::DEFAULTS["output"]), asset_dir)) #this is where compiled assets will go
    end

    #copy over the default files
    templates = File.join(File.dirname(__FILE__), *%w[.. .. templates])
    FileUtils.cp(File.join(templates, "app.js"), File.join(File.join(name, Bartender::DEFAULTS["assets"]), "js"))     #app.js
    FileUtils.cp(File.join(templates, "app.css"), File.join(File.join(name, Bartender::DEFAULTS["assets"]), "css"))   #app.css
    FileUtils.cp(File.join(templates, "application.html.erb"), File.join(name, Bartender::DEFAULTS["layouts"]))       #application.html.erb
    FileUtils.cp(File.join(templates, "index.html.erb"), File.join(name, Bartender::DEFAULTS["pages"]))               #index.html.erb

  end

end

Instance Method Details

#add_asset_urlObject

add the asset_url method so that



77
78
79
80
81
82
83
84
# File 'lib/bartender/site.rb', line 77

def add_asset_url
  @sprockets_env.context_class.class_eval do
    def asset_url(filename)
      asset = Bartender::Asset.new(filename, environment)
      asset.site_path if asset.found
    end
  end
end

#compile_asset(asset) ⇒ Object

compile each asset expects a file name to be passed to it



39
40
41
42
43
44
45
# File 'lib/bartender/site.rb', line 39

def compile_asset(asset)
  asset = Bartender::Asset.new(asset.gsub(File.join(Bartender::DEFAULTS["assets"], '/'), ''), @sprockets_env)

  if asset.found #make sure sprockets can find the asset
    asset.sprockets_object.write_to(asset.dest_path)
  end
end

#compile_assetsObject

compile the css and js assets for use in the page uses sprokets set an environment, compile them, then get accessed



29
30
31
32
33
34
35
# File 'lib/bartender/site.rb', line 29

def compile_assets
  Dir.glob(File.join(Bartender::DEFAULTS["output"], '/**/*')).each {|asset| File.delete(asset) unless File.directory?(asset)}#delete the old files
  Dir.glob(File.join(Bartender::DEFAULTS["assets"], '/**/*')).each do |asset|
    asset.gsub!(/\..*$/, '') unless asset.scan("images").length > 0 #remove the extensions, let's let sprockets tell us what the final extension should be
    compile_asset(asset)
  end
end

#compile_page(page) ⇒ Object

compile the page expects to be passed a path to a tilt template



23
24
25
# File 'lib/bartender/site.rb', line 23

def compile_page(page)
  Page.new(page, @sprockets_env).compile
end