Skip to content

Commit

Permalink
Option for loading files at package root (#24)
Browse files Browse the repository at this point in the history
Rails conventions for app/controllers, app/domain etc are not very
useful for projects defining a large domain and utilizing packs to
organize code.

This change allows adding application code to the eager load path
located at the root of where the package.yml is defined giving the user
full control of the file structure.

previously you could have a subpacks that looked like this:

```
- packs/
-   accounting/
-     package.yml
-     app/
-       public/
-         api.rb
-       domain/
-         subprocess/
-           package.yml
-           run_subprocess.rb
-         subprocess2/
-           package.yml
-           run_subprocess.rb

```

The subpack works fine, but all of the dependencies have to reference
into the rails code structure, i.e:

```
dependencies:
  - packs/accounting/app/domain/subprocess
```

Specifying autoload_glob allows you to have complete control for
structuring your pack.

```
-   accounting/
-     package.yml
-     app/
-       public/
-         api.rb
-     subprocess/
-       package.yml
-       run_subprocess.rb
-     subprocess2/
-       package.yml
-       run_subprocess.rb

```

Dependencies can referece the pack by:

```
dependencies:
  - packs/accounting/subprocess
```
  • Loading branch information
daicoden authored and gap777 committed Jan 15, 2025
1 parent 4ff0e79 commit 6aaf2b5
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c
- uses: actions/checkout@cbb722410c2e876e24abbe8de2cc27693e501dcb
- name: Tag and Push Gem
id: tag-and-push-gem
# This action basically runs `bundle exec rake release` if there is not an existing tag in GitHub
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jobs:
strategy:
matrix:
ruby:
- 2.7
# Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
- '3.0'
- 3.1
Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.4
11 changes: 6 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
automatic_namespaces (0.4.0)
automatic_namespaces (0.5.0)
activesupport
packs-rails

Expand Down Expand Up @@ -98,6 +98,7 @@ GEM
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.8)
minitest (5.16.3)
net-imap (0.3.1)
net-protocol
Expand All @@ -107,10 +108,9 @@ GEM
timeout
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.13.9-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.9-x86_64-linux)
nio4r (2.7.4)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
packs (0.0.6)
sorbet-runtime
Expand Down Expand Up @@ -196,6 +196,7 @@ GEM

PLATFORMS
arm64-darwin-21
arm64-darwin-24
x86_64-linux

DEPENDENCIES
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ If bundler is not being used to manage dependencies, install the gem by executin

Given the `package.yml` of a strongly namespaced pack:

```
```yml
enforce_dependencies: true
enforce_privacy: true
public_path: app/public/
Expand All @@ -72,7 +72,7 @@ metadata:
modify the metadata to opt into automatic namespacing:
```
```yml
metadata:
automatic_pack_namespace: true
```
Expand All @@ -99,7 +99,7 @@ metadata:
If your package / namespace name requires ActiveSupport inflections, you will probably need to tell `automatic_namespaces`
what the correct namespace name should be in that package:

```
```yml
# packs/shoes_ui/package.yml
metadata:
automatic_pack_namespace: true
Expand All @@ -109,6 +109,16 @@ metadata:
This is necessary because `automatic_namespaces` works by modifying the autoloader paths, which has to
happen during Rails application initialization; but the inflector is not available for use then.

If you would like to use your own file layout conventions for packs (i.e. not `app/*`) you can specify
your own glob by using `autoload_glob` to append the glob to the folder containing package.yml. This defaults
to `'/**/app/*'`

```yml
metadata:
# Put the folder containing package.yml as the root for the autoloader.
autoload_glob: ''
```

## Development

After checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests.
Expand Down
3 changes: 2 additions & 1 deletion lib/automatic_namespaces/autoloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def set_namespace_for_dir(pack_dir, package_namespace)
end

def pack_directories(pack_root_dir, metadata)
Dir.glob("#{pack_root_dir}/**/app/*").select { |dir| namespaced_directory?(dir, metadata) }
glob = metadata['autoload_glob'] || "/**/app/*"
Dir.glob("#{pack_root_dir}#{glob}").select { |dir| namespaced_directory?(dir, metadata) }
end

def namespaced_directory?(dir, metadata)
Expand Down
2 changes: 1 addition & 1 deletion lib/automatic_namespaces/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AutomaticNamespaces
VERSION = "0.4.0"
VERSION = "0.5.1"
end
6 changes: 6 additions & 0 deletions spec/automatic_namespaces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@
expect(defined?(Sneaker)).to eq("constant")
end
end

it "can find classes that are located at the root of the package.yml" do
expect(defined?(Summer::Fun)).to eq("constant")
expect(defined?(Summer::Swim)).to eq("constant")
expect(defined?(Summer::Swim::Trunks)).to eq("constant")
end
end
1 change: 1 addition & 0 deletions spec/fixtures/rails-7.0/packs/summer/app/domain/fun.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Summer::Fun; end
2 changes: 2 additions & 0 deletions spec/fixtures/rails-7.0/packs/summer/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
metadata:
automatic_pack_namespace: true
4 changes: 4 additions & 0 deletions spec/fixtures/rails-7.0/packs/summer/swim/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
metadata:
automatic_pack_namespace: true
namespace_override: "Summer::Swim"
autoload_glob: ''
1 change: 1 addition & 0 deletions spec/fixtures/rails-7.0/packs/summer/swim/trunks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Summer::Swim::Trunks; end

0 comments on commit 6aaf2b5

Please sign in to comment.