Class: Temp::Copier
- Inherits:
-
Object
- Object
- Temp::Copier
- Defined in:
- lib/temp/copier.rb
Overview
A Copier object provides various methods for creating a project from a template. It can also be given a path to the directory where templates can be found.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#create_project(project = nil, template) ⇒ Object
Creates a new project from a template.
-
#find_files(template, ignore = []) ⇒ Object
Returns an array of all files in a template, optionally ignoring all files in ignore.
-
#initialize(options = {}) ⇒ Copier
constructor
A new instance of Copier.
-
#read_tempignore(template) ⇒ Object
Returns an array of files in a template to ignore.
Constructor Details
#initialize(options = {}) ⇒ Copier
Returns a new instance of Copier.
12 13 14 15 |
# File 'lib/temp/copier.rb', line 12 def initialize( = {}) @options = @template_dir = File.([:template_dir] || '~/.temp') end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/temp/copier.rb', line 10 def @options end |
Instance Method Details
#create_project(project = nil, template) ⇒ Object
Creates a new project from a template. If a path is supplied, the project’s name will be the last item in the path. Otherwise, it will have the same name as the template and be created in the current working directory.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/temp/copier.rb', line 21 def create_project(project = nil, template) project = File.(project || template) template_path = File.(File.join(@template_dir, template)) raise 'project already exists' if File.exist? project raise 'template does not exist' unless File.exist? template_path ignore = read_tempignore(template) files = find_files(template, ignore) if File.file? template_path FileUtils.cp(template_path, project) else FileUtils.mkdir(project) files.each do |file| p = File.join(project, file) t = File.join(template_path, file) if File.directory? t FileUtils.mkdir(p) else FileUtils.cp(t, p) end end end end |
#find_files(template, ignore = []) ⇒ Object
Returns an array of all files in a template, optionally ignoring all files in ignore.
50 51 52 53 54 55 |
# File 'lib/temp/copier.rb', line 50 def find_files(template, ignore = []) template = File.(File.join(@template_dir, template)) Dir.glob(File.join(template, '**/*')).map do |file| file.sub(template + '/', '') end - ignore end |
#read_tempignore(template) ⇒ Object
Returns an array of files in a template to ignore.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/temp/copier.rb', line 58 def read_tempignore(template) template = File.(File.join(@template_dir, template)) tempignore = File.join(template, '.tempignore') files = ['.tempignore'] if File.exist? tempignore files |= File.read(tempignore).split(?\n).map do |line| Dir.glob(File.join(template, line)).map do |file| file.sub(template + '/', '') end end.flatten end files end |