-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
173 lines (153 loc) · 5.17 KB
/
Rakefile
File metadata and controls
173 lines (153 loc) · 5.17 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# frozen_string_literal: true
require "rubygems"
require "html-proofer"
require "rake"
require 'yaml'
require 'time'
SOURCE = "."
COLLECTIONS = "collections"
ASSETS = "assets"
DIRS = {
'layouts' => File.join(SOURCE, "_layouts"),
'scripts' => File.join(SOURCE, "scripts"),
'pages' => File.join(SOURCE, "pages"),
'site' => File.join(SOURCE, "_site"),
'drafts' => File.join(SOURCE, COLLECTIONS, "_drafts"),
'posts' => File.join(SOURCE, COLLECTIONS, "_posts"),
'posts_asset' => File.join(SOURCE, ASSETS, "posts"),
'post_ext' => "md",
'page_ext' => "md"
}
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
def true?(obj)
obj.to_s.downcase == "true"
end
IS_COMMENT_ENABLE = true
IS_DISPLAY_ENABLE = true
# Usage: rake post [title="${title}"] [date="YYYY-MM-DD"] [tags=[${tag1},${tag2}...]] [category="${category}"]
desc "Begin a new post in #{DIRS['posts']}"
task :post do
abort("rake aborted: '#{DIRS['posts']}' directory not found.") unless FileTest.directory?(DIRS['posts'])
title = ENV["title"] || "new-post"
tags = ENV["tags"] || "[]"
category = ENV["category"] || ""
category = "\"#{category.gsub(/-/,' ')}\"" if !category.empty?
# slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
slug = "post"
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(DIRS['posts'], "#{date}-#{slug}.#{DIRS['post_ext']}")
dirname_asset = File.join(DIRS['posts_asset'], "#{date}")
if File.exist?(filename)
abort("rake aborted!") unless ask("#{filename} already exists. Do you want to overwrite?", %w(y n)) == 'y'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
# post.puts "layout: post"
post.puts "title: \"#{title.strip}\""
post.puts "tags: #{tags}"
post.puts "category: \"#{category}\""
# post.puts "display: #{IS_DISPLAY_ENABLE.to_s}"
post.puts "comment: #{IS_COMMENT_ENABLE.to_s}"
post.puts "---"
end
mkdir_p dirname_asset
end # task :post
IS_TITLE_DISPLAY = true
# Usage: rake page title="${pagename}"
desc "Create a new page folder with index page"
task :page do
abort("rake aborted: the page title must be given.") unless ENV["title"] && !ENV["title"].empty?
title = ENV["title"].strip
dirname = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
filename = File.join(DIRS['pages'], [dirname, "index.#{DIRS['page_ext']}"])
# title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
if File.exist?(filename)
abort("rake aborted!") unless ask("#{filename} already exists. Do you want to overwrite?", %w(y n)) == 'y'
end
mkdir_p File.dirname(filename)
puts "Creating new page folder: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: page"
post.puts "title: \"#{title}\""
post.puts "title_image: \"\""
post.puts "title_display: #{IS_TITLE_DISPLAY.to_s}"
post.puts "footer_quote: \"\\\"quote\\\"---person\""
post.puts "permalink: \"/#{dirname}/\""
post.puts "---"
end
end # task :page
namespace :check do
desc "Outputs any deprecation or configuration issues"
task :doctor do
system 'bundle exec jekyll doctor'
end
desc "Perform Ruby programming syntax check"
task :syntax do
system 'bundle exec rubocop -D -S'
end
end
namespace :test do
desc "Test the HTML output elements"
task :html do
begin
system 'bundle exec jekyll build'
HTMLProofer.check_directory(DIRS['site'], check_html: true).run
rescue Exception => ex
puts "#{ex.message}"
end
end
desc "Test urls validity in data files"
# Usage: rake urltest [format=[bool]]
task :url do
format = true?(ENV["format"] || false)
args = []
args << "-f" if format
system ["./verify-data-urls.rb", args].join(' '), chdir: DIRS['scripts']
end
end
desc "Reset bundle install(system-wide)"
task :reset do
abort("rake aborted!") unless ask("Sure to reset bundle install(system-wide)?", %w(y n)) == 'y'
system 'sudo bundle clean --force'
system 'sudo bundle install'
end
desc "Lint the files format"
task :lint do
system "./lint-src.sh", chdir: DIRS['scripts']
end
desc "Launch preview environment"
task :preview do
system "bundle exec jekyll serve -w -l --port 4000"
end
desc "Reload the site info"
task :update do
system "./get-pages-latest-date.rb", chdir: DIRS['scripts']
end
desc "Push the current code to the master branch"
task :cmpush => :update do
abort("rake aborted!") unless ask("Sure to commit all the change to remote?", %w(y n)) == 'y'
system "./git-commit-push.sh", chdir: DIRS['scripts']
end
desc "Push the current code to the master branch"
task :verpush => :update do
abort("rake aborted!") unless ask("Sure to push the new version to remote?", %w(y n)) == 'y'
system "./git-version-push.sh", chdir: DIRS['scripts']
end