Class: BowerRails::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/bower-rails/dsl.rb

Constant Summary collapse

DEFAULT_DEPENDENCY_GROUP =
:dependencies

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ Dsl

Returns a new instance of Dsl.



21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



19
20
21
# File 'lib/bower-rails/dsl.rb', line 19

def dependencies
  @dependencies
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



19
20
21
# File 'lib/bower-rails/dsl.rb', line 19

def root_path
  @root_path
end

Class Method Details

.eval_file_methodObject



15
16
17
# 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) ⇒ Object



9
10
11
12
13
# 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

Instance Method Details

#asset(name, *args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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 = {}, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
# 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

#directoriesObject



72
73
74
# File 'lib/bower-rails/dsl.rb', line 72

def directories
  @dependencies.keys
end

#eval_file(file) ⇒ Object



76
77
78
# 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) ⇒ Object



80
81
82
83
84
85
86
# 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_pathObject



88
89
90
91
92
# 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_dotbowerrcObject



94
95
96
97
98
# 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 = {}, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



100
101
102
# File 'lib/bower-rails/dsl.rb', line 100

def group_assets_path group
  group.last[:assets_path]
end

#main_files(value = nil) ⇒ Object

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



144
145
146
147
148
149
# 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) ⇒ Object



114
115
116
# File 'lib/bower-rails/dsl.rb', line 114

def resolution(name, version)
  @resolutions[name] = version
end

#resolutions_with_rootObject



118
119
120
# File 'lib/bower-rails/dsl.rb', line 118

def resolutions_with_root
  { :resolutions => @resolutions }
end

#write_bower_jsonObject



122
123
124
125
126
127
128
129
130
131
# 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_dotbowerrcObject



133
134
135
136
137
138
139
140
141
# 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