1 | require "yaml" |
---|
2 | require "optparse" |
---|
3 | module Blueprint |
---|
4 | class Compressor |
---|
5 | TEST_FILES = ["index.html", |
---|
6 | "parts/elements.html", |
---|
7 | "parts/forms.html", |
---|
8 | "parts/grid.html", |
---|
9 | "parts/sample.html"] |
---|
10 | |
---|
11 | attr_accessor :namespace, :custom_css, :custom_layout, :semantic_classes, :project_name, :plugins |
---|
12 | attr_reader :custom_path, :loaded_from_settings, :destination_path, :script_name |
---|
13 | |
---|
14 | # overridden setter method for destination_path |
---|
15 | # also sets custom_path flag on Blueprint::Compressor instance |
---|
16 | def destination_path=(path) |
---|
17 | @destination_path = path |
---|
18 | @custom_path = @destination_path != Blueprint::BLUEPRINT_ROOT_PATH |
---|
19 | end |
---|
20 | |
---|
21 | # constructor |
---|
22 | def initialize |
---|
23 | # set up defaults |
---|
24 | @script_name = File.basename($0) |
---|
25 | @loaded_from_settings = false |
---|
26 | @settings_file = Blueprint::SETTINGS_FILE |
---|
27 | self.namespace = "" |
---|
28 | self.destination_path = Blueprint::BLUEPRINT_ROOT_PATH |
---|
29 | self.custom_layout = CustomLayout.new |
---|
30 | self.project_name = nil |
---|
31 | self.custom_css = {} |
---|
32 | self.semantic_classes = {} |
---|
33 | self.plugins = [] |
---|
34 | |
---|
35 | self.options.parse!(ARGV) |
---|
36 | initialize_project_from_yaml(self.project_name) |
---|
37 | end |
---|
38 | |
---|
39 | # generates output CSS based on any args passed in |
---|
40 | # overwrites any existing CSS, as well as grid.png and tests |
---|
41 | def generate! |
---|
42 | output_header # information to the user (in the console) describing custom settings |
---|
43 | generate_css_files # loops through Blueprint::CSS_FILES to generate output CSS |
---|
44 | generate_tests # updates HTML with custom namespaces in order to test the generated library. TODO: have tests kick out to custom location |
---|
45 | output_footer # informs the user that the CSS generation process is complete |
---|
46 | end |
---|
47 | |
---|
48 | def options #:nodoc:# |
---|
49 | OptionParser.new do |o| |
---|
50 | o.set_summary_indent(" ") |
---|
51 | o.banner = "Usage: #{@script_name} [options]" |
---|
52 | o.define_head "Blueprint Compressor" |
---|
53 | o.separator "" |
---|
54 | o.separator "options" |
---|
55 | o.on( "-fSETTINGS_FILE", "--settings_file=SETTINGS_FILE", String, |
---|
56 | "Specify a non-default settings file path.") { |file| @settings_file = file } |
---|
57 | o.on( "-oOUTPUT_PATH", "--output_path=OUTPUT_PATH", String, |
---|
58 | "Define a different path to output generated CSS files to.") { |path| self.destination_path = path } |
---|
59 | o.on( "-nBP_NAMESPACE", "--namespace=BP_NAMESPACE", String, |
---|
60 | "Define a namespace prepended to all Blueprint classes (e.g. .your-ns-span-24)") { |ns| self.namespace = ns } |
---|
61 | o.on( "-pPROJECT_NAME", "--project=PROJECT_NAME", String, |
---|
62 | "If using the settings.yml file, PROJECT_NAME is the project name you want to export") {|project| @project_name = project } |
---|
63 | o.on( "--column_width=COLUMN_WIDTH", Integer, |
---|
64 | "Set a new column width (in pixels) for the output grid") {|cw| self.custom_layout.column_width = cw } |
---|
65 | o.on( "--gutter_width=GUTTER_WIDTH", Integer, |
---|
66 | "Set a new gutter width (in pixels) for the output grid") {|gw| self.custom_layout.gutter_width = gw } |
---|
67 | o.on( "--column_count=COLUMN_COUNT", Integer, |
---|
68 | "Set a new column count for the output grid") {|cc| self.custom_layout.column_count = cc } |
---|
69 | #o.on("-v", "--verbose", "Turn on verbose output.") { |$verbose| } |
---|
70 | o.on("-h", "--help", "Show this help message.") { puts o; exit } |
---|
71 | end |
---|
72 | end |
---|
73 | |
---|
74 | private |
---|
75 | |
---|
76 | # attempts to load output settings from settings.yml |
---|
77 | def initialize_project_from_yaml(project_name = nil) |
---|
78 | # ensures project_name is set and settings.yml is present |
---|
79 | return unless (project_name && File.exist?(@settings_file)) |
---|
80 | |
---|
81 | # loads yaml into hash |
---|
82 | projects = YAML::load(File.path_to_string(@settings_file)) |
---|
83 | |
---|
84 | if (project = projects[project_name]) # checks to see if project info is present |
---|
85 | self.destination_path = if self.destination_path == Blueprint::BLUEPRINT_ROOT_PATH |
---|
86 | project["path"] |
---|
87 | else |
---|
88 | self.destination_path |
---|
89 | end |
---|
90 | self.destination_path ||= Blueprint::BLUEPRINT_ROOT_PATH |
---|
91 | self.namespace = project["namespace"] || "" |
---|
92 | self.custom_css = project["custom_css"] || {} |
---|
93 | self.semantic_classes = project["semantic_classes"] || {} |
---|
94 | self.plugins = project["plugins"] || [] |
---|
95 | |
---|
96 | if (layout = project["custom_layout"]) |
---|
97 | self.custom_layout = CustomLayout.new(:column_count => layout["column_count"], |
---|
98 | :column_width => layout["column_width"], |
---|
99 | :gutter_width => layout["gutter_width"], |
---|
100 | :input_padding => layout["input_padding"], |
---|
101 | :input_border => layout["input_border"]) |
---|
102 | end |
---|
103 | @loaded_from_settings = true |
---|
104 | end |
---|
105 | end |
---|
106 | |
---|
107 | def generate_css_files |
---|
108 | Blueprint::CSS_FILES.each do |output_file_name, css_source_file_names| |
---|
109 | css_output_path = File.join(destination_path, output_file_name) |
---|
110 | puts "\n Assembling to #{custom_path ? css_output_path : "default blueprint path"}" |
---|
111 | |
---|
112 | # CSS file generation |
---|
113 | css_output = css_file_header # header included on all three Blueprint-generated files |
---|
114 | css_output += "\n\n" |
---|
115 | |
---|
116 | # Iterate through src/ .css files and compile to individual core compressed file |
---|
117 | css_source_file_names.each do |css_source_file| |
---|
118 | puts " + src/#{css_source_file}" |
---|
119 | css_output += "/* #{css_source_file} */\n" if css_source_file_names.any? |
---|
120 | |
---|
121 | source_options = if self.custom_layout && css_source_file == "grid.css" |
---|
122 | self.custom_layout.generate_grid_css |
---|
123 | else |
---|
124 | File.path_to_string File.join(Blueprint::SOURCE_PATH, css_source_file) |
---|
125 | end |
---|
126 | |
---|
127 | css_output += Blueprint::CSSParser.new(source_options, |
---|
128 | :namespace => namespace).to_s |
---|
129 | css_output += "\n" |
---|
130 | end |
---|
131 | |
---|
132 | #append CSS from plugins |
---|
133 | css_output = append_plugin_css(css_output, output_file_name) |
---|
134 | |
---|
135 | # append CSS from custom files |
---|
136 | css_output = append_custom_css(css_output, output_file_name) |
---|
137 | |
---|
138 | #save CSS to correct path, stripping out any extra whitespace at the end of the file |
---|
139 | File.string_to_file(css_output.rstrip, css_output_path) |
---|
140 | end |
---|
141 | |
---|
142 | # append semantic class names if set |
---|
143 | append_semantic_classes |
---|
144 | |
---|
145 | #attempt to generate a grid.png file |
---|
146 | if (grid_builder = GridBuilder.new(:column_width => self.custom_layout.column_width, |
---|
147 | :gutter_width => self.custom_layout.gutter_width, |
---|
148 | :output_path => File.join(self.destination_path, "src"))) |
---|
149 | grid_builder.generate! |
---|
150 | end |
---|
151 | end |
---|
152 | |
---|
153 | def append_custom_css(css, current_file_name) |
---|
154 | # check to see if a custom (non-default) location was used for output files |
---|
155 | # if custom path is used, handle custom CSS, if any |
---|
156 | return css unless self.custom_path and self.custom_css[current_file_name] |
---|
157 | |
---|
158 | self.custom_css[current_file_name].each do |custom_css| |
---|
159 | overwrite_base = custom_css || "my-#{current_file_name}" |
---|
160 | overwrite_path = File.join(destination_path, overwrite_base) |
---|
161 | overwrite_css = if File.exists?(overwrite_path) |
---|
162 | File.path_to_string(overwrite_path) |
---|
163 | else |
---|
164 | "" |
---|
165 | end |
---|
166 | |
---|
167 | # if there's CSS present, add it to the CSS output |
---|
168 | unless overwrite_css.blank? |
---|
169 | puts " + custom styles (#{custom_css})\n" |
---|
170 | css += "/* #{overwrite_base} */\n" |
---|
171 | css += CSSParser.new(overwrite_css).to_s + "\n" |
---|
172 | end |
---|
173 | end |
---|
174 | |
---|
175 | css |
---|
176 | end |
---|
177 | |
---|
178 | def append_plugin_css(css, current_file_name) |
---|
179 | return css unless self.plugins.any? |
---|
180 | |
---|
181 | plugin_css = "" |
---|
182 | |
---|
183 | self.plugins.each do |plugin| |
---|
184 | plugin_file_specific = File.join(Blueprint::PLUGINS_PATH, plugin, current_file_name) |
---|
185 | plugin_file_generic = File.join(Blueprint::PLUGINS_PATH, plugin, "#{plugin}.css") |
---|
186 | |
---|
187 | file = if File.exists?(plugin_file_specific) |
---|
188 | plugin_file_specific |
---|
189 | elsif File.exists?(plugin_file_generic) && current_file_name =~ /^screen|print/ |
---|
190 | plugin_file_generic |
---|
191 | end |
---|
192 | |
---|
193 | if file |
---|
194 | puts " + #{plugin} plugin\n" |
---|
195 | plugin_css += "/* #{plugin} */\n" |
---|
196 | plugin_css += CSSParser.new(File.path_to_string(file)).to_s + "\n" |
---|
197 | |
---|
198 | Dir.glob(File.join(File.dirname(file), "**", "**")).each do |cp| |
---|
199 | short_path = cp.gsub(/#{File.dirname(file)}./ , "") |
---|
200 | # make directory if it doesn't exist |
---|
201 | directory = File.dirname(short_path) |
---|
202 | if directory != "." |
---|
203 | FileUtils.mkdir_p(File.join(destination_path, directory)) |
---|
204 | end |
---|
205 | |
---|
206 | unless File.directory?(File.join(File.dirname(file), short_path)) || cp == file |
---|
207 | FileUtils.cp(cp, File.join(destination_path, short_path)) |
---|
208 | end |
---|
209 | end |
---|
210 | end |
---|
211 | end |
---|
212 | |
---|
213 | css += plugin_css |
---|
214 | end |
---|
215 | |
---|
216 | def append_semantic_classes |
---|
217 | screen_output_path = File.join(self.destination_path, "screen.css") |
---|
218 | semantic_classes = SemanticClassNames.new(:namespace => self.namespace, |
---|
219 | :source_file => screen_output_path) |
---|
220 | semantic_styles = semantic_classes.css_from_assignments(self.semantic_classes) |
---|
221 | return if semantic_styles.blank? |
---|
222 | |
---|
223 | css = File.path_to_string(screen_output_path) |
---|
224 | css += "\n\n/* semantic class names */\n" |
---|
225 | css += semantic_styles |
---|
226 | File.string_to_file(css.rstrip, screen_output_path) |
---|
227 | end |
---|
228 | |
---|
229 | def generate_tests |
---|
230 | puts "\n Updating namespace to \"#{namespace}\" in test files:" |
---|
231 | test_files = Compressor::TEST_FILES.map do |file| |
---|
232 | File.join(Blueprint::TEST_PATH, *file.split(/\//)) |
---|
233 | end |
---|
234 | |
---|
235 | test_files.each do |file| |
---|
236 | puts " + #{file}" |
---|
237 | Namespace.new(file, namespace) |
---|
238 | end |
---|
239 | end |
---|
240 | |
---|
241 | def output_header |
---|
242 | puts "\n" |
---|
243 | puts " #{"*" * 100}" |
---|
244 | puts " **" |
---|
245 | puts " ** Blueprint CSS Compressor" |
---|
246 | puts " **" |
---|
247 | puts " ** Builds compressed files from the source directory." |
---|
248 | puts " **" |
---|
249 | puts " ** Loaded from settings.yml" if loaded_from_settings |
---|
250 | puts " ** Namespace: '#{namespace}'" unless namespace.blank? |
---|
251 | puts " ** Output to: #{destination_path}" |
---|
252 | puts " ** Grid Settings:" |
---|
253 | puts " ** - Column Count: #{self.custom_layout.column_count}" |
---|
254 | puts " ** - Column Width: #{self.custom_layout.column_width}px" |
---|
255 | puts " ** - Gutter Width: #{self.custom_layout.gutter_width}px" |
---|
256 | puts " ** - Total Width : #{self.custom_layout.page_width}px" |
---|
257 | puts " **" |
---|
258 | puts " #{"*" * 100}" |
---|
259 | end |
---|
260 | |
---|
261 | def output_footer |
---|
262 | puts "\n\n" |
---|
263 | puts " #{"*" * 100}" |
---|
264 | puts " **" |
---|
265 | puts " ** Done!" |
---|
266 | puts " ** Your compressed files and test files are now up-to-date." |
---|
267 | puts " **" |
---|
268 | puts " #{"*" * 100}\n\n" |
---|
269 | end |
---|
270 | |
---|
271 | def css_file_header |
---|
272 | %(/* ----------------------------------------------------------------------- |
---|
273 | |
---|
274 | |
---|
275 | Blueprint CSS Framework 1.0 |
---|
276 | http://blueprintcss.org |
---|
277 | |
---|
278 | * Copyright (c) 2007-Present. See LICENSE for more info. |
---|
279 | * See README for instructions on how to use Blueprint. |
---|
280 | * For credits and origins, see AUTHORS. |
---|
281 | * This is a compressed file. See the sources in the 'src' directory. |
---|
282 | |
---|
283 | ----------------------------------------------------------------------- */) |
---|
284 | end |
---|
285 | |
---|
286 | def putsv(str) |
---|
287 | puts str if $verbose |
---|
288 | end |
---|
289 | end |
---|
290 | end |
---|