-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency_resolver.rb
54 lines (41 loc) · 1.02 KB
/
dependency_resolver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'json'
require_relative 'errors.rb'
class DependencyResolver
attr_reader :json_data
attr_accessor :bash_file
def initialize(file_path)
@json_data = JSON.parse(File.read(file_path))
@bash_file = File.open('tasks.sh', 'w')
@bash_file.puts "#!/usr/bin/env ruby\n\n"
end
def add(task)
return if finished_tasks.include?(task['name'])
raise CircularDependencyError if visited_tasks.include? task['name']
visited_tasks << task['name']
task['requires']&.each { |name| add(find_task_by_name(name)) }
finished_tasks << task['name']
bash_file.puts task['command']
end
def sort
tasks.each(&method(:add))
end
private
def tasks
json_data['tasks']
end
def finished_tasks
self.class.finished_tasks
end
def visited_tasks
self.class.visited_tasks
end
def self.finished_tasks
@finished_tasks ||= []
end
def self.visited_tasks
@visited_tasks ||= []
end
def find_task_by_name(name)
tasks.select {|h| h.values.include?(name) }.first
end
end