From 6cf744b84cf71c423543ff039e48364789a6db7e Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 4 Apr 2025 08:14:53 +0200 Subject: [PATCH] test: Update build setup --- .env | 2 -- .rubocop.yml | 4 +-- .tool-versions | 1 - Gemfile | 35 ++++++++++-------- Makefile | 56 ++++++++++++++--------------- README.md | 45 +---------------------- Rakefile | 11 ++++++ bin/rails | 2 +- docker-compose.yml | 25 ------------- extra/.bashrc | 3 ++ extra/.env | 7 ++++ extra/Dockerfile | 28 ++++++++++----- extra/Dockerfile.dockerignore | 6 ++++ extra/Dockerfile_alpine | 18 ---------- extra/README.md | 21 ----------- extra/development.md | 39 ++++++++++++++++++++ extra/docker-compose.yml | 19 ++++++++++ extra/entrypoint.sh | 8 ----- spec/dummy/db/schema_development.rb | 2 +- spec/dummy/db/seeds.rb | 7 +--- 20 files changed, 156 insertions(+), 183 deletions(-) delete mode 100644 .env delete mode 100644 .tool-versions delete mode 100644 docker-compose.yml create mode 100644 extra/.bashrc create mode 100644 extra/.env create mode 100644 extra/Dockerfile.dockerignore delete mode 100644 extra/Dockerfile_alpine delete mode 100644 extra/README.md create mode 100644 extra/development.md create mode 100644 extra/docker-compose.yml delete mode 100644 extra/entrypoint.sh diff --git a/.env b/.env deleted file mode 100644 index 3247105..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -UID=1000 -GID=1000 diff --git a/.rubocop.yml b/.rubocop.yml index 37280f0..b25149f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,14 +4,12 @@ inherit_from: plugins: - rubocop-capybara + - rubocop-packaging - rubocop-performance - rubocop-rails - rubocop-rspec - rubocop-rspec_rails -require: - - rubocop-packaging - AllCops: Exclude: - bin/* diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index ae5ecdb..0000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -ruby 3.4.2 diff --git a/Gemfile b/Gemfile index 7ea67bc..7fa953f 100644 --- a/Gemfile +++ b/Gemfile @@ -3,27 +3,32 @@ source 'https://rubygems.org' if ENV['DEVEL'] == '1' - # for Docker dev - rails_ver = ENV.fetch('RAILS_VERSION') - gem 'rails', rails_ver - - gem 'activeadmin', ENV.fetch('ACTIVEADMIN_VERSION') gem 'activeadmin_quill_editor', path: './' - gem 'appraisal', '~> 2.4' - - if rails_ver.start_with?('7.0') - gem 'concurrent-ruby', '1.3.4' - gem 'sqlite3', '~> 1.4' - else - gem 'sqlite3' - end else gemspec - - gem 'sqlite3' 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}"] +gem(*rails) + +activeadmin = activeadmin_ver.empty? ? ['activeadmin'] : ['activeadmin', "~> #{activeadmin_ver}"] +gem(*activeadmin) + +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'] +gem(*sqlite3) + +# NOTE: to avoid error: uninitialized constant ActiveSupport::LoggerThreadSafeLevel::Logger +gem 'concurrent-ruby', '1.3.4' + +# Misc gem 'bigdecimal' +gem 'csv' gem 'mutex_m' gem 'puma' gem 'sassc' diff --git a/Makefile b/Makefile index 5e5c51d..780d3fb 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,36 @@ +include extra/.env + help: - @echo "Main targets: up / down / console / shell" + @echo "Main targets: build / specs / up / server / specs / shell" -# Docker commands -down: - docker compose down +# System commands -up: - docker compose up +build: + @rm -f Gemfile.lock spec/dummy/db/*.sqlite3 + @docker compose -f extra/docker-compose.yml build -attach: - docker compose attach app +db_reset: + @docker compose -f extra/docker-compose.yml run --rm app bin/rails db:reset db:test:prepare -up_attach: - docker compose up -d && docker compose attach app - -cleanup: - docker container rm -f activeadmin_quill_editor_app && docker image rm -f activeadmin_quill_editor-app - -# Rails specific commands -console: - docker compose exec -e "PAGER=more" app bin/rails console - -routes: - docker compose exec app bin/rails routes - -specs: - docker compose exec app bin/rspec --fail-fast - -# Other commands -bundle: - docker compose exec app bundle +up: build db_reset + @docker compose -f extra/docker-compose.yml up shell: - docker compose exec -e "PAGER=more" app bash + @docker compose -f extra/docker-compose.yml exec app bash + +down: + @docker compose -f extra/docker-compose.yml down --volumes --rmi local --remove-orphans + +# App commands + +console: + @docker compose -f extra/docker-compose.yml exec app bin/rails console lint: - docker compose exec app bin/rubocop + @docker compose -f extra/docker-compose.yml exec app bin/rubocop + +server: + @docker compose -f extra/docker-compose.yml exec app bin/rails server -b 0.0.0.0 -p ${SERVER_PORT} + +specs: + @docker compose -f extra/docker-compose.yml exec app bin/rspec --fail-fast diff --git a/README.md b/README.md index 9f54444..163e26e 100644 --- a/README.md +++ b/README.md @@ -117,50 +117,7 @@ Some methods are provided for advanced use cases: Project created by [Mattia Roccoberton](http://blocknot.es), thanks also to the good guys that opened issues and pull requests from time to time. -There 3 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 edit docker-compose.yml -# For more commands please check the Makefile -``` - -2) Using Appraisal: - -```sh -export RAILS_ENV=development -# Install dependencies -bin/appraisal -# Run server (or any command) -bin/appraisal rails s -# Or with specific versions -bin/appraisal rails71-activeadmin rails s -``` - -3) With a local setup: - -```sh -# Dev setup (set the required envs) -source extra/dev_setup.sh -# Install dependencies -bundle -# Run server (or any command) -bin/rails s -# To try different versions of Rails/ActiveAdmin edit extra/dev_setup.sh -``` +For development information please check [this document](extra/development.md). ## Do you like it? Star it! diff --git a/Rakefile b/Rakefile index 33129e7..dbb5eaf 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,16 @@ # frozen_string_literal: true +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__) +load 'rails/tasks/engine.rake' + +load 'rails/tasks/statistics.rake' + require 'bundler/gem_tasks' begin diff --git a/bin/rails b/bin/rails index ea1edbc..32d76a2 100755 --- a/bin/rails +++ b/bin/rails @@ -2,7 +2,7 @@ # This command will automatically be run when you run "rails" with Rails gems # installed from the root of your application. -ENV['RAILS_ENV'] ||= 'test' +ENV['RAILS_ENV'] ||= 'development' ENGINE_ROOT = File.expand_path('..', __dir__) ENGINE_PATH = File.expand_path('../lib/activeadmin/quill_editor/engine', __dir__) diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index b8a7c35..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,25 +0,0 @@ -services: - app: - container_name: 'activeadmin_quill_editor_app' - build: - context: . - dockerfile: ./extra/Dockerfile - # dockerfile: ./extra/Dockerfile_alpine - args: - # Debian-based Ruby image: - RUBY_IMAGE: ruby:3.4-slim - UID: ${UID} - environment: - ACTIVEADMIN_VERSION: ~> 3.3 - RAILS_VERSION: ~> 8.0 - user: "${UID}:${GID}" - ports: - - '3000:3000' - working_dir: '/app' - volumes: - - '.:/app' - stdin_open: true - tty: true - entrypoint: - - /bin/sh - - ./extra/entrypoint.sh diff --git a/extra/.bashrc b/extra/.bashrc new file mode 100644 index 0000000..78ebf4f --- /dev/null +++ b/extra/.bashrc @@ -0,0 +1,3 @@ +alias ls='ls --color' +alias ll='ls -l' +alias la='ls -la' diff --git a/extra/.env b/extra/.env new file mode 100644 index 0000000..625bdac --- /dev/null +++ b/extra/.env @@ -0,0 +1,7 @@ +COMPOSE_PROJECT_NAME=activeadmin_quill_editor + +BUNDLER_VERSION=2.5.23 +SERVER_PORT=4000 + +UID=1000 +GID=1000 diff --git a/extra/Dockerfile b/extra/Dockerfile index ca9287b..e515d1e 100644 --- a/extra/Dockerfile +++ b/extra/Dockerfile @@ -1,21 +1,31 @@ ARG RUBY_IMAGE=ruby:3 FROM ${RUBY_IMAGE} -ARG UID - +ENV DEBIAN_FRONTEND=noninteractive ENV DEVEL=1 ENV LANG=C.UTF-8 -ENV RAILS_ENV=development RUN apt-get update -qq -RUN DEBIAN_FRONTEND=noninteractive apt-get install -yqq --no-install-recommends build-essential chromium libyaml-dev nano +RUN apt-get install -yqq --no-install-recommends build-essential chromium less libyaml-dev nano netcat-traditional pkg-config -RUN gem install bundler +ARG BUNDLER_VERSION +RUN gem install bundler -v ${BUNDLER_VERSION} RUN echo 'gem: --no-document' > /etc/gemrc -RUN useradd -u ${UID} --shell /bin/bash app -RUN mkdir -p /home/app && chown -R app:app /home/app -RUN chown -R app /usr/local/bundle +ARG UID +RUN useradd -u $UID --shell /bin/bash app -USER ${UID} +RUN mkdir -p /home/app && chown -R app:app /home/app + +ARG RAILS_VERSION +ENV RAILS_VERSION=$RAILS_VERSION + +ARG ACTIVEADMIN_VERSION +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 ln -s /app/extra/.bashrc /home/app/.bashrc diff --git a/extra/Dockerfile.dockerignore b/extra/Dockerfile.dockerignore new file mode 100644 index 0000000..d8535e6 --- /dev/null +++ b/extra/Dockerfile.dockerignore @@ -0,0 +1,6 @@ +# Ignore everything but the required files for bundle install +/**/* + +!/*.gemspec +!/Gemfile +!/lib diff --git a/extra/Dockerfile_alpine b/extra/Dockerfile_alpine deleted file mode 100644 index e960879..0000000 --- a/extra/Dockerfile_alpine +++ /dev/null @@ -1,18 +0,0 @@ -FROM ruby:3.3-alpine - -ARG UID - -ENV DEVEL=1 -ENV LANG=C.UTF-8 -ENV RAILS_ENV=development - -RUN apk update && apk add --no-cache build-base chromium tzdata yaml-dev - -RUN gem install bundler -RUN echo 'gem: --no-document' > /etc/gemrc - -RUN adduser -u ${UID} -D app -RUN mkdir -p /home/app && chown -R app:app /home/app -RUN chown -R app /usr/local/bundle - -COPY . /app diff --git a/extra/README.md b/extra/README.md deleted file mode 100644 index a92af4a..0000000 --- a/extra/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# Development - -Overcommit can be used to ensure that Conventional commits are good. - -## Releases - -```sh -# Update version.rb with the new version -# Update the gemfiles: -bin/appraisal -``` - -## Testing - -```sh -# Running specs using a specific configuration: -bin/appraisal rails60-activeadmin22 rspec -# Using latest activeadmin version: -bin/appraisal rails60-activeadmin rspec -# See gemfiles for more configurations -``` diff --git a/extra/development.md b/extra/development.md new file mode 100644 index 0000000..897b504 --- /dev/null +++ b/extra/development.md @@ -0,0 +1,39 @@ +## Development + +Overcommit can be used to ensure that Conventional commits are good. + +### Dev setup + +There 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 +``` + +2) With a local setup: + +```sh +# Dev setup (set the required envs): +source extra/dev_setup.sh +# Install dependencies: +bundle update +# Run server (or any command): +bin/rails s +# To try different versions of Rails/ActiveAdmin edit extra/dev_setup.sh +``` diff --git a/extra/docker-compose.yml b/extra/docker-compose.yml new file mode 100644 index 0000000..8879307 --- /dev/null +++ b/extra/docker-compose.yml @@ -0,0 +1,19 @@ +services: + app: + build: + context: .. + dockerfile: extra/Dockerfile + args: + ACTIVEADMIN_VERSION: ${ACTIVEADMIN:-} + BUNDLER_VERSION: ${BUNDLER_VERSION} + RAILS_VERSION: ${RAILS:-} + RUBY_IMAGE: ruby:${RUBY:-3.4}-slim + UID: ${UID} + user: ${UID}:${GID} + ports: + - ${SERVER_PORT}:${SERVER_PORT} + working_dir: /app + volumes: + - ..:/app + stdin_open: true + tty: true diff --git a/extra/entrypoint.sh b/extra/entrypoint.sh deleted file mode 100644 index 0c9b41c..0000000 --- a/extra/entrypoint.sh +++ /dev/null @@ -1,8 +0,0 @@ -echo "> Install dependencies" -rm -f Gemfile.lock && bundle install - -echo "> Run pending migrations" -cd spec/dummy && bundle exec rails db:migrate - -echo "> Start Rails server" -cd /app && rm -f spec/dummy/tmp/pids/server.pid && bundle exec rails s -b 0.0.0.0 diff --git a/spec/dummy/db/schema_development.rb b/spec/dummy/db/schema_development.rb index aa9216f..4ea933c 100644 --- a/spec/dummy/db/schema_development.rb +++ b/spec/dummy/db/schema_development.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2018_06_07_053739) do +ActiveRecord::Schema.define(version: 2018_06_07_053739) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace" t.text "body" diff --git a/spec/dummy/db/seeds.rb b/spec/dummy/db/seeds.rb index 38505e0..48a94e1 100644 --- a/spec/dummy/db/seeds.rb +++ b/spec/dummy/db/seeds.rb @@ -1,13 +1,9 @@ # frozen_string_literal: true -puts 'Seeds:' - -puts 'Tags...' (11..16).each do |i| Tag.find_or_create_by!(name: "Tag #{i}") end -puts 'Authors...' (11..20).each do |i| age = 21 + 3 * (i - 10) attrs = { name: "Author #{i}", age: age, email: "some@email#{i}.com" } @@ -16,7 +12,6 @@ puts 'Authors...' end end -puts 'Posts...' authors = Author.pluck(:id) tags = Tag.where.not(name: 'A test tag').pluck(:id) (11..40).each do |i| @@ -38,4 +33,4 @@ end Author.find_or_create_by!(name: 'A test author', email: 'aaa@bbb.ccc', age: 30) Tag.find_or_create_by!(name: 'A test tag') -puts 'done.' +puts '> Seeds: done.'