From 7235b44477df43175119a3511df4af10691b13b4 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Fri, 25 Oct 2019 11:07:03 -0400 Subject: [PATCH 1/5] Automatically add all module/lib paths from _installdir --- files/task_helper.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/files/task_helper.rb b/files/task_helper.rb index a8769ce..f2004be 100644 --- a/files/task_helper.rb +++ b/files/task_helper.rb @@ -23,6 +23,15 @@ def task(params = {}) raise TaskHelper::Error.new(msg, 'tasklib/not-implemented') end + # Adds the /lib/ directory for all modules in the _installdir. + # This eases the pain for module authors when writing tasks that utilize + # code in the lib/ directory that `require` files also in that same lib/ directory. + def self.add_module_lib_paths(install_dir) + Dir.glob(File.join([install_dir, '*'])).each do |mod| + $LOAD_PATH << File.join([mod, 'lib']) + end + end + # Accepts a Data object and returns a copy with all hash keys # symbolized. def self.walk_keys(data) @@ -41,6 +50,7 @@ def self.walk_keys(data) def self.run input = STDIN.read params = walk_keys(JSON.parse(input)) + add_module_lib_paths(params[:_installdir]) if params[:_installdir] # This method accepts a hash of parameters to run the task, then executes # the task. Unhandled errors are caught and turned into an error result. From 6593e3b725897fd826a8f54d801a3f6d592eec88 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Fri, 25 Oct 2019 12:45:17 -0400 Subject: [PATCH 2/5] Added ability to use the task helper from a global context --- files/task_helper.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/files/task_helper.rb b/files/task_helper.rb index f2004be..4730739 100644 --- a/files/task_helper.rb +++ b/files/task_helper.rb @@ -26,7 +26,14 @@ def task(params = {}) # Adds the /lib/ directory for all modules in the _installdir. # This eases the pain for module authors when writing tasks that utilize # code in the lib/ directory that `require` files also in that same lib/ directory. - def self.add_module_lib_paths(install_dir) + def self.add_module_lib_paths(install_dir=ENV['PT__installdir']) + if install_dir.nil? + error = TaskHelper::Error.new('"PT__installdir" environment variable was not set. You should try setting "input": "both" in your task metadata JSON file.', + 'ArgumentError') + STDOUT.print({ _error: error.to_h }.to_json) + exit 1 + end + Dir.glob(File.join([install_dir, '*'])).each do |mod| $LOAD_PATH << File.join([mod, 'lib']) end From 45af3b7868c537163adbc864e52971aa3dc4b816 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Fri, 25 Oct 2019 13:02:32 -0400 Subject: [PATCH 3/5] Fixing rubocop warnings --- files/task_helper.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/files/task_helper.rb b/files/task_helper.rb index 4730739..c82af86 100644 --- a/files/task_helper.rb +++ b/files/task_helper.rb @@ -25,11 +25,13 @@ def task(params = {}) # Adds the /lib/ directory for all modules in the _installdir. # This eases the pain for module authors when writing tasks that utilize - # code in the lib/ directory that `require` files also in that same lib/ directory. - def self.add_module_lib_paths(install_dir=ENV['PT__installdir']) + # code in the lib/ directory that `require` files also in that same lib/ + # directory. + def self.add_module_lib_paths(install_dir = ENV['PT__installdir']) if install_dir.nil? - error = TaskHelper::Error.new('"PT__installdir" environment variable was not set. You should try setting "input": "both" in your task metadata JSON file.', - 'ArgumentError') + msg = '"PT__installdir" environment variable was not set. You should try'\ + 'setting "input": "both" in your task metadata JSON file.' + error = TaskHelper::Error.new(msg, 'ArgumentError') STDOUT.print({ _error: error.to_h }.to_json) exit 1 end From b73857411fd7ff7db70774ecca7fde43bdb1d98b Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Fri, 25 Oct 2019 13:43:18 -0400 Subject: [PATCH 4/5] Don't rely on environment variables, always parse parameters from STDIN --- files/task_helper.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/files/task_helper.rb b/files/task_helper.rb index c82af86..90105bb 100644 --- a/files/task_helper.rb +++ b/files/task_helper.rb @@ -1,6 +1,8 @@ require 'json' class TaskHelper + @@params = nil + class Error < RuntimeError attr_reader :kind, :details, :issue_code @@ -27,15 +29,9 @@ def task(params = {}) # This eases the pain for module authors when writing tasks that utilize # code in the lib/ directory that `require` files also in that same lib/ # directory. - def self.add_module_lib_paths(install_dir = ENV['PT__installdir']) - if install_dir.nil? - msg = '"PT__installdir" environment variable was not set. You should try'\ - 'setting "input": "both" in your task metadata JSON file.' - error = TaskHelper::Error.new(msg, 'ArgumentError') - STDOUT.print({ _error: error.to_h }.to_json) - exit 1 - end - + def self.add_module_lib_paths(install_dir = nil) + # load install_dir from params (STDIN) if it isn't passed in + install_dir ||= params[:_installdir] Dir.glob(File.join([install_dir, '*'])).each do |mod| $LOAD_PATH << File.join([mod, 'lib']) end @@ -56,11 +52,15 @@ def self.walk_keys(data) end end - def self.run + def self.params + return @@params if @@params input = STDIN.read - params = walk_keys(JSON.parse(input)) - add_module_lib_paths(params[:_installdir]) if params[:_installdir] - + @@params = walk_keys(JSON.parse(input)) + end + + def self.run + add_module_lib_paths(params[:_installdir]) + # This method accepts a hash of parameters to run the task, then executes # the task. Unhandled errors are caught and turned into an error result. # @param [Hash] params A hash of params for the task From f2682d7f1784a5df4d7af92e722abd26c9b3a8f1 Mon Sep 17 00:00:00 2001 From: Nick Maludy Date: Fri, 25 Oct 2019 13:54:31 -0400 Subject: [PATCH 5/5] Trying to fix rubocop errors about class vars --- files/task_helper.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/files/task_helper.rb b/files/task_helper.rb index 90105bb..072590c 100644 --- a/files/task_helper.rb +++ b/files/task_helper.rb @@ -1,8 +1,8 @@ require 'json' class TaskHelper - @@params = nil - + @@params = nil # rubocop:disable Style/ClassVars + class Error < RuntimeError attr_reader :kind, :details, :issue_code @@ -53,14 +53,16 @@ def self.walk_keys(data) end def self.params + # rubocop:disable Style/ClassVars return @@params if @@params input = STDIN.read @@params = walk_keys(JSON.parse(input)) + # rubocop:enable Style/ClassVars end - + def self.run add_module_lib_paths(params[:_installdir]) - + # This method accepts a hash of parameters to run the task, then executes # the task. Unhandled errors are caught and turned into an error result. # @param [Hash] params A hash of params for the task