-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
134 lines (117 loc) · 3.48 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require "rubygems"
require 'rake'
require 'yaml'
require 'time'
SOURCE = "."
CONFIG = {
'version' => "0.3.0",
'posts' => File.join(SOURCE, "_posts"),
'bars' => File.join(SOURCE, "bars/_posts"),
'food' => File.join(SOURCE, "food/_posts"),
'post_ext' => "md",
'theme_package_version' => "0.1.0"
}
# Path configuration helper
module JB
class Path
SOURCE = "."
Paths = {
:food => "food/_posts",
:bars => "bars/_posts"
}
def self.base
SOURCE
end
# build a path relative to configured path settings.
def self.build(path, opts = {})
opts[:root] ||= SOURCE
path = "#{opts[:root]}/#{Paths[path.to_sym]}/#{opts[:node]}".split("/")
path.compact!
File.__send__ :join, path
end
end #Path
end #JB
# Usage: rake bar name="A name" [date="2012-02-09"] [lat="51.7..."] [lon="8.7..."]
desc "Add a new bar in #{CONFIG['bars']}"
task :bar do
abort("rake aborted: '#{CONFIG['bars']}' directory not found.") unless FileTest.directory?(CONFIG['bars'])
name = ENV["name"] || "new-bar"
if ENV["lat"].to_f.between?(51.6,51.8)
lat = ENV["lat"]
else
lat = ""
end
if ENV["lon"].to_f.between?(8.6,8.9)
lon = ENV["lon"]
else
lon = ""
end
slug = name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(CONFIG['bars'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new bar: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "name: \"#{name.gsub(/-/,' ')}\""
post.puts 'special: ""'
post.puts "lat: #{lat}"
post.puts "lon: #{lon}"
post.puts "---"
end
end # task :bar
# Usage: rake food name="A name" [date="2012-02-09"] [lat="51.7..."] [lon="8.7..."]
desc "Add a new fressbude in #{CONFIG['food']}"
task :food do
abort("rake aborted: '#{CONFIG['food']}' directory not found.") unless FileTest.directory?(CONFIG['food'])
name = ENV["name"] || "new-fressbude"
if ENV["lat"].to_f.between?(51.6,51.8)
lat = ENV["lat"]
else
lat = ""
end
if ENV["lon"].to_f.between?(8.6,8.9)
lon = ENV["lon"]
else
lon = ""
end
slug = name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(CONFIG['food'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new fressbude: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "name: \"#{name.gsub(/-/,' ')}\""
post.puts 'special: ""'
post.puts "lat: #{lat}"
post.puts "lon: #{lon}"
post.puts "---"
end
end # task :food
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def get_stdin(message)
print message
STDIN.gets.chomp
end