Fixed exceptions and added constraints to commands
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
namespace MycroForge.CLI.Commands.Attributes;
|
||||
|
||||
public class RequiresFeatureAttribute : Attribute
|
||||
{
|
||||
public string FeatureName { get; }
|
||||
|
||||
public RequiresFeatureAttribute(string featureName)
|
||||
{
|
||||
FeatureName = featureName;
|
||||
}
|
||||
|
||||
public void RequireFeature(string command)
|
||||
{
|
||||
if (!Directory.Exists(Path.Join(Environment.CurrentDirectory, FeatureName)))
|
||||
throw new($"Command '{command}' requires feature {FeatureName}");
|
||||
}
|
||||
}
|
||||
17
MycroForge.CLI/Commands/Attributes/RequiresFileAttribute.cs
Normal file
17
MycroForge.CLI/Commands/Attributes/RequiresFileAttribute.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace MycroForge.CLI.Commands.Attributes;
|
||||
|
||||
public class RequiresFileAttribute : Attribute
|
||||
{
|
||||
public string FilePath { get; }
|
||||
|
||||
public RequiresFileAttribute(string filePath)
|
||||
{
|
||||
FilePath = filePath;
|
||||
}
|
||||
|
||||
public void RequireFile(string command)
|
||||
{
|
||||
if (!File.Exists(Path.Join(Environment.CurrentDirectory, FilePath)))
|
||||
throw new($"Command '{command}' requires file {FilePath}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.FileSystemGlobbing;
|
||||
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
|
||||
|
||||
namespace MycroForge.CLI.Commands.Attributes;
|
||||
|
||||
public class RequiresPluginAttribute : Attribute
|
||||
{
|
||||
public void RequirePluginProject(string command)
|
||||
{
|
||||
var matcher = new Matcher()
|
||||
.AddInclude("*.csproj")
|
||||
.Execute(new DirectoryInfoWrapper(new DirectoryInfo(Environment.CurrentDirectory)));
|
||||
|
||||
if (!matcher.HasMatches)
|
||||
throw new($"Command '{command}' must be run in a command plugin project.");
|
||||
|
||||
var csprojFileName = $"{Path.GetDirectoryName(Environment.CurrentDirectory)}.csproj";
|
||||
bool IsCsprojFile(FilePatternMatch file) => Path.GetFileNameWithoutExtension(file.Path).Equals(csprojFileName);
|
||||
var hasCsprojFile = matcher.Files.Any(IsCsprojFile);
|
||||
|
||||
if (!hasCsprojFile)
|
||||
throw new($"File '{csprojFileName}' was not found, make sure you're in a command plugin project.");
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
using System.CommandLine;
|
||||
using MycroForge.CLI.Commands.Attributes;
|
||||
using MycroForge.Core.Contract;
|
||||
|
||||
namespace MycroForge.CLI.Commands;
|
||||
|
||||
public partial class MycroForge
|
||||
{
|
||||
[RequiresFeature(Features.Api.FeatureName)]
|
||||
public partial class Api : Command, ISubCommandOf<MycroForge>
|
||||
{
|
||||
public Api(IEnumerable<ISubCommandOf<Api>> commands) :
|
||||
public Api(IEnumerable<ISubCommandOf<Api>> commands) :
|
||||
base("api", "API related commands")
|
||||
{
|
||||
foreach (var command in commands)
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using System.CommandLine;
|
||||
using MycroForge.CLI.Commands.Attributes;
|
||||
using MycroForge.Core.Contract;
|
||||
|
||||
namespace MycroForge.CLI.Commands;
|
||||
|
||||
public partial class MycroForge
|
||||
{
|
||||
[RequiresFeature(Features.Db.FeatureName)]
|
||||
public partial class Db : Command, ISubCommandOf<MycroForge>
|
||||
{
|
||||
public Db(IEnumerable<ISubCommandOf<Db>> commands)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.CommandLine;
|
||||
using MycroForge.CLI.Commands.Attributes;
|
||||
using MycroForge.Core;
|
||||
using MycroForge.Core.Contract;
|
||||
|
||||
@@ -6,11 +7,12 @@ namespace MycroForge.CLI.Commands;
|
||||
|
||||
public partial class MycroForge
|
||||
{
|
||||
[RequiresFile("requirements.txt")]
|
||||
public class Hydrate : Command, ISubCommandOf<MycroForge>
|
||||
{
|
||||
private readonly ProjectContext _context;
|
||||
|
||||
public Hydrate(ProjectContext context)
|
||||
|
||||
public Hydrate(ProjectContext context)
|
||||
: base("hydrate", "Initialize venv and install dependencies from requirements.txt")
|
||||
{
|
||||
_context = context;
|
||||
|
||||
@@ -28,7 +28,7 @@ public partial class MycroForge
|
||||
|
||||
if (packs.Length == 0)
|
||||
{
|
||||
Console.WriteLine("m4g install requires at least one package.");
|
||||
Console.WriteLine("'m4g install' requires at least one package.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Humanizer;
|
||||
using Microsoft.Extensions.FileSystemGlobbing;
|
||||
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
|
||||
using MycroForge.CLI.Commands.Attributes;
|
||||
using MycroForge.Core;
|
||||
using MycroForge.Core.Contract;
|
||||
|
||||
@@ -11,6 +12,7 @@ public partial class MycroForge
|
||||
{
|
||||
public partial class Plugin
|
||||
{
|
||||
[RequiresPlugin]
|
||||
public class Install : Command, ISubCommandOf<Plugin>
|
||||
{
|
||||
public enum TargetPlatform
|
||||
|
||||
@@ -27,9 +27,17 @@ public partial class MycroForge
|
||||
|
||||
private async Task ExecuteAsync(IEnumerable<string> packages, bool yes)
|
||||
{
|
||||
var packs = packages.ToArray();
|
||||
|
||||
if (packs.Length == 0)
|
||||
{
|
||||
Console.WriteLine("'m4g uninstall' requires at least one package.");
|
||||
return;
|
||||
}
|
||||
|
||||
await _context.Bash(
|
||||
"source .venv/bin/activate",
|
||||
$"pip uninstall{(yes ? " --yes " : " ")}{string.Join(' ', packages)}",
|
||||
$"pip uninstall{(yes ? " --yes " : " ")}{string.Join(' ', packs)}",
|
||||
"pip freeze > requirements.txt"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user