/
rails.rb
133 lines (115 loc) · 4.08 KB
/
rails.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module Compass
module Installers
class RailsInstaller < Base
def configuration_defaults
{
:sass_dir => (sass_dir || prompt_sass_dir),
:css_dir => (css_dir || prompt_css_dir),
:images_dir => default_images_dir,
:javascripts_dir => default_javascripts_dir,
:http_stylesheets_path => default_http_stylesheets_path,
:http_javascripts_path => default_http_javascripts_path,
:http_images_path => default_http_images_path
}
end
def write_configuration_files(config_file = nil)
config_file ||= targetize('config/compass.config')
write_file config_file, config_contents
write_file targetize('config/initializers/compass.rb'), initializer_contents
end
def config_files_exist?
File.exists?(targetize('config/compass.config')) &&
File.exists?(targetize('config/initializers/compass.rb'))
end
def prepare
write_configuration_files unless config_files_exist?
end
def finalize(options = {})
if options[:create]
puts <<-NEXTSTEPS
Congratulations! Your rails project has been configured to use Compass.
Sass will automatically compile your stylesheets during the next
page request and keep them up to date when they change.
Make sure you restart your server!
NEXTSTEPS
end
puts "\nNext add these lines to the head of your layouts:\n\n"
puts stylesheet_links
puts "\n(You are using haml, aren't you?)"
end
def default_images_dir
separate("public/images")
end
def default_javascripts_dir
separate("public/javascripts")
end
def default_http_images_path
"/images"
end
def default_http_javascripts_path
"/javascripts"
end
def default_http_stylesheets_path
"/stylesheets"
end
def prompt_sass_dir
recommended_location = separate('app/stylesheets')
default_location = separate('public/stylesheets/sass')
print %Q{Compass recommends that you keep your stylesheets in #{recommended_location}
instead of the Sass default location of #{default_location}.
Is this OK? (Y/n) }
answer = gets.downcase[0]
answer == ?n ? default_location : recommended_location
end
def prompt_css_dir
recommended_location = separate("public/stylesheets/compiled")
default_location = separate("public/stylesheets")
puts
print %Q{Compass recommends that you keep your compiled css in #{recommended_location}/
instead the Sass default of #{default_location}/.
However, if you're exclusively using Sass, then #{default_location}/ is recommended.
Emit compiled stylesheets to #{recommended_location}/? (Y/n) }
answer = gets.downcase[0]
answer == ?n ? default_location : recommended_location
end
def config_contents
Compass.configuration.serialize do |prop, value|
if prop == :project_path
"project_path = RAILS_ROOT if defined?(RAILS_ROOT)\n"
elsif prop == :output_style
""
end
end
end
def initializer_contents
%Q{require 'compass'
# If you have any compass plugins, require them here.
Compass.configuration.parse(File.join(RAILS_ROOT, "config", "compass.config"))
Compass.configuration.environment = RAILS_ENV.to_sym
Compass.configure_sass_plugin!
}
end
def stylesheet_prefix
if css_dir.length >= 19
"#{css_dir[19..-1]}/"
else
nil
end
end
def stylesheet_links
html = "%head\n"
manifest.each_stylesheet do |stylesheet|
ss_line = " = stylesheet_link_tag '#{stylesheet_prefix}#{stylesheet.to.sub(/\.sass$/,'.css')}'"
if stylesheet.options[:media]
ss_line += ", :media => '#{stylesheet.options[:media]}'"
end
if stylesheet.options[:condition]
ss_line = " /[if #{stylesheet.options[:condition]}]\n " + ss_line
end
html << ss_line + "\n"
end
html
end
end
end
end