Skip to content

Commit 4ed2757

Browse files
helloswetahsbt
authored andcommitted
[rubygems/rubygems] Update gemspec based on provided github username when exists
* Conditionally set changelog_url if gh username passed and enabled * conditionally set homepage, source code uri, homepage uri when gh username passed in * update documentation to say username will also be used for gemspec file rubygems/rubygems@1c1ada593b
1 parent 3feba18 commit 4ed2757

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

lib/bundler/cli/gem.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ def run
4747
git_author_name = use_git ? `git config user.name`.chomp : ""
4848
git_username = use_git ? `git config github.user`.chomp : ""
4949
git_user_email = use_git ? `git config user.email`.chomp : ""
50+
github_username = github_username(git_username)
5051

51-
github_username = if options[:github_username].nil?
52-
git_username
53-
elsif options[:github_username] == false
54-
""
52+
if github_username.empty?
53+
homepage_uri = "TODO: Put your gem's website or public repo URL here."
54+
source_code_uri = "TODO: Put your gem's public repo URL here."
55+
changelog_uri = "TODO: Put your gem's CHANGELOG.md URL here."
5556
else
56-
options[:github_username]
57+
homepage_uri = "https://github.com/#{github_username}/#{name}"
58+
source_code_uri = "https://github.com/#{github_username}/#{name}"
59+
changelog_uri = "https://github.com/#{github_username}/#{name}/blob/main/CHANGELOG.md"
5760
end
5861

5962
config = {
@@ -76,6 +79,9 @@ def run
7679
rust_builder_required_rubygems_version: rust_builder_required_rubygems_version,
7780
minitest_constant_name: minitest_constant_name,
7881
ignore_paths: %w[bin/],
82+
homepage_uri: homepage_uri,
83+
source_code_uri: source_code_uri,
84+
changelog_uri: changelog_uri,
7985
}
8086
ensure_safe_gem_name(name, constant_array)
8187

@@ -479,5 +485,15 @@ def rubocop_version
479485
def standard_version
480486
"1.3"
481487
end
488+
489+
def github_username(git_username)
490+
if options[:github_username].nil?
491+
git_username
492+
elsif options[:github_username] == false
493+
""
494+
else
495+
options[:github_username]
496+
end
497+
end
482498
end
483499
end

lib/bundler/man/bundle-config.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Ignore the current machine's platform and install only \fBruby\fR platform gems\
108108
Disallow any automatic changes to \fBGemfile\.lock\fR\. Bundler commands will be blocked unless the lockfile can be installed exactly as written\. Usually this will happen when changing the \fBGemfile\fR manually and forgetting to update the lockfile through \fBbundle lock\fR or \fBbundle install\fR\.
109109
.TP
110110
\fBgem\.github_username\fR (\fBBUNDLE_GEM__GITHUB_USERNAME\fR)
111-
Sets a GitHub username or organization to be used in \fBREADME\fR file when you create a new gem via \fBbundle gem\fR command\. It can be overridden by passing an explicit \fB\-\-github\-username\fR flag to \fBbundle gem\fR\.
111+
Sets a GitHub username or organization to be used in the \fBREADME\fR and \fB\.gemspec\fR files when you create a new gem via \fBbundle gem\fR command\. It can be overridden by passing an explicit \fB\-\-github\-username\fR flag to \fBbundle gem\fR\.
112112
.TP
113113
\fBgem\.push_key\fR (\fBBUNDLE_GEM__PUSH_KEY\fR)
114114
Sets the \fB\-\-key\fR parameter for \fBgem push\fR when using the \fBrake release\fR command with a private gemstash server\.

lib/bundler/man/bundle-config.1.ronn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ learn more about their operation in [bundle install(1)](bundle-install.1.html).
131131
Usually this will happen when changing the `Gemfile` manually and forgetting
132132
to update the lockfile through `bundle lock` or `bundle install`.
133133
* `gem.github_username` (`BUNDLE_GEM__GITHUB_USERNAME`):
134-
Sets a GitHub username or organization to be used in `README` file when you
135-
create a new gem via `bundle gem` command. It can be overridden by passing an
134+
Sets a GitHub username or organization to be used in the `README` and `.gemspec` files
135+
when you create a new gem via `bundle gem` command. It can be overridden by passing an
136136
explicit `--github-username` flag to `bundle gem`.
137137
* `gem.push_key` (`BUNDLE_GEM__PUSH_KEY`):
138138
Sets the `--key` parameter for `gem push` when using the `rake release`

lib/bundler/templates/newgem/newgem.gemspec.tt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
1010

1111
spec.summary = "TODO: Write a short summary, because RubyGems requires one."
1212
spec.description = "TODO: Write a longer description or delete this line."
13-
spec.homepage = "TODO: Put your gem's website or public repo URL here."
13+
spec.homepage = "<%= config[:homepage_uri] %>"
1414
<%- if config[:mit] -%>
1515
spec.license = "MIT"
1616
<%- end -%>
@@ -20,10 +20,11 @@ Gem::Specification.new do |spec|
2020
<%- end -%>
2121

2222
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
23-
2423
spec.metadata["homepage_uri"] = spec.homepage
25-
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
26-
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24+
spec.metadata["source_code_uri"] = "<%= config[:source_code_uri] %>"
25+
<%- if config[:changelog] -%>
26+
spec.metadata["changelog_uri"] = "<%= config[:changelog_uri] %>"
27+
<%- end -%>
2728

2829
# Specify which files should be added to the gem when it is released.
2930
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.

spec/bundler/commands/newgem_spec.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ def create_temporary_dir(dir)
16451645
end
16461646
end
16471647

1648-
context "without git config set" do
1648+
context "without git config github.user set" do
16491649
before do
16501650
git("config --global --unset github.user")
16511651
end
@@ -1664,9 +1664,32 @@ def create_temporary_dir(dir)
16641664
end
16651665
it_behaves_like "--github-username option", "gh_user"
16661666
end
1667+
1668+
context "when changelog is enabled" do
1669+
it "sets gemspec changelog_uri, homepage, homepage_uri, source_code_uri to TODOs" do
1670+
bundle "gem #{gem_name} --changelog"
1671+
1672+
expect(generated_gemspec.metadata["changelog_uri"]).
1673+
to eq("TODO: Put your gem's CHANGELOG.md URL here.")
1674+
expect(generated_gemspec.homepage).to eq("TODO: Put your gem's website or public repo URL here.")
1675+
expect(generated_gemspec.metadata["homepage_uri"]).to eq("TODO: Put your gem's website or public repo URL here.")
1676+
expect(generated_gemspec.metadata["source_code_uri"]).to eq("TODO: Put your gem's public repo URL here.")
1677+
end
1678+
end
1679+
1680+
context "when changelog is not enabled" do
1681+
it "sets gemspec homepage, homepage_uri, source_code_uri to TODOs and changelog_uri to nil" do
1682+
bundle "gem #{gem_name}"
1683+
1684+
expect(generated_gemspec.metadata["changelog_uri"]).to be_nil
1685+
expect(generated_gemspec.homepage).to eq("TODO: Put your gem's website or public repo URL here.")
1686+
expect(generated_gemspec.metadata["homepage_uri"]).to eq("TODO: Put your gem's website or public repo URL here.")
1687+
expect(generated_gemspec.metadata["source_code_uri"]).to eq("TODO: Put your gem's public repo URL here.")
1688+
end
1689+
end
16671690
end
16681691

1669-
context "with git config set" do
1692+
context "with git config github.user set" do
16701693
context "with github-username option in bundle config settings set to some value" do
16711694
before do
16721695
global_config "BUNDLE_GEM__GITHUB_USERNAME" => "different_username"
@@ -1682,6 +1705,29 @@ def create_temporary_dir(dir)
16821705
end
16831706
it_behaves_like "--github-username option", "gh_user"
16841707
end
1708+
1709+
context "when changelog is enabled" do
1710+
it "sets gemspec changelog_uri, homepage, homepage_uri, source_code_uri based on git username" do
1711+
bundle "gem #{gem_name} --changelog"
1712+
1713+
expect(generated_gemspec.metadata["changelog_uri"]).
1714+
to eq("https://github.com/bundleuser/#{gem_name}/blob/main/CHANGELOG.md")
1715+
expect(generated_gemspec.homepage).to eq("https://github.com/bundleuser/#{gem_name}")
1716+
expect(generated_gemspec.metadata["homepage_uri"]).to eq("https://github.com/bundleuser/#{gem_name}")
1717+
expect(generated_gemspec.metadata["source_code_uri"]).to eq("https://github.com/bundleuser/#{gem_name}")
1718+
end
1719+
end
1720+
1721+
context "when changelog is not enabled" do
1722+
it "sets gemspec source_code_uri, homepage, homepage_uri but not changelog_uri" do
1723+
bundle "gem #{gem_name}"
1724+
1725+
expect(generated_gemspec.metadata["changelog_uri"]).to be_nil
1726+
expect(generated_gemspec.homepage).to eq("https://github.com/bundleuser/#{gem_name}")
1727+
expect(generated_gemspec.metadata["homepage_uri"]).to eq("https://github.com/bundleuser/#{gem_name}")
1728+
expect(generated_gemspec.metadata["source_code_uri"]).to eq("https://github.com/bundleuser/#{gem_name}")
1729+
end
1730+
end
16851731
end
16861732

16871733
context "standard gem naming" do

0 commit comments

Comments
 (0)