-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
178 lines (155 loc) · 4.6 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
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
174
175
176
177
178
require 'sqlite3'
require 'toml'
require 'dotenv'
require 'logger'
require 'fileutils'
Dotenv.load
namespace :dash do
TOML_PATH = './snippets'
DUMP_PATH = './snippets/dump'
SNIPPET = 'snippet'
module Fields
TITLE = 'title'
BODY = 'body'
SYNTAX = 'syntax'
TAG = 'tag'
end
module OutputRecordsIndex
TITLE = 0
BODY = 1
SYNTAX = 2
TAG = 3
end
def build_snippets
initialize_log
@logger.debug('start build snippets')
load_snippets
connect_sqlite
delete_tables
@snippets.each.with_index(0) do |snippet, _i|
records = read_snippets_by_title(snippet[Fields::TITLE])
record_count = get_record_count(records)
create_snippet(snippet)
end
@logger.debug('finish build snippets')
end
def dump_snippets
initialize_log
@logger.debug('start dump snippets')
connect_sqlite
snippets = load_db_snippets
@logger.debug("\tsnippet count = #{snippets.size}")
snippets.each do |snippet|
outer = {}
inner = {}
inner[:title] = snippet[OutputRecordsIndex::TITLE]
inner[:body] = snippet[OutputRecordsIndex::BODY]
inner[:syntax] = snippet[OutputRecordsIndex::SYNTAX]
inner[:tag] = snippet[OutputRecordsIndex::TAG]
outer[:snippet] = inner
FileUtils.mkdir_p(DUMP_PATH)
filename = normalize_filename(inner[:title])
File.open("#{DUMP_PATH}/#{filename}.toml", "w") {|e|e.puts TOML.dump(outer) }
@logger.debug("\t complete output #{DUMP_PATH}/#{filename}.toml ")
end
@logger.debug('finish dump snippets')
end
private
def initialize_log
@logger = Logger.new(STDOUT)
level = ENV['LOG_LEVEL']
@logger.level = Object.const_get("Logger::#{level}")
@logger.formatter =-> (severity, datetime, progname, message) {
"#{datetime.strftime('%Y/%m/%d %H:%M:%S')} - #{level} - #{message}\n"
}
end
def load_snippets
@snippets = Dir.glob("#{TOML_PATH}/**/*.toml").each_with_object([]) do |e, memo|
memo << TOML.load_file(e)[SNIPPET]
end
end
def delete_tables
@logger.debug("\tstart delete snippets")
@db = connect_sqlite
delete_table('snippets')
delete_table('tagsIndex')
delete_table('tags')
@logger.debug("\tfinish delete snippets")
end
def delete_table(table_name)
@db.execute("delete from #{table_name}")
@logger.debug("\t\tsuccess delete #{table_name}")
end
def connect_sqlite
@db = SQLite3::Database.new(ENV['DASH_SNIPPET_PATH'])
end
def read_snippets_by_title(title)
@db.execute('select count(*) from snippets where title = ?', title)
end
def get_record_count(records)
records.first.first
end
def create_snippet(snippet)
@logger.debug("\tcreate #{snippet[Fields::TITLE]}")
insert_tags_index(snippet)
create_tag_if_not_exist(snippet[Fields::TAG])
insert_snippet(snippet)
end
def insert_tags_index(snippet)
@logger.debug("\t\tinsert tagsIndex for title:#{snippet[Fields::TITLE]} tag: #{snippet[Fields::TAG]}")
if tag_exist?(snippet[Fields::TAG])
@db.execute('insert into tagsIndex values ((select tid from tags where tag = ?), ?)', snippet[Fields::TAG], next_sid)
else
@db.execute('insert into tagsIndex values (?, ?)', next_tid, next_sid)
end
end
def create_tag_if_not_exist(tag)
return if tag_exist?(tag)
@logger.debug("\t\tcreate tag #{tag}")
@db.execute('insert into tags values ((select max(tid) from tagsIndex), ?)', tag)
end
def next_sid
next_id('tagsIndex', 'sid')
end
def next_tid
next_id('tags', 'tid')
end
def next_id(table, column)
count = @db.execute("select count(*) from #{table}").first.first
return 1 if count == 0
@db.execute("select max(#{column}) from #{table}").first.first + 1
end
def tag_exist?(tag)
tag_count = @db.execute('select count(*) from tags where tag = ?', tag).first.first
tag_count == 1
end
def insert_snippet(snippet)
@logger.debug("\t\tcreate snippet #{snippet[Fields::TITLE]}")
@db.execute('insert into snippets values((select max(sid) from tagsIndex),?,?,?,?)',
snippet[Fields::TITLE],
snippet[Fields::BODY],
snippet[Fields::SYNTAX],
0
)
end
def load_db_snippets
sql =<<-SQL
select s.title, s.body, s.syntax, t.tag
from snippets s
inner join tagsIndex ti on s.sid == ti.sid
inner join tags t on ti.tid = t.tid
SQL
@db.execute(sql)
end
def normalize_filename(filename)
filename.gsub(/([^a-z|A-Z|0-9|\-_])/, '')
end
desc 'build snippets'
task :build do
build_snippets
end
desc 'dump snippets'
task :dump do
dump_snippets
end
end