-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeMapper.linq
82 lines (62 loc) · 2.21 KB
/
TimeMapper.linq
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
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>System.Globalization</Namespace>
</Query>
#load ".\CalendarService"
#load ".\UserInterfaceUtilities"
const string CALENDAR_NAME = "Time Map";
const string TEMPLATE_FILE_NAME = "template.json";
const string TIME_SPAN_FORMAT_STRING = @"h\:mm";
private CalendarService Service { get; set; }
async Task Main()
{
Service = new CalendarService();
var entries = LoadEntries();
for(var index = 0; index < entries.Count; index++)
{
var entry = entries[index];
await UpdateEvent(entry);
if (entry.Title != String.Empty)
CreateEvent(entry);
var nextEntry = index < entries.Count - 1 ? entries[index + 1] : null;
if (nextEntry != null && !nextEntry.StartTime.HasValue)
nextEntry.StartTime = entry.EndTime;
}
entries.Dump();
}
void CreateEvent(Entry entry)
=> Service.CreateEvent(entry, CALENDAR_NAME);
string GetScriptDirectoryPath()
=> Path.GetDirectoryName(Util.CurrentQueryPath);
string GetTemplateFileFullPath()
=> Path.Combine(GetScriptDirectoryPath(), TEMPLATE_FILE_NAME);
async Task<string> GetUserInputAsync(string prompt, string defaultResponse)
=> await UserInterfaceUtilities.UserInputAsync(prompt, defaultResponse: defaultResponse, hideOnContinue: false);
async Task<TimeSpan> GetUserInputAsync(string prompt, TimeSpan? defaultResponse)
=> TimeSpan.ParseExact(
await GetUserInputAsync(
prompt,
defaultResponse?.ToString(TIME_SPAN_FORMAT_STRING)
),
TIME_SPAN_FORMAT_STRING,
CultureInfo.InvariantCulture
);
List<Entry> LoadEntries()
{
var filePath = GetTemplateFileFullPath();
var entriesJson = File.ReadAllText(filePath);
var entries = JsonConvert.DeserializeObject<List<Entry>>(entriesJson);
return entries;
}
async Task UpdateEvent(Entry entry)
{
entry.Title = await GetUserInputAsync("Title", entry.Title);
if (entry.Title == String.Empty)
return;
entry.StartTime = await GetUserInputAsync("Start Time", entry.StartTime);
entry.Duration = await GetUserInputAsync("Duration", entry.Duration);
}