I first wrote Ruby code, I want a review. Checked rubocop ohm, he has no objections.
UPD: I wrote rubocop rakefile , and it was necessary rubocop Rakefile . Corrected everything, except for too long line. What to do with the string?
Task: write a rake command to create a documentation file template. The markup and content of the file are irrelevant
In the header of the file - the date of the document in the format YYYY Month DD . On the title is an anchor link with a date in the format YYYYMMDD . Next, the contents of the template file are copied.
What causes my doubts:
- Do I work correctly with reading and writing to files?
- In particular, instead of line-by-line rewriting of a file, wasn’t it worth it to somehow copy it entirely? Is there any functional for this?
- Perhaps, instead of templating strings, it would be better to template the entire file? In Python, I would apply Jinja in this case.
- Should I write some help? Or will everyone use
rake --tasks?
Rakefile code:
require 'date' desc 'Begin new release notes' task :rnotes, [:date] do |_t, args| if args.date date = args.date else puts 'Enter the date for the release notes in YYYYMMDD format: ' date = STDIN.gets.chomp end filename = "release-notes/#{date}.md" puts "Creating new release notes: #{filename}" d = Date.strptime(date, '%Y%m%d') longdate = d.strftime('%Y %B %d') open(filename, 'w') do |release_notes| release_notes.puts "## #{longdate} <a id=\"#{date}\" class=\"anchor\" href=\"#{date}\"></a>" release_notes.puts '' template = 'templates/rnotes.md' File.readlines(template).each do |line| release_notes.puts line end end end