Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/demo_scripts/gem_swapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,17 @@ def swap_gem_to_github(content, gem_name, info)
# Extract options after version (if any)
options = rest.sub(/^\s*,\s*(['"])[^'"]*\1/, '') # Remove version if present

# Build replacement: gem 'name', github: 'user/repo', branch: 'branch-name' [, options...]
# Use tag: for tags, branch: for branches (default to :branch if not specified)
ref_type = info[:ref_type] || :branch
param_name = ref_type == :tag ? 'tag' : 'branch'

# Only omit ref when it's a branch (not tag) and the branch is 'main' or 'master'
# Tags must always be explicit, even if named 'main' or 'master'
should_omit_ref = ref_type == :branch && %w[main master].include?(info[:branch])

# Build replacement: gem 'name', github: 'user/repo', branch/tag: 'ref-name' [, options...]
replacement = "#{indent}gem #{quote}#{gem_name}#{quote}, github: #{quote}#{info[:repo]}#{quote}"
replacement += ", branch: #{quote}#{info[:branch]}#{quote}" if info[:branch] != 'main'
replacement += ", #{param_name}: #{quote}#{info[:branch]}#{quote}" unless should_omit_ref
replacement += options unless options.strip.empty?
replacement
end
Expand Down
40 changes: 40 additions & 0 deletions spec/demo_scripts/gem_swapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,46 @@
expect(result).to eq("gem 'shakapacker', github: 'shakacode/shakapacker', branch: 'develop', require: false\n")
end
end

context 'with tag instead of branch' do
let(:gemfile_content) { "gem 'shakapacker', '~> 9.0.0'\n" }
let(:github_info) { { repo: 'shakacode/shakapacker', branch: 'v1.0.0', ref_type: :tag } }

it 'uses tag parameter instead of branch' do
result = swapper.send(:swap_gem_to_github, gemfile_content, 'shakapacker', github_info)
expect(result).to eq("gem 'shakapacker', github: 'shakacode/shakapacker', tag: 'v1.0.0'\n")
end
end

context 'with explicit branch ref_type' do
let(:gemfile_content) { "gem 'shakapacker', '~> 9.0.0'\n" }
let(:github_info) { { repo: 'shakacode/shakapacker', branch: 'develop', ref_type: :branch } }

it 'uses branch parameter' do
result = swapper.send(:swap_gem_to_github, gemfile_content, 'shakapacker', github_info)
expect(result).to eq("gem 'shakapacker', github: 'shakacode/shakapacker', branch: 'develop'\n")
end
end

context 'with tag named main' do
let(:gemfile_content) { "gem 'shakapacker', '~> 9.0.0'\n" }
let(:github_info) { { repo: 'shakacode/shakapacker', branch: 'main', ref_type: :tag } }

it 'includes tag parameter even though it is named main' do
result = swapper.send(:swap_gem_to_github, gemfile_content, 'shakapacker', github_info)
expect(result).to eq("gem 'shakapacker', github: 'shakacode/shakapacker', tag: 'main'\n")
end
end

context 'with master branch' do
let(:gemfile_content) { "gem 'shakapacker', '~> 9.0.0'\n" }
let(:github_info) { { repo: 'shakacode/shakapacker', branch: 'master', ref_type: :branch } }

it 'omits branch parameter for master (like main)' do
result = swapper.send(:swap_gem_to_github, gemfile_content, 'shakapacker', github_info)
expect(result).to eq("gem 'shakapacker', github: 'shakacode/shakapacker'\n")
end
end
end

describe '#swap_package_json' do
Expand Down