forked from floger/Guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
115 lines (97 loc) · 2.68 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
require File.dirname(__FILE__) + "/github-emojis"
##
# this repository's path
DOCGUIDES_REPO_PATH = "#{Dir.home}/docs/guides"
##
# where you clone rails/rails
RAILS_EDGE_REPO_PATH = "#{Dir.home}/dev/rails-edge/guides/source"
##
# task 'whatsoever'.to_sym do ; end
# This is necessary because rake will try to run every given args
# as a task, if you did not define it will raise an error.
##
##
# rake new README.md
desc "Add a new file to GitHub."
task :new do
file_name = ARGV.last
git_action(file_name, message: "Add #{file_name}", action: 'add')
task file_name.to_sym do ; end
end
##
# rake mods
desc "Add all modified files and push to GitHub"
task :mods do
git_action(nil, message: modified_msg, action: 'add -u')
end
##
# rake all
desc "Add all files and push to GitHub (including untracked)"
task :all do
git_action(nil, message: 'Update the whole repository', action: 'add -A')
end
##
# rake update README.md
desc "Update a existing file to GitHub."
task :update do
file_name = ARGV.last
git_action(file_name)
task file_name.to_sym do ; end
end
##
# rake msg "Update Guides." README.md
desc "Write a custom commit message then push to GitHub."
task :msg do
message = ARGV[-2]
file_name = ARGV.last
git_action(file_name, message: message)
task message.to_sym do ; end
task file_name.to_sym do ; end
end
desc "Update edge guides from Rails repo."
task :update_guide do
system "cd #{RAILS_EDGE_REPO_PATH}"
puts "Pulling Latest Changes from rails/rails..."
system "git pull"
system "cp #{RAILS_EDGE_REPO_PATH}/*.md #{DOCGUIDES_REPO_PATH}/guides/edge/"
puts "All Guides Updated Successfully."
end
def git_action(file, **opts)
message = if opts[:message].nil?
"Update #{file}"
else
opts[:message]
end
message = if str_larger_than(modified_files_str, 38)
"#{GITHUB_EMOJIS.sample} #{message} @ #{what_time_is_it}"
else
"#{message} @ #{what_time_is_it} #{GITHUB_EMOJIS.sample}"
end
if opts[:action].nil?
system "git add #{file}"
else
system "git #{opts[:action]} #{file}"
end
system "git commit -m \"#{message}\""
system "git push origin master"
puts "\nDeploy to GitHub complete :)"
end
def what_time_is_it
Time.now.to_s.gsub('+0800', '(Taipei Time)')
end
def str_larger_than(str, size)
str.length > size
end
def modified_files_str
`git ls-files -m`.chomp.gsub("\n", ", ")
end
##
# this will automatically create a list of modified file names
# if the string's length > 80, switch to a simplified message.
def modified_msg
if str_larger_than(modified_files_str, 80)
"Update Many Guides"
else
"Update ".concat(modified_files_str)
end
end