Link exclusion, readme exclusion

This commit is contained in:
2024-03-01 23:29:43 +01:00
parent f1b2495fb6
commit 89f1c37cf6
5 changed files with 114 additions and 32 deletions

View File

@@ -4,6 +4,8 @@ public class ModuleConfiguration
{ {
public string? Target { get; set; } public string? Target { get; set; }
public Dictionary<string, string>? Links { get; set; } public Dictionary<string, string>? Links { get; set; }
public List<string>? Exclude { get; set; }
public bool ExcludeReadme { get; set; } = true;
public string? Install { get; set; } public string? Install { get; set; }
public string? Configure { get; set; } public string? Configure { get; set; }

View File

@@ -3,6 +3,7 @@ using Alma.Configuration.Module;
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Data; using Alma.Data;
using Alma.Logging; using Alma.Logging;
using Alma.Models;
using Alma.Services; using Alma.Services;
namespace Alma.Command.Link; namespace Alma.Command.Link;
@@ -35,6 +36,24 @@ public class LinkCommand : RepositoryModuleCommandBase
public override async Task Run(List<string> parameters) public override async Task Run(List<string> parameters)
{ {
if (parameters.Contains("--help"))
{
_logger.LogInformation(
"""
Usage:
alma link [module]
alma link [repository] [module]
Options:
--help Show this message
-d, --dry-run Show what would be linked without actually linking
"""
);
return;
}
var dryRun = parameters.Contains("-d") || parameters.Contains("--dry-run");
var (repoName, moduleName) = GetRepositoryAndModuleName(parameters); var (repoName, moduleName) = GetRepositoryAndModuleName(parameters);
if (moduleName is null) if (moduleName is null)
{ {
@@ -82,14 +101,46 @@ public class LinkCommand : RepositoryModuleCommandBase
moduleDir, moduleDir,
currentTargetDirectory, currentTargetDirectory,
moduleConfiguration)).ToList(); moduleConfiguration)).ToList();
// Exclude
if (moduleConfigurationFile is not null) itemsToLink.RemoveAll(i => i.SourcePath == moduleConfigurationFileFullPath); if (moduleConfigurationFile is not null) itemsToLink.RemoveAll(i => i.SourcePath == moduleConfigurationFileFullPath);
var successfulLinks = CreateLinks(itemsToLink); if (moduleConfiguration?.Exclude is { } excludeList)
{
foreach (var itemToExclude in excludeList)
{
var excludePath = Path.Combine(moduleDirectory, Path.Combine(itemToExclude.Split('/')));
itemsToLink.RemoveAll(
i => i.SourcePath == excludePath
|| i.SourcePath.StartsWith(excludePath + Path.DirectorySeparatorChar)
);
}
}
if(moduleConfiguration?.ExcludeReadme ?? false)
{
foreach (var readmeFile in Enum.GetValues<ReadmeFiles>())
{
var readmeFilePath = Path.Combine(moduleDirectory, readmeFile.GetFileName());
itemsToLink.RemoveAll(i => i.SourcePath == readmeFilePath);
}
}
// Linking
if (dryRun)
{
_logger.LogInformation("Dry run. No links will be created. The following links would be created:");
}
var successfulLinks = CreateLinks(itemsToLink, dryRun);
if (dryRun) return;
await _metadataHandler.SaveLinkedItemsAsync(successfulLinks, moduleDir, currentTargetDirectory); await _metadataHandler.SaveLinkedItemsAsync(successfulLinks, moduleDir, currentTargetDirectory);
} }
private List<ItemToLink> CreateLinks(List<ItemToLink> itemsToLink) private List<ItemToLink> CreateLinks(List<ItemToLink> itemsToLink, bool dryRun)
{ {
var successfulLinks = new List<ItemToLink>(); var successfulLinks = new List<ItemToLink>();
@@ -108,6 +159,8 @@ public class LinkCommand : RepositoryModuleCommandBase
_logger.LogInformation($"Linking: '{itemToLink.SourcePath}' '{itemToLink.TargetPath}'"); _logger.LogInformation($"Linking: '{itemToLink.SourcePath}' '{itemToLink.TargetPath}'");
if (!dryRun)
{
if (sourceFileExists) if (sourceFileExists)
{ {
File.CreateSymbolicLink(itemToLink.TargetPath, itemToLink.SourcePath); File.CreateSymbolicLink(itemToLink.TargetPath, itemToLink.SourcePath);
@@ -121,6 +174,7 @@ public class LinkCommand : RepositoryModuleCommandBase
_logger.LogInformation("Source not exists: " + itemToLink.SourcePath); _logger.LogInformation("Source not exists: " + itemToLink.SourcePath);
continue; continue;
} }
}
successfulLinks.Add(itemToLink); successfulLinks.Add(itemToLink);
} }

View File

@@ -3,6 +3,7 @@
using Alma.Command.Install; using Alma.Command.Install;
using Alma.Configuration.Repository; using Alma.Configuration.Repository;
using Alma.Logging; using Alma.Logging;
using Alma.Models;
using Alma.Services; using Alma.Services;
namespace Alma.Command.List; namespace Alma.Command.List;
@@ -15,6 +16,8 @@ public class ReadMeCommand : RepositoryModuleCommandBase
public override string[] CommandAliases => Array.Empty<string>(); public override string[] CommandAliases => Array.Empty<string>();
private readonly Dictionary<ReadmeFiles, Func<string, Task>> _readmeFilePrinters;
public ReadMeCommand( public ReadMeCommand(
ILogger<InstallCommand> logger, ILogger<InstallCommand> logger,
IRepositoryConfiguration repositoryConfiguration, IRepositoryConfiguration repositoryConfiguration,
@@ -23,6 +26,13 @@ public class ReadMeCommand : RepositoryModuleCommandBase
: base(repositoryConfiguration, pathHelperService, moduleConfigurationResolver) : base(repositoryConfiguration, pathHelperService, moduleConfigurationResolver)
{ {
_logger = logger; _logger = logger;
_readmeFilePrinters = new Dictionary<ReadmeFiles, Func<string, Task>>
{
{ ReadmeFiles.Markdown, PrintReadMeMd },
{ ReadmeFiles.Text, PrintReadMeText },
{ ReadmeFiles.NoExtension, PrintReadMeText },
};
} }
public override async Task Run(List<string> parameters) public override async Task Run(List<string> parameters)
@@ -42,23 +52,17 @@ public class ReadMeCommand : RepositoryModuleCommandBase
} }
var fileFound = false; var fileFound = false;
var readmeMdPath = Path.Combine(repoSource, moduleName, "README.md");
var readmeTxtPath = Path.Combine(repoSource, moduleName, "README.md"); foreach (var readmeFile in _readmeFilePrinters.Keys)
var readmePath = Path.Combine(repoSource, moduleName, "README.md"); {
if(File.Exists(readmeMdPath)) // TODO: make this case insensitive
var readmeFilePath = Path.Combine(repoSource, moduleName, readmeFile.ToString());
if (File.Exists(readmeFilePath))
{ {
fileFound = true; fileFound = true;
await PrintReadMeMd(readmeMdPath); await _readmeFilePrinters[readmeFile](readmeFilePath);
break;
} }
else if(File.Exists(readmeTxtPath))
{
fileFound = true;
await PrintReadMeText(readmeMdPath);
}
else if(File.Exists(readmePath))
{
fileFound = true;
await PrintReadMeText(readmePath);
} }
if (!fileFound) if (!fileFound)

View File

@@ -41,6 +41,8 @@ public abstract class RepositoryModuleCommandBase : ICommand
string? repositoryName = null; string? repositoryName = null;
string? moduleName = null; string? moduleName = null;
parameters = parameters.Where(p => !p.StartsWith("-")).ToList();
if (parameters.Count == 1) if (parameters.Count == 1)
{ {
if (singleParamIsRepo) if (singleParamIsRepo)

View File

@@ -0,0 +1,20 @@
namespace Alma.Models;
public enum ReadmeFiles
{
Markdown,
Text,
NoExtension
}
public static class ReadmeFileTypeExtensions
{
public static string GetFileName(this ReadmeFiles type)
=> type switch
{
ReadmeFiles.Markdown => "README.md",
ReadmeFiles.Text => "README.txt",
ReadmeFiles.NoExtension => "README",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}