From 08e676578e21e7a70481a49bef73f38aad6d7d92 Mon Sep 17 00:00:00 2001 From: Juan Gallego IV <juan@gallegoiv.com> Date: Fri, 8 Mar 2024 22:01:35 +0100 Subject: [PATCH 1/5] Installing spring to accelerate code execution on dev/test --- Gemfile | 1 + Gemfile.lock | 2 ++ bin/rails | 1 + bin/spring | 14 ++++++++++++++ config/environments/test.rb | 3 ++- docker-compose.yml | 2 ++ 6 files changed, 22 insertions(+), 1 deletion(-) create mode 100755 bin/spring diff --git a/Gemfile b/Gemfile index eb3c0b7..1c0732d 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ group :development do gem "brakeman" gem "bundler-audit" gem "rubocop" + gem "spring" gem "web-console" end diff --git a/Gemfile.lock b/Gemfile.lock index a2e896b..989b945 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -228,6 +228,7 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) + spring (4.1.3) sprockets (4.2.1) concurrent-ruby (~> 1.0) rack (>= 2.2.4, < 4) @@ -282,6 +283,7 @@ DEPENDENCIES rails (~> 7.1.3, >= 7.1.3.2) rubocop selenium-webdriver + spring sprockets-rails stimulus-rails tailwindcss-rails diff --git a/bin/rails b/bin/rails index efc0377..c8b5338 100755 --- a/bin/rails +++ b/bin/rails @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +load File.expand_path("spring", __dir__) APP_PATH = File.expand_path("../config/application", __dir__) require_relative "../config/boot" require "rails/commands" diff --git a/bin/spring b/bin/spring new file mode 100755 index 0000000..a81373a --- /dev/null +++ b/bin/spring @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +# This file loads Spring without loading other gems in the Gemfile in order to be fast. +# It gets overwritten when you run the `spring binstub` command. + +if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"]) + require "bundler" + + Bundler.locked_gems.specs.find { |spec| spec.name == "spring" }&.tap do |spring| + Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path + gem "spring", spring.version + require "spring/binstub" + end +end diff --git a/config/environments/test.rb b/config/environments/test.rb index 8a33329..c8ab2d4 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -9,9 +9,10 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. + config.cache_classes = false # While tests run files are not watched, reloading is not necessary. - config.enable_reloading = false + config.enable_reloading = true # Eager loading loads your entire application. When running a single test locally, # this is usually not necessary, and can slow down your test suite. However, it's diff --git a/docker-compose.yml b/docker-compose.yml index 22bc66f..8bf4375 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,6 +19,8 @@ services: - .:/rails ports: - "3000:3000" + environment: + DATABASE_URL: postgres://postgres:postgres@postgres/pictoplan_test depends_on: postgres: condition: service_healthy From bcf77eda12ae453cc2204210cdf76d28916a6301 Mon Sep 17 00:00:00 2001 From: Juan Gallego IV <juan@gallegoiv.com> Date: Fri, 8 Mar 2024 22:04:38 +0100 Subject: [PATCH 2/5] Welcome packwerk to my life --- Gemfile | 2 ++ Gemfile.lock | 23 +++++++++++++++++++++++ bin/packwerk | 27 +++++++++++++++++++++++++++ package.yml | 11 +++++++++++ packwerk.yml | 23 +++++++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100755 bin/packwerk create mode 100644 package.yml create mode 100644 packwerk.yml diff --git a/Gemfile b/Gemfile index 1c0732d..01a7e87 100644 --- a/Gemfile +++ b/Gemfile @@ -32,3 +32,5 @@ group :test do gem "capybara" gem "selenium-webdriver" end + +gem "packwerk", "~> 3.2" diff --git a/Gemfile.lock b/Gemfile.lock index 989b945..6065217 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,13 @@ GEM public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) base64 (0.2.0) + better_html (2.0.2) + actionview (>= 6.0) + activesupport (>= 6.0) + ast (~> 2.0) + erubi (~> 1.4) + parser (>= 2.4) + smart_properties bigdecimal (3.1.6) bindex (0.8.1) bootsnap (1.18.3) @@ -100,6 +107,7 @@ GEM xpath (~> 3.2) concurrent-ruby (1.2.3) connection_pool (2.4.1) + constant_resolver (0.2.0) crass (1.0.6) date (3.3.4) debug (1.9.1) @@ -152,11 +160,23 @@ GEM racc (~> 1.4) nokogiri (1.16.2-x86_64-linux) racc (~> 1.4) + packwerk (3.2.0) + activesupport (>= 6.0) + ast + better_html + bundler + constant_resolver (>= 0.2.0) + parallel + parser + prism (>= 0.24.0) + sorbet-runtime (>= 0.5.9914) + zeitwerk (>= 2.6.1) parallel (1.24.0) parser (3.3.0.5) ast (~> 2.4.1) racc pg (1.5.6) + prism (0.24.0) psych (5.1.2) stringio public_suffix (5.0.4) @@ -228,6 +248,8 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) + smart_properties (1.17.0) + sorbet-runtime (0.5.11287) spring (4.1.3) sprockets (4.2.1) concurrent-ruby (~> 1.0) @@ -278,6 +300,7 @@ DEPENDENCIES debug importmap-rails jbuilder + packwerk (~> 3.2) pg (~> 1.1) puma (>= 5.0) rails (~> 7.1.3, >= 7.1.3.2) diff --git a/bin/packwerk b/bin/packwerk new file mode 100755 index 0000000..f94a0f3 --- /dev/null +++ b/bin/packwerk @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'packwerk' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +bundle_binstub = File.expand_path("bundle", __dir__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("packwerk", "packwerk") diff --git a/package.yml b/package.yml new file mode 100644 index 0000000..649f22b --- /dev/null +++ b/package.yml @@ -0,0 +1,11 @@ +# This file represents the root package of the application +# Please validate the configuration using `packwerk validate` (for Rails applications) or running the auto generated +# test case (for non-Rails projects). You can then use `packwerk check` to check your code. + +# Change to `true` to turn on dependency checks for this package +enforce_dependencies: false + +# A list of this package's dependencies +# Note that packages in this list require their own `package.yml` file +# dependencies: +# - "packages/billing" diff --git a/packwerk.yml b/packwerk.yml new file mode 100644 index 0000000..28abfe8 --- /dev/null +++ b/packwerk.yml @@ -0,0 +1,23 @@ +# See: Setting up the configuration file +# https://github.com/Shopify/packwerk/blob/main/USAGE.md#configuring-packwerk + +# List of patterns for folder paths to include +# include: +# - "**/*.{rb,rake,erb}" + +# List of patterns for folder paths to exclude +# exclude: +# - "{bin,node_modules,script,tmp,vendor}/**/*" + +# Patterns to find package configuration files +# package_paths: "**/" + +# List of custom associations, if any +# custom_associations: +# - "cache_belongs_to" + +# Whether or not you want the cache enabled (disabled by default) +# cache: true + +# Where you want the cache to be stored (default below) +# cache_directory: 'tmp/cache/packwerk' From 2decd23cc6d9e90b4680b528cefe7ef528cdeb8b Mon Sep 17 00:00:00 2001 From: Juan Gallego IV <juan@gallegoiv.com> Date: Fri, 8 Mar 2024 22:11:39 +0100 Subject: [PATCH 3/5] More packwerk setup --- app/controllers/application_controller.rb | 1 + config/application.rb | 2 ++ package.yml | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7944f9f..ce84fad 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true class ApplicationController < ActionController::Base + append_view_path(Dir.glob(Rails.root.join("app/packages/*/views"))) end diff --git a/config/application.rb b/config/application.rb index 6c605be..3f87a39 100644 --- a/config/application.rb +++ b/config/application.rb @@ -18,6 +18,8 @@ class Application < Rails::Application # Common ones are `templates`, `generators`, or `middleware`, for example. config.autoload_lib(ignore: %w(assets tasks)) + config.paths.add "app/packages", glob: "{*,*/concerns}", eager_load: true + # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files diff --git a/package.yml b/package.yml index 649f22b..f06842f 100644 --- a/package.yml +++ b/package.yml @@ -3,7 +3,7 @@ # test case (for non-Rails projects). You can then use `packwerk check` to check your code. # Change to `true` to turn on dependency checks for this package -enforce_dependencies: false +enforce_dependencies: true # A list of this package's dependencies # Note that packages in this list require their own `package.yml` file From e49cd124d18d3f3097e67f27c2f986839f679c8f Mon Sep 17 00:00:00 2001 From: Juan Gallego IV <juan@gallegoiv.com> Date: Fri, 8 Mar 2024 22:15:12 +0100 Subject: [PATCH 4/5] Add packwerk check to CI --- .github/workflows/ci.yml | 15 +++++++++++++++ bin/check-packages | 3 +++ 2 files changed, 18 insertions(+) create mode 100755 bin/check-packages diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e182db4..773fcff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,6 +53,21 @@ jobs: - name: Lint code for consistent style run: bin/check-libraries + check-packages: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: .ruby-version + bundler-cache: true + + - name: Prevent packages' boundaries violations + run: bin/check-packages + test: runs-on: ubuntu-latest services: diff --git a/bin/check-packages b/bin/check-packages new file mode 100755 index 0000000..5dbd157 --- /dev/null +++ b/bin/check-packages @@ -0,0 +1,3 @@ +#!/bin/bash -e + +bundle exec packwerk check ${@} From b91b1f68482e89ec15e2fc3f52b3b9609a547fcc Mon Sep 17 00:00:00 2001 From: Juan Gallego IV <juan@gallegoiv.com> Date: Fri, 8 Mar 2024 22:26:27 +0100 Subject: [PATCH 5/5] Refactor CI workflow --- .github/workflows/ci.yml | 86 ++++++++++------------------------------ 1 file changed, 22 insertions(+), 64 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 773fcff..4ea717a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,67 +7,6 @@ on: branches: [ main ] jobs: - check-security: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: .ruby-version - bundler-cache: true - - - name: Scan for security vulnerabilities - run: bin/check-security - - check-style: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: .ruby-version - bundler-cache: true - - - name: Lint code for consistent style - run: bin/check-style - - check-libraries: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: .ruby-version - bundler-cache: true - - - name: Lint code for consistent style - run: bin/check-libraries - - check-packages: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: .ruby-version - bundler-cache: true - - - name: Prevent packages' boundaries violations - run: bin/check-packages - test: runs-on: ubuntu-latest services: @@ -92,10 +31,29 @@ jobs: ruby-version: .ruby-version bundler-cache: true + - name: Configure DB + env: + RAILS_ENV: "test" + DATABASE_URL: "postgres://postgres:postgres@localhost:5432/pictoplan_test" + run: bin/rails db:setup + + - name: Scan for security vulnerabilities + run: bin/check-security + + - name: Lint code for consistent style + run: bin/check-style + + - name: Audit dependencies + run: bin/check-libraries + + - name: Prevent packages' boundaries violations + env: + RAILS_ENV: "test" + DATABASE_URL: "postgres://postgres:postgres@localhost:5432/pictoplan_test" + run: bin/check-packages + - name: Run Tests env: RAILS_ENV: "test" DATABASE_URL: "postgres://postgres:postgres@localhost:5432/pictoplan_test" - run: | - bin/rails db:setup - bin/rails test:all + run: bin/rails test:all