Module: DevOops::ScriptsLoader
- Defined in:
- lib/dev_oops/scripts_loader.rb
Defined Under Namespace
Classes: ScriptConfig
Constant Summary collapse
- FORBIDDEN_NAMES =
%w[help install edit edit_sh rm local_install].freeze
- GLOBAL_DIR =
"#{Dir.home}/.dev_oops"
Class Method Summary collapse
- .build_action(config) ⇒ Object
- .find_dev_oops_dirs ⇒ Object
- .load ⇒ Object
- .script_dir(script_name) ⇒ Object
Class Method Details
.build_action(config) ⇒ Object
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 |
# File 'lib/dev_oops/scripts_loader.rb', line 62 def self.build_action(config) Class.new(Thor::Group) do (config.args || []).each do |arg| class_option( arg['name'], desc: arg['desc'] || '', aliases: arg['aliases'] || [], required: arg['required'] || false, default: arg['default'] ) end define_singleton_method('banner') { config.usage } define_method('perform') do env_vars = .map { |k, v| "#{k}=#{v}" }.join(' ') if config.script_location && !config.script_location.empty? location = if config.script_location.start_with?('/') config.script_location else "#{ENV['HOME']}/#{config.script_location}" end system("#{env_vars} #{ENV['SHELL']} -c #{location}") end end end end |
.find_dev_oops_dirs ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dev_oops/scripts_loader.rb', line 20 def self.find_dev_oops_dirs [ GLOBAL_DIR, *Dir.pwd.gsub("#{Dir.home}/", '').split('/').reduce( [] ) { |res, new_dir| [*res, "#{res.last}/#{new_dir}"] }.map do |dir| "#{Dir.home}#{dir}/dev_oops" end.select { |dir| Dir.exist?(dir) } ] end |
.load ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dev_oops/scripts_loader.rb', line 31 def self.load find_dev_oops_dirs.flat_map do |dir| Dir.glob("#{dir}/*.json") end.map do |filename| script_location = filename.gsub(/\.json$/, '.sh') script_location = '' unless File.exist?(script_location) json_config = nil File.open(filename) { |file| json_config = JSON.parse(file.read) } script_name = File.basename(filename, '.json') ScriptConfig.create( script_name, script_location, json_config, File.dirname(filename) ) end.reverse.reduce([]) do |res, script_config| if res.any? { |script_c| script_c.name == script_config.name } res else [*res, script_config] end end end |
.script_dir(script_name) ⇒ Object
56 57 58 59 60 |
# File 'lib/dev_oops/scripts_loader.rb', line 56 def self.script_dir(script_name) scripts = self.load scripts.find { |script| script.name == script_name }&.dir || Dir.exist?("#{Dir.pwd}/dev_oops") && "#{Dir.pwd}/dev_oops" || GLOBAL_DIR end |