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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/schema_dev/gem.rb', line 18

def initialize(name)
  self.gem_name = name.underscore
  self.gem_root = Pathname.new(gem_name)
  if gem_name =~ /^(schema_plus)_(.*)/
    parent, base = [$1, $2]
    self.gem_module = [parent, base].map(&:camelize).join('::')
    self.gem_lib_path = [parent, base].join('/')
    self.gem_parent_name = parent
    self.gem_base_name = base
    @subdir = true
  else
    self.gem_module = gem_name.camelize
    self.gem_lib_path = gem_name
    self.gem_base_name = gem_name
    @subdir = false
  end
  get_fullname_and_email
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#fullnameObject

Returns the value of attribute fullname.



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

def fullname
  @fullname
end

#gem_base_nameObject

Returns the value of attribute gem_base_name.



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

def gem_base_name
  @gem_base_name
end

#gem_lib_pathObject

Returns the value of attribute gem_lib_path.



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

def gem_lib_path
  @gem_lib_path
end

#gem_moduleObject

Returns the value of attribute gem_module.



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

def gem_module
  @gem_module
end

#gem_nameObject

Returns the value of attribute gem_name.



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

def gem_name
  @gem_name
end

#gem_parent_nameObject

Returns the value of attribute gem_parent_name.



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

def gem_parent_name
  @gem_parent_name
end

#gem_rootObject

Returns the value of attribute gem_root.



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

def gem_root
  @gem_root
end

Class Method Details

.build(name) ⇒ Object



12
13
14
# File 'lib/schema_dev/gem.rb', line 12

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

Instance Method Details

#buildObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/schema_dev/gem.rb', line 37

def build
  ensure_not_in_git
  ensure_doesnt_exist
  copy_template
  self.gem_root = self.gem_root.realpath
  rename_files
  fixup_subdir if @subdir
  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



82
83
84
85
# File 'lib/schema_dev/gem.rb', line 82

def copy_template
  FileUtils.cp_r Templates.root + "gem", gem_root
  (gem_root + "gitignore").rename gem_root + ".gitignore"
end

#dependency(v) ⇒ Object



128
129
130
131
132
133
# File 'lib/schema_dev/gem.rb', line 128

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

#die(msg) ⇒ Object



58
59
60
# File 'lib/schema_dev/gem.rb', line 58

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

#ensure_doesnt_existObject



68
69
70
71
72
# File 'lib/schema_dev/gem.rb', line 68

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

#ensure_not_in_gitObject



62
63
64
65
66
# File 'lib/schema_dev/gem.rb', line 62

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

#fixup_subdirObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/schema_dev/gem.rb', line 96

def fixup_subdir
  libdir = gem_root + "lib"
  aside = libdir.to_s + "x"
  subdir = libdir + gem_parent_name

  FileUtils.mv libdir, aside
  libdir.mkpath
  FileUtils.mv aside, subdir
  (gem_root + "lib" + "#{gem_name}.rb").write <<-END.lstrip
    require_relative '#{gem_parent_name}/#{gem_base_name}.rb'
  END
end

#freshenObject



142
143
144
145
146
# File 'lib/schema_dev/gem.rb', line 142

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

#get_fullname_and_emailObject



74
75
76
77
78
79
80
# File 'lib/schema_dev/gem.rb', line 74

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



148
149
150
151
152
153
154
# File 'lib/schema_dev/gem.rb', line 148

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

#rename_filesObject



87
88
89
90
91
92
93
94
# File 'lib/schema_dev/gem.rb', line 87

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

#schema_plus_core_versionObject



135
136
137
138
139
140
# File 'lib/schema_dev/gem.rb', line 135

def schema_plus_core_version
  @core_version ||= begin
                        gems = JSON.parse Faraday.get('https://rubygems.org/api/v1/versions/schema_plus_core.json').body
                        gems.reject(&it["prerelease"]).sort_by(&it["number"].split('.')).last["number"]
                      end
end

#subs(s) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/schema_dev/gem.rb', line 116

def subs(s)
  s = s.gsub('%GEM_NAME%', gem_name)
  s = s.gsub('%GEM_BASE_NAME%', gem_base_name)
  s = s.gsub('%GEM_LIB_PATH%', gem_lib_path)
  s = s.gsub('%GEM_MODULE%', gem_module)
  s = s.gsub('%FULLNAME%', fullname)
  s = s.gsub('%EMAIL%', email)
  s = s.gsub('%SCHEMA_PLUS_CORE_DEPENDENCY%', dependency(schema_plus_core_version))
  s = s.gsub('%SCHEMA_DEV_DEPENDENCY%', dependency(SchemaDev::VERSION))
  s = s.gsub('%YEAR%', Time.now.strftime("%Y"))
end

#substitute_keysObject



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

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