Class: Dockerizer::Rails

Inherits:
Thor
  • Object
show all
Defined in:
lib/dockerizer/rails.rb

Instance Method Summary collapse

Instance Method Details

#with(db_type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dockerizer/rails.rb', line 22

def with(db_type)
    @db_type = db_type
    valid_db_types = ["postgresql"]
    unless valid_db_types.include?(@db_type)
        puts "Invalid db type #{@db_type}. Valid types are #{valid_db_types}"
        exit!
    end

    if options[:name]
        @project_name = options[:name]
    else
        puts "What is your project name?"
        @project_name = STDIN.gets.chomp
    end
    if options[:db_username]
        @db_username = options[:db_username]
    else
        puts "Enter a db username..."
        @db_username = STDIN.gets.chomp
    end
    if options[:db_password]
        @db_password = options[:db_password]
    else
        puts "Enter a db password..."
        @db_password = STDIN.gets.chomp
    end
    puts "Generating rails aplication with #{@db_type} as a database in #{@project_name}..."
    puts "Creating #{@project_name} dir in #{Dir.pwd}"

    project_dir = Dir.pwd + "/#{@project_name}"
    if Dir.exist?(project_dir)
        puts "Found dir with same project name!"
        puts "Replacing dir with the current project..."
        FileUtils.remove_dir(project_dir)
    end

    puts "Creating project folder..."
    FileUtils.mkdir @project_name

    puts "Copy template files..."
    template_dir = Dir.glob(File.expand_path(File.dirname(__FILE__) + "/template/rails/common/*"))
    FileUtils.cp_r(template_dir, project_dir)

    docker_compose_dir = File.expand_path("#{File.dirname(__FILE__)}/template/rails/#{@db_type}/docker-compose.yml.erb")
    config_dir = File.expand_path("#{File.dirname(__FILE__)}/config/rails/database.yml.erb")

    puts "docker-compose file found at #{docker_compose_dir}"
    docker_compose_file = File.read(docker_compose_dir)
    docker_compose_renderer = ERB.new(docker_compose_file)
    docker_compose_output = docker_compose_renderer.result(binding)
    File.open("#{project_dir}/docker-compose.yml", "w") do |f|
        f.write(docker_compose_output)
        f.close()
    end
    puts "docker-compose file added successfuly"

    puts "Database config found at #{config_dir}"
    config_file = File.read(config_dir)
    renderer = ERB.new(config_file)
    config_output = renderer.result(binding)
    
    is_api = options[:api] ? '--api' : ''

    puts "Initializing rails application..."
    Dir.chdir(project_dir) do 
        puts "Currently in #{Dir.pwd}"
        system "docker-compose run web rails new . --force -T --database=#{db_type} " + is_api
        system "docker-compose build"
        File.open("config/database.yml", "w") do |f|
            f.write(config_output)
            f.close()
        end
    end
end