lib/marv/builder.rb in marv-0.2.5 vs lib/marv/builder.rb in marv-0.3.0

- old
+ new

@@ -4,18 +4,18 @@ require 'zip' require 'marv/engines' module Marv class Builder + def initialize(project) @project = project @task = project.task @templates_path = @project.templates_path @assets_path = @project.assets_path @functions_path = @project.functions_path @includes_path = @project.includes_path - @extras_path = @project.extras_path @package_path = @project.package_path init_sprockets end @@ -23,11 +23,11 @@ def build clean_build_directory copy_templates copy_functions copy_includes - copy_extras + copy_folders build_assets end # Use the rubyzip library to build a zip from the generated source def zip(filename=nil) @@ -95,35 +95,44 @@ end end end def clean_functions - FileUtils.rm File.join(@project.build_path, 'functions.php') - FileUtils.rm_rf File.join(@project.build_path, 'functions') + #remove functions php + FileUtils.rm File.join(@project.build_path, 'functions.php') if File.exists?(File.join(@project.build_path, 'functions.php')) + # Remove plugin file + FileUtils.rm File.join(@project.build_path, @project.project_php_file) if File.exists?(@project.project_php_file) + # Remove functions folder + FileUtils.rm_rf File.join(@project.build_path, 'functions') if File.directory?(File.join(@project.build_path, 'functions')) end def copy_functions functions_erb_path = File.join(@functions_path, 'functions.php.erb') functions_php_path = File.join(@functions_path, 'functions.php') + plugin_php_path = File.join(@functions_path, @project.project_php_file) if File.exists?(functions_erb_path) destination = File.join(@project.build_path, 'functions.php') write_erb(functions_erb_path, destination) elsif File.exists?(functions_php_path) FileUtils.cp functions_php_path, @project.build_path end + if File.exists?(plugin_php_path) + FileUtils.cp plugin_php_path, @project.build_path + end + functions_paths = Dir.glob(File.join(@functions_path, '*')).reject do |filename| - [functions_erb_path, functions_php_path].include?(filename) + [functions_erb_path, functions_php_path, plugin_php_path].include?(filename) end unless functions_paths.empty? # Create the functions folder in the build directory FileUtils.mkdir_p(File.join(@project.build_path, 'functions')) # Iterate over all files in source/functions, skipping the actual functions.php file - paths = Dir.glob(File.join(@functions_path, '**', '*')).reject {|filename| [functions_erb_path, functions_php_path].include?(filename) } + paths = Dir.glob(File.join(@functions_path, '**', '*')).reject {|filename| [functions_erb_path, functions_php_path, plugin_php_path].include?(filename) } copy_paths_with_erb(paths, @functions_path, File.join(@project.build_path, 'functions')) end end @@ -140,25 +149,49 @@ paths = Dir.glob(File.join(@includes_path, '**', '*')) copy_paths_with_erb(paths, @includes_path, File.join(@project.build_path, 'includes')) end end - def copy_extras - unless Dir.glob(File.join(@extras_path, '*')).empty? + def clean_folders + Dir.glob(File.join(@project.source_path, '*')).each do |folder| + unless [@assets_path, @templates_path, @functions_path, @includes_path].include?(folder) + if File.directory?(folder) + relative_path = folder.gsub(@project.source_path, '') + destination = File.join(@project.build_path, relative_path) - # Iterate over all files in source/extras, so we can exclude if necessary - paths = Dir.glob(File.join(@extras_path, '**', '*')) - copy_paths_with_erb(paths, @extras_path, File.join(@project.build_path, '')) + FileUtils.rm_rf destination + end + end end end + def copy_folders + Dir.glob(File.join(@project.source_path, '*')).each do |folder| + unless [@assets_path, @templates_path, @functions_path, @includes_path].include?(folder) + if File.directory?(folder) + paths = Dir.glob(File.join(folder, '**', '*')) + copy_paths_with_erb(paths, @project.source_path, @project.build_path) + end + end + end + end + def clean_images FileUtils.rm_rf File.join(@project.build_path, 'images') end def build_assets - [['style.css'], ['admin.css'], ['javascripts', 'theme.js'], ['javascripts', 'admin.js']].each do |asset| + default_assets = [['style.css'], ['admin.css'], ['javascripts', 'theme.js'], ['javascripts', 'admin.js']] + additional_assets = @project.config[:additional_assets] + + if additional_assets + assets = default_assets + additional_assets + else + assets = default_assets + end + + assets.each do |asset| destination = File.join(@project.build_path, asset) # Catch any sprockets errors and continue the process begin @task.shell.mute do @@ -167,11 +200,11 @@ FileUtils.mkdir_p(File.dirname(destination)) unless File.directory?(File.dirname(destination)) sprocket.write_to(destination) unless sprocket.nil? end rescue Exception => e @task.say "Error while building #{asset.last}:" - @task.say e.message, Thor::Shell::Color::RED + @task.say e.message, :red File.open(destination, 'w') do |file| file.puts(e.message) end @@ -271,11 +304,12 @@ @project.parse_erb(source) end end rescue Exception => e @task.say "Error while building #{File.basename(source)}:" - @task.say e.message + "\n", Thor::Shell::Color::RED + @task.say e.message + "\n", :red exit - end + end end + end end