This repository was archived by the owner on Sep 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
61 lines (53 loc) · 1.62 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
require 'open3'
import 'parser/Rakefile'
def run_sqlite3_commands(commands)
Open3.popen3("sqlite3 development.db") do |i, o, e, ts|
commands.each {|c| i.puts c}
s=o.read
puts s unless s.empty?
s=e.read
puts s unless s.empty?
end
end
namespace 'expenses' do
desc "Count entries in DB (filter by year optional)"
task :count, [:year] do |t, args|
args.with_defaults(:year => 'year')
run_sqlite3_commands([
"select count(*) from expenses where year=#{args.year};",
".exit"
])
end
desc "Clear entries in DB (filter by year optional)"
task :delete, [:year] do |t, args|
args.with_defaults(:year => 'year')
run_sqlite3_commands([
"delete from expenses where year=#{args.year};",
".exit"
])
end
desc "Import expense data into DB for given year"
task :import, [:year] do |t, args|
filename = "parser/output/#{args.year}/expenses.csv"
puts "Importing file #{filename}..."
run_sqlite3_commands([
'.mode csv',
'.separator "|"',
".import #{filename} expenses",
'.exit'
])
end
# Note: Run Rake in silent mode (-s) so output can be redirected into file easily
desc "Export programme totals from DB, excluding internal transfers (year optional)"
task :export_programmes, [:year] do |t, args|
args.with_defaults(:year => 'year')
run_sqlite3_commands([
'.mode csv',
'.separator "|"',
"select programme, description, sum(amount) from expenses \
where year=#{args.year} and programme<>'' and concept='' and programme<>'000X' \
group by programme;",
".exit"
])
end
end