Class: Jammer::PackageCreator
- Inherits:
-
Object
- Object
- Jammer::PackageCreator
- Defined in:
- lib/jammer/package_creator.rb
Instance Attribute Summary collapse
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Instance Method Summary collapse
- #base_destination_dir ⇒ Object
- #base_template_dir ⇒ Object
- #create_directory(destination, template_dir) ⇒ Object
- #create_file(destination, template_file) ⇒ Object
- #create_files ⇒ Object
- #create_package(package_info) ⇒ Object
- #dot_dir ⇒ Object
- #file_name(file) ⇒ Object
- #file_path(dir, file_name) ⇒ Object
- #get_binding(package, user) ⇒ Object
-
#initialize(file_utils = FileUtils, user = Jammer::User.new) ⇒ PackageCreator
constructor
A new instance of PackageCreator.
- #make_dir(name) ⇒ Object
- #prepare_directory ⇒ Object
- #remove_dir(name) ⇒ Object
- #render(template) ⇒ Object
- #template_binding ⇒ Object
- #templates(dir) ⇒ Object
- #write_file(file_destination, file_contents) ⇒ Object
- #write_files(destination, template_dir) ⇒ Object
Constructor Details
#initialize(file_utils = FileUtils, user = Jammer::User.new) ⇒ PackageCreator
Returns a new instance of PackageCreator.
9 10 11 12 |
# File 'lib/jammer/package_creator.rb', line 9 def initialize(file_utils = FileUtils, user = Jammer::User.new) @file_utils = file_utils @user = user end |
Instance Attribute Details
#package ⇒ Object (readonly)
Returns the value of attribute package.
7 8 9 |
# File 'lib/jammer/package_creator.rb', line 7 def package @package end |
Instance Method Details
#base_destination_dir ⇒ Object
88 89 90 |
# File 'lib/jammer/package_creator.rb', line 88 def base_destination_dir File.join Dir.pwd, package.name end |
#base_template_dir ⇒ Object
84 85 86 |
# File 'lib/jammer/package_creator.rb', line 84 def base_template_dir File.join(File.dirname(__FILE__), 'templates') end |
#create_directory(destination, template_dir) ⇒ Object
32 33 34 35 |
# File 'lib/jammer/package_creator.rb', line 32 def create_directory(destination, template_dir) make_dir destination write_files destination, template_dir end |
#create_file(destination, template_file) ⇒ Object
53 54 55 56 57 |
# File 'lib/jammer/package_creator.rb', line 53 def create_file(destination, template_file) file_destination = file_path(destination, file_name(template_file)) template = ERB.new(File.read(template_file)) write_file(file_destination, render(template)) end |
#create_files ⇒ Object
28 29 30 |
# File 'lib/jammer/package_creator.rb', line 28 def create_files create_directory base_destination_dir, base_template_dir end |
#create_package(package_info) ⇒ Object
14 15 16 17 18 |
# File 'lib/jammer/package_creator.rb', line 14 def create_package(package_info) @package = package_info prepare_directory create_files end |
#dot_dir ⇒ Object
92 93 94 |
# File 'lib/jammer/package_creator.rb', line 92 def dot_dir ->(dir) { dir == '.' || dir == '..' } end |
#file_name(file) ⇒ Object
71 72 73 |
# File 'lib/jammer/package_creator.rb', line 71 def file_name(file) File.basename file, '.erb' #strip ".erb" off template file name end |
#file_path(dir, file_name) ⇒ Object
75 76 77 78 |
# File 'lib/jammer/package_creator.rb', line 75 def file_path(dir, file_name) file_name.sub! 'main', package.name File.join dir, file_name end |
#get_binding(package, user) ⇒ Object
67 68 69 |
# File 'lib/jammer/package_creator.rb', line 67 def get_binding(package, user) Kernel.binding end |
#make_dir(name) ⇒ Object
37 38 39 |
# File 'lib/jammer/package_creator.rb', line 37 def make_dir(name) @file_utils.mkdir_p name end |
#prepare_directory ⇒ Object
20 21 22 |
# File 'lib/jammer/package_creator.rb', line 20 def prepare_directory remove_dir(package.name) if File.exists? package.name end |
#remove_dir(name) ⇒ Object
24 25 26 |
# File 'lib/jammer/package_creator.rb', line 24 def remove_dir(name) @file_utils.rm_rf name, verbose: false, secure: true end |
#render(template) ⇒ Object
59 60 61 |
# File 'lib/jammer/package_creator.rb', line 59 def render(template) template.result(template_binding) end |
#template_binding ⇒ Object
63 64 65 |
# File 'lib/jammer/package_creator.rb', line 63 def template_binding @binding ||= get_binding(@package, @user) end |
#templates(dir) ⇒ Object
80 81 82 |
# File 'lib/jammer/package_creator.rb', line 80 def templates(dir) Dir.entries(dir).delete_if(&dot_dir) end |
#write_file(file_destination, file_contents) ⇒ Object
96 97 98 |
# File 'lib/jammer/package_creator.rb', line 96 def write_file(file_destination, file_contents) File.open(file_destination, 'w') { |file| file.write file_contents } end |
#write_files(destination, template_dir) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jammer/package_creator.rb', line 41 def write_files(destination, template_dir) templates(template_dir).each do |template| template_file = File.join(template_dir, template) if File.directory?(template_file) dir = file_path(destination, template) create_directory(dir, template_file) else create_file destination, template_file end end end |