modulorails 1.5.0.pre3 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e6eab252232df18bbd0d7cd328d568ed4dda0d254cf4db3842de66759be6349
4
- data.tar.gz: 43051d6b7f325d46150a348a7fafd1efbff924bf43569311bb103e55392b2d4e
3
+ metadata.gz: 49cd77852f9aee93c053c1722f8332ad44fa712f2ff00830ddd44ab0e873a722
4
+ data.tar.gz: 6ce2b6f0314ee71f843bdc4f012ceda11ead2b326e98e954b517e334e1646fde
5
5
  SHA512:
6
- metadata.gz: 3f23a3e4df54aef59a6ee1f7aae9616b2554da45c9f6f625b040e3c2c5f47939073df02cfc41dab8f3a7e1b92f229e4b94d9de12b578c69a9b96dc3d73cf8db1
7
- data.tar.gz: a2a3c61713a40049024c0905da304a0260750e922dedb4494068a7f03541b7a1d148d93f927405a4cba3aaf1cccfad3ee75c06774e3db2ad4975606cffe4dc42
6
+ metadata.gz: 104ac0e4ab98f6194fec633367c0a87b25198fa5c8f0aef3cadac808ff8328c1c96fa1b11bd6881690f414c0400b9eacfc86a083c982a60e87039358c5ad8274
7
+ data.tar.gz: 3652ed356e878e708d54ae4d17cb18707afd7deabe57846cacdb2fc53caff699ae1c0cb3a932699a0829fd01be80978a406960df7eb534fec1c16daa1b124c36
data/CHANGELOG.md CHANGED
@@ -4,7 +4,7 @@ This file is used to list changes made in each version of the gem.
4
4
 
5
5
  # Unreleased
6
6
 
7
- # 1.5.0
7
+ # 1.5.1
8
8
 
9
9
  - Update templates according to new standards:
10
10
  - Optimize layers in Dockerfile.prod.
@@ -28,6 +28,10 @@ This file is used to list changes made in each version of the gem.
28
28
  - Deprecate `Modulorails::BaseService#log` and `Modulorails::LogsForMethodService`.
29
29
  - Add a common base for all generators.
30
30
 
31
+ # 1.5.0
32
+
33
+ - Released then
34
+
31
35
  # 1.4.0.1
32
36
 
33
37
  - Fix auto-update.
@@ -21,4 +21,4 @@ if [ "$1" = "./bin/rails" ] && [ "$2" = "server" ]; then
21
21
  fi
22
22
 
23
23
  # Execute the CMD
24
- exec "$@"
24
+ exec "${@}"
@@ -18,7 +18,7 @@ module Modulorails
18
18
  private
19
19
 
20
20
  def create_hook_executor
21
- template 'dockeruby.sh', 'bin/dockeruby'
21
+ template 'dockeruby.rb', 'bin/dockeruby'
22
22
  chmod 'bin/dockeruby', 0o755
23
23
  end
24
24
 
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/ruby
2
+ # frozen_string_literal: true
3
+
4
+ def require_or_install(gem_name, version=nil)
5
+ gem(gem_name, "~> #{version}") unless version.nil?
6
+ require gem_name
7
+ rescue LoadError
8
+ warn "Installing gem #{gem_name}"
9
+ if version.nil?
10
+ Gem.install(gem_name)
11
+ else
12
+ Gem.install(gem_name, "~> #{version}")
13
+ gem(gem_name, "~> #{version}")
14
+ end
15
+ require gem_name
16
+ end
17
+
18
+ require_or_install('shellwords')
19
+
20
+ def check_dockerfile(verbose: false)
21
+ return true if File.exist?('Dockerfile')
22
+
23
+ puts('No Dockerfile') if verbose
24
+ false
25
+ end
26
+
27
+ def entrypoint_location
28
+ entrypoint_line = File.readlines('Dockerfile').find { |line| line.start_with?('ENTRYPOINT') }
29
+
30
+ return nil if entrypoint_line.nil?
31
+
32
+ md = /\[["'](.+)["']\]/.match(entrypoint_line)
33
+ return nil if md.nil? || md[1].nil?
34
+
35
+ md[1]
36
+ end
37
+
38
+ VALID_LAST_INSTRUCTION = /exec "\$\{?@}?"/
39
+
40
+ def check_entrypoint(verbose: false)
41
+ el = entrypoint_location
42
+ return true if el.nil?
43
+
44
+ unless File.exist?(el)
45
+ warn("Entrypoint not found at location: #{el}") if verbose
46
+ return false
47
+ end
48
+
49
+ last_line = File.readlines(el).last&.strip
50
+ return true if VALID_LAST_INSTRUCTION.match?(last_line)
51
+
52
+ warn("Invalid entrypoint: Last instruction should be 'exec \"${@}\"' instead of '#{last_line}'") if verbose
53
+
54
+ false
55
+ end
56
+
57
+ def executer_docker_run(docker_args, verbose: false)
58
+ pwd = Dir.pwd
59
+ working_directory = File.basename(pwd)
60
+
61
+ volumes = `docker volume ls -q -f name=modulogem`
62
+ volumes = volumes.split("\n").map(&:strip)
63
+ modulogem_gems = volumes.find { |volume| volume.include?('modulogem_gems') }
64
+ modulogem = volumes.find { |volume| volume.include?('modulogem') }
65
+ modulogem_gems_option = modulogem_gems.nil? ? '' : "-v #{modulogem_gems}:/usr/local/bundle"
66
+ modulogem_option = modulogem.nil? ? '' : "-v #{modulogem}:/root"
67
+
68
+ # Check if the shell is a TTY
69
+ tty_option = $stdout.isatty ? '-ti' : ''
70
+
71
+ # Build the command string
72
+ # rubocop:disable Layout/LineLength
73
+ 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})
74
+ # rubocop:enable Layout/LineLength
75
+
76
+ puts(command) if verbose
77
+ exec(command)
78
+ end
79
+
80
+ def executer_compose_run(docker_args, verbose: false)
81
+ entrypoint_option = check_entrypoint(verbose: verbose) ? '' : '--entrypoint "sh -c"'
82
+ git_email = `git config --get user.email`.strip
83
+ git_name = `git config --get user.name`.strip
84
+
85
+ # Check if the shell is a TTY
86
+ tty_option = $stdout.isatty ? '-ti' : ''
87
+
88
+ # rubocop:disable Layout/LineLength
89
+ 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)
90
+ command = if entrypoint_option == ''
91
+ "#{command} #{docker_args}"
92
+ else
93
+ "#{command} '#{docker_args}'"
94
+ end
95
+ # rubocop:enable Layout/LineLength
96
+ puts(command) if verbose
97
+ exec(command)
98
+ end
99
+
100
+ def main(args, verbose: false)
101
+ # Escape each argument individually
102
+ escaped_args = args.map { |arg| Shellwords.escape(arg) }
103
+
104
+ # Check if the arguments contain a Ruby command or only options
105
+ contains_command = false
106
+ escaped_args.each_with_index do |arg, index|
107
+ if !arg.start_with?('-') && (index.zero? || !escaped_args[index - 1].start_with?('-'))
108
+ contains_command = true
109
+ break
110
+ end
111
+ end
112
+
113
+ docker_args = if contains_command
114
+ escaped_args.join(' ')
115
+ else
116
+ "ruby #{escaped_args.join(' ')}"
117
+ end
118
+
119
+ if check_dockerfile(verbose: verbose)
120
+ executer_compose_run(docker_args, verbose: verbose)
121
+ else
122
+ executer_docker_run(docker_args, verbose: verbose)
123
+ end
124
+ end
125
+
126
+ main(ARGV, verbose: true)
@@ -1,6 +1,6 @@
1
1
  module Modulorails
2
2
 
3
- VERSION = '1.5.0.pre3'.freeze
3
+ VERSION = '1.5.1'.freeze
4
4
 
5
5
  # Useful to compare the current Ruby version
6
6
  COMPARABLE_RUBY_VERSION = Gem::Version.new(RUBY_VERSION)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modulorails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0.pre3
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu Ciappara
@@ -183,7 +183,7 @@ files:
183
183
  - lib/generators/modulorails/docker/templates/entrypoints/docker-entrypoint.sh.tt
184
184
  - lib/generators/modulorails/docker/templates/entrypoints/webpack-entrypoint.sh.tt
185
185
  - lib/generators/modulorails/githooks/githooks_generator.rb
186
- - lib/generators/modulorails/githooks/templates/dockeruby.sh
186
+ - lib/generators/modulorails/githooks/templates/dockeruby.rb
187
187
  - lib/generators/modulorails/githooks/templates/post-rewrite.sh
188
188
  - lib/generators/modulorails/githooks/templates/pre-merge-commit.sh
189
189
  - lib/generators/modulorails/githooks/templates/refresh_generations.sh
@@ -1,113 +0,0 @@
1
- #!/bin/sh
2
- set -e
3
-
4
- # shellcheck disable=SC2016
5
- VALID_LAST_INSTRUCTION='exec "${@}"'
6
- VALID_LAST_INSTRUCTION2='exec "$@"'
7
-
8
- # Check if the Dockerfile exists
9
- check_dockerfile() {
10
- if [ -f "Dockerfile" ]; then
11
- return 0
12
- else
13
- echo "No Dockerfile"
14
- return 1
15
- fi
16
- }
17
-
18
- # Get the entrypoint location from the Dockerfile
19
- entrypoint_location() {
20
- entrypoint_line=$(grep '^ENTRYPOINT' Dockerfile || true)
21
-
22
- if [ -z "$entrypoint_line" ]; then
23
- echo ""
24
- else
25
- echo "$entrypoint_line" | sed -n 's/.*\[\(.*\)\].*/\1/p' | tr -d '"'
26
- fi
27
- }
28
-
29
- # Check if the entrypoint is valid
30
- check_entrypoint() {
31
- el=$(entrypoint_location)
32
- if [ -z "$el" ]; then
33
- return 0
34
- fi
35
-
36
- if [ ! -f "$el" ]; then
37
- echo "Entrypoint not found at location: $el"
38
- return 1
39
- fi
40
-
41
- last_line=$(tail -n 1 "$el" | xargs)
42
-
43
- if [ "$last_line" != "$VALID_LAST_INSTRUCTION" ] && [ "$last_line" != "$VALID_LAST_INSTRUCTION2" ]; then
44
- echo "Invalid entrypoint: Last instruction should be '$VALID_LAST_INSTRUCTION' instead of '$last_line'"
45
- return 1
46
- fi
47
-
48
- return 0
49
- }
50
-
51
- # Run docker with the necessary options
52
- executer_docker_run() {
53
- pwd=$(pwd)
54
- working_directory=$(basename "$pwd")
55
-
56
- tty_option=""
57
- if [ -t 1 ]; then
58
- tty_option="-ti"
59
- fi
60
-
61
- command="docker run --rm -v '$pwd:/app/$working_directory' -w '/app/$working_directory' $tty_option $git_environment ezveus/ruby:latest $docker_args"
62
- echo "$command"
63
- exec "$command"
64
- }
65
-
66
- executer_dockerfile_run() {
67
- if check_entrypoint; then
68
- entrypoint_option=""
69
- else
70
- entrypoint_option="--entrypoint \"sh -c\""
71
- fi
72
-
73
- command="docker compose build && docker compose run --rm $tty_option $git_environment $entrypoint_option app $docker_args"
74
- echo "$command"
75
- exec "$command"
76
- }
77
-
78
- # Main function
79
- main() {
80
- args="$*"
81
- docker_args=""
82
- contains_command=false
83
- git_name=$(git config --get user.name || whoami)
84
- git_email=$(git config --get user.email || echo "$git_name@local")
85
- git_environment="-e \"GIT_AUTHOR_EMAIL=$git_email\" -e \"GIT_AUTHOR_NAME=$git_name\" -e \"GIT_COMMITTER_EMAIL=$git_email\" -e \"GIT_COMMITTER_NAME=$git_name\""
86
-
87
- tty_option=""
88
- if [ -t 1 ]; then
89
- tty_option="-ti"
90
- fi
91
-
92
- for arg in "$@"; do
93
- # Check if any argument is not an option (doesn't start with '-')
94
- if [ "${arg#-}" = "$arg" ]; then
95
- contains_command=true
96
- break
97
- fi
98
- done
99
-
100
- if [ "$contains_command" = false ]; then
101
- docker_args="ruby $args"
102
- else
103
- docker_args="$args"
104
- fi
105
-
106
- if check_dockerfile; then
107
- executer_dockerfile_run
108
- else
109
- executer_docker_run "$docker_args"
110
- fi
111
- }
112
-
113
- main "$@"