-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincme.rb
138 lines (113 loc) · 3.44 KB
/
incme.rb
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
require 'find'
require 'fileutils.rb'
$options = {}
ARGV.each do |arg|
res = (/--(.+)=(.+)/ =~ arg)
val = $2
$options[$1.gsub("-", "_").to_sym] = val if res and $1 and val
end
# UTILS
# PUSHES ON THE STANDARD OUTPUT OR TO A LOG FILE
def log(str, log_type = :info)
str = "- ERROR - #{str}" if log_type == :error
puts str
end
# USAGE OF THE SCRIPT
def usage
puts "USAGE: ruby site-maker.rb --source-dir=<path-to-source-dir> --dest-dir=<path-to-dest-dir>"
end
def remove_trailing_slash(str)
str.gsub(/(\/|\\)$/, "")
end
if (not $options[:source_dir] or (not $options[:dest_dir]))
usage
exit
end
# CONTANTS
SOURCE_DIR = remove_trailing_slash($options[:source_dir])
DEST_DIR = remove_trailing_slash($options[:dest_dir])
EXCLUDE_EXTS = ["js", "gif", "jpeg", "jpg", "png"]
INCLUDES_DIR = SOURCE_DIR + "/" + "_includes_"
NOT_ALLOWED_DIRS = ["/", "/home", "/home/Users", "c:\\", "d:\\"] #"
if NOT_ALLOWED_DIRS.include?(SOURCE_DIR.strip.downcase)
puts "The --source-dir cannot be amoung #{NOT_ALLOWED_DIRS.inspect}"
puts "Exiting..."
exit
end
if NOT_ALLOWED_DIRS.include?(DEST_DIR.strip.downcase)
puts "The --dest-dir cannot be amoung #{NOT_ALLOWED_DIRS.inspect}"
puts "Exiting..."
exit
end
if SOURCE_DIR == DEST_DIR
puts "The --source-dir cannot be the same as the --dest-dir"
puts "Exiting..."
end
# THE INCME CLASS
class Incme
# RETURNS THE LIST OF FILES TO BE OPERATED ON
def operate_on_file
files = []
Find.find(SOURCE_DIR){|path| files << path}
files.reject{|path| (/#{INCLUDES_DIR}/ =~ path) or (path == SOURCE_DIR)} # Removes all the _includes_ files from the list
end
def copy_file(from, to)
log "Copying #{from} to destination"
File.copy(from, to)
end
def clean_destination_dir
# DELETING THE DESTINATION DIRECTORY GENERATED FILES
log "Deleting the destination folder..."
FileUtils.rm_rf(DEST_DIR)
# CREATING AN EMPTY DESTINATION DIRECTORY
log "Creating destination folder..."
Dir.mkdir DEST_DIR
end
# THE MAKER "INC" COMMAND
def inc(inc_name)
IO.read(INCLUDES_DIR + "/" + inc_name)
rescue Exception => e
log "#{e.message}", :error
log "Skipping include #{inc_name}", :error
""
end
# INTERNAL FUNCTION: NOT TO BE CALLED FROM OUTSITE
# PERFORMS THE TEXT SUBSTITUTION ON THE PASSED TEXT
def _perform(text)
lines = text.split("\n")
lines.map! {|line|
line.gsub(/\[\[(.)+\]\]/i){|found|
/\[\[([a-z_]*): ([a-z_]*)\]\]/i =~ found
_perform(self.send($1.to_sym, $2))
}
}
lines.join("\n")
end
# ISSUES THE TEXT SUBSTITUTION COMMAND ON THE FILE
def perform(from, to)
log "Performing substitution in #{from}"
text = _perform(IO.read(from))
open(to, 'w'){|f| f << text}
end
# RETURNS TRUE IF THIS FILE SHOULD BE EXCLUDED FORM THE "PERFORM" COMMAND.
# THIS FILE IS DIRECTLY BE COPIED OVER TO THE DESTINATION FOLDER
def exclude_file?(path)
EXCLUDE_EXTS.include?(File.extname(path).sub(".", "").downcase)
end
def run(files)
files.each do |path|
gen_path = path.sub(/#{SOURCE_DIR}/, "#{DEST_DIR}")
if FileTest.directory?(path)
log "Creating directory '#{gen_path}'"
Dir.mkdir(gen_path)
else
exclude_file?(path) ? copy_file(path, gen_path) : perform(path, gen_path)
end
end
end
end
incme = Incme.new
files = incme.operate_on_file
incme.clean_destination_dir
incme.run files
log "\nSUCCESS: Your files have been generated at '#{DEST_DIR}'\n"