From a9ac6d8bc2dc7e503bff7f90e8119275e4e675d2 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 12 Jun 2023 02:17:54 +0200 Subject: [PATCH 1/3] Change to camelCase --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 8b0b175..d793869 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,7 +10,7 @@ indent_style = space tab_width = 4 # Naming Conventions -dotnet_naming_style.pascal_case_style.capitalization = pascal_case +dotnet_naming_style.camel_case.capitalization = camel_case # New line preferences csharp_new_line_before_open_brace = none -- 2.47.1 From 013bc2fe33787126452760c52f1074dd51ff327f Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 12 Jun 2023 02:19:07 +0200 Subject: [PATCH 2/3] Refactor pacserver --- src/Pacserver.Tests/TransferFilesTest.cs | 11 ++- src/Pacserver.Tests/pacmanCache_Test.cs | 13 ---- src/Pacserver.Tests/pacmanDatabase_Test.cs | 12 ---- src/Pacserver.Tests/readPacmanConfigTest.cs | 18 +++++ src/Pacserver/Pacserver.cs | 75 +++++++++------------ src/Pacserver/Program.cs | 4 +- 6 files changed, 61 insertions(+), 72 deletions(-) delete mode 100644 src/Pacserver.Tests/pacmanCache_Test.cs delete mode 100644 src/Pacserver.Tests/pacmanDatabase_Test.cs create mode 100644 src/Pacserver.Tests/readPacmanConfigTest.cs diff --git a/src/Pacserver.Tests/TransferFilesTest.cs b/src/Pacserver.Tests/TransferFilesTest.cs index 52905f8..4de7999 100644 --- a/src/Pacserver.Tests/TransferFilesTest.cs +++ b/src/Pacserver.Tests/TransferFilesTest.cs @@ -4,10 +4,15 @@ namespace Pacserver.Tests; public class TranserFilesTest { [Fact] - public void TransferPacmanCacheTest() { - string result = PacserverUtils.determinePacmanCacheDirectory(); - PacserverUtils.TransferPacmanCache(); + public void transferPacmanCache_doesNotFail() { + // Arrange + PacserverUtils utils = new PacserverUtils(); + // Act + utils.readPacmanConfig(); + utils.transferPacmanCache(); + + // Assert //Assert.NotEmpty(Directory.GetFiles("/home/rene/test/")); } } diff --git a/src/Pacserver.Tests/pacmanCache_Test.cs b/src/Pacserver.Tests/pacmanCache_Test.cs deleted file mode 100644 index 9305fd5..0000000 --- a/src/Pacserver.Tests/pacmanCache_Test.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Pacserver.Utils; - -namespace Pacserver.Tests; - -public class pacmanCache_Test { - [Fact] - public void doesPacmanCacheExist() { - string result = PacserverUtils.determinePacmanCacheDirectory(); - - Assert.Equivalent(result, "/var/cache/pacman/pkg/"); - } -} - diff --git a/src/Pacserver.Tests/pacmanDatabase_Test.cs b/src/Pacserver.Tests/pacmanDatabase_Test.cs deleted file mode 100644 index 8b08cd4..0000000 --- a/src/Pacserver.Tests/pacmanDatabase_Test.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Pacserver.Utils; - -namespace Pacserver.Tests; - -public class pacmanDatabase_Test { - [Fact] - public void doesPacmanDatabaseExist() { - string result = PacserverUtils.determinePacmanDatabaseDirectory(); - - Assert.Equivalent(result, "/var/lib/pacman/"); - } -} \ No newline at end of file diff --git a/src/Pacserver.Tests/readPacmanConfigTest.cs b/src/Pacserver.Tests/readPacmanConfigTest.cs new file mode 100644 index 0000000..90d5fd3 --- /dev/null +++ b/src/Pacserver.Tests/readPacmanConfigTest.cs @@ -0,0 +1,18 @@ +using Pacserver.Utils; + +namespace Pacserver.Tests; + +public class readPacmanConfigTest { + [Fact] + public void readPacmanConfig_returnsNoException() { + // Arrange + PacserverUtils utils = new PacserverUtils(); + + // Act + var exception = Record.Exception(() => utils.readPacmanConfig()); + + // Assert + Assert.Null(exception); + } +} + diff --git a/src/Pacserver/Pacserver.cs b/src/Pacserver/Pacserver.cs index 172f707..5fc9a72 100644 --- a/src/Pacserver/Pacserver.cs +++ b/src/Pacserver/Pacserver.cs @@ -3,69 +3,58 @@ using System.Text.RegularExpressions; namespace Pacserver.Utils; public class PacserverUtils { - public static string pacmanCacheDirectory { get; set; } = string.Empty; - public static string determinePacmanCacheDirectory() { - string defaultPacmanCacheDirectory = "/var/cache/pacman/pkg/"; - - Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 - string? line; - StreamReader file = new StreamReader("/etc/pacman.conf"); - while ((line = file.ReadLine()) is not null) { - if (line.Contains("CacheDir")) { - Match match = regex.Match(line); - if (match.Success) { - pacmanCacheDirectory = match.ToString(); - } else { - throw new Exception("Could not determine where pacman cache is! Would normally be found here " + defaultPacmanCacheDirectory); - } - } - } - file.Close(); - - return pacmanCacheDirectory; - } - + public string pacmanCacheDirectory { get; set; } = string.Empty; public static string pacmanDatabaseDirectory { get; set; } = string.Empty; - public static string determinePacmanDatabaseDirectory() { - string defaultPacmanDatabaseDirectory = "/var/lib/pacman/"; + public static List pathsToDetermine = new List() { "CacheDir", "DBPath" }; + public void readPacmanConfig() { + using (StreamReader file = new StreamReader("/etc/pacman.conf")) { + Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 + string? line; - Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 - string? line; - StreamReader file = new StreamReader("/etc/pacman.conf"); - while ((line = file.ReadLine()) is not null) { - if (line.Contains("DBPath")) { - Match match = regex.Match(line); - if (match.Success) { - pacmanDatabaseDirectory = match.ToString(); - } else { - throw new Exception("Could not determine where pacman database is! Would normally be found here " + defaultPacmanDatabaseDirectory); + while ((line = file.ReadLine()) is not null) { + foreach (string path in pathsToDetermine) { + if (line.Contains(path)) { + Match match = regex.Match(line); + if (match.Success) { + switch (path) { + case "CacheDir": + pacmanCacheDirectory = match.ToString(); + break; + case "DBPath": + pacmanDatabaseDirectory = match.ToString(); + break; + default: + throw new Exception("Could not deal with " + match.ToString()); + } + } else { + string pathsToDetermineString = string.Join(",", pathsToDetermine); + throw new Exception("Could not determine the necessary file paths: " + pathsToDetermineString); + } + } } } } - file.Close(); - - return pacmanDatabaseDirectory; } - public static void checkForNewerPackagesAndDatabases() { + public void checkForNewerPackagesAndDatabases() { } - private static List NewerPackagesAndDatabases = new List(); - public static async void TransferPacmanCache() { + private static List newerPackagesAndDatabases = new List(); + public async void transferPacmanCache() { HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/"); MultipartFormDataContent content = new MultipartFormDataContent(); - foreach (String PkgOrDb in NewerPackagesAndDatabases) { - content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + PkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + PkgOrDb)); + foreach (string pkgOrDb in newerPackagesAndDatabases) { + content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + pkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + pkgOrDb)); } request.Content = content; await client.SendAsync(request); } - public static void transferPacmanDatabases() { + public void transferPacmanDatabases() { } } \ No newline at end of file diff --git a/src/Pacserver/Program.cs b/src/Pacserver/Program.cs index cab8ffd..9840ca8 100644 --- a/src/Pacserver/Program.cs +++ b/src/Pacserver/Program.cs @@ -12,7 +12,9 @@ public class Program { switch (args[0]) { case "determinePacmanCacheDirectory": - Console.WriteLine(PacserverUtils.determinePacmanCacheDirectory()); + PacserverUtils utils = new PacserverUtils(); + utils.readPacmanConfig(); + Console.WriteLine(utils.pacmanCacheDirectory); break; default: Console.ForegroundColor = ConsoleColor.Red; -- 2.47.1 From 405b3580b60263ac55a1f2249b7b73f9adf5f634 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Thu, 15 Jun 2023 20:23:25 +0200 Subject: [PATCH 3/3] Made justfile portable --- justfile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/justfile b/justfile index 67d572b..d66ba9f 100644 --- a/justfile +++ b/justfile @@ -17,15 +17,15 @@ run: @dotnet run build: - @dotnet build src/Pacserver/pacserver.csproj - @dotnet build src/Pacserver.Tests/Pacserver.Tests.csproj + @dotnet build src/{{uppercase_project_name}}/{{project_name}}.csproj + @dotnet build src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj publish: format - @dotnet publish --configuration Release src/Pacserver/pacserver.csproj + @dotnet publish --configuration Release src/{{uppercase_project_name}}/{{project_name}}.csproj format: - @dotnet format src/Pacserver - @dotnet format src/Pacserver.Tests + @dotnet format src/{{uppercase_project_name}} + @dotnet format src/{{uppercase_project_name}}.Tests test: build - @dotnet test src/Pacserver.Tests + @dotnet test src/{{uppercase_project_name}}.Tests -- 2.47.1