Skip to content

Commit

Permalink
feat: Add ability to generate config files
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Oct 30, 2019
1 parent 2130998 commit a537163
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
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:

0 comments on commit a537163

Please sign in to comment.