Class: ConvertTheme

Inherits:
Object
  • Object
show all
Defined in:
lib/convert_theme.rb,
lib/convert_theme/cli.rb

Defined Under Namespace

Classes: CLI, ConvertThemeGenerator

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConvertTheme

Returns a new instance of ConvertTheme.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/convert_theme.rb', line 15

def initialize(options = {})
  @template_root  = File.expand_path(options[:template_root] || File.dirname('.'))
  @index_path     = options[:index_path] || "index.html"
  @content_id     = options[:content_id] || "content"
  @stylesheet_dir = options[:stylesheet_dir] || detect_stylesheet_dir
  @javascript_dir = options[:javascript_dir] || detect_javascript_dir
  @image_dir      = options[:image_dir] || detect_image_dir
  @stdout         = options[:stdout] || $stdout
  
  setup_template_temp_path
end

Instance Attribute Details

#content_idObject (readonly)

Returns the value of attribute content_id.



13
14
15
# File 'lib/convert_theme.rb', line 13

def content_id
  @content_id
end

#image_dirObject (readonly)

Returns the value of attribute image_dir.



12
13
14
# File 'lib/convert_theme.rb', line 12

def image_dir
  @image_dir
end

#index_pathObject (readonly)

Returns the value of attribute index_path.



11
12
13
# File 'lib/convert_theme.rb', line 11

def index_path
  @index_path
end

#javascript_dirObject (readonly)

Returns the value of attribute javascript_dir.



12
13
14
# File 'lib/convert_theme.rb', line 12

def javascript_dir
  @javascript_dir
end

#rails_pathObject (readonly)

Returns the value of attribute rails_path.



11
12
13
# File 'lib/convert_theme.rb', line 11

def rails_path
  @rails_path
end

#stylesheet_dirObject (readonly)

Returns the value of attribute stylesheet_dir.



12
13
14
# File 'lib/convert_theme.rb', line 12

def stylesheet_dir
  @stylesheet_dir
end

#template_rootObject (readonly)

Returns the value of attribute template_root.



11
12
13
# File 'lib/convert_theme.rb', line 11

def template_root
  @template_root
end

#template_typeObject (readonly)

Returns the value of attribute template_type.



11
12
13
# File 'lib/convert_theme.rb', line 11

def template_type
  @template_type
end

Instance Method Details

#apply_to(rails_path, options = {}) ⇒ Object



27
28
29
30
31
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
61
62
63
64
65
66
# File 'lib/convert_theme.rb', line 27

def apply_to(rails_path, options = {})
  @rails_path = rails_path
  @template_type = (options[:template_type] || detect_template).to_s

  File.open(File.join(template_temp_path, 'app/views/layouts/application.html.erb'), "w") do |file|
    index_file = File.read(File.join(template_root, index_path)).gsub(/\r/, '')
    doc = Hpricot(index_file)
    doc.search("##{content_id}").each do |div|
      div.inner_html = "<%= yield %>"
    end
    contents = doc.to_html
    contents.gsub!(%r{(["'])/?#{image_dir}}, '\1/images')
    contents.gsub!(%r{(["'])/?#{stylesheet_dir}}, '\1/stylesheets')
    contents.gsub!(%r{(["'])/?#{javascript_dir}}, '\1/javascripts')
    file << contents
  end
  
  template_stylesheets.each do |file|
    File.open(File.join(template_temp_path, 'public/stylesheets', File.basename(file)), "w") do |f|
      contents = File.read(file)
      contents.gsub!(%r{url\((["']?)[./]*#{image_dir}}, 'url(\1/images')
      f << contents
    end
  end
  template_javascripts.each do |file|
    FileUtils.cp_r(file, File.join(template_temp_path, 'public/javascripts'))
  end
  template_images.each do |file|
    FileUtils.cp_r(file, File.join(template_temp_path, 'public/images'))
  end
  
  # now use rubigen to install the files into the rails app
  # so users can get conflict resolution options from command line
  RubiGen::Base.reset_sources
  RubiGen::Base.prepend_sources(RubiGen::PathSource.new(:internal, File.dirname(__FILE__)))
  generator_options = options[:generator] || {}
  generator_options.merge!(:stdout => @stdout, :no_exit => true,
    :source => template_temp_path, :destination => rails_path)
  RubiGen::Scripts::Generate.new.run(["convert_theme"], generator_options)
end

#erb?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/convert_theme.rb', line 89

def erb?
  template_type == 'erb'
end

#haml?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/convert_theme.rb', line 85

def haml?
  template_type == 'haml'
end

#setup_template_temp_pathObject



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

def setup_template_temp_path
  FileUtils.rm_rf(template_temp_path)
  FileUtils.mkdir_p(template_temp_path)
  %w[app/views/layouts public/images public/javascripts public/stylesheets].each do |app_path|
    FileUtils.mkdir_p(File.join(template_temp_path, app_path))
  end
end

#template_temp_pathObject

This generator’s templates folder is temporary and is accessed via source_root within the generator.



70
71
72
73
74
75
# File 'lib/convert_theme.rb', line 70

def template_temp_path
  @template_temp_path ||= begin
    tmp_dir = ENV['TMPDIR'] || '/tmp'
    template_path = File.join(tmp_dir, "convert_theme", "templates")
  end
end