Module: Gamefic::Sdk::Scaffold

Defined in:
lib/gamefic-sdk/scaffold.rb

Defined Under Namespace

Classes: Binder

Class Method Summary collapse

Class Method Details

.build(name, destination, **opts) ⇒ Object

Raises:

  • (LoadError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gamefic-sdk/scaffold.rb', line 46

def build name, destination, **opts
  dir = File.join(SCAFFOLDS_PATH, name)
  raise LoadError, "Scaffold `#{name}` does not exist" unless File.directory?(dir)

  path = Pathname.new('.').join(destination).realdirpath
  data = Binder.new(name: File.basename(path), **opts)
  files = Dir.glob(File.join(dir, '**', '*'), File::FNM_DOTMATCH).select { |entry| File.file?(entry) }
  map = {}
  files.each do |file|
    next if file.include?('/spec') && !opts[:specs]

    rename = File.join(File.dirname(file), File.basename(file)).gsub('__name__', data.name)
    dst = File.join(destination, rename[dir.length..-1])
    raise "Gamefic generation would overwrite existing file #{rename}" if File.file?(dst)

    map[file] = dst
  end
  map.each_pair { |src, dst| custom_copy src, dst, data }
  Bundler.with_unbundled_env do
    Dir.chdir(path) { system 'bundle install' }
  end
end

.custom_copy(origin, destination, data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gamefic-sdk/scaffold.rb', line 34

def custom_copy origin, destination, data
  FileUtils.mkdir_p File.dirname(destination)
  if origin.end_with?('.gf.erb')
    content = render(origin, data)
    return if content.empty?

    File.write destination[0..-8], content
  else
    FileUtils.cp_r origin, destination
  end
end

.render(file, data) ⇒ Object



28
29
30
31
32
# File 'lib/gamefic-sdk/scaffold.rb', line 28

def render(file, data)
  template = File.read(file)
  erb = ERB.new(template, trim_mode: '-')
  erb.result(data.get_binding)
end