rails_script_runner 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1176b0be7233020733bb362cad19a8f5c884a322e4d41861392da7023926f033
4
+ data.tar.gz: 6d00e3dfd345264be718a5d0f80e4e0a8fdd3eacce0bfa6e9f2d0e6ec8ab38d7
5
+ SHA512:
6
+ metadata.gz: 58dd560681ec3b20212d4568055d7e6349755863715313429c7bd557e690839d8d38d60e545d076c41dc4aed5ac0705ef17ae4acd20cd92e27fb3a41f6509b9c
7
+ data.tar.gz: 4ed60ab4d5e9abd0ad7a028572587499d9fa358a7774ef628f1675bc998026e85632a2363be073aaae14fcaf9b1a095de0b38debf359620af6b9a0ce51cf77db
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Hernan D'Amico
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # rails-script-runner
2
+
3
+ ## License
4
+
5
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
@@ -0,0 +1,28 @@
1
+ module RailsScriptRunner
2
+ class CLI
3
+ include Directory
4
+ include HistoryManager
5
+
6
+ def call
7
+ require_pending_files
8
+ run_pending_scripts
9
+ add_new_records
10
+ end
11
+
12
+ private
13
+
14
+ def require_pending_files
15
+ pending_files.each do |f|
16
+ require "#{main_directory}/#{f}"
17
+ end
18
+ end
19
+
20
+ def run_pending_scripts
21
+ ::RailsScriptRunner::Release.descendants.each(&:run)
22
+ end
23
+
24
+ def pending_files
25
+ @pending_files ||= scripts_filename - executed_scripts
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ module RailsScriptRunner
2
+ module Directory
3
+ private
4
+
5
+ def script_history_file_path
6
+ @script_history_file_path ||= dir_files.find { |f| f.end_with?("scripts_history.json") }
7
+ end
8
+
9
+ def scripts_filename
10
+ dir_files.map { |f| f.match(%r{[^/]+.rb$}).to_s }.reject(&:empty?)
11
+ end
12
+
13
+ def main_directory
14
+ @main_directory ||= File.expand_path("./lib/scripts")
15
+ end
16
+
17
+ def read_history_file
18
+ File.read(script_history_file_path)
19
+ end
20
+
21
+ def dir_files
22
+ Dir["#{main_directory}/*"]
23
+ end
24
+
25
+ def create_history_file
26
+ body = '{ "executed_scripts": [] }'
27
+ File.write("#{main_directory}/scripts_history.json", body)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ module RailsScriptRunner
2
+ module HistoryManager
3
+ def executed_scripts
4
+ history_file["executed_scripts"]
5
+ end
6
+
7
+ def pending_files
8
+ @pending_files ||= scripts_filename - executed_scripts
9
+ end
10
+
11
+ private
12
+
13
+ def add_new_records
14
+ executed_scripts.concat pending_files
15
+ write_history_file
16
+ end
17
+
18
+ def write_history_file
19
+ File.open(script_history_file_path, "w") do |f|
20
+ f.write JSON.pretty_generate(history_file)
21
+ end
22
+ end
23
+
24
+ def history_file
25
+ @history_file ||= find_or_create_history_file
26
+ end
27
+
28
+ def find_or_create_history_file
29
+ create_history_file unless history_file_exists?
30
+ JSON.parse(read_history_file)
31
+ end
32
+
33
+ def history_file_exists?
34
+ !script_history_file_path.nil?
35
+ end
36
+ end
37
+ end
38
+
39
+ require "json"
@@ -0,0 +1,13 @@
1
+ require "rails_script_runner"
2
+ require "rails"
3
+
4
+ module RailsScriptRunner
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :rails_script_runner
7
+
8
+ rake_tasks do
9
+ path = File.expand_path(__dir__)
10
+ load "#{path}/tasks/rails_script_runner/execute.rake"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module RailsScriptRunner
2
+ class Release
3
+ def self.run
4
+ new.run
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require "rails_script_runner"
2
+
3
+ namespace :rails_script_runner do
4
+ task execute: :environment do
5
+ RailsScriptRunner::CLI.new.call
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RailsScriptRunner
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,8 @@
1
+ module RailsScriptRunner
2
+ autoload :CLI, "rails_script_runner/cli"
3
+ autoload :VERSION, "rails_script_runner/version"
4
+ autoload :Directory, "rails_script_runner/directory"
5
+ autoload :HistoryManager, "rails_script_runner/history_manager"
6
+ autoload :Release, "rails_script_runner/release"
7
+ require "rails_script_runner/railtie" if defined?(Rails)
8
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_script_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hernan D'Amico
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - hernan.damico67@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/rails_script_runner.rb
23
+ - lib/rails_script_runner/cli.rb
24
+ - lib/rails_script_runner/directory.rb
25
+ - lib/rails_script_runner/history_manager.rb
26
+ - lib/rails_script_runner/railtie.rb
27
+ - lib/rails_script_runner/release.rb
28
+ - lib/rails_script_runner/tasks/rails_script_runner/execute.rake
29
+ - lib/rails_script_runner/version.rb
30
+ homepage: https://github.com/hdamico/rails-script-runner
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ bug_tracker_uri: https://github.com/hdamico/rails-script-runner/issues
35
+ changelog_uri: https://github.com/hdamico/rails-script-runner/releases
36
+ source_code_uri: https://github.com/hdamico/rails-script-runner
37
+ homepage_uri: https://github.com/hdamico/rails-script-runner
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.1.6
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: ''
57
+ test_files: []