Skip to content

Commit

Permalink
Updated to .NET 9 and fixed code analysis warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgurung committed Nov 15, 2024
1 parent 4663c18 commit 6d74ce6
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 28 deletions.
15 changes: 15 additions & 0 deletions GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@

[assembly: SuppressMessage("Style", "IDE0130:Namespace does not match folder structure",
Justification = "Intentional")]

[assembly: SuppressMessage("Style", "IDE0072:Add missing cases",
Justification = "Intent is already clear", Scope = "member", Target = "~M:TimetableCalendarGenerator.CalendarWriterFactory.GetCalendarWriter(System.String)")]

[assembly: SuppressMessage("Style", "IDE0046:Convert to conditional expression",
Justification = "Readability", Scope = "member", Target = "~M:TimetableCalendarGenerator.EventComparer`1.Equals(`0,`0)")]

[assembly: SuppressMessage("Style", "IDE0058:Expression value is never used",
Justification = "Use of discards is unnecessary")]

[assembly: SuppressMessage("Style", "IDE0010:Add missing cases",
Justification = "Intent is already clear", Scope = "member", Target = "~M:TimetableCalendarGenerator.CalendarWriterFactory.GetCalendarWriter(System.String)")]

[assembly: SuppressMessage("Maintainability", "CA1515:Consider making public types internal",
Justification = "Public types are preferred")]
2 changes: 1 addition & 1 deletion Properties/PublishProfiles/publish-win-x64.pubxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<PublishDir>bin\Publish\win-x64\</PublishDir>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
2 changes: 1 addition & 1 deletion Properties/PublishProfiles/publish-xplat.pubxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<PublishDir>bin\Publish\xplat\</PublishDir>
<SelfContained>false</SelfContained>
<UseAppHost>False</UseAppHost>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This is a cross-platform command line tool for bulk generating student and teach

### Usage on other platforms

1. Ensure you have the [.NET 8 runtime](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) installed (on the download page, look for the latest ".NET Runtime 8.0.x" heading in the right-hand column).
1. Ensure you have the [.NET 9 runtime](https://dotnet.microsoft.com/en-us/download/dotnet/9.0) installed (on the download page, look for the latest ".NET Runtime 9.0.x" heading in the right-hand column).
1. Download and extract the `xplat` ZIP package from our [Releases page](https://github.com/jamesgurung/timetable-calendar-generator/releases).
1. In the "inputs" directory, add the input files defined below.
1. Run commands in the format: `dotnet makecal.dll --csv`
Expand Down
11 changes: 5 additions & 6 deletions makecal.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AnalysisMode>all</AnalysisMode>
<AnalysisMode>All</AnalysisMode>
<RootNamespace>TimetableCalendarGenerator</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>None</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Google.Apis.Calendar.v3" Version="1.68.0.3473" />
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Google.Apis.Calendar.v3" Version="1.68.0.3592" />
<PackageReference Include="KBCsv" Version="6.0.0" />
<PackageReference Include="Microsoft.Graph.Beta" Version="5.78.0-preview" />
<PackageReference Include="Microsoft.Graph.Beta" Version="5.86.0-preview" />
</ItemGroup>
</Project>
9 changes: 5 additions & 4 deletions src/CalendarGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IList<CalendarEvent> Generate(Person person)
var overridePeriod = Settings.Overrides.FirstOrDefault(o => o.Date == date && o.Period == period && (o.YearGroups?.Contains(yearGroup) ?? true));

string title = null, room = null;

if (overridePeriod is not null)
{
if (overridePeriod.CopyFromPeriod is not null && myLessons.TryGetValue($"{dayCode}:{overridePeriod.CopyFromPeriod}", out var lessonToCopy))
Expand Down Expand Up @@ -55,8 +55,9 @@ public IList<CalendarEvent> Generate(Person person)
var start = new DateTime(date.Year, date.Month, date.Day, lessonTime.StartHour, lessonTime.StartMinute, 0);
var end = start.AddMinutes(lessonTime.Duration);

if (char.IsDigit(period[0])) {
title = $"P{period}. {title}";
if (char.IsDigit(period[0]))
{
title = $"P{period}. {title}";
}
else if (period is "AM" or "PM")
{
Expand All @@ -81,7 +82,7 @@ public IList<CalendarEvent> Generate(Person person)

return events;
}

private (string Title, string Room) GetTitleAndRoom(Lesson lesson, int? yearGroup, DateOnly date)
{
if (yearGroup is not null && Settings.Absences.Any(o => o.YearGroups.Contains(yearGroup.Value) && o.StartDate <= date && o.EndDate >= date)) return (null, null);
Expand Down
2 changes: 2 additions & 0 deletions src/CalendarWriterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public CalendarWriterFactory(OutputType outputType, string googleServiceAccountK
var credential = new ClientSecretCredential(microsoftClientKey.TenantId, microsoftClientKey.ClientId, microsoftClientKey.ClientSecret);
MicrosoftClient = new GraphServiceClient(credential);
break;
default:
throw new NotImplementedException();
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/CalendarWriters/Google/GoogleCalendarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static async Task<IList<Event>> FetchAllWithRetryAsync(this EventsResourc
}

public static async Task ExecuteWithRetryAsync(this BatchRequest request)
=> await ExecuteWithRetryAsync(async () => { await request.ExecuteAsync(); return 0; });

{
await ExecuteWithRetryAsync(async () => { await request.ExecuteAsync(); return 0; });
}
}
2 changes: 1 addition & 1 deletion src/CalendarWriters/Google/GoogleCalendarWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class GoogleCalendarWriter(string email, string serviceAccountKey) : ICal
public async Task WriteAsync(IList<CalendarEvent> events)
{
var existingEvents = await GetExistingEventsAsync();

var expectedEvents = events.Select(o => new Event
{
Summary = o.Title,
Expand Down
3 changes: 2 additions & 1 deletion src/CalendarWriters/Microsoft/MicrosoftCalendarWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private async Task<IList<CalendarEventWithId>> GetExistingEventsAsync()
config => { config.Headers.Add("Prefer", "outlook.timezone=\"Europe/London\""); return config; }
);
await iterator.IterateAsync();
return events.Select(ev => new CalendarEventWithId {
return events.Select(ev => new CalendarEventWithId
{
Id = ev.Id,
Title = ev.Subject,
Location = ev.Location.DisplayName,
Expand Down
5 changes: 3 additions & 2 deletions src/CalendarWriters/Microsoft/MicrosoftUnlimitedBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ public async Task ExecuteWithRetryAsync()
if (batch.BatchRequestSteps.Count == 0) continue;
var current = batch;

for (var attempt = 1; attempt <= MaxAttempts; attempt++) {
for (var attempt = 1; attempt <= MaxAttempts; attempt++)
{
var stepsToRetry = current.BatchRequestSteps.Select(o => o.Key).ToList();
List<KeyValuePair<string, HttpStatusCode>> responses = null;
var wait = RetryFirst * (int)Math.Pow(RetryMultiplier, attempt - 1);
try
{
var result = await _service.Batch.PostAsync(current);
responses = [.. (await result.GetResponsesStatusCodesAsync())];
var failures = responses.Where(o => (int)o.Value < 200 || (int)o.Value > 299).ToList();
var failures = responses.Where(o => (int)o.Value is < 200 or > 299).ToList();
stepsToRetry = failures.Select(o => o.Key).ToList();
if (stepsToRetry.Count == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class ConsoleHelper
private static int startLine;
private static int lastLine;
private static readonly ConsoleColor defaultBackground = Console.BackgroundColor;
private static readonly object consoleLock = new();
private static readonly Lock consoleLock = new();

public static void ConfigureSize(int count, int maxNameWidth)
{
Expand Down
8 changes: 4 additions & 4 deletions src/EventComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public int GetHashCode(TEvent obj)
unchecked
{
var hash = 17;
hash = hash * 23 + (GetOriginalTitle(Title(obj))?.GetHashCode(StringComparison.Ordinal) ?? 0);
hash = hash * 23 + (Start(obj)?.GetHashCode() ?? 0);
hash = hash * 23 + (End(obj)?.GetHashCode() ?? 0);
hash = hash * 23 + (Location(obj) ?? string.Empty).GetHashCode(StringComparison.Ordinal);
hash = (hash * 23) + (GetOriginalTitle(Title(obj))?.GetHashCode(StringComparison.Ordinal) ?? 0);
hash = (hash * 23) + (Start(obj)?.GetHashCode() ?? 0);
hash = (hash * 23) + (End(obj)?.GetHashCode() ?? 0);
hash = (hash * 23) + (Location(obj) ?? string.Empty).GetHashCode(StringComparison.Ordinal);
return hash;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/InputReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static async Task<IList<Person>> LoadPeopleAsync()
{
throw new InvalidOperationException($"You must include at least one of '{StudentsFileName}' and '{TeachersFileName}'.");
}

if (people.Count == 0)
{
throw new InvalidOperationException("No students or teachers were found in the input files.");
Expand Down
4 changes: 2 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Graph.Beta.Models.ODataErrors;

[assembly:CLSCompliant(true)]
[assembly: CLSCompliant(true)]
namespace TimetableCalendarGenerator;

public static class Program
Expand All @@ -21,7 +21,7 @@ private static async Task Main(string[] args)
var calendarWriterFactory = new CalendarWriterFactory(outputType, googleKey, microsoftKey);
Console.WriteLine(calendarWriterFactory.DisplayText);

var numberColumnWidth = (int)Math.Log10(people.Count) * 2 + 5;
var numberColumnWidth = ((int)Math.Log10(people.Count) * 2) + 5;
var maxNameWidth = people.Max(o => o.Email.Length) + numberColumnWidth + 2;
ConsoleHelper.ConfigureSize(people.Count, maxNameWidth);

Expand Down
3 changes: 2 additions & 1 deletion src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public class Timing

public string StartTime
{
set {
set
{
if (value is null) throw new ArgumentNullException(nameof(value), "Timing start time is required.");
var parts = value.Split(':');
StartHour = int.Parse(parts[0], CultureInfo.InvariantCulture);
Expand Down

0 comments on commit 6d74ce6

Please sign in to comment.