Class: SchemaDev::Gem

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

Defined Under Namespace

Classes: TemplateEnv

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Gem

Returns a new instance of Gem.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/schema_dev/gem.rb', line 59

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/schema_dev/gem.rb', line 78

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



122
123
124
# File 'lib/schema_dev/gem.rb', line 122

def copy_template
  Templates.install_subtree src: "gem", dst: gem_root, bound: template_binding
end

#die(msg) ⇒ Object



98
99
100
# File 'lib/schema_dev/gem.rb', line 98

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

#ensure_doesnt_existObject



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

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

#ensure_not_in_gitObject



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

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

#erb(s) ⇒ Object



149
150
151
# File 'lib/schema_dev/gem.rb', line 149

def erb(s)
  ERB.new(s).result template_binding
end

#fixup_subdirObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/schema_dev/gem.rb', line 136

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}'
  END
end

#freshenObject



157
158
159
160
161
# File 'lib/schema_dev/gem.rb', line 157

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

#get_fullname_and_emailObject



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

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



163
164
165
166
167
168
169
# File 'lib/schema_dev/gem.rb', line 163

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



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

def rename_files
  (gem_root + "gitignore").rename gem_root + ".gitignore"
  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

#template_bindingObject



153
154
155
# File 'lib/schema_dev/gem.rb', line 153

def template_binding
  @template_binding ||= TemplateEnv.new(self).get_binding
end