Class: SchemaDev::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_dev/gem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Gem

Returns a new instance of Gem.



16
17
18
19
20
# File 'lib/schema_dev/gem.rb', line 16

def initialize(name)
  self.gem_name = name.underscore
  self.gem_module = gem_name.camelize
  self.gem_root = Pathname.new(gem_name)
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



14
15
16
# File 'lib/schema_dev/gem.rb', line 14

def email
  @email
end

#fullnameObject

Returns the value of attribute fullname.



14
15
16
# File 'lib/schema_dev/gem.rb', line 14

def fullname
  @fullname
end

#gem_moduleObject

Returns the value of attribute gem_module.



14
15
16
# File 'lib/schema_dev/gem.rb', line 14

def gem_module
  @gem_module
end

#gem_nameObject

Returns the value of attribute gem_name.



14
15
16
# File 'lib/schema_dev/gem.rb', line 14

def gem_name
  @gem_name
end

#gem_rootObject

Returns the value of attribute gem_root.



14
15
16
# File 'lib/schema_dev/gem.rb', line 14

def gem_root
  @gem_root
end

Class Method Details

.build(name) ⇒ Object



10
11
12
# File 'lib/schema_dev/gem.rb', line 10

def self.build(name)
  new(name).build
end

Instance Method Details

#buildObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/schema_dev/gem.rb', line 22

def build
  ensure_not_in_git
  ensure_doesnt_exist
  get_fullname_and_email
  copy_template
  self.gem_root = self.gem_root.realpath
  rename_files
  substitute_keys
  freshen
  git_init
  puts <<-END.strip_heredoc

     Created #{gem_name}.  Your recommended next steps are:

            $ cd #{gem_name}
            $ bundle install
            $ schema_dev bundle install
            $ schema_dev rspec
  END
end

#copy_templateObject



67
68
69
# File 'lib/schema_dev/gem.rb', line 67

def copy_template
  FileUtils.cp_r Templates.root + "gem", gem_root
end

#dependency(v) ⇒ Object



94
95
96
97
98
99
# File 'lib/schema_dev/gem.rb', line 94

def dependency(v)
  major, minor, patch = v.split('.')
  dep = %Q{"~> #{major}.#{minor}"}
  dep += %Q{, ">= #{v}"} if patch != "0"
  dep
end

#die(msg) ⇒ Object



43
44
45
# File 'lib/schema_dev/gem.rb', line 43

def die(msg)
  abort "schema_dev: #{msg}"
end

#ensure_doesnt_existObject



53
54
55
56
57
# File 'lib/schema_dev/gem.rb', line 53

def ensure_doesnt_exist
  if gem_root.exist?
    die "Cannot create new gem: '#{gem_root}' already exists"
  end
end

#ensure_not_in_gitObject



47
48
49
50
51
# File 'lib/schema_dev/gem.rb', line 47

def ensure_not_in_git
  if system("git rev-parse >& /dev/null")
    die "Cannot create new gem inside existing git worktree; please cd elsewhere"
  end
end

#freshenObject



108
109
110
111
112
# File 'lib/schema_dev/gem.rb', line 108

def freshen
  Dir.chdir gem_root do 
    Runner.new(Config.read).freshen(quiet:true)
  end
end

#get_fullname_and_emailObject



59
60
61
62
63
64
65
# File 'lib/schema_dev/gem.rb', line 59

def get_fullname_and_email
  {'fullname' => 'name', 'email' => 'email' }.each do |myattr, gitattr|
    if (self.send myattr+"=", `git config user.#{gitattr}`.strip).blank?
      die "Who are you?  Please run 'git config --global user.#{gitattr} <your-#{gitattr}>'"
    end
  end
end

#git_initObject



114
115
116
117
118
119
120
# File 'lib/schema_dev/gem.rb', line 114

def git_init
  Dir.chdir gem_name do 
    system "git init"
    system "git add #{gem_root.find.select(&:exist?).join(' ')}"
    system "git commit -m 'Initial skeleton generated by `schema_dev gem #{gem_name}`'"
  end
end

#rename_filesObject



71
72
73
74
75
# File 'lib/schema_dev/gem.rb', line 71

def rename_files
  Dir.glob(gem_root + "**/*GEM_NAME*").each do |path|
    FileUtils.mv path, path.gsub(/GEM_NAME/, gem_name)
  end
end

#schema_monkey_versionObject



101
102
103
104
105
106
# File 'lib/schema_dev/gem.rb', line 101

def schema_monkey_version
  @monkey_version ||= begin
                        gems = JSON.parse Faraday.get('https://rubygems.org/api/v1/versions/schema_monkey.json').body
                        gems.reject(&it["prerelease"]).sort_by{|g| Time.new(g["built_at"])}.last["number"]
                      end
end

#subs(s) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/schema_dev/gem.rb', line 84

def subs(s)
  s = s.gsub('%GEM_NAME%', gem_name)
  s = s.gsub('%GEM_MODULE%', gem_module)
  s = s.gsub('%FULLNAME%', fullname)
  s = s.gsub('%EMAIL%', email)
  s = s.gsub('%SCHEMA_MONKEY_DEPENDENCY%', dependency(schema_monkey_version))
  s = s.gsub('%SCHEMA_DEV_DEPENDENCY%', dependency(SchemaDev::VERSION))
  s = s.gsub('%YEAR%', Time.now.strftime("%Y"))
end

#substitute_keysObject



77
78
79
80
81
82
# File 'lib/schema_dev/gem.rb', line 77

def substitute_keys
  gem_root.find.each do |path|
    next unless path.file?
    path.write subs(path.read)
  end
end