-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Carry over pin options when pinning an existing pin #274
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d465298
Update packager’s `package?` to return Boolean type
vietqhoang 41de67a
Add `pin_options_for_package`
vietqhoang 3eaf7a1
Update `packager#vendored_pin_for`
vietqhoang e36fe93
Simplify normalization of the delimiter
vietqhoang 9709923
Just use the optional character option
vietqhoang c110d4a
Add test to make it clear any defined option is carried over
vietqhoang 9e35750
Update assert to show multiple options are carried over
vietqhoang 1fca7d7
Preserve order of `to` pin option
vietqhoang 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 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 |
---|---|---|
|
@@ -39,16 +39,18 @@ def pin_for(package, url) | |
def vendored_pin_for(package, url) | ||
filename = package_filename(package) | ||
version = extract_package_version_from(url) | ||
|
||
if "#{package}.js" == filename | ||
%(pin "#{package}" # #{version}) | ||
else | ||
%(pin "#{package}", to: "#{filename}" # #{version}) | ||
end | ||
line_formatted_pin_options = pin_options_for_package(package).except("to").map { |option, value| %(#{option}: #{value.is_a?(String) ? %("#{value}") : value}) } | ||
pin_components = [ | ||
%(pin "#{package}"), | ||
(%(to: "#{filename}") unless "#{package}.js" == filename), | ||
*line_formatted_pin_options | ||
].compact | ||
|
||
%(#{pin_components.join(", ")} # #{version}) | ||
end | ||
|
||
def packaged?(package) | ||
importmap.match(/^pin ["']#{package}["'].*$/) | ||
package_line_in_importmap(package).present? | ||
end | ||
|
||
def download(package, url) | ||
|
@@ -62,6 +64,19 @@ def remove(package) | |
remove_package_from_importmap(package) | ||
end | ||
|
||
def pin_options_for_package(package) | ||
line = package_line_in_importmap(package) || "" | ||
raw_options = line.match(/^#{base_package_line_regex(package)}?,[\s+]?(?<pin_options>.*) #.*$/) | ||
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. I noticed the dummy app’s |
||
|
||
return {} if raw_options.blank? | ||
|
||
raw_options[:pin_options].split(/,\s?/).each_with_object({}) do |option, hash| | ||
match_data = option.match(/^(?<option_name>[^:]*):[\s+]?["']?(?<option_value>.*[^"'])["']?$/) | ||
|
||
hash[match_data[:option_name]] = cast_option_value(match_data[:option_value]) | ||
end | ||
end | ||
|
||
private | ||
def post_json(body) | ||
Net::HTTP.post(self.class.endpoint, body.to_json, "Content-Type" => "application/json") | ||
|
@@ -146,4 +161,19 @@ def package_filename(package) | |
def extract_package_version_from(url) | ||
url.match(/@\d+\.\d+\.\d+/)&.to_a&.first | ||
end | ||
|
||
def package_line_in_importmap(package) | ||
importmap.match(/^#{base_package_line_regex(package)}.*$/).try(:[], 0) | ||
end | ||
|
||
def base_package_line_regex(package) | ||
/pin ["']#{package}["']/ | ||
end | ||
|
||
def cast_option_value(object) | ||
return true if object == "true" | ||
return false if object == "false" | ||
|
||
object | ||
end | ||
end |
This file contains 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,7 @@ | ||
pin_all_from "app/assets/javascripts" | ||
|
||
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true # 1.0.2 | ||
pin "not_there", to: "nowhere.js", preload: false # 1.9.1 | ||
pin "some_file" # 0.2.1 | ||
pin "another_file",to:'another_file.js' # @0.0.16 | ||
pin "random", random_option: "foobar", hello: "world" # 7.7.7 |
This file contains 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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
require "minitest/mock" | ||
|
||
class Importmap::PackagerTest < ActiveSupport::TestCase | ||
setup { @packager = Importmap::Packager.new(Rails.root.join("config/importmap.rb")) } | ||
setup { @packager = Importmap::Packager.new(Rails.root.join("../fixtures/files/pins_with_various_options_importmap.rb")) } | ||
|
||
test "successful import with mock" do | ||
response = Class.new do | ||
|
@@ -54,5 +54,14 @@ def code() "200" end | |
test "vendored_pin_for" do | ||
assert_equal %(pin "react" # @17.0.2), @packager.vendored_pin_for("react", "https://cdn/[email protected]") | ||
assert_equal %(pin "javascript/react", to: "javascript--react.js" # @17.0.2), @packager.vendored_pin_for("javascript/react", "https://cdn/[email protected]") | ||
assert_equal %(pin "md5", preload: true # @2.1.3), @packager.vendored_pin_for("md5", "https://cdn/[email protected]") | ||
assert_equal %(pin "random", random_option: "foobar", hello: "world" # @8.8.8), @packager.vendored_pin_for("random", "https://cdn/[email protected]") | ||
end | ||
|
||
test "pin_options_for_package" do | ||
assert_equal ({ "to" => "https://cdn.skypack.dev/md5", "preload" => true }), @packager.pin_options_for_package('md5') | ||
assert_equal ({ "to" => "nowhere.js", "preload" => false }), @packager.pin_options_for_package('not_there') | ||
assert_equal ({ }), @packager.pin_options_for_package('some_file') | ||
assert_equal ({ "to" => "another_file.js" }), @packager.pin_options_for_package('another_file') | ||
end | ||
end |
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.
An alternative to managing the
to
is to update the pin options keyto
with the new value instead of straight up excluding it first and then reintroducing it again inpin_components
.A benefit to the alternative is that it maintains the original order of the pin options. This makes it a little nicer to review changes on git (and Github) since the ordering is maintain and thus highlighted nicely.
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.
Changes made to preserve ordering, 1fca7d7