11# frozen_string_literal: true
22
33require 'bundler/setup'
4+ require 'fileutils'
45
56require 'rspec/core/rake_task'
67require 'rubocop/rake_task'
78
9+ # RVM (and similar) gem binstubs use `ruby_executable_hooks`; ensure it is on PATH
10+ # when Rake spawns subshells via `sh`.
11+ def ensure_gem_exec_path!
12+ paths = ENV . fetch ( 'PATH' , '' ) . split ( File ::PATH_SEPARATOR )
13+ [ ENV [ 'GEM_HOME' ] , Gem . dir ] . compact . filter_map do |base |
14+ File . join ( base , 'bin' ) if base && !base . empty?
15+ end . each do |bin |
16+ paths . unshift ( bin ) unless paths . include? ( bin )
17+ end
18+ ENV [ 'PATH' ] = paths . join ( File ::PATH_SEPARATOR )
19+ end
20+
21+ ensure_gem_exec_path!
22+
823APP_HOST = ENV . fetch ( 'HOST' , '0.0.0.0' )
924APP_PORT = ENV . fetch ( 'PORT' , '8080' )
1025CONTAINERFILE = 'Containerfile'
1126IMAGE = 'asset-monitoring:latest'
12- RACKUP_CMD = "bundle exec rackup config.ru --host #{ APP_HOST } -p #{ APP_PORT } "
27+ DATA_DIR = File . expand_path ( ENV . fetch ( 'DATA_DIR' , 'data' ) , __dir__ )
28+ CONTAINER_DATA_DIR = '/data'
29+ PRICE_HISTORY_DB_PATH = File . join ( CONTAINER_DATA_DIR , 'asset_history.db' )
30+ RACKUP_CMD = [ 'bundle' , 'exec' , 'rackup' , 'config.ru' , '--host' , APP_HOST , '-p' , APP_PORT ]
1331
1432RSpec ::Core ::RakeTask . new ( :spec )
1533RuboCop ::RakeTask . new ( :rubocop )
3351namespace :security do
3452 desc 'Run Bundler audit'
3553 task :audit do
36- sh 'bundle exec bundle-audit check --update'
54+ sh 'bundle' , ' exec' , ' bundle-audit' , ' check' , ' --update'
3755 end
3856
3957 desc 'Run Brakeman'
4058 task :brakeman do
41- sh 'bundle exec brakeman --no-pager'
59+ sh 'bundle' , ' exec' , ' brakeman' , ' --no-pager'
4260 end
4361end
4462
@@ -53,34 +71,45 @@ task brakeman: 'security:brakeman'
5371
5472desc 'Install dependencies'
5573task :install do
56- sh 'bundle install'
74+ sh 'bundle' , ' install'
5775end
5876
5977desc 'Start the application server'
6078task :server do
61- sh RACKUP_CMD
79+ sh ( * RACKUP_CMD )
6280end
6381
6482desc 'Start the application server with auto-reload (development)'
6583task :dev do
66- sh " bundle exec rerun -- #{ RACKUP_CMD } "
84+ sh ' bundle' , ' exec' , ' rerun' , '--' , * RACKUP_CMD
6785end
6886
6987desc 'Start an interactive console'
7088task :console do
7189 ENV [ 'METRICS_SCHEDULER_DISABLED' ] = '1'
72- sh 'bundle exec pry -Ilib -r asset_monitoring'
90+ sh 'bundle' , ' exec' , ' pry' , ' -Ilib' , '-r' , ' asset_monitoring'
7391end
7492
7593namespace :podman do
7694 desc 'Build the container image'
7795 task :build do
78- sh " podman build -t #{ IMAGE } -f #{ CONTAINERFILE } ."
96+ sh ' podman' , ' build' , '-t' , IMAGE , '-f' , CONTAINERFILE , '.'
7997 end
8098
81- desc 'Run the container'
99+ desc 'Run the container (1-year price history, SQLite in ./data bind-mounted to /data) '
82100 task :run do
83- sh "podman run -p #{ APP_PORT } :#{ APP_PORT } #{ IMAGE } "
101+ retention_days = ENV . fetch ( 'PRICE_HISTORY_RETENTION_DAYS' , '365' )
102+ db_path = ENV . fetch ( 'PRICE_HISTORY_DB_PATH' , PRICE_HISTORY_DB_PATH )
103+ container_data_dir = File . dirname ( db_path )
104+
105+ FileUtils . mkdir_p ( DATA_DIR )
106+
107+ sh 'podman' , 'run' , '--rm' ,
108+ '-p' , "#{ APP_PORT } :#{ APP_PORT } " ,
109+ '-e' , "PRICE_HISTORY_RETENTION_DAYS=#{ retention_days } " ,
110+ '-e' , "PRICE_HISTORY_DB_PATH=#{ db_path } " ,
111+ '-v' , "#{ DATA_DIR } :#{ container_data_dir } :Z,U" ,
112+ IMAGE
84113 end
85114end
86115
0 commit comments