Class: Bundler::CLI::Gem

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

Constant Summary collapse

TEST_FRAMEWORK_VERSIONS =
{
  "rspec" => "3.0",
  "minitest" => "5.16",
  "test-unit" => "3.0",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, gem_name, thor) ⇒ Gem

Returns a new instance of Gem.



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

def initialize(options, gem_name, thor)
  @options = options
  @gem_name = resolve_name(gem_name)

  @thor = thor
  thor.behavior = :invoke
  thor.destination_root = nil

  @name = @gem_name
  @target = SharedHelpers.pwd.join(gem_name)

  @extension = options[:ext]

  validate_ext_name if @extension
  validate_rust_builder_rubygems_version if @extension == "rust"
  travis_removal_info
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



18
19
20
# File 'lib/bundler/cli/gem.rb', line 18

def extension
  @extension
end

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



18
19
20
# File 'lib/bundler/cli/gem.rb', line 18

def gem_name
  @gem_name
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/bundler/cli/gem.rb', line 18

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/bundler/cli/gem.rb', line 18

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



18
19
20
# File 'lib/bundler/cli/gem.rb', line 18

def target
  @target
end

#thorObject (readonly)

Returns the value of attribute thor.



18
19
20
# File 'lib/bundler/cli/gem.rb', line 18

def thor
  @thor
end

Instance Method Details

#runObject



38
39
40
41
42
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/bundler/cli/gem.rb', line 38

def run
  Bundler.ui.confirm "Creating gem '#{name}'..."

  underscored_name = name.tr("-", "_")
  namespaced_path = name.tr("-", "/")
  constant_name = name.gsub(/-[_-]*(?![_-]|$)/) { "::" }.gsub(/([_-]+|(::)|^)(.|$)/) { $2.to_s + $3.upcase }
  constant_array = constant_name.split("::")
  minitest_constant_name = constant_array.clone.tap {|a| a[-1] = "Test#{a[-1]}" }.join("::") # Foo::Bar => Foo::TestBar

  use_git = Bundler.git_present? && options[:git]

  git_author_name = use_git ? `git config user.name`.chomp : ""
  git_username = use_git ? `git config github.user`.chomp : ""
  git_user_email = use_git ? `git config user.email`.chomp : ""

  github_username = if options[:github_username].nil?
    git_username
  elsif options[:github_username] == false
    ""
  else
    options[:github_username]
  end

  config = {
    name: name,
    underscored_name: underscored_name,
    namespaced_path: namespaced_path,
    makefile_path: "#{underscored_name}/#{underscored_name}",
    constant_name: constant_name,
    constant_array: constant_array,
    author: git_author_name.empty? ? "TODO: Write your name" : git_author_name,
    email: git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
    test: options[:test],
    ext: extension,
    exe: options[:exe],
    bundler_version: bundler_dependency_version,
    git: use_git,
    github_username: github_username.empty? ? "[USERNAME]" : github_username,
    required_ruby_version: required_ruby_version,
    rust_builder_required_rubygems_version: rust_builder_required_rubygems_version,
    minitest_constant_name: minitest_constant_name,
  }
  ensure_safe_gem_name(name, constant_array)

  templates = {
    "#{Bundler.preferred_gemfile_name}.tt" => Bundler.preferred_gemfile_name,
    "lib/newgem.rb.tt" => "lib/#{namespaced_path}.rb",
    "lib/newgem/version.rb.tt" => "lib/#{namespaced_path}/version.rb",
    "sig/newgem.rbs.tt" => "sig/#{namespaced_path}.rbs",
    "newgem.gemspec.tt" => "#{name}.gemspec",
    "Rakefile.tt" => "Rakefile",
    "README.md.tt" => "README.md",
    "bin/console.tt" => "bin/console",
    "bin/setup.tt" => "bin/setup",
  }

  executables = %w[
    bin/console
    bin/setup
  ]

  templates.merge!("gitignore.tt" => ".gitignore") if use_git

  if test_framework = ask_and_set_test_framework
    config[:test] = test_framework
    config[:test_framework_version] = TEST_FRAMEWORK_VERSIONS[test_framework]

    case test_framework
    when "rspec"
      templates.merge!(
        "rspec.tt" => ".rspec",
        "spec/spec_helper.rb.tt" => "spec/spec_helper.rb",
        "spec/newgem_spec.rb.tt" => "spec/#{namespaced_path}_spec.rb"
      )
      config[:test_task] = :spec
    when "minitest"
      # Generate path for minitest target file (FileList["test/**/test_*.rb"])
      #   foo     => test/test_foo.rb
      #   foo-bar => test/foo/test_bar.rb
      #   foo_bar => test/test_foo_bar.rb
      paths = namespaced_path.rpartition("/")
      paths[2] = "test_#{paths[2]}"
      minitest_namespaced_path = paths.join("")

      templates.merge!(
        "test/minitest/test_helper.rb.tt" => "test/test_helper.rb",
        "test/minitest/test_newgem.rb.tt" => "test/#{minitest_namespaced_path}.rb"
      )
      config[:test_task] = :test
    when "test-unit"
      templates.merge!(
        "test/test-unit/test_helper.rb.tt" => "test/test_helper.rb",
        "test/test-unit/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb"
      )
      config[:test_task] = :test
    end
  end

  config[:ci] = ask_and_set_ci
  case config[:ci]
  when "github"
    templates.merge!("github/workflows/main.yml.tt" => ".github/workflows/main.yml")
    config[:ci_config_path] = ".github "
  when "gitlab"
    templates.merge!("gitlab-ci.yml.tt" => ".gitlab-ci.yml")
    config[:ci_config_path] = ".gitlab-ci.yml "
  when "circle"
    templates.merge!("circleci/config.yml.tt" => ".circleci/config.yml")
    config[:ci_config_path] = ".circleci "
  end

  if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?",
    "This means that any other developer or company will be legally allowed to use your code " \
    "for free as long as they admit you created it. You can read more about the MIT license " \
    "at https://choosealicense.com/licenses/mit.")
    config[:mit] = true
    Bundler.ui.info "MIT License enabled in config"
    templates.merge!("LICENSE.txt.tt" => "LICENSE.txt")
  end

  if ask_and_set(:coc, "Do you want to include a code of conduct in gems you generate?",
    "Codes of conduct can increase contributions to your project by contributors who " \
    "prefer collaborative, safe spaces. You can read more about the code of conduct at " \
    "contributor-covenant.org. Having a code of conduct means agreeing to the responsibility " \
    "of enforcing it, so be sure that you are prepared to do that. Be sure that your email " \
    "address is specified as a contact in the generated code of conduct so that people know " \
    "who to contact in case of a violation. For suggestions about " \
    "how to enforce codes of conduct, see https://bit.ly/coc-enforcement.")
    config[:coc] = true
    Bundler.ui.info "Code of conduct enabled in config"
    templates.merge!("CODE_OF_CONDUCT.md.tt" => "CODE_OF_CONDUCT.md")
  end

  if ask_and_set(:changelog, "Do you want to include a changelog?",
    "A changelog is a file which contains a curated, chronologically ordered list of notable " \
    "changes for each version of a project. To make it easier for users and contributors to" \
    " see precisely what notable changes have been made between each release (or version) of" \
    " the project. Whether consumers or developers, the end users of software are" \
    " human beings who care about what's in the software. When the software changes, people " \
    "want to know why and how. see https://keepachangelog.com")
    config[:changelog] = true
    Bundler.ui.info "Changelog enabled in config"
    templates.merge!("CHANGELOG.md.tt" => "CHANGELOG.md")
  end

  config[:linter] = ask_and_set_linter
  case config[:linter]
  when "rubocop"
    config[:linter_version] = rubocop_version
    Bundler.ui.info "RuboCop enabled in config"
    templates.merge!("rubocop.yml.tt" => ".rubocop.yml")
  when "standard"
    config[:linter_version] = standard_version
    Bundler.ui.info "Standard enabled in config"
    templates.merge!("standard.yml.tt" => ".standard.yml")
  end

  templates.merge!("exe/newgem.tt" => "exe/#{name}") if config[:exe]

  if extension == "c"
    templates.merge!(
      "ext/newgem/extconf-c.rb.tt" => "ext/#{name}/extconf.rb",
      "ext/newgem/newgem.h.tt" => "ext/#{name}/#{underscored_name}.h",
      "ext/newgem/newgem.c.tt" => "ext/#{name}/#{underscored_name}.c"
    )
  end

  if extension == "rust"
    templates.merge!(
      "Cargo.toml.tt" => "Cargo.toml",
      "ext/newgem/Cargo.toml.tt" => "ext/#{name}/Cargo.toml",
      "ext/newgem/extconf-rust.rb.tt" => "ext/#{name}/extconf.rb",
      "ext/newgem/src/lib.rs.tt" => "ext/#{name}/src/lib.rs",
    )
  end

  if target.exist? && !target.directory?
    Bundler.ui.error "Couldn't create a new gem named `#{gem_name}` because there's an existing file named `#{gem_name}`."
    exit Bundler::BundlerError.all_errors[Bundler::GenericSystemCallError]
  end

  if use_git
    Bundler.ui.info "Initializing git repo in #{target}"
    require "shellwords"
    `git init #{target.to_s.shellescape}`

    config[:git_default_branch] = File.read("#{target}/.git/HEAD").split("/").last.chomp
  end

  templates.each do |src, dst|
    destination = target.join(dst)
    thor.template("newgem/#{src}", destination, config)
  end

  executables.each do |file|
    path = target.join(file)
    executable = (path.stat.mode | 0o111)
    path.chmod(executable)
  end

  if use_git
    IO.popen(%w[git add .], { chdir: target }, &:read)
  end

  # Open gemspec in editor
  open_editor(options["edit"], target.join("#{name}.gemspec")) if options[:edit]

  Bundler.ui.info "Gem '#{name}' was successfully created. " \
    "For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html"
end