Last active
November 20, 2023 08:49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run: | |
# rake file:convert:to_json[path/to/file.yml] | |
namespace :file do | |
namespace :convert do | |
desc "convert yml file to json" | |
task :to_json, [:file_path] do |task, args| | |
puts 'File path:' | |
file_path = args[:file_path] | |
puts file_path | |
puts '='*10 | |
puts 'Check file present...' | |
raise 'File not exist' unless File.file?(file_path) | |
puts 'Success' | |
file_name, ext = file_path.split('/').last.split('.') | |
puts 'Check extension...' | |
raise 'Only *.yml file supported' unless is_yml?(ext) | |
puts 'Success' | |
puts "Convert file to json..." | |
hash = YAML.load_file(file_path) | |
File.open("#{file_name}.json", 'w') { |f| f << JSON.pretty_generate(hash) } | |
puts 'Done!' | |
end | |
def is_yml?(ext) | |
['yml', 'yaml'].include? ext | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment