Skip to content

Commit

Permalink
Merge pull request #4303 from mostafahussein/newgem-templates
Browse files Browse the repository at this point in the history
Respect user configured default branch in README links in new generated gems
  • Loading branch information
deivid-rodriguez committed Mar 2, 2021
2 parents a2644ac + aeaa0a4 commit 8a712a0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 77 deletions.
40 changes: 23 additions & 17 deletions bundler/lib/bundler/cli/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def run
constant_name = name.gsub(/-[_-]*(?![_-]|$)/) { "::" }.gsub(/([_-]+|(::)|^)(.|$)/) { $2.to_s + $3.upcase }
constant_array = constant_name.split("::")

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

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

config = {
:name => name,
Expand All @@ -58,6 +58,7 @@ def run
:ext => options[:ext],
:exe => options[:exe],
:bundler_version => bundler_dependency_version,
:git => use_git,
:github_username => github_username.empty? ? "[USERNAME]" : github_username,
:required_ruby_version => Gem.ruby_version < Gem::Version.new("2.4.a") ? "2.3.0" : "2.4.0",
}
Expand All @@ -79,7 +80,7 @@ def run
bin/setup
]

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

if test_framework = ask_and_set_test_framework
config[:test] = test_framework
Expand Down Expand Up @@ -175,24 +176,31 @@ def run
)
end

if File.exist?(target) && !File.directory?(target)
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}"
`git init #{target}`

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

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

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

if Bundler.git_present? && options[:git]
Bundler.ui.info "Initializing git repo in #{target}"
if use_git
Dir.chdir(target) do
`git init`
`git add .`
end
end
Expand All @@ -202,8 +210,6 @@ def run

Bundler.ui.info "Gem '#{name}' was successfully created. " \
"For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html"
rescue Errno::EEXIST => e
raise GenericSystemCallError.new(e, "There was a conflict while creating the new gem.")
end

private
Expand Down
8 changes: 5 additions & 3 deletions bundler/lib/bundler/templates/newgem/README.md.tt
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ TODO: Write usage instructions here
After checking out the repo, run `bin/setup` to install dependencies.<% if config[:test] %> Then, run `rake <%= config[:test].sub('mini', '').sub('rspec', 'spec') %>` to run the tests.<% end %> You can also run `bin/console` for an interactive prompt that will allow you to experiment.<% if config[:bin] %> Run `bundle exec <%= config[:name] %>` to use the gem in this directory, ignoring other installed copies of this gem.<% end %>

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
<% if config[:git] -%>

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/<%= config[:github_username] %>/<%= config[:name] %>.<% if config[:coc] %> This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/<%= config[:github_username] %>/<%= config[:name] %>/blob/master/CODE_OF_CONDUCT.md).<% end %>
Bug reports and pull requests are welcome on GitHub at https://github.com/<%= config[:github_username] %>/<%= config[:name] %>.<% if config[:coc] %> This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/<%= config[:github_username] %>/<%= config[:name] %>/blob/<%= config[:git_default_branch] %>/CODE_OF_CONDUCT.md).<% end %>
<% end -%>
<% if config[:mit] -%>

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
<% end -%>
<% if config[:coc] -%>
<% if config[:git] && config[:coc] -%>

## Code of Conduct

Everyone interacting in the <%= config[:constant_name] %> project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/<%= config[:github_username] %>/<%= config[:name] %>/blob/master/CODE_OF_CONDUCT.md).
Everyone interacting in the <%= config[:constant_name] %> project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/<%= config[:github_username] %>/<%= config[:name] %>/blob/<%= config[:git_default_branch] %>/CODE_OF_CONDUCT.md).
<% end -%>
94 changes: 37 additions & 57 deletions bundler/spec/commands/newgem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,9 @@ def bundle_exec_rubocop
let(:require_path) { "mygem" }

before do
git_config_content = <<-EOF
[user]
name = "Bundler User"
email = user@example.com
[github]
user = bundleuser
EOF
@git_config_location = ENV["GIT_CONFIG"]
path = "#{tmp}/test_git_config.txt"
File.open(path, "w") {|f| f.write(git_config_content) }
ENV["GIT_CONFIG"] = path
end

after do
FileUtils.rm(ENV["GIT_CONFIG"]) if File.exist?(ENV["GIT_CONFIG"])
ENV["GIT_CONFIG"] = @git_config_location
end

shared_examples_for "git config is present" do
context "git config user.{name,email} present" do
it "sets gemspec author to git user.name if available" do
expect(generated_gemspec.authors.first).to eq("Bundler User")
end

it "sets gemspec email to git user.email if available" do
expect(generated_gemspec.email.first).to eq("user@example.com")
end
end
end

shared_examples_for "git config is absent" do
it "sets gemspec author to default message if git user.name is not set or empty" do
expect(generated_gemspec.authors.first).to eq("TODO: Write your name")
end

it "sets gemspec email to default message if git user.email is not set or empty" do
expect(generated_gemspec.email.first).to eq("TODO: Write your email address")
end
sys_exec("git config --global user.name 'Bundler User'")
sys_exec("git config --global user.email user@example.com")
sys_exec("git config --global github.user bundleuser")
end

describe "git repo initialization" do
Expand Down Expand Up @@ -125,19 +90,24 @@ def bundle_exec_rubocop
end

shared_examples_for "--coc flag" do
before do
bundle "gem #{gem_name} --coc"
end
it "generates a gem skeleton with MIT license" do
bundle "gem #{gem_name} --coc"
gem_skeleton_assertions
expect(bundled_app("#{gem_name}/CODE_OF_CONDUCT.md")).to exist
end

describe "README additions" do
it "generates the README with a section for the Code of Conduct" do
expect(bundled_app("#{gem_name}/README.md").read).to include("## Code of Conduct")
expect(bundled_app("#{gem_name}/README.md").read).to include("https://github.com/bundleuser/#{gem_name}/blob/master/CODE_OF_CONDUCT.md")
end
it "generates the README with a section for the Code of Conduct" do
bundle "gem #{gem_name} --coc"
expect(bundled_app("#{gem_name}/README.md").read).to include("## Code of Conduct")
expect(bundled_app("#{gem_name}/README.md").read).to match(%r{https://github\.com/bundleuser/#{gem_name}/blob/.*/CODE_OF_CONDUCT.md})
end

it "generates the README with a section for the Code of Conduct, respecting the configured git default branch" do
sys_exec("git config --global init.defaultBranch main")
bundle "gem #{gem_name} --coc"

expect(bundled_app("#{gem_name}/README.md").read).to include("## Code of Conduct")
expect(bundled_app("#{gem_name}/README.md").read).to include("https://github.com/bundleuser/#{gem_name}/blob/main/CODE_OF_CONDUCT.md")
end
end

Expand All @@ -150,11 +120,9 @@ def bundle_exec_rubocop
expect(bundled_app("#{gem_name}/CODE_OF_CONDUCT.md")).to_not exist
end

describe "README additions" do
it "generates the README without a section for the Code of Conduct" do
expect(bundled_app("#{gem_name}/README.md").read).not_to include("## Code of Conduct")
expect(bundled_app("#{gem_name}/README.md").read).not_to include("https://github.com/bundleuser/#{gem_name}/blob/master/CODE_OF_CONDUCT.md")
end
it "generates the README without a section for the Code of Conduct" do
expect(bundled_app("#{gem_name}/README.md").read).not_to include("## Code of Conduct")
expect(bundled_app("#{gem_name}/README.md").read).not_to match(%r{https://github\.com/bundleuser/#{gem_name}/blob/.*/CODE_OF_CONDUCT.md})
end
end

Expand Down Expand Up @@ -302,7 +270,7 @@ def bundle_exec_rubocop

context "git config github.user is absent" do
before do
sys_exec("git config --unset github.user")
sys_exec("git config --global --unset github.user")
bundle "gem #{gem_name}"
end

Expand Down Expand Up @@ -413,17 +381,29 @@ def create_temporary_dir(dir)
bundle "gem #{gem_name}"
end

it_should_behave_like "git config is present"
it "sets gemspec author to git user.name if available" do
expect(generated_gemspec.authors.first).to eq("Bundler User")
end

it "sets gemspec email to git user.email if available" do
expect(generated_gemspec.email.first).to eq("user@example.com")
end
end

context "git config user.{name,email} is not set" do
before do
sys_exec("git config --unset user.name", :dir => bundled_app)
sys_exec("git config --unset user.email", :dir => bundled_app)
sys_exec("git config --global --unset user.name")
sys_exec("git config --global --unset user.email")
bundle "gem #{gem_name}"
end

it_should_behave_like "git config is absent"
it "sets gemspec author to default message if git user.name is not set or empty" do
expect(generated_gemspec.authors.first).to eq("TODO: Write your name")
end

it "sets gemspec email to default message if git user.email is not set or empty" do
expect(generated_gemspec.email.first).to eq("TODO: Write your email address")
end
end

it "sets gemspec metadata['allowed_push_host']" do
Expand Down Expand Up @@ -1140,7 +1120,7 @@ def create_temporary_dir(dir)
it "should fail gracefully" do
FileUtils.touch(bundled_app("conflict-foobar"))
bundle "gem conflict-foobar", :raise_on_error => false
expect(err).to include("Errno::ENOTDIR")
expect(err).to eq("Couldn't create a new gem named `conflict-foobar` because there's an existing file named `conflict-foobar`.")
expect(exitstatus).to eql(32)
end
end
Expand Down

0 comments on commit 8a712a0

Please sign in to comment.