-
Notifications
You must be signed in to change notification settings - Fork 119
Allow packwerk to scan for packages inside of engines #216
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
Open
AndrewSwerlick
wants to merge
8
commits into
Shopify:main
Choose a base branch
from
AndrewSwerlick:packages-anywhere
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fcaa395
Refactor out package scanning into PackagePaths
AndrewSwerlick e89cc4d
Allow packwerk to scan for packages inside of engines
AndrewSwerlick e64ce0e
Address code review feedback
AndrewSwerlick ca16a5f
Updating docs
AndrewSwerlick c3eae4a
Text change to make CI do it's thing
AndrewSwerlick 9baa416
Small sorbet fix
AndrewSwerlick ab1a847
Fix stuff after rebasing
AndrewSwerlick 897fa13
add back tests
AndrewSwerlick 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
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,71 @@ | ||
| # typed: strict | ||
| # frozen_string_literal: true | ||
|
|
||
| require "pathname" | ||
| require "bundler" | ||
|
|
||
| module Packwerk | ||
| class PackagePaths | ||
| PACKAGE_CONFIG_FILENAME = "package.yml" | ||
| PathSpec = T.type_alias { T.any(String, T::Array[String]) } | ||
|
|
||
| extend T::Sig | ||
| extend T::Generic | ||
|
|
||
| sig do | ||
| params( | ||
| root_path: String, | ||
| package_pathspec: T.nilable(PathSpec), | ||
| exclude_pathspec: T.nilable(PathSpec), | ||
| scan_for_packages_outside_of_app_dir: T.nilable(T::Boolean) | ||
| ).void | ||
| end | ||
| def initialize(root_path, package_pathspec, exclude_pathspec = nil, scan_for_packages_outside_of_app_dir = false) | ||
| @root_path = root_path | ||
| @package_pathspec = package_pathspec | ||
| @exclude_pathspec = exclude_pathspec | ||
| @scan_for_packages_outside_of_app_dir = scan_for_packages_outside_of_app_dir | ||
| end | ||
|
|
||
| sig {returns(T::Array[Pathname])} | ||
| def all_paths | ||
| exclude_pathspec = Array(@exclude_pathspec).dup | ||
| .push(Bundler.bundle_path.join("**").to_s) | ||
| .map { |glob| File.expand_path(glob) } | ||
|
|
||
| paths_to_scan = if @scan_for_packages_outside_of_app_dir | ||
| engine_paths_to_scan.push(@root_path) | ||
| else | ||
| [@root_path] | ||
| end | ||
|
|
||
| glob_patterns = paths_to_scan.product(Array(@package_pathspec)).map do |path, pathspec| | ||
| File.join(path, pathspec, PACKAGE_CONFIG_FILENAME) | ||
| end | ||
|
|
||
| Dir.glob(glob_patterns) | ||
| .map { |path| Pathname.new(path).cleanpath } | ||
| .reject { |path| exclude_path?(exclude_pathspec, path) } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| sig { params(globs: T::Array[String], path: Pathname).returns(T::Boolean) } | ||
| def exclude_path?(globs, path) | ||
| globs.any? do |glob| | ||
| path.realpath.fnmatch(glob, File::FNM_EXTGLOB) | ||
| end | ||
| end | ||
|
|
||
| sig { returns(T::Array[String]) } | ||
| def engine_paths_to_scan | ||
| bundle_path_match = Bundler.bundle_path.join("**") | ||
|
|
||
| Rails.application.railties | ||
| .select { |r| r.is_a?(Rails::Engine) } | ||
| .map { |r| Pathname.new(r.root).expand_path } | ||
| .reject { |path| path.fnmatch(bundle_path_match.to_s) } # reject paths from vendored gems | ||
| .map(&:to_s) | ||
| 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
5 changes: 5 additions & 0 deletions
5
test/fixtures/external_packages/app/components/timeline/app/models/concerns/has_timeline.rb
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,5 @@ | ||
| # typed: ignore | ||
| # frozen_string_literal: true | ||
|
|
||
| module HasTimeline | ||
| end |
Empty file.
2 changes: 2 additions & 0 deletions
2
test/fixtures/external_packages/app/components/timeline/app/models/private_thing.rb
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,2 @@ | ||
| class PrivateThing | ||
| end |
Empty file.
1 change: 1 addition & 0 deletions
1
test/fixtures/external_packages/app/components/timeline/nested/package.yml
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 @@ | ||
| enforce_dependencies: true |
2 changes: 2 additions & 0 deletions
2
test/fixtures/external_packages/app/components/timeline/package.yml
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,2 @@ | ||
| enforce_privacy: | ||
| - "::PrivateThing" |
Empty file.
Empty file.
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 @@ | ||
| packages_outside_of_app_dir_enabled: true |
5 changes: 5 additions & 0 deletions
5
test/fixtures/external_packages/sales/components/sales/app/models/order.rb
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,5 @@ | ||
| # typed: ignore | ||
| # frozen_string_literal: true | ||
|
|
||
| class Order | ||
| end |
7 changes: 7 additions & 0 deletions
7
test/fixtures/external_packages/sales/components/sales/app/models/sales/order.rb
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,7 @@ | ||
| # typed: ignore | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sales | ||
| class Order | ||
| end | ||
| end |
7 changes: 7 additions & 0 deletions
7
test/fixtures/external_packages/sales/components/sales/app/public/sales/record_new_order.rb
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,7 @@ | ||
| # typed: ignore | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sales | ||
| module RecordNewOrder | ||
| end | ||
| end |
8 changes: 8 additions & 0 deletions
8
test/fixtures/external_packages/sales/components/sales/package.yml
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,8 @@ | ||
| --- | ||
| enforce_privacy: true | ||
|
|
||
| metadata: | ||
| stewards: | ||
| - "@Shopify/sales" | ||
| slack_channels: | ||
| - "#sales" |
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.
We don't depend on railties in the gemspec so I'd rather not depend on rails constants here. I think this also assumes all local engines are packages. Couldn't you just glob for package.yml files instead?
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.
I'll address this in your second comment below about paths because I think they're kind of related
That's not exactly accurate. It assumes any local engine might contain packages, but not that the engine itself is a package. The engine doesn't have to contain packages either. It's just adding all the paths to the list of paths that later get scanned for via glob for a
package.ymlfile.