Module: Motion::Gem::Command::Template

Defined in:
lib/motion/gem/command/template.rb

Class Method Summary collapse

Class Method Details

.render(template, destination, config) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/motion/gem/command/template.rb', line 8

def render(template, destination, config)
  config.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  pretty_destination = destination.sub("#{Dir.pwd}/", '')
  content = ERB.new(File.read(template)).result(binding)

  if File.exists?(destination) && File.read(destination).eql?(content)
    Motion::Project::App.log "\e[0;34;49midentical\e[0m", pretty_destination
    return
  elsif File.exists?(destination)
    Motion::Project::App.log "\e[0;31;49mconflict\e[0m", "#{pretty_destination} already exists"
    print "Overwrite #{destination}? [Yn] "
    user_input = $stdin.gets.chomp

    if user_input.downcase[0].eql?('n')
      Motion::Project::App.log "\e[0;33;49mskip\e[0m", pretty_destination
      return
    else
      Motion::Project::App.log "\e[0;33;49mforce\e[0m", pretty_destination
      forced = true
    end
  end

  FileUtils.mkdir_p(File.dirname(destination))
  File.open(destination, 'w') do |file|
    file << content
  end

  unless forced
    Motion::Project::App.log "\e[0;32;49mcreate\e[0m", pretty_destination
  end
end

.scaffold_gem(gem_name) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/motion/gem/command/template.rb', line 43

def scaffold_gem(gem_name)
  gem_name = gem_name.chomp('/')
  gem_path = gem_name.tr('-', '/')
  destination = File.join(Dir.pwd, gem_name)

  constant_name  = gem_name.split('_').map(&:capitalize).join
  constant_name  = constant_name.split('-').map(&:capitalize).join('::') if constant_name =~ /-/
  constants      = constant_name.split('::')

  git_user  = `git config user.name`.chomp
  git_email = `git config user.email`.chomp

  config = {
    :gem_name      => gem_name,
    :gem_path      => gem_path,
    :constant_name => constant_name,
    :constants     => constants,
    :author        => git_user,
    :email         => git_email
  }

  if Motion::Project::App.respond_to?(:template)
    template_ext = Motion::Project::App.template
  end
  template_ext ||= 'ios'
  template_root = File.join(File.expand_path('../../templates/', __FILE__), template_ext)

  render File.join(template_root, 'Gemfile.erb'), File.join(destination, 'Gemfile'), config
  render File.join(template_root, 'LICENSE.md.erb'), File.join(destination, 'LICENSE.md'), config
  render File.join(template_root, 'README.md.erb'), File.join(destination, 'README.md'), config
  render File.join(template_root, 'Rakefile.erb'), File.join(destination, 'Rakefile'), config
  render File.join(template_root, 'gitignore.erb'), File.join(destination, '.gitignore'), config
  render File.join(template_root, 'newgem.gemspec.erb'), File.join(destination, "#{gem_name}.gemspec"), config
  render File.join(template_root, 'lib/newgem.rb.erb'), File.join(destination, "lib/#{gem_path}.rb"), config
  render File.join(template_root, 'lib/newgem/version.rb.erb'), File.join(destination, "lib/#{gem_path}/version.rb"), config

  puts "Initializing git repository in #{destination}"
  Dir.chdir(destination) do
    `git init`
    `git add .`
  end
end