lib/generators/actions.rb in padrino-gen-0.1.1 vs lib/generators/actions.rb in padrino-gen-0.1.2

- old
+ new

@@ -1,7 +1,8 @@ module Padrino module Generators + class AppRootNotFound < RuntimeError; end module Actions def self.included(base) base.extend(ClassMethods) end @@ -12,10 +13,19 @@ return true && say("Skipping generator for #{component} component...", :yellow) if choice.to_s == 'none' say "Applying '#{choice}' (#{component})...", :yellow self.class.send(:include, generator_module_for(choice, component)) send("setup_#{component}") if respond_to?("setup_#{component}") end + + # Includes the component module for the given component and choice + # Determines the choice using .components file + # include_component_module_for(:mock) + # include_component_module_for(:mock, 'rr') + def include_component_module_for(component, root=nil, choice=nil) + choice = fetch_component_choice(component, root) unless choice + self.class.send(:include, generator_module_for(choice, component)) + end # Prompts the user if necessary until a valid choice is returned for the component # resolve_valid_choice(:mock) => 'rr' def resolve_valid_choice(component) available_string = self.class.available_choices_for(component).join(", ") @@ -51,9 +61,27 @@ # Loads the component config back into a hash # i.e retrieve_component_config(...) => { :mock => 'rr', :test => 'riot', ... } def retrieve_component_config(target) YAML.load_file(target) + end + + # Returns the component choice stored within the .component file of an application + # fetch_component_choice(:mock) + def fetch_component_choice(component, root=nil) + comp_path = root ? File.join(root, '.components') : '.components' + retrieve_component_config(comp_path)[component] + end + + # Returns true if inside a Padrino application + def in_app_root?(root=nil) + root ? File.exist?(File.join(root, 'config/boot.rb')) : File.exist?('config/boot.rb') + end + + # Returns the app_name for the application at root + def fetch_app_name(root=nil) + app_path = root ? File.join(root, 'app.rb') : 'app.rb' + @app_name ||= File.read(app_path).scan(/class\s(.*?)\s</).flatten[0] end module ClassMethods # Defines a class option to allow a component to be chosen and add to component type list # Also builds the available_choices hash of which component choices are supported