/
process_inbox.rb
executable file
·88 lines (85 loc) · 3.34 KB
/
process_inbox.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
#!/usr/bin/env ruby
$:<<ENV['TM_SUPPORT_PATH']
$:<<ENV['TM_BUNDLE_SUPPORT']
require 'lib/ui.rb'
require 'lib/exit_codes.rb'
require 'lib/GTD.rb'
require 'fileutils.rb'
include GTD
class NormalException < Exception
end
dir = ENV['TM_GTD_DIRECTORY'] || ""
inboxfile = ENV['TM_GTD_INBOX'] || ""
unless dir != "" && inboxfile != "" && File.exist?(File.expand_path(dir)) && File.exist?(File.expand_path(inboxfile)) then
TextMate.exit_show_html("<h1>Some files need to be created first!</h1>" + `#{e_sh File.join(ENV['TM_SUPPORT_PATH'],'bin',"MarkDown.pl")} #{e_sh File.join(ENV['TM_BUNDLE_SUPPORT'],"/INBOX.txt")} `)
end
dir = File.expand_path(dir)
inboxfile = File.expand_path(inboxfile)
tempInboxFilename = File.join(dir, "temp.gtd")
`touch "#{tempInboxFilename}"` unless File.exist?(tempInboxFilename)
objects = GTD.process_directory(dir)
inbox_object = objects.find{|o| o.file == tempInboxFilename}
objects << (inbox_object = GTDFile.new(tempInboxFilename)) unless inbox_object
projects = GTD.projects
contexts = GTDContexts.contexts
f = File.open(inboxfile, "r")
lines = f.readlines.map{|i| i.chomp}
f.close
$log = []
$unsorted = []
for text in lines do
begin
m = text.match(/^\s*@(\S+)\s+([^>]+)(?:>\s*(.*))?$/)
raise(NormalException, text.match(/^\s*$/) ? "" : "Malformed Input Line: #{text}") unless m
context, action, project = m[1..3]
newContext = contexts.find_all{|c| c.downcase.index(context.downcase) != nil}
case newContext.length
when 0
contexts = GTDContexts.contexts << context
$log << "Created new context: #{context}."
newContext = context
when 1
newContext = newContext[0]
else
raise NormalException, "Too many contexts matching: #{context}."
end
targetProj = inbox_object
unless project == nil or project == "" then
proj = projects.find_all{|pro| pro.name.downcase.index(project.downcase) != nil}
case proj.length
when 0
choice = TextMate::UI.request_item(:title => "No matching project", :prompt => "Did not find any project matching: #{project}. Please select a project from the list", :items => projects.map{|pro| pro.name} )
if choice then
targetProj = projects.find{|pro| pro.name == choice}
else
raise NormalException, "Did not find any project matching: #{project}."
end
when 1
targetProj = proj[0]
else
choice = TextMate::UI.request_item(:title => "Too many matching projects", :prompt => "Found too many projects matching: #{project}. Please select a project from the list", :items => proj.map{|pro| pro.name} )
if choice then
targetProj = projects.find{|pro| pro.name == choice}
else
raise NormalException, "Too many projects matching: #{project}."
end
end
end
act = Action.new(:name => action,:context => newContext)
targetProj.add_item(act)
$log << "Added action #{act.name} with context: #{newContext} to project: #{targetProj.name}."
rescue NormalException => e
$unsorted << text
$log << e.to_s
next
rescue Exception => e
raise e
end
end
# Create files we want
data = objects.map{|o| [o.file.to_s,o.dump_object]} << [inboxfile,$unsorted.join("\n")]
GTD.safe_write_with_backup(data)
# Return feedback
puts $log.join("\n")
puts "LINES THAT COULD NOT BE PROCESSED:"
puts $unsorted.join("\n")