Class: Dev::Template::Docker::Ruby::Application

Inherits:
ApplicationInterface show all
Defined in:
lib/firespring_dev_commands/templates/docker/ruby/application.rb

Overview

Class for default rake tasks associated with a ruby project

Instance Attribute Summary collapse

Attributes inherited from ApplicationInterface

#name

Attributes inherited from BaseInterface

#exclude

Instance Method Summary collapse

Methods inherited from BaseInterface

#create_tasks!

Constructor Details

#initialize(application, container_path: nil, local_path: nil, start_container_dependencies_on_test: false, test_isolation: false, coverage: nil, lint_artifacts: nil, test_artifacts: nil, exclude: []) ⇒ Application

Create the templated rake tasks for the ruby application

Parameters:

  • application (String)

    The name of the application

  • container_path (String) (defaults to: nil)

    The path to the application inside of the container

  • local_path (String) (defaults to: nil)

    The path to the application on your local system

  • start_container_dependencies_on_test (Boolean) (defaults to: false)

    Whether or not to start up container dependencies when running tests

  • test_isolation (Boolean) (defaults to: false)

    Whether or not to start tests in an isolated project and clean up after tests are run

  • coverage (Dev::Coverage::Base) (defaults to: nil)

    The coverage class which is an instance of Base to be used to evaluate coverage

  • lint_artifacts (Dev::Docker::Artifact) (defaults to: nil)

    An array of lint artifacts to copy back from the container

  • test_artifacts (Dev::Docker::Artifact) (defaults to: nil)

    An array of test artifacts to copy back from the container

  • exclude (Array<Symbol>) (defaults to: [])

    An array of default template tasks to exclude



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 23

def initialize(
  application,
  container_path: nil,
  local_path: nil,
  start_container_dependencies_on_test: false,
  test_isolation: false,
  coverage: nil,
  lint_artifacts: nil,
  test_artifacts: nil,
  exclude: []
)
  @ruby = Dev::Ruby.new(container_path:, local_path:, coverage:)
  @start_container_dependencies_on_test = start_container_dependencies_on_test
  @test_isolation = test_isolation
  @lint_artifacts = lint_artifacts
  @test_artifacts = test_artifacts
  raise 'lint artifact must be instance of Dev::Docker::Artifact' if lint_artifacts&.any? { |it| !it.is_a?(Dev::Docker::Artifact) }
  raise 'test artifact must be instance of Dev::Docker::Artifact' if test_artifacts&.any? { |it| !it.is_a?(Dev::Docker::Artifact) }

  super(application, exclude:)
end

Instance Attribute Details

#rubyObject (readonly)

Returns the value of attribute ruby.



10
11
12
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 10

def ruby
  @ruby
end

#start_container_dependencies_on_testObject (readonly)

Returns the value of attribute start_container_dependencies_on_test.



10
11
12
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 10

def start_container_dependencies_on_test
  @start_container_dependencies_on_test
end

#test_isolationObject (readonly)

Returns the value of attribute test_isolation.



10
11
12
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 10

def test_isolation
  @test_isolation
end

Instance Method Details

#create_audit_task!Object

Create the rake task which runs the security audits for the application packages



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 202

def create_audit_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  application = @name
  ruby = @ruby
  exclude = @exclude
  return if exclude.include?(:audit)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace application do
      desc 'Run all security audits'
      task audit: [:'ruby:audit'] do
        # This is just a placeholder to execute the dependencies
      end

      namespace :ruby do
        desc 'Run Bundle Audit on the target application' \
             "\n\tuse MIN_SEVERITY=(info low moderate high critical) to fetch only severity type selected and above (default=high)." \
             "\n\tuse IGNORELIST=(comma delimited list of ids) removes the entry from the list." \
             "\n\t(optional) use OPTS=... to pass additional options to the command"
        task audit: %w(init_docker up_no_deps) do
          # Retrieve results of the scan.
          options = []
          options << '-T' if Dev::Common.new.running_codebuild?
          environment = ['OPTS']
          data = Dev::Docker::Compose.new(services: application, options:, environment:, capture: true).exec(*ruby.audit_command)
          Dev::Ruby::Audit.new(data).to_report.check
        end

        # namespace :audit do
        #   desc 'Run NPM Audit fix command'
        #   task fix: %w(init_docker up_no_deps) do
        #     raise 'not implemented'
        #     # environment = ['OPTS']
        #     # Dev::Docker::Compose.new(services: application, environment:).exec(*ruby.audit_fix_command)
        #   end
        # end
      end
    end
  end
end

#create_eol_task!Object

Create the rake task for the ruby eol method



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 244

def create_eol_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  exclude = @exclude
  ruby = @ruby

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    return if exclude.include?(:eol)

    task eol: [:'eol:ruby'] do
      # This is just a placeholder to execute the dependencies
    end

    namespace :eol do
      desc 'Compares the current date to the EOL date for supported packages in the ruby package file'
      task ruby: %w(init) do
        eol = Dev::EndOfLife::Ruby.new(ruby)
        ruby_products = eol.default_products

        puts
        puts "Ruby product versions (in #{eol.lockfile})".light_yellow
        Dev::EndOfLife.new(product_versions: ruby_products).status
      end
    end
  end
end

#create_install_task!Object

Create the rake task which runs the install command for the application packages



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 181

def create_install_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  application = @name
  ruby = @ruby
  exclude = @exclude
  return if exclude.include?(:install)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace application do
      namespace :ruby do
        desc 'Install all bundled gems'
        task install: %w(init_docker up_no_deps) do
          environment = ['OPTS']
          Dev::Docker::Compose.new(services: application, environment:).exec(*ruby.install_command)
        end
      end
    end
  end
end

#create_lint_task!Object

rubocop:disable Metrics/MethodLength Create the rake task which runs linting for the application name



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
96
97
98
99
100
101
102
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 47

def create_lint_task!
  application = @name
  ruby = @ruby
  exclude = @exclude
  lint_artifacts = @lint_artifacts
  return if exclude.include?(:lint)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace application do
      desc 'Run all linting software'
      task lint: %w(ruby:lint) do
        # This is just a placeholder to execute the dependencies
      end

      namespace :lint do
        desc 'Run all linting software and apply all available fixes'
        task fix: %w(ruby:lint:fix) do
          # This is just a placeholder to execute the dependencies
        end
      end

      namespace :ruby do
        desc "Run the ruby linting software against the #{application}'s codebase" \
             "\n\t(optional) use OPTS=... to pass additional options to the command"
        task lint: %w(init_docker up_no_deps) do
          LOG.debug("Check for ruby linting errors in the #{application} codebase")

          # Run the lint command
          options = []
          options << '-T' if Dev::Common.new.running_codebuild?
          environment = ['OPTS']
          Dev::Docker::Compose.new(services: application, options:, environment:).exec(*ruby.lint_command)
        ensure
          # Copy any defined artifacts back
          container = Dev::Docker::Compose.new.container_by_name(application)
          lint_artifacts&.each do |artifact|
            Dev::Docker.new.copy_from_container(container, artifact.container_path, artifact.local_path)
          end
        end

        namespace :lint do
          desc "Run the ruby linting software against the #{application}'s codebase and apply all available fixes"
          task fix: %w(init_docker up_no_deps) do
            LOG.debug("Check and fix all ruby linting errors in the #{application} codebase")

            # Run the lint fix command
            options = []
            options << '-T' if Dev::Common.new.running_codebuild?
            environment = ['OPTS']
            Dev::Docker::Compose.new(services: application, options:, environment:).exec(*ruby.lint_fix_command)
          end
        end
      end
    end
  end
end

#create_test_task!Object

rubocop:disable Metrics/MethodLength Create the rake task which runs all tests for the application name



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/firespring_dev_commands/templates/docker/ruby/application.rb', line 107

def create_test_task!
  application = @name
  ruby = @ruby
  exclude = @exclude
  test_isolation = @test_isolation
  up_prefix = @test_isolation ? :up_empty : :up # NOTE: This should maybe be it's own variable at some point?
  up_cmd = @start_container_dependencies_on_test ? :"#{up_prefix}" : :"#{up_prefix}_no_deps"
  test_artifacts = @test_artifacts
  return if exclude.include?(:test)

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace application do
      desc 'Run all tests'
      task test: [:'ruby:test'] do
        # This is just a placeholder to execute the dependencies
      end

      task test_init_docker: %w(init_docker) do
        Dev::Docker::Compose.configure do |c|
          c.project_name = SecureRandom.hex if test_isolation
        end
      end

      namespace :ruby do
        desc "Run all ruby tests against the #{application}'s codebase" \
             "\n\t(optional) use OPTS=... to pass additional options to the command"
        task test: %W(test_init_docker #{up_cmd}) do
          begin
            LOG.debug("Running all ruby tests in the #{application} codebase")

            options = []
            options << '-T' if Dev::Common.new.running_codebuild?
            environment = %w(OPTS TESTS)
            Dev::Docker::Compose.new(services: application, options:, environment:).exec(*ruby.test_command)
            ruby.check_test_coverage(application:)
          ensure
            # Copy any defined artifacts back
            container = Dev::Docker::Compose.new.container_by_name(application)
            test_artifacts&.each do |artifact|
              Dev::Docker.new.copy_from_container(container, artifact.container_path, artifact.local_path)
            end
          end
        ensure
          # Clean up resources if we are on an isolated project name
          if test_isolation
            # Need to call stop before down because other the "run" containers aren't stopped
            Dev::Docker.new.stop_project_containers(project_name: Dev::Docker::Compose.config.project_name)
            Dev::Docker::Compose.new.down
            Dev::Docker.new.prune_project_volumes(project_name: Dev::Docker::Compose.config.project_name)
          end
        end

        # If using test isolation then give users a way to 'sh' into the isolated container
        if test_isolation
          namespace :test do
            desc "Open a shell into a test #{application} container"
            task sh: %W(test_init_docker #{up_cmd} _pre_sh_hooks) do
              Dev::Docker::Compose.new(services: @default_service).sh
              Rake::Task[:_post_sh_hooks].execute
            ensure
              # Need to call stop before down because other the "run" containers aren't stopped
              Dev::Docker.new.stop_project_containers(project_name: Dev::Docker::Compose.config.project_name)
              Dev::Docker::Compose.new.down
              Dev::Docker.new.prune_project_volumes(project_name: Dev::Docker::Compose.config.project_name)
            end
          end
        end
      end
    end
  end
end