class Wrap::Bootstrap::Rails
Constants
- DIRNAME_MAP
- FG_RED
- VERSION
Public Class Methods
new(target_dir, gem_name)
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 13 def initialize(target_dir, gem_name) @wrap_dir = File.expand_path(target_dir) @gem_name = gem_name end
Public Instance Methods
create_gem!()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 18 def create_gem! check_gem_dir_existence check_bundler_existence system("bundle", "gem", gem_name) enginize_gem add_railtie_dependency copy_assets fix_asset_references write_readme end
Private Instance Methods
abort_execution(message)
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 193 def abort_execution(message) puts "\033[#{FG_RED}m#{message} Aborting.\033[0m" exit end
add_railtie_dependency()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 50 def add_railtie_dependency gemspec_path = File.join(gem_dir, "#{gem_name}.gemspec") lines = File.read(gemspec_path).split("\n") lines.insert(-2, " spec.add_dependency 'railties'") File.write(gemspec_path, lines.join("\n")) end
app_path()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 198 def app_path @app_path ||= File.join(gem_dir, "app") end
check_bundler_existence()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 187 def check_bundler_existence if `which bundle`.empty? abort_execution("Could not find bundle in your PATH.") end end
check_gem_dir_existence()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 181 def check_gem_dir_existence if File.exists?(gem_dir) abort_execution("Gem create destination ('#{gem_dir}') already exists.") end end
copy_all_in(src_dir, dst_dir)
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 171 def copy_all_in(src_dir, dst_dir) Dir.foreach(src_dir) do |dir| next if dir.match(/^\./) target_path = File.join(src_dir, dir) dst_path = File.join(dst_dir, dir) FileUtils.mv(target_path, dst_path) end end
copy_assets()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 58 def copy_assets asset_src_path = File.join(wrap_dir, "assets") FileUtils.mkdir_p(app_path) FileUtils.cp_r(asset_src_path, app_path) rename_directories separate_namespace end
enginize_gem()
click to toggle source
Define Rails::Engine to export vendor/assets directory to rails app.
# File lib/wrap/bootstrap/rails.rb, line 34 def enginize_gem source = <<-EOS.unindent require "#{gem_name}/version" module #{gem_name.capitalize} module Rails class Engine < ::Rails::Engine end end end EOS gem_path = File.join(gem_dir, "lib", "#{gem_name}.rb") File.write(gem_path, source) end
fix_asset_references()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 67 def fix_asset_references Find.find(app_path) do |path| next unless path.match(/\.css$/) update_url_for_asset_pipeline(path) end end
gem_dir()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 206 def gem_dir @gem_dir ||= File.join(File.expand_path("."), gem_name) end
gem_name()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 202 def gem_name @gem_name end
rename_directories()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 146 def rename_directories DIRNAME_MAP.each do |org_name, new_name| org_path = File.join(app_path, "assets", org_name) if File.exists?(org_path) new_path = File.join(app_path, "assets", new_name) FileUtils.mv(org_path, new_path) end end end
separate_namespace()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 156 def separate_namespace working_dir = File.join("/tmp", "wrap-bootstrap-rails", gem_name) vendor_assets_path = File.join(app_path, "assets") Dir.foreach(vendor_assets_path) do |dir| next if dir.match(/^\./) target_dir = File.join(vendor_assets_path, dir) FileUtils.rm_rf(working_dir) FileUtils.mkdir_p(working_dir) copy_all_in(target_dir, working_dir) FileUtils.mv(working_dir, target_dir) end end
update_url_for_asset_pipeline(path)
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 130 def update_url_for_asset_pipeline(path) source = File.read(path) source.gsub!(/url\((\"|\')(.+?)(\'|\")\)/) do url = $2 case url when /^\.\.\// url.gsub!(/^\.\.\/[^\/]+/, "/assets/#{gem_name}") when /^[^\/]+\// url.gsub!(/^[^\/]+/, "/assets/#{gem_name}") end "url('#{url}')" end File.write(path, source) end
wrap_dir()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 210 def wrap_dir @wrap_dir end
write_readme()
click to toggle source
# File lib/wrap/bootstrap/rails.rb, line 74 def write_readme html_path = File.join(wrap_dir, "html", "index.html") return unless File.exists?(html_path) html = File.read(html_path) js_paths = [] html.scan(/<script src="([^\"]+)"><\/script>/) do raw_url = $1 js_paths << raw_url.gsub(/^\.\.\/assets\/js/, gem_name) end css_paths = [] html.scan(/<link rel="stylesheet" href="([^\"]+)" \/>/) do raw_url = $1 css_paths << raw_url.gsub(/^\.\.\/assets\/css/, gem_name) end source = <<-EOS.unindent # #{gem_name.capitalize} Wrap Bootstrap design template - #{gem_name.capitalize} This gem is generated by [wrap-bootstrap-rails](https://github.com/k0kubun/wrap-bootstrap-rails) ## Usage 1. You have to purchase a license to use this design template at [WrapBootstrap](https://wrapbootstrap.com/) 2. Add this line to your application's Gemfile: ```ruby gem '#{gem_name}', git: 'git://github.com/#{gem_name}/#{gem_name}.git' ``` ## Javascripts You may want to require: ``` #{js_paths.map{ |path| '//= require ' + path }.join("\n ")} ``` ## Stylesheets You may want to require: ``` /* #{css_paths.map{ |path| ' *= require ' + path }.join("\n ")} */ ``` EOS readme_path = File.join(gem_dir, "README.md") File.write(readme_path, source) end