Move PathHelper to service, ConigureCommand, %DOCUMENTS%
This commit is contained in:
@@ -6,12 +6,14 @@ public class ModuleConfiguration
|
|||||||
public Dictionary<string, string>? Links { get; set; }
|
public Dictionary<string, string>? Links { get; set; }
|
||||||
|
|
||||||
public string? Install { get; set; }
|
public string? Install { get; set; }
|
||||||
|
public string? Configure { get; set; }
|
||||||
public ModuleConfiguration(string? target, Dictionary<string, string>? links, string? install)
|
|
||||||
|
public ModuleConfiguration(string? target, Dictionary<string, string>? links, string? install, string? configure)
|
||||||
{
|
{
|
||||||
Target = target;
|
Target = target;
|
||||||
Links = links;
|
Links = links;
|
||||||
Install = install;
|
Install = install;
|
||||||
|
Configure = configure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModuleConfiguration Merge(ModuleConfiguration merge)
|
public ModuleConfiguration Merge(ModuleConfiguration merge)
|
||||||
@@ -21,10 +23,11 @@ public class ModuleConfiguration
|
|||||||
return new ModuleConfiguration(
|
return new ModuleConfiguration(
|
||||||
merge.Target ?? Target,
|
merge.Target ?? Target,
|
||||||
new Dictionary<string, string>(mergedLinks),
|
new Dictionary<string, string>(mergedLinks),
|
||||||
merge.Install ?? Install
|
merge.Install ?? Install,
|
||||||
|
merge.Configure ?? Configure
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ModuleConfiguration Empty() =>
|
public static ModuleConfiguration Empty() =>
|
||||||
new(null, new Dictionary<string, string>(), null);
|
new(null, new Dictionary<string, string>(), null, null);
|
||||||
}
|
}
|
||||||
3
src/Alma.Abstraction/Data/SpecialPathResolver.cs
Normal file
3
src/Alma.Abstraction/Data/SpecialPathResolver.cs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
namespace Alma.Data;
|
||||||
|
|
||||||
|
public record SpecialPathResolver(string PathName, Func<string> Resolver, bool? SkipCombiningCurrentDirectory = null);
|
||||||
6
src/Alma.Abstraction/Services/IPathHelperService.cs
Normal file
6
src/Alma.Abstraction/Services/IPathHelperService.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Alma.Services;
|
||||||
|
|
||||||
|
public interface IPathHelperService
|
||||||
|
{
|
||||||
|
string ResolvePath(string path, string? currentDirectory = null);
|
||||||
|
}
|
||||||
75
src/Alma.App/Command/Configure/ConfigureCommand.cs
Normal file
75
src/Alma.App/Command/Configure/ConfigureCommand.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
using Alma.Command.Install;
|
||||||
|
using Alma.Configuration.Repository;
|
||||||
|
using Alma.Data;
|
||||||
|
using Alma.Logging;
|
||||||
|
using Alma.Services;
|
||||||
|
|
||||||
|
namespace Alma.Command.Configure;
|
||||||
|
|
||||||
|
public class ConfigureCommand : RepositoryModuleCommandBase
|
||||||
|
{
|
||||||
|
private readonly ILogger<InstallCommand> _logger;
|
||||||
|
private readonly IModuleConfigurationResolver _moduleConfigurationResolver;
|
||||||
|
private readonly IShellService _shellService;
|
||||||
|
public override string CommandString => "configure";
|
||||||
|
|
||||||
|
public ConfigureCommand(
|
||||||
|
ILogger<InstallCommand> logger,
|
||||||
|
IRepositoryConfiguration repositoryConfiguration,
|
||||||
|
IModuleConfigurationResolver moduleConfigurationResolver,
|
||||||
|
IShellService shellService,
|
||||||
|
IPathHelperService pathHelperService)
|
||||||
|
: base(repositoryConfiguration, pathHelperService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_moduleConfigurationResolver = moduleConfigurationResolver;
|
||||||
|
_shellService = shellService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Run(List<string> parameters)
|
||||||
|
{
|
||||||
|
var (repoName, moduleName) = GetRepositoryAndModuleName(parameters);
|
||||||
|
if (moduleName is null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("No module specified");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string sourceDirectory = Path.Combine(Environment.CurrentDirectory);
|
||||||
|
string targetDirectory = Path.Combine(Environment.CurrentDirectory, "..");
|
||||||
|
|
||||||
|
string moduleNameAsPath = moduleName.Replace('/', Path.DirectorySeparatorChar);
|
||||||
|
(sourceDirectory, _) = GetModuleSourceAndTargetDirectory(repoName, sourceDirectory, targetDirectory);
|
||||||
|
|
||||||
|
string moduleDirectory = Path.Combine(sourceDirectory, moduleNameAsPath);
|
||||||
|
|
||||||
|
var moduleConfigFileStub = Path.Combine(moduleDirectory, Constants.ModuleConfigFileStub);
|
||||||
|
var (moduleConfiguration, moduleConfigurationFile) = await _moduleConfigurationResolver.ResolveModuleConfiguration(moduleConfigFileStub);
|
||||||
|
|
||||||
|
if (moduleConfiguration is null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation($"No module configuration found for module '{moduleName}'{(repoName is null ? "" : $" in repository '{repoName}'")}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var configureLines = moduleConfiguration.Configure?.Split(Environment.NewLine);
|
||||||
|
|
||||||
|
if (configureLines is null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("No configure command is found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation($"Configure command: {string.Join("\n", configureLines)}");
|
||||||
|
|
||||||
|
if (configureLines.Length == 1)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Running configure command '" + configureLines[0] + "'");
|
||||||
|
await _shellService.RunCommandAsync(configureLines[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogError("Multi line scripts are not currently supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,13 +14,15 @@ public class InfoCommand : ICommand
|
|||||||
private readonly ILogger<InfoCommand> _logger;
|
private readonly ILogger<InfoCommand> _logger;
|
||||||
private readonly IOsInformation _osInformation;
|
private readonly IOsInformation _osInformation;
|
||||||
private readonly IVersionService _versionService;
|
private readonly IVersionService _versionService;
|
||||||
|
private readonly IPathHelperService _pathHelperService;
|
||||||
|
|
||||||
public InfoCommand(
|
public InfoCommand(
|
||||||
IFolderService folderService,
|
IFolderService folderService,
|
||||||
IRepositoryConfiguration repositoryConfiguration,
|
IRepositoryConfiguration repositoryConfiguration,
|
||||||
ILogger<InfoCommand> logger,
|
ILogger<InfoCommand> logger,
|
||||||
IOsInformation osInformation,
|
IOsInformation osInformation,
|
||||||
IVersionService versionService
|
IVersionService versionService,
|
||||||
|
IPathHelperService pathHelperService
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_folderService = folderService;
|
_folderService = folderService;
|
||||||
@@ -28,6 +30,7 @@ public class InfoCommand : ICommand
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
_osInformation = osInformation;
|
_osInformation = osInformation;
|
||||||
_versionService = versionService;
|
_versionService = versionService;
|
||||||
|
_pathHelperService = pathHelperService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Run(List<string> parameters)
|
public async Task Run(List<string> parameters)
|
||||||
@@ -59,7 +62,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(PathHelper.ResolvePath(repository.RepositoryPath)))
|
if (repository.RepositoryPath is not null && !Directory.Exists(_pathHelperService.ResolvePath(repository.RepositoryPath)))
|
||||||
{
|
{
|
||||||
Console.Write($" (containing folder not exists {repository.RepositoryPath})");
|
Console.Write($" (containing folder not exists {repository.RepositoryPath})");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ public class InstallCommand : RepositoryModuleCommandBase
|
|||||||
ILogger<InstallCommand> logger,
|
ILogger<InstallCommand> logger,
|
||||||
IRepositoryConfiguration repositoryConfiguration,
|
IRepositoryConfiguration repositoryConfiguration,
|
||||||
IModuleConfigurationResolver moduleConfigurationResolver,
|
IModuleConfigurationResolver moduleConfigurationResolver,
|
||||||
IShellService shellService)
|
IShellService shellService,
|
||||||
: base(repositoryConfiguration)
|
IPathHelperService pathHelperService)
|
||||||
|
: base(repositoryConfiguration, pathHelperService)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_moduleConfigurationResolver = moduleConfigurationResolver;
|
_moduleConfigurationResolver = moduleConfigurationResolver;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public class LinkCommand : RepositoryModuleCommandBase
|
|||||||
private readonly IRepositoryConfiguration _repositoryConfiguration;
|
private readonly IRepositoryConfiguration _repositoryConfiguration;
|
||||||
private readonly IModuleConfigurationResolver _moduleConfigurationResolver;
|
private readonly IModuleConfigurationResolver _moduleConfigurationResolver;
|
||||||
private readonly IMetadataHandler _metadataHandler;
|
private readonly IMetadataHandler _metadataHandler;
|
||||||
|
private readonly IPathHelperService _pathHelperService;
|
||||||
private readonly ILogger<LinkCommand> _logger;
|
private readonly ILogger<LinkCommand> _logger;
|
||||||
|
|
||||||
public override string CommandString => "link";
|
public override string CommandString => "link";
|
||||||
@@ -21,12 +22,14 @@ public class LinkCommand : RepositoryModuleCommandBase
|
|||||||
IRepositoryConfiguration repositoryConfiguration,
|
IRepositoryConfiguration repositoryConfiguration,
|
||||||
IModuleConfigurationResolver moduleConfigurationResolver,
|
IModuleConfigurationResolver moduleConfigurationResolver,
|
||||||
IMetadataHandler metadataHandler,
|
IMetadataHandler metadataHandler,
|
||||||
|
IPathHelperService pathHelperService,
|
||||||
ILogger<LinkCommand> logger)
|
ILogger<LinkCommand> logger)
|
||||||
: base(repositoryConfiguration)
|
: base(repositoryConfiguration, pathHelperService)
|
||||||
{
|
{
|
||||||
_repositoryConfiguration = repositoryConfiguration;
|
_repositoryConfiguration = repositoryConfiguration;
|
||||||
_moduleConfigurationResolver = moduleConfigurationResolver;
|
_moduleConfigurationResolver = moduleConfigurationResolver;
|
||||||
_metadataHandler = metadataHandler;
|
_metadataHandler = metadataHandler;
|
||||||
|
_pathHelperService = pathHelperService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +67,7 @@ public class LinkCommand : RepositoryModuleCommandBase
|
|||||||
|
|
||||||
if (moduleConfiguration?.Target is string moduleTargetDir)
|
if (moduleConfiguration?.Target is string moduleTargetDir)
|
||||||
{
|
{
|
||||||
targetDirectory = PathHelper.ResolvePath(moduleTargetDir, targetDirectory);
|
targetDirectory = _pathHelperService.ResolvePath(moduleTargetDir, targetDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Directory.Exists(targetDirectory))
|
if (!Directory.Exists(targetDirectory))
|
||||||
@@ -130,7 +133,7 @@ public class LinkCommand : RepositoryModuleCommandBase
|
|||||||
_logger.LogInformation("An error occured while creating links: " + e.Message);
|
_logger.LogInformation("An error occured while creating links: " + e.Message);
|
||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("On Windows symlinks can be greated only with Administrator privileges.");
|
_logger.LogInformation("On Windows symlinks can be created only with Administrator privileges.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +160,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, PathHelper.ResolvePath(moduleConfiguration.Links[relativePath], targetDirectory.FullName)));
|
filesToLink.Add(new ItemToLink(subDir.FullName, _pathHelperService.ResolvePath(moduleConfiguration.Links[relativePath], targetDirectory.FullName)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,17 +1,22 @@
|
|||||||
using Alma.Configuration.Repository;
|
using Alma.Configuration.Repository;
|
||||||
using Alma.Helper;
|
using Alma.Helper;
|
||||||
|
using Alma.Services;
|
||||||
|
|
||||||
namespace Alma.Command;
|
namespace Alma.Command;
|
||||||
|
|
||||||
public abstract class RepositoryModuleCommandBase : ICommand
|
public abstract class RepositoryModuleCommandBase : ICommand
|
||||||
{
|
{
|
||||||
private readonly IRepositoryConfiguration _repositoryConfiguration;
|
private readonly IRepositoryConfiguration _repositoryConfiguration;
|
||||||
|
private readonly IPathHelperService _pathHelperService;
|
||||||
public abstract string CommandString { get; }
|
public abstract string CommandString { get; }
|
||||||
public abstract Task Run(List<string> parameters);
|
public abstract Task Run(List<string> parameters);
|
||||||
|
|
||||||
protected RepositoryModuleCommandBase(IRepositoryConfiguration repositoryConfiguration)
|
protected RepositoryModuleCommandBase(
|
||||||
|
IRepositoryConfiguration repositoryConfiguration,
|
||||||
|
IPathHelperService pathHelperService)
|
||||||
{
|
{
|
||||||
_repositoryConfiguration = repositoryConfiguration;
|
_repositoryConfiguration = repositoryConfiguration;
|
||||||
|
_pathHelperService = pathHelperService;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected (string?, string?) GetRepositoryAndModuleName(List<string> parameters)
|
protected (string?, string?) GetRepositoryAndModuleName(List<string> parameters)
|
||||||
@@ -40,11 +45,11 @@ public abstract class RepositoryModuleCommandBase : ICommand
|
|||||||
{
|
{
|
||||||
fallbackSourceDirectory =
|
fallbackSourceDirectory =
|
||||||
repoConfig.RepositoryPath is { } repoPath
|
repoConfig.RepositoryPath is { } repoPath
|
||||||
? PathHelper.ResolvePath(repoPath)
|
? _pathHelperService.ResolvePath(repoPath)
|
||||||
: fallbackSourceDirectory;
|
: fallbackSourceDirectory;
|
||||||
fallbackTargetDirectory =
|
fallbackTargetDirectory =
|
||||||
repoConfig.LinkPath is { } linkPath
|
repoConfig.LinkPath is { } linkPath
|
||||||
? PathHelper.ResolvePath(linkPath)
|
? _pathHelperService.ResolvePath(linkPath)
|
||||||
: fallbackTargetDirectory;
|
: fallbackTargetDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
39
src/Alma.App/Services/PathHelperService.cs
Normal file
39
src/Alma.App/Services/PathHelperService.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using Alma.Data;
|
||||||
|
|
||||||
|
namespace Alma.Services;
|
||||||
|
|
||||||
|
public class PathHelperService : IPathHelperService
|
||||||
|
{
|
||||||
|
private static List<SpecialPathResolver> _specialPathResolvers = new()
|
||||||
|
{
|
||||||
|
new("~", () => Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), true),
|
||||||
|
new("%DOCUMENTS%", () => Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)),
|
||||||
|
};
|
||||||
|
|
||||||
|
public 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;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
foreach (var specialPathResolver in _specialPathResolvers)
|
||||||
|
{
|
||||||
|
if (path.Contains(specialPathResolver.PathName))
|
||||||
|
{
|
||||||
|
var parts = path.Split(specialPathResolver.PathName);
|
||||||
|
path = string.Join(specialPathResolver.Resolver(), parts);
|
||||||
|
skipCombiningCurrentDirectory = (specialPathResolver.SkipCombiningCurrentDirectory ?? false) || skipCombiningCurrentDirectory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: more special character
|
||||||
|
|
||||||
|
return currentDirectory is null || skipCombiningCurrentDirectory
|
||||||
|
? path
|
||||||
|
: Path.Combine(currentDirectory, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Alma.Command;
|
using Alma.Command;
|
||||||
|
using Alma.Command.Configure;
|
||||||
using Alma.Command.Help;
|
using Alma.Command.Help;
|
||||||
using Alma.Command.Info;
|
using Alma.Command.Info;
|
||||||
using Alma.Command.Install;
|
using Alma.Command.Install;
|
||||||
@@ -47,10 +48,12 @@ public static class Program
|
|||||||
[Singleton(typeof(ICommand), typeof(ListCommand))]
|
[Singleton(typeof(ICommand), typeof(ListCommand))]
|
||||||
[Singleton(typeof(ICommand), typeof(InstallCommand))]
|
[Singleton(typeof(ICommand), typeof(InstallCommand))]
|
||||||
[Singleton(typeof(ICommand), typeof(HelpCommand))]
|
[Singleton(typeof(ICommand), typeof(HelpCommand))]
|
||||||
|
[Singleton(typeof(ICommand), typeof(ConfigureCommand))]
|
||||||
[Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))]
|
[Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))]
|
||||||
[Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))]
|
[Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))]
|
||||||
[Singleton(typeof(IShellService), typeof(ShellService))]
|
[Singleton(typeof(IShellService), typeof(ShellService))]
|
||||||
[Singleton(typeof(IVersionService), typeof(VersionService))]
|
[Singleton(typeof(IVersionService), typeof(VersionService))]
|
||||||
|
[Singleton(typeof(IPathHelperService), typeof(PathHelperService))]
|
||||||
[Singleton(typeof(Application))]
|
[Singleton(typeof(Application))]
|
||||||
[Transient(typeof(ILogger<>), Factory = nameof(CustomLoggerFactory))]
|
[Transient(typeof(ILogger<>), Factory = nameof(CustomLoggerFactory))]
|
||||||
internal partial class AlmaServiceProvider
|
internal partial class AlmaServiceProvider
|
||||||
|
|||||||
Reference in New Issue
Block a user