From 00af4d0ec581789b14bf88eadc714f5c14ffc4ba Mon Sep 17 00:00:00 2001 From: meatball Date: Fri, 15 Nov 2024 19:42:40 +0100 Subject: [PATCH] Split utils methods into its own module --- generatorv2/lib/generator.rb | 52 +++--------------------------------- generatorv2/lib/utils.rb | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 49 deletions(-) create mode 100644 generatorv2/lib/utils.rb diff --git a/generatorv2/lib/generator.rb b/generatorv2/lib/generator.rb index 47e99cd2fb..ead487ebec 100644 --- a/generatorv2/lib/generator.rb +++ b/generatorv2/lib/generator.rb @@ -4,8 +4,11 @@ require 'json' require 'erb' require 'rubocop' +require_relative 'utils' class Generator + include Utils + def initialize(exercise = nil) @first = true @exercise = exercise @@ -43,53 +46,4 @@ def status end "skip" end - - def toml(path = "./exercises/practice/#{@exercise}/.meta/tests.toml") - raise "Toml not found: #{path}" unless File.exist?(path) - - uuid = TomlRB.load_file(path) - uuid = uuid.filter do |_k, v| - v.none? { |k, inner_value| k == "include" && !inner_value } - end - uuid.keys - end - - def remote_files - url = URI.parse("https://raw.githubusercontent.com/exercism/problem-specifications/main/exercises/#{@exercise}/canonical-data.json") - response = Net::HTTP.get_response(url) - case response - when Net::HTTPSuccess - JSON.parse(response.body) - when Net::HTTPNotFound - check_for_local_canonical_data - else - raise "Error while requesting the #{@exercise} data file from GitHub... " \ - "Status was #{response.code}" - end - end - - def check_for_local_canonical_data(path = "./exercises/practice/#{@exercise}/canonical-data.json") - raise "No canonical-data.json found in #{@exercise} directory" unless File.exist?(path) - - JSON.parse(File.read(path)) - end - - def additional_json(json) - file_path = "./exercises/practice/#{@exercise}/.meta/additional_tests.json" - return unless File.exist?(file_path) - - JSON.parse(File.read(file_path))["cases"].each do |test| - json["cases"] << test - end - end - - def remove_tests(uuid, json) - json["cases"].each_with_object([]) do |x, acc| - if x["cases"] - acc << remove_tests(uuid, json) - elsif uuid.include?(x["uuid"]) - acc << x - end - end - end end diff --git a/generatorv2/lib/utils.rb b/generatorv2/lib/utils.rb new file mode 100644 index 0000000000..d30d11eaf6 --- /dev/null +++ b/generatorv2/lib/utils.rb @@ -0,0 +1,50 @@ +module Utils + def toml(path = "./exercises/practice/#{@exercise}/.meta/tests.toml") + raise "Toml not found: #{path}" unless File.exist?(path) + + uuid = TomlRB.load_file(path) + uuid = uuid.filter do |_k, v| + v.none? { |k, inner_value| k == "include" && !inner_value } + end + uuid.keys + end + + def remote_files + url = URI.parse("https://raw.githubusercontent.com/exercism/problem-specifications/main/exercises/#{@exercise}/canonical-data.json") + response = Net::HTTP.get_response(url) + case response + when Net::HTTPSuccess + JSON.parse(response.body) + when Net::HTTPNotFound + check_for_local_canonical_data + else + raise "Error while requesting the #{@exercise} data file from GitHub... " \ + "Status was #{response.code}" + end + end + + def check_for_local_canonical_data(path = "./exercises/practice/#{@exercise}/canonical-data.json") + raise "No canonical-data.json found in #{@exercise} directory" unless File.exist?(path) + + JSON.parse(File.read(path)) + end + + def additional_json(json) + file_path = "./exercises/practice/#{@exercise}/.meta/additional_tests.json" + return unless File.exist?(file_path) + + JSON.parse(File.read(file_path))["cases"].each do |test| + json["cases"] << test + end + end + + def remove_tests(uuid, json) + json["cases"].each_with_object([]) do |x, acc| + if x["cases"] + acc << remove_tests(uuid, json) + elsif uuid.include?(x["uuid"]) + acc << x + end + end + end +end