Skip to content

Instantly share code, notes, and snippets.

@san9dev
Last active November 20, 2023 08:49
Show Gist options
  • Save san9dev/b97943ea419b8c93a72c05ed8444520d to your computer and use it in GitHub Desktop.
Save san9dev/b97943ea419b8c93a72c05ed8444520d to your computer and use it in GitHub Desktop.
# 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