This repository was archived by the owner on Apr 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[Feature] bundle change #6597
Closed
Closed
[Feature] bundle change #6597
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8328098
Add heredoc string to expected_gemfile
agrim123 9af434b
Add specs for inital draft
agrim123 b0a25a1
Add remove and add command to change
agrim123 9bf8a5f
Keep version intact when changing other options
agrim123 b8044f0
Keep groups intact when changing other options
agrim123 c35ba32
add rescue block to prevent undesirable gemfile changes
agrim123 8161fd6
Add specs for empty options
agrim123 80475a4
Fix failing specs
agrim123 e20e0eb
fix resolver crash when version is >= 0
agrim123 ef4ca84
Add documentation for bundle change
agrim123 267ef98
Add support for mutiple groups
agrim123 f522377
Add specs for --source
agrim123 63019ca
Add specs to retain optimistic version prefix
agrim123 c18e70a
Change write_to_gemfile to write_file
agrim123 4543aad
Use FriendlyErrors to exit with proper code
agrim123 cc6fee8
Remove aliases for options
agrim123 0111d05
Add spec for --pessimistic option for add command
agrim123 e795c01
Remove unnecessary ivars
agrim123 b750e26
Update documentation
agrim123 adcec42
Add test for no change in gemfile
agrim123 352d6e0
Add check for unsupported options
agrim123 74ddac3
Add method to support source option
agrim123 8eaa22f
Fix groups in specs
agrim123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Bundler | ||
| class CLI::Change | ||
| def initialize(options, gem_name) | ||
| @gem_name = gem_name | ||
| @options = options | ||
| end | ||
|
|
||
| def run | ||
| raise InvalidOption, "Please supply at least one option to change." unless @options[:group] || @options[:version] || @options[:source] | ||
|
|
||
| dep = Bundler.definition.dependencies.find {|d| d.name == @gem_name } | ||
|
|
||
| raise InvalidOption, "`#{@gem_name}` could not be found in the Gemfile." unless dep | ||
|
|
||
| check_for_unsupported_options(dep) | ||
|
|
||
| add_options = {} | ||
|
|
||
| initial_gemfile = IO.readlines(Bundler.default_gemfile) | ||
|
|
||
| set_group_options(dep.groups, add_options) | ||
|
|
||
| set_version_options(dep.requirement, add_options) | ||
|
|
||
| set_source_options(dep.options[:source], add_options) | ||
|
|
||
| begin | ||
| require "bundler/cli/remove" | ||
| CLI::Remove.new([@gem_name], {}).run | ||
|
|
||
| require "bundler/cli/add" | ||
| CLI::Add.new(add_options, [@gem_name]).run | ||
| rescue StandardError => e | ||
| SharedHelpers.write_file(Bundler.default_gemfile, initial_gemfile) | ||
| raise e | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # If version of the gem is specified in Gemfile then we preserve | ||
| # it and the prefix | ||
| # else if @options[:version] is present then we prefer strict version | ||
| # and for a empty version we let resolver get version and set as pessimistic | ||
| # | ||
| # @param [requirement] requirement requirement of the gem. | ||
| # @param [Hash] add_options Options to pass to add command | ||
| # @return | ||
| def set_version_options(requirement, add_options) | ||
| req = requirement.requirements[0] | ||
| version_prefix = req[0] | ||
| version = req[1].to_s | ||
| case version_prefix | ||
| when "=" | ||
| add_options[:strict] = true | ||
| when ">=" | ||
| add_options[:optimistic] = true unless version == "0" | ||
| else | ||
| add_options[:pessimistic] = true | ||
| end | ||
|
|
||
| add_options[:version] = if @options[:version].nil? | ||
| version.to_i.zero? ? nil : version | ||
| else | ||
| @options[:version] | ||
| end | ||
| end | ||
|
|
||
| # @param [groups] groups Groups of the gem. | ||
| # @param [Hash] add_options Options to pass to add command | ||
| # @return | ||
| def set_group_options(groups, add_options) | ||
| if @options[:group] | ||
| uniq_groups = @options[:group].split(",").uniq | ||
| common_groups = uniq_groups & groups.map(&:to_s) | ||
|
|
||
| Bundler.ui.warn "`#{@gem_name}` is already present in `#{common_groups.join(",")}`." unless common_groups.empty? | ||
|
|
||
| add_options["group"] = uniq_groups.join(",") | ||
| else | ||
| add_options["group"] = groups.map(&:to_s).join(",") | ||
| end | ||
| end | ||
|
|
||
| def set_source_options(source, add_options) | ||
| add_options["source"] = source.options["remotes"].first if source.is_a?(Bundler::Source) | ||
|
|
||
| add_options["source"] = @options[:source] if @options[:source] | ||
| end | ||
|
|
||
| def check_for_unsupported_options(dep) | ||
| gem_options = dep.options.delete_if {|_k, v| v.nil? || (v.is_a?(Array) && v.empty?) } | ||
|
|
||
| raise InvalidOption, "`git` is not yet supported." if dep.options[:source].is_a?(Bundler::Source::Git) | ||
|
|
||
| raise InvalidOption, "`platforms` is not yet supported." if gem_options[:platforms] | ||
|
|
||
| raise InvalidOption, "`env` is not yet supported." if gem_options[:env] | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| bundle-change(1) -- Changes properties of a gem | ||
| ================================================================ | ||
|
|
||
| ## SYNOPSIS | ||
|
|
||
| `bundle change` <GEM_NAME> [--group=GROUP] [--version=VERSION] [--source=SOURCE] | ||
|
|
||
| ## DESCRIPTION | ||
| Provide flexibilty of editing gemfile from command line by providing option to change gem properties. | ||
|
|
||
| Example: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this example should show the before & after gemfiles |
||
|
|
||
| $ cat Gemfile | grep "rails" | ||
|
|
||
| gem "rails" | ||
|
|
||
| $ bundle change rails --group dev --version 5.1 | ||
|
|
||
| $ cat Gemfile | grep "rails" | ||
|
|
||
| gem "rails", "= 5.1", :group => [:dev] | ||
|
|
||
| ## OPTIONS | ||
| * `--version`: | ||
| Specify version requirements for the added gem. | ||
|
|
||
| * `--group`: | ||
| Specify the groups for the added gem. Multiple groups should be separated by commas. | ||
|
|
||
| * `--source`: | ||
| Specify the source for the added gem. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this option needs to be tested