From 45e7c0a6ebae1c0560f9716941a62f7e1a253555 Mon Sep 17 00:00:00 2001 From: Misha Merkushin Date: Fri, 2 Aug 2019 16:59:52 +0300 Subject: [PATCH] feat: Add ability to generate config files --- README.md | 19 ++++++- lib/dip/cli.rb | 8 +++ lib/dip/generators/ruby/gem/generator.rb | 23 +++++++++ .../generators/ruby/gem/templates/dip.yml.tt | 49 +++++++++++++++++++ .../ruby/gem/templates/docker-compose.yml.tt | 34 +++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 lib/dip/generators/ruby/gem/generator.rb create mode 100644 lib/dip/generators/ruby/gem/templates/dip.yml.tt create mode 100644 lib/dip/generators/ruby/gem/templates/docker-compose.yml.tt diff --git a/README.md b/README.md index 0c3dad2..329b41d 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,24 @@ dip --help dip SUBCOMMAND --help ``` -### dip.yml +### dip generate + +If your project has a typical schema, dip can generate all necessary config files with a single command. +Available generators you can find at [here](lib/dip/generators) + +```sh +dip generate ruby/gem --ruby 2.6 --bundler 2.0.2 --postgres 11.4 + +dip generate ruby/rails --ruby 2.6 --bundler 2.0.2 --node 11 --yarn 1.13.0 --postgres 11.4 --redis 4 --webpacker --selenium +``` + +You can omit any of above options. To list all available generator's options: + +```sh +dip generate [STACK] --help +``` + +### dip file reference The configuration file `dip.yml` should be placed in a project root directory. Also, in some cases, you may want to change the default config path by providing an environment variable `DIP_FILE`. diff --git a/lib/dip/cli.rb b/lib/dip/cli.rb index c003252..b58299f 100644 --- a/lib/dip/cli.rb +++ b/lib/dip/cli.rb @@ -78,6 +78,14 @@ def provision end end + desc "generate STACK [OPTIONS]", "Generate config files for a given stack" + def generate(stack, *argv) + # TODO: Add ability to download stack from any github repository. + require_relative "generators/#{stack}/generator.rb" + + Dip::Generator.start(argv) + end + require_relative 'cli/ssh' desc "ssh", "ssh-agent container commands" subcommand :ssh, Dip::CLI::SSH diff --git a/lib/dip/generators/ruby/gem/generator.rb b/lib/dip/generators/ruby/gem/generator.rb new file mode 100644 index 0000000..fc0d12c --- /dev/null +++ b/lib/dip/generators/ruby/gem/generator.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Dip + class Generator < Thor::Group + include Thor::Actions + + class_option :ruby, default: "latest" + class_option :postgres, default: false + class_option :appraisal, default: false + + def self.source_root + File.join(__dir__, "templates") + end + + def create_docker_compose_config + template("docker-compose.yml.tt", File.join("tmp", "docker-compose.yml")) + end + + def create_dip_config + template("dip.yml.tt", File.join("tmp", "dip.yml")) + end + end +end diff --git a/lib/dip/generators/ruby/gem/templates/dip.yml.tt b/lib/dip/generators/ruby/gem/templates/dip.yml.tt new file mode 100644 index 0000000..3044a51 --- /dev/null +++ b/lib/dip/generators/ruby/gem/templates/dip.yml.tt @@ -0,0 +1,49 @@ +version: '2' + +environment: + BUNDLE_GEMFILE: /app/Gemfile + +compose: + files: + - docker-compose.yml + +interaction: + app: + service: app + subcommands: + bash: + command: /usr/bin/bash + console: + command: ./bin/console + clean: + command: rm -rf Gemfile.lock<%= " gemfiles/*.gemfile.*" if options["appraisal"] %> + + bundle: + service: app + command: bundle + + <%- if options["appraisal"] -%> + appraisal: + service: app + command: bundle exec appraisal + + <%- end -%> + rspec: + service: app + <%- if options["appraisal"] -%> + command: bundle exec appraisal bundle exec rspec + <%- else -%> + command: bundle exec rspec + <%- end -%> + + rubocop: + service: app + command: bundle exec rubocop + compose_run_options: [no-deps] + +provision: + - dip app clean + - dip bundle install + <%- if options["appraisal"] -%> + - dip appraisal install + <%- end -%> diff --git a/lib/dip/generators/ruby/gem/templates/docker-compose.yml.tt b/lib/dip/generators/ruby/gem/templates/docker-compose.yml.tt new file mode 100644 index 0000000..ca92dc0 --- /dev/null +++ b/lib/dip/generators/ruby/gem/templates/docker-compose.yml.tt @@ -0,0 +1,34 @@ +version: '3.4' + +services: + app: + image: ruby:<%= options["ruby"] %> + environment: + BUNDLE_PATH: /bundle + BUNDLE_CONFIG: /app/.bundle/config + <%- if options["postgres"] -%> + DB_HOST: db + DB_NAME: docker + DB_USERNAME: postgres + <%- end -%> + command: bash + working_dir: /app + volumes: + - .:/app:cached + - bundler_data:/bundle + tmpfs: + - /tmp + <%- if options["postgres"] -%> + depends_on: + - db + <%- end -%> + + <%- if options["postgres"] -%> + db: + image: postgres:<%= options["postgres"] %> + environment: + - POSTGRES_DB=docker + + <%- end -%> +volumes: + bundler_data: