From df4706cf4d4142561718c3a44c895dc2059bd02d Mon Sep 17 00:00:00 2001 From: Rei Date: Mon, 4 Nov 2024 17:45:27 +0800 Subject: [PATCH] Add devcontainer (#1446) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了 devcontainer 配置,用 vscode 或者其他支持 devcontainer 的编辑器/IDE 打开,在弹出提示中选择在容器中打开,进入命令行后用 `bin/dev` 即可启动服务。 --- .devcontainer/Dockerfile | 13 ++++++++++ .devcontainer/devcontainer.json | 19 ++++++++++++++ .devcontainer/docker-compose.yml | 40 ++++++++++++++++++++++++++++++ .gitignore | 1 + Gemfile.lock | 19 +++++++++----- Procfile.dev | 2 ++ bin/dev | 13 ++++++++-- test/controllers/api/nodes_test.rb | 2 +- test/models/reply_test.rb | 2 +- 9 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.yml create mode 100644 Procfile.dev diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000000..f9e7b9ef4a --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,13 @@ +FROM ruby:3.3-alpine + +RUN apk update && apk add curl gcc g++ gnupg make build-base ca-certificates socat git htop tzdata imagemagick ruby-dev openssl libpq-dev libxml2-dev libxslt-dev nodejs-current npm yarn cmake clang clang-dev gcompat + +RUN curl https://get.acme.sh | sh +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y +ENV PATH="/root/.cargo/bin:$PATH" +ENV RUSTFLAGS="-C target-feature=-crt-static" + +RUN gem install bundler -v 2.5.3 && \ + bundle config set --local path vendor/bundle + +WORKDIR /rails diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000000..8ca36efbbc --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,19 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/ruby-rails-postgres +{ + "name": "Homeland", + "dockerComposeFile": "docker-compose.yml", + "service": "app", + "workspaceFolder": "/rails", + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // This can be used to network with other containers or the host. + // "forwardPorts": [3000, 5432], + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "bundle install && yarn install && bin/rails db:setup" + // Configure tool-specific properties. + // "customizations": {}, + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml new file mode 100644 index 0000000000..d76e23b607 --- /dev/null +++ b/.devcontainer/docker-compose.yml @@ -0,0 +1,40 @@ +version: '3' + +services: + app: + build: + context: .. + dockerfile: .devcontainer/Dockerfile + + volumes: + - ..:/rails:cached + + # Overrides default command so things don't shut down after the process ends. + command: sleep infinity + + # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. + # network_mode: service:db + + # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + environment: + DATABASE_URL: postgres://postgres:postgres@postgres:5432 + REDIS_URL: redis://redis:6379 + + postgres: + image: postgres:latest + restart: unless-stopped + volumes: + - postgres-data:/var/lib/postgresql/data + environment: + POSTGRES_USER: postgres + POSTGRES_DB: postgres + POSTGRES_PASSWORD: postgres + + redis: + image: redis:latest + volumes: + - ./tmp/redis:/data + +volumes: + postgres-data: diff --git a/.gitignore b/.gitignore index 574d9633a8..14346796e6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ config/nginx/nginx.conf *.sublime-workspace .rvmrc vendor/ruby +vendor/bundle .idea/ spec/examples.txt .vagrant diff --git a/Gemfile.lock b/Gemfile.lock index 1ed0eb5dae..8d61046661 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,8 @@ GEM nokogiri (~> 1.6) rest-client (~> 2.0) ast (2.4.2) + autocorrect-rb (2.13.0-aarch64-linux) + rb_sys (>= 0.9.54) autocorrect-rb (2.13.0-arm64-darwin) rb_sys (>= 0.9.54) autocorrect-rb (2.13.0-x86_64-linux) @@ -184,6 +186,7 @@ GEM net-http faraday-rack (2.0.0) faraday (~> 2.0) + ffi (1.17.0-aarch64-linux-musl) ffi (1.17.0-arm64-darwin) ffi (1.17.0-x86_64-linux-gnu) ffi (1.17.0-x86_64-linux-musl) @@ -287,6 +290,8 @@ GEM net-protocol netrc (0.11.0) nio4r (2.7.4) + nokogiri (1.16.7-aarch64-linux) + racc (~> 1.4) nokogiri (1.16.7-arm64-darwin) racc (~> 1.4) nokogiri (1.16.7-x86_64-linux) @@ -373,9 +378,8 @@ GEM rack (>= 3.0.0) rack-test (2.1.0) rack (>= 1.3) - rackup (2.1.0) + rackup (2.2.0) rack (>= 3) - webrick (~> 1.8) rails (8.0.0.rc2) actioncable (= 8.0.0.rc2) actionmailbox (= 8.0.0.rc2) @@ -478,13 +482,16 @@ GEM ffi (~> 1.12) logger ruby2_keywords (0.0.5) - rucaptcha (3.2.3-arm64-darwin) + rucaptcha (3.2.4-aarch64-linux-musl) + railties (>= 3.2) + rb_sys (>= 0.9.86) + rucaptcha (3.2.4-arm64-darwin) railties (>= 3.2) rb_sys (>= 0.9.86) - rucaptcha (3.2.3-x86_64-linux) + rucaptcha (3.2.4-x86_64-linux) railties (>= 3.2) rb_sys (>= 0.9.86) - rucaptcha (3.2.3-x86_64-linux-musl) + rucaptcha (3.2.4-x86_64-linux-musl) railties (>= 3.2) rb_sys (>= 0.9.86) sass-rails (6.0.0) @@ -551,13 +558,13 @@ GEM method_source (~> 1.0) warden (1.2.9) rack (>= 2.0.9) - webrick (1.8.2) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) zeitwerk (2.7.1) PLATFORMS + aarch64-linux-musl arm64-darwin-21 arm64-darwin-22 arm64-darwin-23 diff --git a/Procfile.dev b/Procfile.dev new file mode 100644 index 0000000000..319eee0c77 --- /dev/null +++ b/Procfile.dev @@ -0,0 +1,2 @@ +web: bin/rails server +js: bin/shakapacker-dev-server diff --git a/bin/dev b/bin/dev index 5f91c20545..d80a02dbc1 100755 --- a/bin/dev +++ b/bin/dev @@ -1,2 +1,11 @@ -#!/usr/bin/env ruby -exec "./bin/rails", "server", *ARGV +#!/usr/bin/env sh + +if gem list --no-installed --exact --silent foreman; then + echo "Installing foreman..." + gem install foreman +fi + +# Default to port 3000 if not specified +export PORT="${PORT:-3000}" + +exec foreman start -f Procfile.dev --env /dev/null "$@" diff --git a/test/controllers/api/nodes_test.rb b/test/controllers/api/nodes_test.rb index 52d023030e..089a8ecca8 100644 --- a/test/controllers/api/nodes_test.rb +++ b/test/controllers/api/nodes_test.rb @@ -4,7 +4,7 @@ describe Api::V3::NodesController do describe "GET /api/nodes.json" do before do - %w[fun ruby nodes].each_with_index { |n, i| create(:node, name: n, id: i + 1) } + %w[fun ruby nodes].each_with_index { |n, i| create(:node, name: n, id: i + 1, sort: (3 - i)) } end it "should return the list of nodes" do diff --git a/test/models/reply_test.rb b/test/models/reply_test.rb index fc305b0de6..ce45607c56 100644 --- a/test/models/reply_test.rb +++ b/test/models/reply_test.rb @@ -223,7 +223,7 @@ class ReplyTest < ActiveSupport::TestCase test ".notification_receiver_ids" do mentioned_user_ids = [1, 2, 3] - user = create(:user) + user = create(:user, id: 99) topic = create(:topic, user_id: 10) reply = create(:reply, user: user, topic: topic, mentioned_user_ids: mentioned_user_ids)