Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate config files #66

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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`.
Expand Down
8 changes: 8 additions & 0 deletions lib/dip/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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
Expand Down
23 changes: 23 additions & 0 deletions lib/dip/generators/ruby/gem/generator.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions lib/dip/generators/ruby/gem/templates/dip.yml.tt
Original file line number Diff line number Diff line change
@@ -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 -%>
34 changes: 34 additions & 0 deletions lib/dip/generators/ruby/gem/templates/docker-compose.yml.tt
Original file line number Diff line number Diff line change
@@ -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: