refactoring site generators (wip)

did committed Jan 14, 2013
commit cf8878c074a9ba5ef1bf4e816c3b1620dd206d07
Showing 10 changed files with 242 additions and 225 deletions
TODO +7 -0
@@ @@ -29,6 +29,8 @@ x content types / liquid
? boilerplate
- copy Bundler / Gemfile
- content types
+ x definitions
+ - data
- push:
- option to select to push only some parts (pages, ...etc)
- --force option
@@ @@ -38,6 +40,11 @@ x content types / liquid
- tests
+ - refactoring:
+ - move the call to the CC generator directly in the builder.rb file
+ - generators/sites -> generators/site
+ - list is only for site generators
+
*** FEATURES ***
- toolbar to:
locomotive/builder/cli.rb b/lib/locomotive/builder/cli.rb +6 -5
@@ @@ -28,6 +28,7 @@ module Locomotive
if path = check_path!
require 'locomotive/builder/generators/content_type'
+ # TODO: move it to the generate method of the builder.rb file
script = Locomotive::Builder::Generators::ContentType.new([name, self.options['path'], fields], {}, {})
script.invoke_all
else
@@ @@ -65,8 +66,8 @@ module Locomotive
desc 'init NAME [PATH]', 'Create a brand new LocomotiveCMS site'
method_option :template, aliases: '-t', type: 'string', default: 'blank', desc: 'instead of building from a blank site, you can have a pre-fetched site with form a template (see the templates command)'
def init(name, path = '.')
- require 'locomotive/builder/generators'
- generator = Locomotive::Builder::Generators.get(options[:template])
+ require 'locomotive/builder/generators/site'
+ generator = Locomotive::Builder::Generators::Site.get(options[:template])
if generator.nil?
say "Unknown site template '#{options[:template]}'", :red
else
@@ @@ -79,11 +80,11 @@ module Locomotive
desc 'list_templates', 'List all the templates to create either a site or a content type'
def list_templates
- require 'locomotive/builder/generators'
- if Locomotive::Builder::Generators.empty?(:site)
+ require 'locomotive/builder/generators/site'
+ if Locomotive::Builder::Generators::Site.empty?
say 'No templates', :red
else
- Locomotive::Builder::Generators.list(:site).each do |info|
+ Locomotive::Builder::Generators::Site.list.each do |info|
say info.name, :bold, false
say " - #{info.description}" unless info.description.blank?
end
locomotive/builder/generators.rb b/lib/locomotive/builder/generators.rb +59 -59
@@ @@ -1,61 +1,61 @@
- require 'locomotive/builder/generators/list'
-
- module Locomotive
- module Builder
-
- module Generators
-
- # Register a generator by adding it to the list of existing generators.
- #
- # @param [ Symbol ] type The type of the generator (:site, :content_type)
- # @param [ String ] name The name of the generator
- # @param [ Class ] klass The class of the generator
- # @param [ String ] description The description of the generator (can be nil)
- #
- # @return [ Boolean ] True if the registration has been successful, false otherwise.
- #
- def self.register(type, name, klass, description = nil)
- Locomotive::Builder::Generators::List.instance.register(type, name, klass, description)
- end
-
- # Return the information about a generator from its name.
- #
- # @param [ String ] name The name of the generator
- #
- # @return [ Object ] The information of the found generator or nil
- #
- def self.get(name)
- Locomotive::Builder::Generators::List.instance.get(name)
- end
-
- # Filter the generators by their type which are for now:
- # :site and :content_type. If the parameter is nil,
- # then all the generators are returned
- #
- # @param [ Symbol ] type The type of the generators or nil.
- #
- # @return [ Array ] The filtered (or not) list of generators
- #
- def self.list(type = nil)
- Locomotive::Builder::Generators::List.instance.filter_by(type)
- end
-
- # Tell if the list of generators is empty or not for
- # a certain kind of generators (or all if the parameter is nil).
- #
- # @param [ Symbol ] type The type of the generators or nil
- #
- # @return [ Boolean ] True if empty
- #
- def self.empty?(type = nil)
- Locomotive::Builder::Generators::List.instance.empty?(type)
- end
-
- end
-
- end
- end
+ # require 'locomotive/builder/generators/list'
+
+ # module Locomotive
+ # module Builder
+
+ # module Generators
+
+ # # Register a generator by adding it to the list of existing generators.
+ # #
+ # # @param [ Symbol ] type The type of the generator (:site, :content_type)
+ # # @param [ String ] name The name of the generator
+ # # @param [ Class ] klass The class of the generator
+ # # @param [ String ] description The description of the generator (can be nil)
+ # #
+ # # @return [ Boolean ] True if the registration has been successful, false otherwise.
+ # #
+ # def self.register(type, name, klass, description = nil)
+ # Locomotive::Builder::Generators::List.instance.register(type, name, klass, description)
+ # end
+
+ # # Return the information about a generator from its name.
+ # #
+ # # @param [ String ] name The name of the generator
+ # #
+ # # @return [ Object ] The information of the found generator or nil
+ # #
+ # def self.get(name)
+ # Locomotive::Builder::Generators::List.instance.get(name)
+ # end
+
+ # # Filter the generators by their type which are for now:
+ # # :site and :content_type. If the parameter is nil,
+ # # then all the generators are returned
+ # #
+ # # @param [ Symbol ] type The type of the generators or nil.
+ # #
+ # # @return [ Array ] The filtered (or not) list of generators
+ # #
+ # def self.list(type = nil)
+ # Locomotive::Builder::Generators::List.instance.filter_by(type)
+ # end
+
+ # # Tell if the list of generators is empty or not for
+ # # a certain kind of generators (or all if the parameter is nil).
+ # #
+ # # @param [ Symbol ] type The type of the generators or nil
+ # #
+ # # @return [ Boolean ] True if empty
+ # #
+ # def self.empty?(type = nil)
+ # Locomotive::Builder::Generators::List.instance.empty?(type)
+ # end
+
+ # end
+
+ # end
+ # end
# call default generators
- require 'locomotive/builder/generators/base'
- require 'locomotive/builder/generators/defaults'
+ # require 'locomotive/builder/generators/base'
+ # require 'locomotive/builder/generators/defaults'
locomotive/builder/generators/base.rb b/lib/locomotive/builder/generators/base.rb +0 -28
@@ @@ -1,28 +0,0 @@
- require 'thor/group'
- require 'active_support'
- require 'active_support/core_ext'
-
- module Locomotive
- module Builder
- module Generators
-
- class Base < Thor::Group
-
- include Thor::Actions
-
- argument :name
- argument :target_path
-
- def self.source_root
- File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'generators', self.name.demodulize.underscore)
- end
-
- def destination
- File.join(target_path, name)
- end
-
- end
-
- end
- end
- end
\ No newline at end of file
locomotive/builder/generators/content_type.rb b/lib/locomotive/builder/generators/content_type.rb +0 -5
@@ @@ -39,13 +39,8 @@ module Locomotive
end
end
- # def destination
- # File.join(target_path, name)
- # end
-
end
- # Locomotive::Builder::Generators.register(:others, :content_type, SimpleContentType)
end
end
end
\ No newline at end of file
locomotive/builder/generators/defaults.rb b/lib/locomotive/builder/generators/defaults.rb +5 -4
@@ @@ -1,5 +1,6 @@
- # sites
- require 'locomotive/builder/generators/sites/blank'
- require 'locomotive/builder/generators/sites/bootstrap'
+ # # sites
+ # require 'locomotive/builder/generators/site/base'
+ # require 'locomotive/builder/generators/site/blank'
+ # require 'locomotive/builder/generators/site/bootstrap'
- require 'locomotive/builder/generators/content_type'
\ No newline at end of file
+ # require 'locomotive/builder/generators/content_type'
\ No newline at end of file
locomotive/builder/generators/list.rb b/lib/locomotive/builder/generators/list.rb +68 -68
@@ @@ -1,79 +1,79 @@
- require 'ostruct'
- require 'singleton'
+ # require 'ostruct'
+ # require 'singleton'
- module Locomotive
- module Builder
- module Generators
- class List
+ # module Locomotive
+ # module Builder
+ # module Generators
+ # class List
- include ::Singleton
+ # include ::Singleton
- attr_accessor :_list
+ # attr_accessor :_list
- def initialize
- self._list = []
- end
+ # def initialize
+ # self._list = []
+ # end
- # Filter the generators by their type which are for now:
- # :site and :content_type. If the parameter is nil,
- # then all the generators are returned
- #
- # @param [ Symbol ] type The type of the generators or nil.
- #
- # @return [ Array ] The filtered (or not) list of generators
- #
- def filter_by(type = nil)
- if type.nil?
- self._list
- else
- self._list.find_all { |entry| entry.type == type.to_sym }
- end
- end
+ # # Filter the generators by their type which are for now:
+ # # :site and :content_type. If the parameter is nil,
+ # # then all the generators are returned
+ # #
+ # # @param [ Symbol ] type The type of the generators or nil.
+ # #
+ # # @return [ Array ] The filtered (or not) list of generators
+ # #
+ # def filter_by(type = nil)
+ # if type.nil?
+ # self._list
+ # else
+ # self._list.find_all { |entry| entry.type == type.to_sym }
+ # end
+ # end
- # Tell if the list of generators is empty or not for
- # a certain kind of generators (or all if the parameter is nil).
- #
- # @param [ Symbol ] type The type of the generators or nil
- #
- # @return [ Boolean ] True if empty
- #
- def empty?(type = nil)
- self.filter_by(type).empty?
- end
+ # # Tell if the list of generators is empty or not for
+ # # a certain kind of generators (or all if the parameter is nil).
+ # #
+ # # @param [ Symbol ] type The type of the generators or nil
+ # #
+ # # @return [ Boolean ] True if empty
+ # #
+ # def empty?(type = nil)
+ # self.filter_by(type).empty?
+ # end
- # Return the information about a generator from its name.
- #
- # @param [ String ] name The name of the generator
- #
- # @return [ Object ] The information of the found generator or nil
- #
- def get(name)
- self._list.detect { |entry| entry.name == name.to_sym }
- end
+ # # Return the information about a generator from its name.
+ # #
+ # # @param [ String ] name The name of the generator
+ # #
+ # # @return [ Object ] The information of the found generator or nil
+ # #
+ # def get(name)
+ # self._list.detect { |entry| entry.name == name.to_sym }
+ # end
- # Register a generator by adding it to the list of existing generators.
- #
- # @param [ Symbol ] type The type of the generator (:site, :content_type)
- # @param [ String ] name The name of the generator
- # @param [ Class ] klass The class of the generator
- # @param [ String ] description The description of the generator (can be nil)
- #
- # @return [ Boolean ] True if the registration has been successful, false otherwise.
- #
- def register(type, name, klass, description = nil)
- return false unless self.get(name).nil?
+ # # Register a generator by adding it to the list of existing generators.
+ # #
+ # # @param [ Symbol ] type The type of the generator (:site, :content_type)
+ # # @param [ String ] name The name of the generator
+ # # @param [ Class ] klass The class of the generator
+ # # @param [ String ] description The description of the generator (can be nil)
+ # #
+ # # @return [ Boolean ] True if the registration has been successful, false otherwise.
+ # #
+ # def register(type, name, klass, description = nil)
+ # return false unless self.get(name).nil?
- self._list << OpenStruct.new({
- type: type.to_sym,
- name: name.to_sym,
- klass: klass,
- description: description ? description.strip.gsub("\n", '') : nil
- })
+ # self._list << OpenStruct.new({
+ # type: type.to_sym,
+ # name: name.to_sym,
+ # klass: klass,
+ # description: description ? description.strip.gsub("\n", '') : nil
+ # })
- self._list.last
- end
+ # self._list.last
+ # end
- end
- end
- end
- end
\ No newline at end of file
+ # end
+ # end
+ # end
+ # end
\ No newline at end of file
locomotive/builder/generators/site.rb b/lib/locomotive/builder/generators/site.rb +97 -0
@@ @@ -0,0 +1,97 @@
+ require 'ostruct'
+ require 'singleton'
+
+ module Locomotive
+ module Builder
+ module Generators
+
+ module Site
+
+ # Register a generator by adding it to the list of existing generators.
+ #
+ # @param [ String ] name The name of the generator
+ # @param [ Class ] klass The class of the generator
+ # @param [ String ] description The description of the generator (can be nil)
+ #
+ # @return [ Boolean ] True if the registration has been successful, false otherwise.
+ #
+ def self.register(name, klass, description = nil)
+ Locomotive::Builder::Generators::Site::List.instance.register(name, klass, description)
+ end
+
+ # Return the information about a generator from its name.
+ #
+ # @param [ String ] name The name of the generator
+ #
+ # @return [ Object ] The information of the found generator or nil
+ #
+ def self.get(name)
+ Locomotive::Builder::Generators::Site::List.instance.get(name)
+ end
+
+ # List all the generators
+ #
+ # @return [ Array ] The filtered (or not) list of generators
+ #
+ def self.list
+ Locomotive::Builder::Generators::Site::List.instance._list
+ end
+
+ # Tell if the list of generators is empty or not .
+ #
+ # @return [ Boolean ] True if empty
+ #
+ def self.empty?
+ Locomotive::Builder::Generators::Site::List.instance._list.empty?
+ end
+
+ class List
+
+ include ::Singleton
+
+ attr_accessor :_list
+
+ def initialize
+ self._list = []
+ end
+
+ # Return the information about a generator from its name.
+ #
+ # @param [ String ] name The name of the generator
+ #
+ # @return [ Object ] The information of the found generator or nil
+ #
+ def get(name)
+ self._list.detect { |entry| entry.name == name.to_sym }
+ end
+
+ # Register a generator by adding it to the list of existing generators.
+ #
+ # @param [ String ] name The name of the generator
+ # @param [ Class ] klass The class of the generator
+ # @param [ String ] description The description of the generator (can be nil)
+ #
+ # @return [ Boolean ] True if the registration has been successful, false otherwise.
+ #
+ def register(name, klass, description = nil)
+ return false unless self.get(name).nil?
+
+ self._list << OpenStruct.new({
+ name: name.to_sym,
+ klass: klass,
+ description: description ? description.strip.gsub("\n", '') : nil
+ })
+
+ self._list.last
+ end
+ end
+
+ end
+
+ end
+ end
+ end
+
+ require 'locomotive/builder/generators/site/base'
+ require 'locomotive/builder/generators/site/blank'
+ require 'locomotive/builder/generators/site/bootstrap'
locomotive/builder/generators/sites/blank.rb b/lib/locomotive/builder/generators/sites/blank.rb +0 -22
@@ @@ -1,22 +0,0 @@
- module Locomotive
- module Builder
- module Generators
- module Sites
-
- class Blank < Locomotive::Builder::Generators::Base
-
- def copy_sources
- directory('.', self.destination, { recursive: true }, {
- name: self.name
- })
- end
-
- end
-
- Locomotive::Builder::Generators.register(:site, :blank, Blank, %{
- A blank LocomotiveCMS site with the minimal files.
- })
- end
- end
- end
- end
\ No newline at end of file
locomotive/builder/generators/sites/bootstrap.rb b/lib/locomotive/builder/generators/sites/bootstrap.rb +0 -34
@@ @@ -1,34 +0,0 @@
- module Locomotive
- module Builder
- module Generators
- module Sites
-
- class Bootstrap < Locomotive::Builder::Generators::Base
-
- def copy_sources
- directory('.', self.destination, { recursive: true }, {
- name: self.name
- })
- end
-
- def choose_haml_over_html
- if yes?('Do you prefer HAML templates ?')
- remove_file File.join(self.destination, 'app/views/pages/index.liquid')
- remove_file File.join(self.destination, 'app/views/pages/404.liquid')
- remove_file File.join(self.destination, 'app/views/snippets/footer.liquid')
- else
- remove_file File.join(self.destination, 'app/views/pages/index.liquid.haml')
- remove_file File.join(self.destination, 'app/views/pages/404.liquid.haml')
- remove_file File.join(self.destination, 'app/views/snippets/footer.liquid.haml')
- end
- end
-
- end
-
- Locomotive::Builder::Generators.register(:site, :bootstrap, Bootstrap, %{
- A LocomotiveCMS site powered by Twitter boostrap.
- })
- end
- end
- end
- end
\ No newline at end of file