class Forge::Project

Attributes

config[RW]
root[RW]
task[RW]

Public Class Methods

create(root, config, task) click to toggle source
# File lib/forge/project.rb, line 7
def create(root, config, task)
  root = File.expand_path(root)

  project = self.new(root, task, config)
  Generator.run(project)

  project
end
new(root, task, config={}, config_file=nil) click to toggle source
# File lib/forge/project.rb, line 19
def initialize(root, task, config={}, config_file=nil)
  @root        = File.expand_path(root)
  @config      = config || {}
  @task        = task
  @config_file = config_file

  self.load_config if @config.empty?
end

Public Instance Methods

assets_path() click to toggle source
# File lib/forge/project.rb, line 28
def assets_path
  @assets_path ||= File.join(self.source_path, 'assets')
end
build_path() click to toggle source
# File lib/forge/project.rb, line 32
def build_path
  File.join(self.root, '.forge', 'build')
end
config_file() click to toggle source
# File lib/forge/project.rb, line 56
def config_file
  @config_file ||= File.join(self.root, 'config.rb')
end
functions_path() click to toggle source
# File lib/forge/project.rb, line 48
def functions_path
  File.join(self.source_path, 'functions')
end
get_binding() click to toggle source
# File lib/forge/project.rb, line 103
def get_binding
  binding
end
global_config_file() click to toggle source
# File lib/forge/project.rb, line 60
def global_config_file
  @global_config_file ||= File.join(ENV['HOME'], '.forge', 'config.rb')
end
includes_path() click to toggle source
# File lib/forge/project.rb, line 52
def includes_path
  File.join(self.source_path, 'includes')
end
load_config() click to toggle source
# File lib/forge/project.rb, line 79
def load_config
  config = {}

  # Check for global (user) config.rb
  if File.exists?(self.global_config_file)
    config.merge!(load_ruby_config(self.global_config_file))
  end

  # Check for config.rb
  if File.exists?(self.config_file)
    config.merge!(load_ruby_config(self.config_file))
  else
    # Old format of config file
    if File.exists?(File.join(self.root, 'config.json'))
      config.merge!(convert_old_config)
    else
      raise Error, "Could not find the config file, are you sure you're in a
      forge project directory?"
    end
  end

  @config = config
end
package_path() click to toggle source
# File lib/forge/project.rb, line 40
def package_path
  File.join(self.root, 'package')
end
parse_erb(file) click to toggle source
# File lib/forge/project.rb, line 107
def parse_erb(file)
  ERB.new(::File.binread(file), nil, '-', '@output_buffer').result(binding)
end
source_path() click to toggle source
# File lib/forge/project.rb, line 36
def source_path
  File.join(self.root, 'source')
end
templates_path() click to toggle source
# File lib/forge/project.rb, line 44
def templates_path
  File.join(self.source_path, 'templates')
end
theme_id() click to toggle source
# File lib/forge/project.rb, line 75
def theme_id
  File.basename(self.root).gsub(/\W/, '_')
end

Private Instance Methods

convert_old_config() click to toggle source
# File lib/forge/project.rb, line 113
def convert_old_config
  require 'json'

  # Let the user know what is going to happen
  @task.say("It looks like you are using the old JSON-format config. Forge will now try converting your config to the new Ruby format.")
  @task.ask(" Press any key to continue...")

  begin
    old_file_name = File.join(self.root, 'config.json')
    # Parse the old config format, convert keys to symbols
    @config = JSON.parse(File.open(old_file_name).read).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

    @task.create_file(@config_file) do
      # Find the config.tt template, and parse it using ERB
      config_template_path = @task.find_in_source_paths(File.join(['config', 'config.tt']))
      parse_erb(File.expand_path(config_template_path))
    end
  rescue Exception => e
    @task.say "Error while building new config file:", Thor::Shell::Color::RED
    @task.say e.message, Thor::Shell::Color::RED
    @task.say "You'll need to either fix the error and try again, or manually convert your config.json file to Ruby format (config.rb)"
    exit
  end

  @task.say "Success! Double-check that all your config values were moved over, and you can now delete config.json.", Thor::Shell::Color::GREEN

  # We now have a Ruby config file, so we can continue loading as normal
  return load_ruby_config(self.config_file)
end
load_ruby_config(file) click to toggle source
# File lib/forge/project.rb, line 143
def load_ruby_config(file)
  config = {}

  begin
    # Config file is just executed as straight ruby
    eval(File.read(file))
  rescue Exception => e
    @task.say "Error while evaluating config file:"
    @task.say e.message, Thor::Shell::Color::RED
  end

  return config
end