diff --git a/src/Alma.App/Command/Info/InfoCommand.cs b/src/Alma.App/Command/Info/InfoCommand.cs index 8a59a92..0ea2617 100644 --- a/src/Alma.App/Command/Info/InfoCommand.cs +++ b/src/Alma.App/Command/Info/InfoCommand.cs @@ -1,4 +1,5 @@ using Alma.Configuration.Repository; +using Alma.Helper; using Alma.Logging; using Alma.Services; @@ -52,7 +53,7 @@ public class InfoCommand : ICommand foreach (var repository in repositories) { Console.Write(repository.Name); - if (repository.RepositoryPath is not null && !Directory.Exists(repository.RepositoryPath)) + if (repository.RepositoryPath is not null && !Directory.Exists(PathHelper.ResolvePath(repository.RepositoryPath))) { Console.Write($" (containing folder not exists {repository.RepositoryPath})"); } diff --git a/src/Alma.App/Command/Install/InstallCommand.cs b/src/Alma.App/Command/Install/InstallCommand.cs index 41cec98..370290e 100644 --- a/src/Alma.App/Command/Install/InstallCommand.cs +++ b/src/Alma.App/Command/Install/InstallCommand.cs @@ -47,7 +47,7 @@ public class InstallCommand : RepositoryModuleCommandBase if (moduleConfiguration is null) { - _logger.LogInformation("No module configuration found."); + _logger.LogInformation($"No module configuration found for module '{moduleName}'{(repoName is null ? "" : $" in repository '{repoName}'")}"); return; } diff --git a/src/Alma.App/Command/Link/LinkCommand.cs b/src/Alma.App/Command/Link/LinkCommand.cs index 6c4ce46..40270c9 100644 --- a/src/Alma.App/Command/Link/LinkCommand.cs +++ b/src/Alma.App/Command/Link/LinkCommand.cs @@ -2,6 +2,7 @@ using System.Runtime.InteropServices; using Alma.Configuration.Module; using Alma.Configuration.Repository; using Alma.Data; +using Alma.Helper; using Alma.Logging; using Alma.Services; @@ -63,7 +64,7 @@ public class LinkCommand : RepositoryModuleCommandBase if (moduleConfiguration?.Target is string moduleTargetDir) { - targetDirectory = ResolvePath(moduleTargetDir, targetDirectory); + targetDirectory = PathHelper.ResolvePath(moduleTargetDir, targetDirectory); } if (!Directory.Exists(targetDirectory)) @@ -156,7 +157,7 @@ public class LinkCommand : RepositoryModuleCommandBase var relativePath = GetRelativePath(subDir.FullName, moduleDirectory.FullName); if (moduleConfiguration?.Links?.ContainsKey(relativePath) ?? false) { - filesToLink.Add(new ItemToLink(subDir.FullName, ResolvePath(moduleConfiguration.Links[relativePath], targetDirectory.FullName))); + filesToLink.Add(new ItemToLink(subDir.FullName, PathHelper.ResolvePath(moduleConfiguration.Links[relativePath], targetDirectory.FullName))); } else { @@ -174,18 +175,5 @@ public class LinkCommand : RepositoryModuleCommandBase return filesToLink.Concat(subDirLinksToAdd); } - private static string ResolvePath(string path, string currentDirectory) - { - if (path.StartsWith("~")) - { - var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - path = path.Length > 1 ? Path.Combine(userProfile, path[2..]) : userProfile; - } - - //TODO: more special character - - return Path.Combine(currentDirectory, path); - } - private static string GetRelativePath(string full, string parent) => full[parent.Length..].TrimStart(Path.DirectorySeparatorChar); } \ No newline at end of file diff --git a/src/Alma.App/Command/RepositoryModuleCommandBase.cs b/src/Alma.App/Command/RepositoryModuleCommandBase.cs index 714acd6..3143a05 100644 --- a/src/Alma.App/Command/RepositoryModuleCommandBase.cs +++ b/src/Alma.App/Command/RepositoryModuleCommandBase.cs @@ -1,4 +1,5 @@ using Alma.Configuration.Repository; +using Alma.Helper; namespace Alma.Command; @@ -12,7 +13,7 @@ public abstract class RepositoryModuleCommandBase : ICommand { _repositoryConfiguration = repositoryConfiguration; } - + protected (string?, string?) GetRepositoryAndModuleName(List parameters) { //TODO: handle parameters @@ -37,8 +38,14 @@ public abstract class RepositoryModuleCommandBase : ICommand if (repoName is not null && _repositoryConfiguration.Configuration.Repositories.FirstOrDefault(r => r.Name == repoName) is { } repoConfig) { - fallbackSourceDirectory = repoConfig.RepositoryPath ?? fallbackSourceDirectory; - fallbackTargetDirectory = repoConfig.LinkPath ?? fallbackTargetDirectory; + fallbackSourceDirectory = + repoConfig.RepositoryPath is { } repoPath + ? PathHelper.ResolvePath(repoPath) + : fallbackSourceDirectory; + fallbackTargetDirectory = + repoConfig.LinkPath is { } linkPath + ? PathHelper.ResolvePath(linkPath) + : fallbackTargetDirectory; } return (fallbackSourceDirectory, fallbackTargetDirectory); diff --git a/src/Alma.App/Helper/PathHelper.cs b/src/Alma.App/Helper/PathHelper.cs new file mode 100644 index 0000000..9345152 --- /dev/null +++ b/src/Alma.App/Helper/PathHelper.cs @@ -0,0 +1,21 @@ +namespace Alma.Helper; + +public static class PathHelper +{ + public static string ResolvePath(string path, string? currentDirectory = null) + { + var skipCombiningCurrentDirectory = false; + if (path.StartsWith("~")) + { + var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + path = path.Length > 1 ? Path.Combine(userProfile, path[2..]) : userProfile; + skipCombiningCurrentDirectory = true; + } + + //TODO: more special character + + return currentDirectory is null || skipCombiningCurrentDirectory + ? path + : Path.Combine(currentDirectory, path); + } +} \ No newline at end of file diff --git a/src/Alma.App/Services/ShellService.cs b/src/Alma.App/Services/ShellService.cs index 5165ecd..99f3e22 100644 --- a/src/Alma.App/Services/ShellService.cs +++ b/src/Alma.App/Services/ShellService.cs @@ -15,38 +15,66 @@ public class ShellService : IShellService public async Task RunCommandAsync(string command) { + Process? process; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - var processStartInfo = new ProcessStartInfo() - { - FileName = "sh", - ArgumentList = {"-c", command}, - RedirectStandardOutput = true, - RedirectStandardInput = true, - CreateNoWindow = true, - UseShellExecute = false - }; - - var process = Process.Start(processStartInfo); - if (process is null) return; - - var reader = process.StandardOutput; - while (!reader.EndOfStream) - { - var content = await reader.ReadLineAsync(); - - if (content is not null) - { - _logger.LogInformation(content); - } - } - - await process.WaitForExitAsync(); - return; + process = CreateLinuxShell(command); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + process = CreateWindowsShell(command); + } + else + { + _logger.LogError("Platform not supported"); + throw new NotSupportedException(); } - _logger.LogError("Platform not supported"); + if (!process.Start()) return; - throw new NotSupportedException(); + var reader = process.StandardOutput; + while (!reader.EndOfStream) + { + var content = await reader.ReadLineAsync(); + + if (content is not null) + { + _logger.LogInformation(content); + } + } + + await process.WaitForExitAsync(); + } + + private Process CreateLinuxShell(string command) + { + var processStartInfo = new ProcessStartInfo + { + FileName = "sh", + ArgumentList = {"-c", command}, + RedirectStandardOutput = true, + RedirectStandardInput = true, + CreateNoWindow = true, + UseShellExecute = false + }; + + return new Process {StartInfo = processStartInfo}; + } + + private Process CreateWindowsShell(string command) + { + var processStartInfo = new ProcessStartInfo + { + //TODO: customizable shell + FileName = "pwsh", + ArgumentList = {"-c", command}, + RedirectStandardOutput = true, + RedirectStandardInput = true, + CreateNoWindow = true, + UseShellExecute = false + }; + + return new Process {StartInfo = processStartInfo}; } } \ No newline at end of file