Class: WebGit::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/web_git/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#already_installed?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/generators/web_git/install_generator.rb', line 40

def already_installed?
  filename = Rails.root.join("config.ru")
  contents = open(filename).read

  contents.match?(/if\s*Rails.env.development\?\s*map\s*'\/git'\s*do\s*run\s*WebGit::Server\s*end\s*end/)
end

#generate_serverObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/web_git/install_generator.rb', line 3

def generate_server
  
  filename = Rails.root.join("config.ru")

  log :insert, "Updating config.ru to run apps in parallel."
  if File.exists?(filename) && already_installed?
    log :identical, "Skipping overrides."
  else
    contents = <<~RUBY

    if Rails.env.development?
      map '/git' do
        run WebGit::Server
      end
    end
  
    map '/' do
    RUBY
  
    filename = "config.ru"
    match_text = "run Rails.application"
  
    insert_into_file filename, contents, before: match_text
    insert_into_file filename, "\nend", after: match_text, force: true
    gsub_file filename, match_text, "\t#{match_text}"
  end

  expect_installed = run("which expect")

  unless expect_installed
    log :insert, "Installing expect."
    run "sudo apt install -y expect"
  else
    log :identical, "expect already installed."
  end
end