Skip to content
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
wants to merge 8 commits into from

Conversation

vietqhoang
Copy link
Contributor

@vietqhoang vietqhoang commented Oct 24, 2024

Closes #243

Problem

When repinning a package, importmap pin some_package_name, the pin options are not carried over.

For example if the following pin already exists:

# importmap.rb

pin "foobar", preload: false # 0.0.1

And the command is executed to update the pin to the latest version, 0.1.0:

importmap pin foobar

The importmap pin replaces the foobar pinned line with the following:

# importmap.rb

pin "foobar" # 0.1.0

When updating packages it requires additional effort by the developer to manually carry over the options.

Expectation

The pin options should carry over.

For example if the following pin already exists:

# importmap.rb

pin "foobar", preload: false # 0.0.1

And the command is executed to update the pin to the latest version, 0.1.0:

importmap pin foobar

The importmap pin replaces the foobar pinned line with the following:

# importmap.rb

pin "foobar", preload: false # 0.1.0

Solution

Implement a method which extract the pin options from the matching line. Update the vendored_pin_for which is used by the pin rake task to output with the carried over pin options.

The prior implementation returned a `MatchData` object if there is a match or `nil` if there is none. Although the existence of the object is truthy, given the method is interrogative my expectation is it should explicitly return a Boolean.

This is being changed beause I want to use the `importmap.match` to extract the line from the file and pull the pin options. This is set up for keeping things DRY and to keep the commits simple for ease of following along the changes.
This returns any options present for the pinned package.

Variations of lines tested against the `raw_options` regex can be viewed here: https://rubular.com/r/RlOaaKZsbAXfEz

Variation of options tested against the option regex can be viewed here: https://rubular.com/r/qPUodCavWU46GX

The method will be used to repopulate the options when the pinned package is updated.

For the test case I changed the set up to use a fixture. I found editting the dummy apps importmap file to break other tests. Instead of spending time reconciling those tests I went with creating a fixture to be used for the particular test file.
The method now preserves the pin option if it is present.
I found the `split` can take in a regular expression.
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}) }
Copy link
Contributor Author

@vietqhoang vietqhoang Oct 24, 2024

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 key to with the new value instead of straight up excluding it first and then reintroducing it again in pin_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.

Copy link
Contributor Author

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

@@ -62,6 +63,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>.*) #.*$/)
Copy link
Contributor Author

@vietqhoang vietqhoang Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the dummy app’s importmap.rb and some of the importmap.rb fixtures do not include the comment with the version for the pins. I think this may be due to how vendored pinning used to output? The regex may need to be relaxed to capture these lines.

@dhh
Copy link
Member

dhh commented Oct 25, 2024

I see where this is coming from, but it's too complicated. It's too far into attempting to turn this into a full-blown package manager. The repinning options will stand out in the git commit anyway, and it'll be up to the developer to ensure those are corrected for.

@dhh dhh closed this Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

importmap pin/update removes preload: false
2 participants