diff --git a/lib/linters/default.rb b/lib/linters/default.rb index 28a5fa68..54f4510f 100644 --- a/lib/linters/default.rb +++ b/lib/linters/default.rb @@ -1,5 +1,13 @@ module Linters module Default - + # + def self.assert_pids(data) + data.each_with_index do |d, i| + warn Rainbow("WARNING: project number #{i} is missing a pid") unless d.key? 'pid' + end + pids = data.map{ |d| d['pid'] } + dupes = pids.select { |p| pids.count(p) > 1 }.uniq! || [] + warn Rainbow("WARNING: The following project pids are not unique!\n#{dupes}") unless dupes.empty? + end end end diff --git a/lib/linters/projects.rb b/lib/linters/projects.rb index ae59d73f..e80b0f76 100644 --- a/lib/linters/projects.rb +++ b/lib/linters/projects.rb @@ -1,5 +1,24 @@ +require 'rainbow' +require 'vars' + module Linters module Projects - + # + def self.assert_people(projects) + people = YAML.load_file(Vars::People.yml_file).map { |h| h['pid'] } + projects.each do |project| + project.dig('pis').each do |pi| + warn Rainbow("WARNING: Published project '#{project['pid']}' references nonexisting person '#{pi}'.").orange unless people.include? pi + end + end + end + # + def self.assert_categories(projects) + categories = ['DH Seed Grant Recipient', 'Grad Fellowship Project', 'Other'] + projects.each do |project| + category = project.dig 'category' + warn Rainbow("WARNING: Published project '#{project['pid']}' references nonexisting category '#{category}'.").orange unless categories.include? category + end + end end end diff --git a/lib/tasks/fetch/courses.rake b/lib/tasks/fetch/courses.rake index 3039a034..0a73f699 100644 --- a/lib/tasks/fetch/courses.rake +++ b/lib/tasks/fetch/courses.rake @@ -17,5 +17,9 @@ namespace :fetch do puts "Parsing courses into #{Vars::Courses.yml_file}" Utils.write_to_file(data.to_yaml, Vars::Courses.yml_file) + + puts Rainbow("Done ✓").green + + Rake::Task["lint:courses"].invoke end end diff --git a/lib/tasks/fetch/people.rake b/lib/tasks/fetch/people.rake index 58bc7646..cf603690 100644 --- a/lib/tasks/fetch/people.rake +++ b/lib/tasks/fetch/people.rake @@ -20,5 +20,7 @@ namespace :fetch do Utils.write_to_file data.to_yaml, Vars::People.yml_file puts Rainbow("Done ✓").green + + Rake::Task["lint:people"].invoke end end diff --git a/lib/tasks/lint/courses.rake b/lib/tasks/lint/courses.rake new file mode 100644 index 00000000..01df2be8 --- /dev/null +++ b/lib/tasks/lint/courses.rake @@ -0,0 +1,13 @@ +require 'rainbow' +require 'linters/default' +require 'vars' +require 'yaml' + +namespace :lint do + desc 'lint the fetched courses yaml data' + task :courses do + courses = YAML.load_file Vars::Courses.yml_file + + Linters::Default.assert_pids courses + end +end diff --git a/lib/tasks/lint/people.rake b/lib/tasks/lint/people.rake new file mode 100644 index 00000000..7944cd3a --- /dev/null +++ b/lib/tasks/lint/people.rake @@ -0,0 +1,13 @@ +require 'rainbow' +require 'linters/default' +require 'vars' +require 'yaml' + +namespace :lint do + desc 'lint the fetched people yaml data' + task :people do + people = YAML.load_file Vars::People.yml_file + + Linters::Default.assert_pids people + end +end diff --git a/lib/tasks/lint/projects.rake b/lib/tasks/lint/projects.rake index 93a753d2..3dc60944 100644 --- a/lib/tasks/lint/projects.rake +++ b/lib/tasks/lint/projects.rake @@ -7,14 +7,10 @@ require 'yaml' namespace :lint do desc 'lint the fetched project yaml data' task :projects do - categories = %w(DH Seed Grant Recipient Grad Fellowship Project Other) - people = YAML.load_file(Vars::People.yml_file).map { |h| h['pid'] } - projects = YAML.load_file Vars::Projects.yml_file + projects = YAML.load_file Vars::Projects.yml_file - projects.each do |project| - project.dig('pis').each do |pi| - puts Rainbow("WARNING: Project '#{project['pid']}' references nonexisting person '#{pi}'.").magenta unless people.include? pi - end - end + Linters::Default.assert_pids projects + Linters::Projects.assert_categories projects + Linters::Projects.assert_people projects end end