lib/uki/project.rb in uki-1.0.2 vs lib/uki/project.rb in uki-1.1.0

- old
+ new

@@ -1,14 +1,16 @@ require 'fileutils' require 'commander/import' require 'erb' require 'pathname' -require 'uki/include_js' +require 'uki/builder' require 'base64' require 'digest/md5' class Uki::Project + CJS_REGEXP = %r{=\s*["']?(([^"' ]+).cjs)} + attr_accessor :dest def initialize dest @dest = dest end @@ -24,18 +26,38 @@ copy_templates copy_images init_jspec if options[:jspec] end - def build target, options = {} - target = File.join(dest, target) unless Pathname.new(target).absolute? - init_target target - containers = find_containers - cjs = extract_cjs(containers) - build_js cjs, target, options - build_containers containers, target, options - build_images target, options + def build files, options = {} + cjs = [] + containers = [] + files.each do |file| + path = to_path(file) + next unless File.exists?(path) + if file.match(/.html$/) + cjs += extract_cjs(path) + containers << path + else + cjs << path + end + end + cjs.uniq! + containers.uniq! + + output = to_path(options[:output]) + FileUtils.rm_rf output if options[:clean_output] + FileUtils.mkdir_p output + + cjs.each do |path| + File.open(File.join(output, File.basename(path)), 'w') do |f| + f.write( Uki::Builder.new(path, :compress => options[:compress]).code ) + end + end + + build_containers containers, output + build_images output if options[:images] end def create_class template, fullName path = fullName.split('.') create_packages path[0..-2] @@ -63,29 +85,33 @@ end place end protected + def to_path file + Pathname.new(file).absolute? ? file : File.join(dest, file) + end + def write_class template, path package_name = path[0..-2].join('.') class_name = path[-1] class_name = class_name[0,1].upcase + class_name[1..-1] file_name = class_name[0,1].downcase + class_name[1..-1] - target = File.join *(path[0..-2] + [file_name]) + target = File.join( *(path[0..-2] + [file_name]) ) target += '.js' File.open(File.join(dest, target), 'w') do |f| f.write template(template).result(binding) end add_include(target) end def write_function template, path package_name = path[0..-2].join('.') function_name = path[-1] - function_name = function_name[0,1].downcase + function_name[1..-1] + function_name = function_name[0,1].downcase + function_name[1..-1] file_name = function_name - target = File.join *(path[0..-2] + [file_name]) + target = File.join( *(path[0..-2] + [file_name]) ) target += '.js' File.open(File.join(dest, target), 'w') do |f| f.write template(template).result(binding) end add_include(target) @@ -112,60 +138,30 @@ end end end end - def build_js cjs, target, options - cjs.each do |c| - c = c.sub('.cjs', '.js') - code = Uki.include_js File.join(dest, c) - FileUtils.mkdir_p File.dirname(c) - filename = File.join(target, c) - File.open(filename, 'w') do |f| - f.write code - end - compile_js filename if options[:compile] - end - end - - def compile_js file - system "java -jar #{path_to_google_compiler} --js #{file} > #{file}.tmp" - FileUtils.rm file - FileUtils.mv "#{file}.tmp", file - end - - def build_containers containers, target, options + def build_containers containers, output containers.each do |c| - code = File.read(c).gsub(%r{=\s*["']?(([^"' ]+).cjs)}) do |match| - md5 = Digest::MD5.file(File.join(target, "#{$2}.js")).hexdigest + code = File.read(c).gsub(CJS_REGEXP) do |match| + md5 = Digest::MD5.file(File.join(output, "#{$2}.js")).hexdigest match.sub('.cjs', ".js?#{md5}") end - File.open(File.join(target, File.basename(c)), 'w') do |f| + File.open(File.join(output, File.basename(c)), 'w') do |f| f.write code end end end - def build_images target, options - FileUtils.cp_r File.join(dest, 'i'), File.join(target, 'i') + def build_images output + FileUtils.cp_r File.join(dest, 'i'), File.join(output, 'i') end - def extract_cjs containers - containers.map do |c| - File.read(c).scan(%r{=\s*["']?([^"' ]+.cjs)}) - end.flatten.uniq + def extract_cjs container + File.read(container).scan(CJS_REGEXP).map { |match| match[0].sub('.cjs', '.js') } end - def find_containers - Dir.glob File.join(dest, '*.html') - end - - def init_target target - FileUtils.rm_rf target - FileUtils.mkdir_p target - end - def init_dest FileUtils.mkdir_p File.join(dest, project_name) ['view', 'model', 'layout', 'controller'].each do |name| FileUtils.mkdir_p File.join(dest, project_name, name) end @@ -238,10 +234,6 @@ def copy_images FileUtils.cp_r images_path, File.join(dest, 'i') end - def path_to_google_compiler - File.join(UKI_ROOT, 'frameworks', 'uki', 'compiler.jar') - end - end \ No newline at end of file