Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module lib/ to LOAD_PATH #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions files/task_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'json'

class TaskHelper
@@params = nil # rubocop:disable Style/ClassVars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to just add this to .rubocop.yml. I trust codereview to catch misuse of class variables.


class Error < RuntimeError
attr_reader :kind, :details, :issue_code

Expand All @@ -23,6 +25,18 @@ def task(params = {})
raise TaskHelper::Error.new(msg, 'tasklib/not-implemented')
end

# Adds the <module>/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 = 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
end

# Accepts a Data object and returns a copy with all hash keys
# symbolized.
def self.walk_keys(data)
Expand All @@ -38,9 +52,16 @@ def self.walk_keys(data)
end
end

def self.run
def self.params
# rubocop:disable Style/ClassVars
return @@params if @@params
input = STDIN.read
params = walk_keys(JSON.parse(input))
@@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.
Expand Down