From d19fc7ddfc3dab60abb9494315dbbea02e1d22ba Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Sun, 13 Apr 2025 14:54:13 +0200 Subject: [PATCH] build: Improve dev setup --- .github/workflows/linters.yml | 23 ++++++++++------------- .github/workflows/specs_rails61.yml | 20 +++++++++++--------- .github/workflows/specs_rails70.yml | 17 +++++++++-------- .github/workflows/specs_rails71.yml | 17 +++++++++-------- .github/workflows/specs_rails72.yml | 15 ++++++++------- .github/workflows/specs_rails80.yml | 17 +++++++++-------- .gitignore | 1 + Gemfile | 24 ++++++++++++++++-------- Makefile | 18 ++++++++++++++---- extra/Dockerfile | 2 +- extra/dev_setup.sh | 2 -- extra/development.md | 28 ++++++++++------------------ extra/docker-compose.yml | 4 ++-- 13 files changed, 100 insertions(+), 88 deletions(-) diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index 591049a..9185aa3 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -2,38 +2,35 @@ name: Linters on: - push: - branches: - - main pull_request: + branches: [main] + push: + branches: [main] jobs: reviewdog: - name: reviewdog + name: Reviewdog runs-on: ubuntu-latest - strategy: - matrix: - ruby: ['3.0'] - env: - RUBY_VERSION: ${{ matrix.ruby }} + RAILS_VERSION: 7.0 steps: - - name: Check out code + - name: Checkout repository uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: ${{ matrix.ruby }} + ruby-version: 3.0 bundler-cache: true - - uses: reviewdog/action-setup@v1 + - name: Set up Reviewdog + uses: reviewdog/action-setup@v1 with: reviewdog_version: latest - - name: Run reviewdog + - name: Run Reviewdog env: REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | diff --git a/.github/workflows/specs_rails61.yml b/.github/workflows/specs_rails61.yml index d4706c1..258909d 100644 --- a/.github/workflows/specs_rails61.yml +++ b/.github/workflows/specs_rails61.yml @@ -1,24 +1,23 @@ --- -name: Specs Rails 6.1 +name: Specs Rails 6.1 with ActiveAdmin 2.9 on: - push: - branches: - - main pull_request: - branches: - - main + branches: [main] + push: + branches: [main] jobs: - tests: + test: runs-on: ubuntu-latest strategy: matrix: - ruby: ['3.0', '3.1'] + ruby: ['3.0'] env: - RAILS_VERSION: 6.1.0 + RAILS_VERSION: 6.0 + ACTIVEADMIN_VERSION: 2.9.0 steps: - name: Checkout repository @@ -30,6 +29,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:create db:migrate db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails70.yml b/.github/workflows/specs_rails70.yml index 8632fed..4ca1c9f 100644 --- a/.github/workflows/specs_rails70.yml +++ b/.github/workflows/specs_rails70.yml @@ -2,23 +2,21 @@ name: Specs Rails 7.0 on: - push: - branches: - - main pull_request: - branches: - - main + branches: [main] + push: + branches: [main] jobs: - tests: + test: runs-on: ubuntu-latest strategy: matrix: - ruby: ['3.1'] + ruby: ['3.0', '3.2'] env: - RAILS_VERSION: 7.0.0 + RAILS_VERSION: 7.0 steps: - name: Checkout repository @@ -30,6 +28,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:create db:migrate db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails71.yml b/.github/workflows/specs_rails71.yml index 6ed29a5..6817c31 100644 --- a/.github/workflows/specs_rails71.yml +++ b/.github/workflows/specs_rails71.yml @@ -2,23 +2,21 @@ name: Specs Rails 7.1 on: - push: - branches: - - main pull_request: - branches: - - main + branches: [main] + push: + branches: [main] jobs: - tests: + test: runs-on: ubuntu-latest strategy: matrix: - ruby: ['3.2'] + ruby: ['3.2', '3.4'] env: - RAILS_VERSION: 7.1.0 + RAILS_VERSION: 7.1 steps: - name: Checkout repository @@ -30,6 +28,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:create db:migrate db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails72.yml b/.github/workflows/specs_rails72.yml index 5bd955d..0937f06 100644 --- a/.github/workflows/specs_rails72.yml +++ b/.github/workflows/specs_rails72.yml @@ -2,15 +2,13 @@ name: Specs Rails 7.2 on: - push: - branches: - - main pull_request: - branches: - - main + branches: [main] + push: + branches: [main] jobs: - tests: + test: runs-on: ubuntu-latest strategy: @@ -18,7 +16,7 @@ jobs: ruby: ['3.2', '3.4'] env: - RAILS_VERSION: 7.2.0 + RAILS_VERSION: 7.2 steps: - name: Checkout repository @@ -30,6 +28,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:create db:migrate db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails80.yml b/.github/workflows/specs_rails80.yml index 3c8c838..a40312a 100644 --- a/.github/workflows/specs_rails80.yml +++ b/.github/workflows/specs_rails80.yml @@ -2,23 +2,21 @@ name: Specs Rails 8.0 on: - push: - branches: - - main pull_request: - branches: - - main + branches: [main] + push: + branches: [main] jobs: - tests: + test: runs-on: ubuntu-latest strategy: matrix: - ruby: ['3.2', '3.3', '3.4'] + ruby: ['3.2', '3.4'] env: - RAILS_VERSION: 8.0.0 + RAILS_VERSION: 8.0 steps: - name: Checkout repository @@ -30,6 +28,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:create db:migrate db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.gitignore b/.gitignore index 0056dbd..a643ffd 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /_misc/ /coverage/ /spec/dummy/db/*.sqlite3* +/spec/dummy/db/schema-dev.rb /spec/dummy/log/ /spec/dummy/storage/ /spec/dummy/tmp/ diff --git a/Gemfile b/Gemfile index 7fa953f..0e16147 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,12 @@ source 'https://rubygems.org' +def eval_version(dependency, version) + return [dependency] if version.empty? + + version.count('.') < 2 ? [dependency, "~> #{version}.0"] : [dependency, version] +end + if ENV['DEVEL'] == '1' gem 'activeadmin_quill_editor', path: './' else @@ -9,20 +15,22 @@ else end ruby_ver = ENV.fetch('RUBY_VERSION', '') -rails_ver = ENV.fetch('RAILS_VERSION', '') -activeadmin_ver = ENV.fetch('ACTIVEADMIN_VERSION', '') -rails = rails_ver.empty? ? ['rails'] : ['rails', "~> #{rails_ver}"] +rails_ver = ENV.fetch('RAILS_VERSION', '') +rails = eval_version('rails', rails_ver) gem(*rails) -activeadmin = activeadmin_ver.empty? ? ['activeadmin'] : ['activeadmin', "~> #{activeadmin_ver}"] -gem(*activeadmin) +active_admin_ver = ENV.fetch('ACTIVEADMIN_VERSION', '') +active_admin = eval_version('activeadmin', active_admin_ver) +gem(*active_admin) -ruby32 = Gem::Version.new(ruby_ver) >= Gem::Version.new('3.2') -rails72 = Gem::Version.new(rails_ver) >= Gem::Version.new('7.2') -sqlite3 = ruby32 || rails72 ? ['sqlite3'] : ['sqlite3', '~> 1.4'] +ruby32 = ruby_ver.empty? || Gem::Version.new(ruby_ver) >= Gem::Version.new('3.2') +rails72 = rails_ver.empty? || Gem::Version.new(rails_ver) >= Gem::Version.new('7.2') +sqlite3 = ruby32 && rails72 ? ['sqlite3'] : ['sqlite3', '~> 1.4'] gem(*sqlite3) +gem 'zeitwerk', '~> 2.6.18' unless ruby32 + # NOTE: to avoid error: uninitialized constant ActiveSupport::LoggerThreadSafeLevel::Logger gem 'concurrent-ruby', '1.3.4' diff --git a/Makefile b/Makefile index 780d3fb..7eb4207 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,14 @@ include extra/.env help: - @echo "Main targets: build / specs / up / server / specs / shell" + @echo -e "${COMPOSE_PROJECT_NAME} - Main project commands:\n\ + make up # starts the dev services (optional env vars: RUBY / RAILS / ACTIVEADMIN)\n\ + make specs # run the tests (after up)\n\ + make lint # run the linters (after up)\n\ + make server # run the server (after up)\n\ + make shell # open a shell (after up)\n\ + make down # cleanup (after up)\n\ + Example: RUBY=3.2 RAILS=7.1 ACTIVEADMIN=3.2.0 make up" # System commands @@ -10,7 +17,7 @@ build: @docker compose -f extra/docker-compose.yml build db_reset: - @docker compose -f extra/docker-compose.yml run --rm app bin/rails db:reset db:test:prepare + @docker compose -f extra/docker-compose.yml run --rm app bin/rails db:create db:migrate db:test:prepare up: build db_reset @docker compose -f extra/docker-compose.yml up @@ -23,13 +30,16 @@ down: # App commands -console: +seed: + @docker compose -f extra/docker-compose.yml exec app bin/rails db:seed + +console: seed @docker compose -f extra/docker-compose.yml exec app bin/rails console lint: @docker compose -f extra/docker-compose.yml exec app bin/rubocop -server: +server: seed @docker compose -f extra/docker-compose.yml exec app bin/rails server -b 0.0.0.0 -p ${SERVER_PORT} specs: diff --git a/extra/Dockerfile b/extra/Dockerfile index e515d1e..bea3373 100644 --- a/extra/Dockerfile +++ b/extra/Dockerfile @@ -26,6 +26,6 @@ ENV ACTIVEADMIN_VERSION=$ACTIVEADMIN_VERSION WORKDIR /app COPY . /app RUN bundle install -RUN mkdir -p /app/spec/dummy/db && chown -R app:app /app/spec/dummy/db /usr/local/bundle +RUN chown -R app:app /usr/local/bundle RUN ln -s /app/extra/.bashrc /home/app/.bashrc diff --git a/extra/dev_setup.sh b/extra/dev_setup.sh index adea0b4..a3d279a 100755 --- a/extra/dev_setup.sh +++ b/extra/dev_setup.sh @@ -4,5 +4,3 @@ export DEVEL=1 export RAILS_VERSION=7.2.2.1 export ACTIVEADMIN_VERSION=3.3.0 - -export RAILS_ENV=development diff --git a/extra/development.md b/extra/development.md index 897b504..3f8733c 100644 --- a/extra/development.md +++ b/extra/development.md @@ -1,29 +1,21 @@ ## Development -Overcommit can be used to ensure that Conventional commits are good. - ### Dev setup -There 2 ways to interact with this project: +There are 2 ways to interact with this project: 1) Using Docker: ```sh -# Run rails server on the dummy app (=> http://localhost:3000 to access to ActiveAdmin): -make up -# Enter in a Rails console (with the dummy app started): -make console -# Enter in a shell (with the dummy app started): -make shell -# Run the linter on the project (with the dummy app started): -make lint -# Run the test suite (with the dummy app started): -make specs -# Remove container and image: -make cleanup -# To try different versions of Ruby/Rails/ActiveAdmin: -RUBY=3.2 RAILS=7.1.0 ACTIVEADMIN=3.2.0 make up -# For more commands please check the Makefile +make up # starts the dev services (optional env vars: RUBY / RAILS / ACTIVEADMIN) +make specs # run the tests (after up) +make lint # run the linters (after up) +make server # run the server (after up) +make shell # open a shell (after up) +make down # cleanup (after up) + +# Example using specific versions: +RUBY=3.2 RAILS=7.1 ACTIVEADMIN=3.2.0 make up ``` 2) With a local setup: diff --git a/extra/docker-compose.yml b/extra/docker-compose.yml index 8879307..020b70f 100644 --- a/extra/docker-compose.yml +++ b/extra/docker-compose.yml @@ -4,10 +4,10 @@ services: context: .. dockerfile: extra/Dockerfile args: - ACTIVEADMIN_VERSION: ${ACTIVEADMIN:-} BUNDLER_VERSION: ${BUNDLER_VERSION} - RAILS_VERSION: ${RAILS:-} RUBY_IMAGE: ruby:${RUBY:-3.4}-slim + RAILS_VERSION: ${RAILS:-} + ACTIVEADMIN_VERSION: ${ACTIVEADMIN:-} UID: ${UID} user: ${UID}:${GID} ports: