Windows shell

This commit is contained in:
2022-11-08 22:21:00 +01:00
parent 30c3266e25
commit b23ab518db
6 changed files with 93 additions and 48 deletions

View File

@@ -1,4 +1,5 @@
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Helper;
using Alma.Logging; using Alma.Logging;
using Alma.Services; using Alma.Services;
@@ -52,7 +53,7 @@ public class InfoCommand : ICommand
foreach (var repository in repositories) foreach (var repository in repositories)
{ {
Console.Write(repository.Name); 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})"); Console.Write($" (containing folder not exists {repository.RepositoryPath})");
} }

View File

@@ -47,7 +47,7 @@ public class InstallCommand : RepositoryModuleCommandBase
if (moduleConfiguration is null) 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; return;
} }

View File

@@ -2,6 +2,7 @@ using System.Runtime.InteropServices;
using Alma.Configuration.Module; using Alma.Configuration.Module;
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Data; using Alma.Data;
using Alma.Helper;
using Alma.Logging; using Alma.Logging;
using Alma.Services; using Alma.Services;
@@ -63,7 +64,7 @@ public class LinkCommand : RepositoryModuleCommandBase
if (moduleConfiguration?.Target is string moduleTargetDir) if (moduleConfiguration?.Target is string moduleTargetDir)
{ {
targetDirectory = ResolvePath(moduleTargetDir, targetDirectory); targetDirectory = PathHelper.ResolvePath(moduleTargetDir, targetDirectory);
} }
if (!Directory.Exists(targetDirectory)) if (!Directory.Exists(targetDirectory))
@@ -156,7 +157,7 @@ public class LinkCommand : RepositoryModuleCommandBase
var relativePath = GetRelativePath(subDir.FullName, moduleDirectory.FullName); var relativePath = GetRelativePath(subDir.FullName, moduleDirectory.FullName);
if (moduleConfiguration?.Links?.ContainsKey(relativePath) ?? false) 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 else
{ {
@@ -174,18 +175,5 @@ public class LinkCommand : RepositoryModuleCommandBase
return filesToLink.Concat(subDirLinksToAdd); 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); private static string GetRelativePath(string full, string parent) => full[parent.Length..].TrimStart(Path.DirectorySeparatorChar);
} }

View File

@@ -1,4 +1,5 @@
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Helper;
namespace Alma.Command; namespace Alma.Command;
@@ -12,7 +13,7 @@ public abstract class RepositoryModuleCommandBase : ICommand
{ {
_repositoryConfiguration = repositoryConfiguration; _repositoryConfiguration = repositoryConfiguration;
} }
protected (string?, string?) GetRepositoryAndModuleName(List<string> parameters) protected (string?, string?) GetRepositoryAndModuleName(List<string> parameters)
{ {
//TODO: handle parameters //TODO: handle parameters
@@ -37,8 +38,14 @@ public abstract class RepositoryModuleCommandBase : ICommand
if (repoName is not null if (repoName is not null
&& _repositoryConfiguration.Configuration.Repositories.FirstOrDefault(r => r.Name == repoName) is { } repoConfig) && _repositoryConfiguration.Configuration.Repositories.FirstOrDefault(r => r.Name == repoName) is { } repoConfig)
{ {
fallbackSourceDirectory = repoConfig.RepositoryPath ?? fallbackSourceDirectory; fallbackSourceDirectory =
fallbackTargetDirectory = repoConfig.LinkPath ?? fallbackTargetDirectory; repoConfig.RepositoryPath is { } repoPath
? PathHelper.ResolvePath(repoPath)
: fallbackSourceDirectory;
fallbackTargetDirectory =
repoConfig.LinkPath is { } linkPath
? PathHelper.ResolvePath(linkPath)
: fallbackTargetDirectory;
} }
return (fallbackSourceDirectory, fallbackTargetDirectory); return (fallbackSourceDirectory, fallbackTargetDirectory);

View File

@@ -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);
}
}

View File

@@ -15,38 +15,66 @@ public class ShellService : IShellService
public async Task RunCommandAsync(string command) public async Task RunCommandAsync(string command)
{ {
Process? process;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{ {
var processStartInfo = new ProcessStartInfo() process = CreateLinuxShell(command);
{ }
FileName = "sh", else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
ArgumentList = {"-c", command}, {
RedirectStandardOutput = true, process = CreateWindowsShell(command);
RedirectStandardInput = true, }
CreateNoWindow = true, else
UseShellExecute = false {
}; _logger.LogError("Platform not supported");
throw new NotSupportedException();
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;
} }
_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};
} }
} }