Top Level Namespace

Defined Under Namespace

Modules: Modulorails

Constant Summary collapse

VALID_LAST_INSTRUCTION =
/exec "\$\{?@}?"/
REDIS_URL =
ENV.fetch('REDIS_URL', 'redis://redis:6379')
REDIS_CLI =
Redis.new(url: REDIS_URL)

Instance Method Summary collapse

Instance Method Details

#check_dockerfile(verbose: false) ⇒ Object



20
21
22
23
24
25
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 20

def check_dockerfile(verbose: false)
  return true if File.exist?('Dockerfile')

  puts('No Dockerfile') if verbose
  false
end

#check_entrypoint(verbose: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 40

def check_entrypoint(verbose: false)
  el = entrypoint_location
  return true if el.nil?

  unless File.exist?(el)
    warn("Entrypoint not found at location: #{el}") if verbose
    return false
  end

  last_line = File.readlines(el).last&.strip
  return true if VALID_LAST_INSTRUCTION.match?(last_line)

  warn("Invalid entrypoint: Last instruction should be 'exec \"${@}\"' instead of '#{last_line}'") if verbose

  false
end

#entrypoint_locationObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 27

def entrypoint_location
  entrypoint_line = File.readlines('Dockerfile').find { |line| line.start_with?('ENTRYPOINT') }

  return nil if entrypoint_line.nil?

  md = /\[["'](.+)["']\]/.match(entrypoint_line)
  return nil if md.nil? || md[1].nil?

  md[1]
end

#executer_compose_run(docker_args, verbose: false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 80

def executer_compose_run(docker_args, verbose: false)
  entrypoint_option = check_entrypoint(verbose: verbose) ? '' : '--entrypoint "sh -c"'
  git_email = `git config --get user.email`.strip
  git_name = `git config --get user.name`.strip

  # Check if the shell is a TTY
  tty_option = $stdout.isatty ? '-ti' : ''

  # rubocop:disable Layout/LineLength
  command = %(docker compose build && docker compose run --rm #{tty_option} -e "GIT_AUTHOR_EMAIL=#{git_email}" -e "GIT_AUTHOR_NAME=#{git_name}" -e "GIT_COMMITTER_EMAIL=#{git_email}" -e "GIT_COMMITTER_NAME=#{git_name}" #{entrypoint_option} app)
  command = if entrypoint_option == ''
              "#{command} #{docker_args}"
            else
              "#{command} '#{docker_args}'"
            end
  # rubocop:enable Layout/LineLength
  puts(command) if verbose
  exec(command)
end

#executer_docker_run(docker_args, verbose: false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 57

def executer_docker_run(docker_args, verbose: false)
  pwd = Dir.pwd
  working_directory = File.basename(pwd)

  volumes = `docker volume ls -q -f name=modulogem`
  volumes = volumes.split("\n").map(&:strip)
  modulogem_gems = volumes.find { |volume| volume.include?('modulogem_gems') }
  modulogem = volumes.find { |volume| volume.include?('modulogem') }
  modulogem_gems_option = modulogem_gems.nil? ? '' : "-v #{modulogem_gems}:/usr/local/bundle"
  modulogem_option = modulogem.nil? ? '' : "-v #{modulogem}:/root"

  # Check if the shell is a TTY
  tty_option = $stdout.isatty ? '-ti' : ''

  # Build the command string
  # rubocop:disable Layout/LineLength
  command = %(docker run --rm #{modulogem_gems_option} #{modulogem_option} -v '#{pwd}:/app/#{working_directory}' #{tty_option} -w '/app/#{working_directory}' ezveus/ruby:latest #{docker_args})
  # rubocop:enable Layout/LineLength

  puts(command) if verbose
  exec(command)
end

#main(args, verbose: false) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 100

def main(args, verbose: false)
  # Escape each argument individually
  escaped_args = args.map { |arg| Shellwords.escape(arg) }

  # Check if the arguments contain a Ruby command or only options
  contains_command = false
  escaped_args.each_with_index do |arg, index|
    if !arg.start_with?('-') && (index.zero? || !escaped_args[index - 1].start_with?('-'))
      contains_command = true
      break
    end
  end

  docker_args = if contains_command
                  escaped_args.join(' ')
                else
                  "ruby #{escaped_args.join(' ')}"
                end

  if check_dockerfile(verbose: verbose)
    executer_compose_run(docker_args, verbose: verbose)
  else
    executer_docker_run(docker_args, verbose: verbose)
  end
end

#require_or_install(gem_name, version = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/generators/modulorails/githooks/templates/dockeruby.rb', line 4

def require_or_install(gem_name, version=nil)
  gem(gem_name, "~> #{version}") unless version.nil?
  require gem_name
rescue LoadError
  warn "Installing gem #{gem_name}"
  if version.nil?
    Gem.install(gem_name)
  else
    Gem.install(gem_name, "~> #{version}")
    gem(gem_name, "~> #{version}")
  end
  require gem_name
end