Class: Cult::Skel

Inherits:
Object
  • Object
show all
Defined in:
lib/cult/skel.rb

Constant Summary collapse

SKEL_DIR =
File.expand_path(File.join(__dir__, '../../skel'))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Skel

Returns a new instance of Skel.



9
10
11
# File 'lib/cult/skel.rb', line 9

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



7
8
9
# File 'lib/cult/skel.rb', line 7

def project
  @project
end

Instance Method Details

#copy!Object



76
77
78
79
80
81
82
83
# File 'lib/cult/skel.rb', line 76

def copy!
  puts "Creating project from skeleton..."
  FileUtils.mkdir_p(project.path)
  skeleton_files.each do |file|
    process_file(file)
  end
  puts
end

#copy_template(name, dst) ⇒ Object



34
35
36
37
38
# File 'lib/cult/skel.rb', line 34

def copy_template(name, dst)
  src = template_file(name)
  dst = project.location_of(dst)
  process_file(src, dst)
end

#process_file(src, dst = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cult/skel.rb', line 41

def process_file(src, dst = nil)
  dst ||= begin
    relative = src.sub(%r/\A#{Regexp.escape(SKEL_DIR)}/, '')
    project.location_of(relative)
  end

  if File.directory?(src)
    return
  end

  dst, data = case src
    when /\.erb\z/
      [ dst.sub(/\.erb\z/, ''),
        template.process(File.read(src), filename: src)]
    else
      [ dst, File.read(src) ]
    end

  display_name = File.basename(dst) == ".keep" ? File.dirname(dst) : dst

  print "  Creating #{display_name}"
  if File.exist?(dst)
    puts " exists, skipped."
    return
  end


  FileUtils.mkdir_p(File.dirname(dst))

  File.write(dst, data)
  File.chmod(File.stat(src).mode, dst)
  puts
end

#skeleton_filesObject

Skeleton files are files that are copied over for a new project. We allow template files to live in the skeleton directory too, but they’re not copied over until needed.



22
23
24
25
26
# File 'lib/cult/skel.rb', line 22

def skeleton_files
  Dir.glob(File.join(SKEL_DIR, "**", "{.*,*}")).reject do |fn|
    fn.match(/template/i)
  end
end

#templateObject



14
15
16
# File 'lib/cult/skel.rb', line 14

def template
  @erb ||= Template.new(project: project)
end

#template_file(name) ⇒ Object



29
30
31
# File 'lib/cult/skel.rb', line 29

def template_file(name)
  File.join(SKEL_DIR, name)
end