-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
54 lines (43 loc) · 1.46 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'fileutils'
task :publish do
# Read files in the _drafts/ directory (skips hidden files and sub directories)
drafts = Dir["./_drafts/*"].select {|f| !File.directory? f}
# Show a numbered list of the name of the files.
drafts.each_with_index do |file_name, index|
puts "#{index + 1}) " + File.basename(file_name)
end
# Ask for the draft number.
puts "Enter the number of the draft you want to publish"
draft_number = $stdin.gets.chomp
# Validate that the input is a number
if draft_number != draft_number.to_i().to_s() then
puts "Input is not a number"
abort
end
# Convert to required type
draft_number = draft_number.to_i()
draft_index = draft_number -1
# Validate that the draft number is not out of bounds.
if drafts.at(draft_index) == nil then
puts "Number does not match any listed draft"
abort
end
# Get a reference to the selected draft
draft = drafts[draft_index]
# Build the post name.
today = Time.now.strftime("%Y-%m-%d")
draft_basename = File.basename(draft)
post = "_posts/#{today}-#{draft_basename}"
# Create the post
FileUtils.cp(draft, post)
puts "Draft promoted to post #{post}"
# Delete the draft
puts "Delete draft (#{draft_basename})? y/n"
delete = $stdin.gets.chomp
if delete == 'y' then
File.delete(draft)
puts "Draft deleted. Bye."
else
puts "Bye."
end
end