Permalink
Browse files

Refactor code generation in Package.

  • Loading branch information...
jcoglan committed Mar 25, 2012
1 parent b97fe66 commit 1c9c77360f06f76435cf9c197a2c42605d400acc
Showing with 43 additions and 27 deletions.
  1. +43 −27 lib/jake/package.rb
View
@@ -30,34 +30,9 @@ def code(build_name, with_header = true)
if packer
packer = packer.merge(:header => head)
-
- if source_map = packer.delete(:source_map)
- output_path = build_path(build_name)
- packer.update(:output_file => output_path)
-
- if source_map == :source
- source_offsets = {}
- code = head ? head + "\n" : ''
-
- files.each do |file|
- source_path = Packr::FileSystem.relative_path(file, output_path)
- source_offsets[source_path] = code.size
- code << Jake.erb(Jake.read(file)).result(@build.helper.scope) << "\n"
- end
-
- packer.update(:source_files => source_offsets)
- else
- source_path = Packr::FileSystem.relative_path(build_path(source_map), output_path)
- packer.update(:source_files => {source_path => 0})
- code = code(source_map)
- end
- else
- code = Jake.erb(source).result(@build.helper.scope)
- end
-
- code = Packr.pack(code, packer)
+ code = code_for_packer(packer, build_name)
else
- code = Jake.erb(source).result(@build.helper.scope)
+ code = compile_erb(source)
code = head + "\n" + code if head
end
@@ -70,6 +45,47 @@ def code(build_name, with_header = true)
cached[:code]
end
+ private
+
+ def code_for_packer(packer, build_name)
+ if source_map = packer.delete(:source_map)
+ code = code_for_source_map(packer, build_path(build_name), source_map)
+ else
+ code = compile_erb(source)
+ end
+ Packr.pack(code, packer)
+ end
+
+ def code_for_source_map(packer, output_path, source_map)
+ packer.update(:output_file => output_path)
+
+ if source_map == :source
+ code_for_original_map(packer)
+ else
+ source_path = Packr::FileSystem.relative_path(build_path(source_map), output_path)
+ packer.update(:source_files => {source_path => 0})
+ code(source_map)
+ end
+ end
+
+ def code_for_original_map(packer)
+ source_offsets = {}
+ code = ''
+
+ files.each do |file|
+ source_path = Packr::FileSystem.relative_path(file, packer[:output_file])
+ source_offsets[source_path] = code.size
+ code << compile_erb(Jake.read(file)) << "\n"
+ end
+
+ packer.update(:source_files => source_offsets)
+ code
+ end
+
+ def compile_erb(template)
+ Jake.erb(template).result(@build.helper.scope)
+ end
+
end
end

0 comments on commit 1c9c773

Please sign in to comment.