class CreateProj::Creator::Creator
Base class for all Creators.
@attr_reader [String] name description of attributes name @attr_reader [Hash] options hash of options from command line input
Attributes
Public Class Methods
Initialize method for class
@param [String] name projects name @param [Hash] options command line options for the project
@return instance of Creator
# File lib/createproj/creator/creator.rb, line 17 def initialize(name, options = {}) @name = name @options = options @precommit_template = '' @precommit_options = {} @gitignore_files = [] end
Public Instance Methods
Add a precommit hook based on a template
@param [String] template name of the template file @param [Hash] options hash to fill in the template
@example Add a linting pre commit hook
add_pre_commit_hook('lint-pre-commit', { linter: 'rubocop', file_ext: '.rb'} )
@return nothing.
# File lib/createproj/creator/creator.rb, line 104 def add_pre_commit_hook(template, options) return if @precommit_template.empty? || @precommit_options.empty? base_path = File.expand_path('../../../../templates', __FILE__) template_path = File.join(base_path, template) hook = Mustache.render(File.read(template_path), options) path_to_git = '.git/hooks/pre-commit' # overwrite precommit hook with templated file File.open(path_to_git, 'w+') do |f| f.write(hook) end # need to update the permissions of the file so can execute FileUtils.chmod('u=rwx, go=rx', path_to_git) end
Create a new directory with the given name
@param [String] name the directory name
@example Create a new directory
create_directory("proj") #=> 'proj' (and dir called proj created)
@return name of directory just created
# File lib/createproj/creator/creator.rb, line 57 def create_directory(name) Dir.mkdir name # To give flexibility to subclasses name end
intended to be overwritten per class
# File lib/createproj/creator/creator.rb, line 130 def extra_setup end
Add a list of files from @gitignore_files
@param [Array] ignore_files array of file_names to write to .gitignore
@example Gitignore files to haskell
gitignore(['cabal.sandbox.config']) #=> writes .gitignore file
@return nothing
# File lib/createproj/creator/creator.rb, line 81 def gitignore(ignore_files) return if ignore_files.empty? file_mode = 'a' file_mode = 'w+' unless File.exist?('.gitignore') File.open('.gitignore', file_mode) do |f| ignore_files.each do |gf| f.write("#{gf}\n") end end end
Initialize a new Git repository in the project directory
@example Create a Git repository
init_git_repository #=> git repository created
@return Git base class (that will not be used)
# File lib/createproj/creator/creator.rb, line 69 def init_git_repository Git.init end
overwrite in subclass
# File lib/createproj/creator/creator.rb, line 126 def install_dependencies end
overwrite in class
# File lib/createproj/creator/creator.rb, line 122 def install_sandbox end
Run instance of creator to create a project with the name and specified options.
A subclass of creator can overwrite any of the methods called in this method if they wish to define a custom behavior
@example Run an instance of the Creator
class
ruby_creator.run #=> ruby project will be created
@return nothing but has side effects
# File lib/createproj/creator/creator.rb, line 35 def run dir_name = create_directory(@name) # all remaining ruby commands done from newly created directory Dir.chdir(dir_name) do init_git_repository add_pre_commit_hook(@precommit_template, @precommit_options) gitignore(@gitignore_files) install_sandbox install_dependencies extra_setup end end