diff --git a/.gitignore b/.gitignore index ff5b00c..e9d6a98 100644 --- a/.gitignore +++ b/.gitignore @@ -261,4 +261,6 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc + +appsettings.json \ No newline at end of file diff --git a/CalendarSync.Console/CalendarSync.Console.csproj b/CalendarSync.Console/CalendarSync.Console.csproj index 8c22aea..0bb9ba5 100644 --- a/CalendarSync.Console/CalendarSync.Console.csproj +++ b/CalendarSync.Console/CalendarSync.Console.csproj @@ -1,4 +1,4 @@ - + Exe @@ -15,10 +15,17 @@ + + + + Always + + + diff --git a/CalendarSync.Console/Program.cs b/CalendarSync.Console/Program.cs index 20b0314..5d44e1b 100644 --- a/CalendarSync.Console/Program.cs +++ b/CalendarSync.Console/Program.cs @@ -1,32 +1,70 @@ using CalendarSync; using CalendarSync.Console; using CommandLine; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Win32.TaskScheduler; -const uint DefaultDaysToSync = 7; +const string DailyTaskName = "SyncCalendar"; +var configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .Build(); -Parser.Default.ParseArguments(args) - .WithParsed(async o => - { - var host = Host.CreateDefaultBuilder().Build(); - var logger = host.Services.GetRequiredService>(); - logger.LogInformation("Logging established"); +var host = Host.CreateDefaultBuilder().Build(); +var logger = host.Services.GetRequiredService>(); +logger.LogInformation(@" + BE NOT AFRAID + Your calendars will begin to sync + DO NOT close this window until you see the final logging message. +"); - var primaryAccountRefreshToken = o.PrimaryAccountRefreshToken; - var secondaryAccountRefreshToken = o.SecondaryAccountRefreshToken; - var clientId = o.ClientId; - var orgConnectionString = o.OrgConnectionString; +var primaryAccountRefreshToken = configuration["PrimaryAccountRefreshToken"]; +var primaryAccountSubjectPrefix = configuration["PrimaryAccountSubjectPrefix"]; +var secondaryAccountRefreshToken = configuration["SecondaryAccountRefreshToken "]; +var secondaryAccountSubjectPrefix = configuration["SecondaryAccountSubjectPrefix"]; +var clientId = configuration["ClientId"]; +var orgConnectionString = configuration["OrgConnectionString "]; +var daysToSync = uint.Parse(configuration["DaysToSync"]); - var daysToSync = o.DaysToSync != 0 ? o.DaysToSync : DefaultDaysToSync; +try +{ + using TaskService ts = new TaskService(); + var calendarSyncTask = ts.GetTask(DailyTaskName); + if (calendarSyncTask == null) + { + TaskDefinition td = ts.NewTask(); + td.RegistrationInfo.Description = "Every day at 9 am sync calendars"; - var source = new SecondaryAccToPrimaryAccProfile(secondaryAccountRefreshToken, o.SecondaryAccountSubjectPrefix); - var dest = new PrimaryAccToSecondaryAccProfile(primaryAccountRefreshToken, o.PrimaryAccountSubjectPrefix); - var service = new CalendarSyncService(dest, source, logger, clientId, orgConnectionString); - var startTime = DateTime.UtcNow; - var endTime = startTime.AddDays(daysToSync); + DailyTrigger trigger = new() + { + StartBoundary = DateTime.Today + new TimeSpan(10, 45, 0), + DaysInterval = 1 + }; + td.Triggers.Add(trigger); - await service.SyncRangeBidirectionalAsync(startTime.ToString("O"), endTime.ToString("O")); - }); + var dir = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/CalendarSync"; + td.Actions.Add(new ExecAction($"{dir}/CalendarSync.Console.exe", dir, null)); + + ts.RootFolder.RegisterTaskDefinition(DailyTaskName, td); + } + + var source = new SecondaryAccToPrimaryAccProfile(secondaryAccountRefreshToken, secondaryAccountSubjectPrefix); + var dest = new PrimaryAccToSecondaryAccProfile(primaryAccountRefreshToken, primaryAccountSubjectPrefix); + var service = new CalendarSyncService(dest, source, logger, clientId, orgConnectionString); + var startTime = DateTime.UtcNow; + var endTime = startTime.AddDays(daysToSync); + + await service.SyncRangeBidirectionalAsync(startTime.ToString("O"), endTime.ToString("O")); + + logger.LogInformation(@" + Calendar syncing complete! + Go forth about your day and be productive. + "); +} +catch (Exception e) +{ + Console.WriteLine(e.ToString()); +} diff --git a/CalendarSync.Console/Properties/PublishProfiles/FolderProfile.pubxml b/CalendarSync.Console/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..2ebdb53 --- /dev/null +++ b/CalendarSync.Console/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,19 @@ + + + + + Release + Any CPU + bin\Release\net6.0\publish\win-x86\ + FileSystem + <_TargetId>Folder + net6.0 + win-x86 + true + true + false + true + + \ No newline at end of file diff --git a/CalendarSync.Console/Properties/launchSettings.json b/CalendarSync.Console/Properties/launchSettings.json index aafa395..0a9bb0a 100644 --- a/CalendarSync.Console/Properties/launchSettings.json +++ b/CalendarSync.Console/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "CalendarSync.Console": { "commandName": "Project", - "commandLineArgs": "--secondaryAccountRefreshToken idk --primaryAccountRefreshToken test --primaryAccountSubjectPrefix [Root16] --secondaryAccountSubjectPrefix [RSM] --clientId lezfooo --orgConnectionString asgsgs --daysToSync I2" + "commandLineArgs": "--secondaryAccountRefreshToken idk --primaryAccountRefreshToken test --primaryAccountSubjectPrefix [primacct] --secondaryAccountSubjectPrefix [secacct] --clientId lezfooo --orgConnectionString asgsgs --daysToSync 2" } } } \ No newline at end of file