class BowerRails::Dsl

Constants

DEFAULT_DEPENDENCY_GROUP

Attributes

dependencies[R]
root_path[R]

Public Class Methods

eval_file_method() click to toggle source
# File lib/bower-rails/dsl.rb, line 15
def self.eval_file_method
  BowerRails.use_gem_deps_for_bowerfile ? :eval_file_with_deps : :eval_file
end
evalute(root_path, filename) click to toggle source
# File lib/bower-rails/dsl.rb, line 9
def self.evalute(root_path, filename)
  new(root_path).tap do |dsl|
    dsl.send(eval_file_method, File.join(root_path, filename))
  end
end
new(root_path) click to toggle source
# File lib/bower-rails/dsl.rb, line 21
def initialize(root_path)
  @dependency_groups = []
  @root_path = root_path
  @bower_dependencies_list = []
  @dependencies = {}
  @resolutions = {}
  @assets_path ||= "assets"
  @main_files = {}
  @current_group = nil
end

Public Instance Methods

asset(name, *args, &block) click to toggle source
# File lib/bower-rails/dsl.rb, line 32
def asset(name, *args, &block)
  @asset_name = name
  group = @current_group || default_group
  options = Hash === args.last ? args.pop.dup : {}

  version = args.last || "latest"
  version = options[:ref] if options[:ref]

  options[:git] = "git://github.com/#{options[:github]}" if options[:github]

  if options[:git]
    version = if version == 'latest'
                options[:git]
              else
                options[:git] + "#" + version
              end
  end

  if options[:main_files]
    main_files(options[:main_files])
  end

  instance_eval(&block) if block_given?

  normalized_group_path = normalize_location_path(group.first, group_assets_path(group))
  @dependencies[normalized_group_path] ||= {}
  @dependencies[normalized_group_path][current_dependency_group_normalized] ||= {}
  @dependencies[normalized_group_path][current_dependency_group_normalized][name] = version
end
dependency_group(name, options = {}) { || ... } click to toggle source
# File lib/bower-rails/dsl.rb, line 62
def dependency_group(name, options = {}, &block)

  assert_dependency_group_name name
  add_dependency_group name

  yield if block_given?

  remove_dependency_group!
end
directories() click to toggle source
# File lib/bower-rails/dsl.rb, line 72
def directories
  @dependencies.keys
end
eval_file(file) click to toggle source
# File lib/bower-rails/dsl.rb, line 76
def eval_file(file)
  instance_eval(File.open(file, 'rb') { |f| f.read }, file.to_s)
end
eval_file_with_deps(file) click to toggle source
# File lib/bower-rails/dsl.rb, line 80
def eval_file_with_deps(file)
  Gem::Specification.map do |dep|
    bowerfile_in_dep = File.join(dep.gem_dir, 'Bowerfile')
    eval_file(bowerfile_in_dep) if bowerfile_in_dep != file && File.exist?(bowerfile_in_dep)
  end
  eval_file(file)
end
final_assets_path() click to toggle source
# File lib/bower-rails/dsl.rb, line 88
def final_assets_path
  groups.map do |group|
    [group.first.to_s, group_assets_path(group)]
  end.uniq
end
generate_dotbowerrc() click to toggle source
# File lib/bower-rails/dsl.rb, line 94
def generate_dotbowerrc
  contents = JSON.parse(File.read(File.join(root_path, '.bowerrc'))) rescue {}
  contents["directory"] = "bower_components"
  JSON.pretty_generate(contents)
end
group(name, options = {}) { || ... } click to toggle source
# File lib/bower-rails/dsl.rb, line 104
def group(name, options = {}, &block)
  options[:assets_path] ||= @assets_path

  assert_asset_path options[:assets_path]
  assert_group_name name

  @current_group = add_group name, options
  yield if block_given?
end
group_assets_path(group) click to toggle source
# File lib/bower-rails/dsl.rb, line 100
def group_assets_path group
  group.last[:assets_path]
end
main_files(value = nil) click to toggle source

accepts string or array to alter main option for bower.json

# File lib/bower-rails/dsl.rb, line 144
def main_files(value = nil)
  if value
    @main_files[@asset_name] = Array(value)
  end
  @main_files
end
resolution(name, version) click to toggle source
# File lib/bower-rails/dsl.rb, line 114
def resolution(name, version)
  @resolutions[name] = version
end
resolutions_with_root() click to toggle source
# File lib/bower-rails/dsl.rb, line 118
def resolutions_with_root
  { :resolutions => @resolutions }
end
write_bower_json() click to toggle source
# File lib/bower-rails/dsl.rb, line 122
def write_bower_json
  @dependencies.each do |dir, data|

    FileUtils.mkdir_p dir unless File.directory? dir
    File.open(File.join(dir, "bower.json"), "w") do |f|
      data.merge!(resolutions_with_root)
      f.write(dependencies_to_json(data))
    end
  end
end
write_dotbowerrc() click to toggle source
# File lib/bower-rails/dsl.rb, line 133
def write_dotbowerrc
  groups.map do |group|
    normalized_group_path = normalize_location_path(group.first, group_assets_path(group))
    File.open(File.join(normalized_group_path, ".bowerrc"), "w") do |f|
      f.write(generate_dotbowerrc)
      f.write("\n")
    end
  end
end

Private Instance Methods

add_dependency_group(dependency_group) click to toggle source

Stores the dependency group name in the stack

# File lib/bower-rails/dsl.rb, line 155
def add_dependency_group(dependency_group)
  @dependency_groups.push dependency_group.to_sym

  dependency_group
end
add_group(*group) click to toggle source
# File lib/bower-rails/dsl.rb, line 161
def add_group(*group)
  @groups = (groups << group) and return group
end
assert_asset_path(path) click to toggle source
# File lib/bower-rails/dsl.rb, line 176
def assert_asset_path(path)
  unless path.start_with?('assets', '/assets')
    raise ArgumentError, "Assets should be stored in /assets directory, try assets_path 'assets/#{path}' instead"
  end
end
assert_dependency_group_name(name) click to toggle source
# File lib/bower-rails/dsl.rb, line 165
def assert_dependency_group_name(name)
  unless [:dependencies, :devDependencies].include?(normalize_dependency_group_name(name))
    raise ArgumentError, "Dependency group should be either dependencies or dev_dependencies, provided: #{name}"
  end
end
assert_group_name(name) click to toggle source
# File lib/bower-rails/dsl.rb, line 182
def assert_group_name name
  raise ArgumentError, "Group name should be :lib or :vendor only" unless [:lib, :vendor].include?(name)
end
assets_path(assets_path) click to toggle source
# File lib/bower-rails/dsl.rb, line 171
def assets_path(assets_path)
  assert_asset_path assets_path
  @assets_path = assets_path
end
current_dependency_group() click to toggle source

Returns name for the current dependency from the stack

# File lib/bower-rails/dsl.rb, line 188
def current_dependency_group
  @dependency_groups.last || DEFAULT_DEPENDENCY_GROUP.to_sym
end
current_dependency_group_normalized() click to toggle source

Returns normalized current dependency group name

# File lib/bower-rails/dsl.rb, line 194
def current_dependency_group_normalized
  normalize_dependency_group_name current_dependency_group
end
default_group() click to toggle source
# File lib/bower-rails/dsl.rb, line 198
def default_group
  [:vendor, { :assets_path => @assets_path }]
end
dependencies_to_json(data) click to toggle source

Attempts to parse data from @dependencies to JSON

# File lib/bower-rails/dsl.rb, line 204
def dependencies_to_json(data)
  JSON.pretty_generate({
    :name => "dsl-generated-dependencies"
  }.merge(data))
end
groups() click to toggle source
# File lib/bower-rails/dsl.rb, line 210
def groups
  @groups ||= [default_group]
end
normalize_dependency_group_name(name) click to toggle source

Implementing ActiveSupport::Inflector camelize(:lower)

# File lib/bower-rails/dsl.rb, line 216
def normalize_dependency_group_name(name)
  segments = name.to_s.dup.downcase.split(/_/)
  [segments.shift, *segments.map{ |word| word.capitalize }].join('').to_sym
end
normalize_location_path(loc, assets_path) click to toggle source
# File lib/bower-rails/dsl.rb, line 221
def normalize_location_path(loc, assets_path)
  File.join(root_path, loc.to_s, assets_path)
end
remove_dependency_group!() click to toggle source

Removes the dependency group name in the stack

# File lib/bower-rails/dsl.rb, line 227
def remove_dependency_group!
  @dependency_groups.pop
end