Skip to content

Commit

Permalink
VCI-102: Add help command (#15)
Browse files Browse the repository at this point in the history
* VCI-102: Add help command

* VCI-102: Fix codesmells

* VCI-102: Make HelpProvider static

* VCI-129: Make help search case insensitive
  • Loading branch information
krankenbro authored Aug 19, 2021
1 parent 75af6eb commit 72d9ac3
Show file tree
Hide file tree
Showing 8 changed files with 1,329 additions and 1,286 deletions.
1,604 changes: 812 additions & 792 deletions Build.cs

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System.ComponentModel;
using Nuke.Common.Tooling;

[TypeConverter(typeof(TypeConverter<Configuration>))]
public class Configuration : Enumeration
namespace VirtoCommerce.Build
{
public readonly static Configuration Debug = new Configuration { Value = nameof(Debug) };
public readonly static Configuration Release = new Configuration { Value = nameof(Release) };

public static implicit operator string(Configuration configuration)
[TypeConverter(typeof(TypeConverter<Configuration>))]
public class Configuration : Enumeration
{
return configuration.Value;
public readonly static Configuration Debug = new Configuration { Value = nameof(Debug) };
public readonly static Configuration Release = new Configuration { Value = nameof(Release) };

public static implicit operator string(Configuration configuration)
{
return configuration.Value;
}
}
}
21 changes: 12 additions & 9 deletions GrabMigrator/Build.GrabMigrator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using Nuke.Common;

internal partial class Build
namespace VirtoCommerce.Build
{
[Parameter("Grab-migrator config file")]
public static string GrabMigratorConfig { get; set; }
internal partial class Build
{
[Parameter("Grab-migrator config file")]
public static string GrabMigratorConfig { get; set; }

public Target GrabMigrator => _ => _
.Requires(() => GrabMigratorConfig)
.Executes(() =>
{
new GrabMigrator.GrabMigrator().Do(GrabMigratorConfig);
});
public Target GrabMigrator => _ => _
.Requires(() => GrabMigratorConfig)
.Executes(() =>
{
new GrabMigrator.GrabMigrator().Do(GrabMigratorConfig);
});
}
}
117 changes: 0 additions & 117 deletions HelpProvider/Build.HelpProvider.cs

This file was deleted.

127 changes: 127 additions & 0 deletions HelpProvider/HelpProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Markdig;
using Markdig.Extensions.CustomContainers;
using Markdig.Helpers;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using Nuke.Common;

namespace VirtoCommerce.Build.HelpProvider
{
public static class HelpProvider
{
public static string GetHelpForTarget(string target)
{
var containers = GetCustomContainers();

var container = containers.FirstOrDefault(c =>
{
var heading = c.Descendants<HeadingBlock>().FirstOrDefault();

if (heading == null)
{
return false;
}

return string.Compare(target, GetTextContent(heading), true) == 0;
});

if (container == null)
{
Logger.Error($"Help is not found for the target {target}");
return string.Empty;
}

var descriptionBlock = container.Descendants<ParagraphBlock>().FirstOrDefault();
var exampleBlock = container.Descendants<FencedCodeBlock>().FirstOrDefault();
var description = GetTextContent(descriptionBlock);
var examples = GetFencedText(exampleBlock);
var result = $"{description}{Environment.NewLine}{examples}";
return result;
}

public static IEnumerable<string> GetTargets()
{
var containers = GetCustomContainers();
var result = new List<string>();
foreach (var container in containers)
{
var heading = container.Descendants<HeadingBlock>().FirstOrDefault();

if (heading == null)
{
continue;
}
result.Add(GetTextContent(heading));
}
return result;
}

private static IEnumerable<CustomContainer> GetCustomContainers()
{
var pipeline = new MarkdownPipelineBuilder().UseCustomContainers().Build();
var rootDirectory = AppDomain.CurrentDomain.BaseDirectory;
var helpFilePath = Path.Combine(rootDirectory, "docs", "targets.md");
var markdownDocument = Markdown.Parse(File.ReadAllText(helpFilePath), pipeline);
return markdownDocument.Descendants<CustomContainer>();
}

private static string GetTextContent(LeafBlock leaf)
{
var inline = leaf?.Inline?.FirstChild;

if (inline is null)
{
return string.Empty;
}

var result = new StringBuilder();

do
{
switch (inline)
{
case LiteralInline literal:
var inlineContent = literal.Content;
result.Append(inlineContent.Text.Substring(inlineContent.Start, inlineContent.Length));
break;

case LineBreakInline:
result.Append(Environment.NewLine);
break;
}

inline = inline.NextSibling;
}
while (inline != null);

return result.ToString();
}

private static string GetFencedText(FencedCodeBlock fencedCodeBlock)
{
if (fencedCodeBlock == null)
{
return string.Empty;
}

var lines = fencedCodeBlock.Lines.Lines.Select(line =>
{
var slice = line.Slice;

if (EqualityComparer<StringSlice>.Default.Equals(slice, default))
{
return string.Empty;
}

return slice.Text?.Substring(line.Slice.Start, line.Slice.Length) ?? string.Empty;
});

return string.Join(Environment.NewLine, lines);
}
}
}
47 changes: 25 additions & 22 deletions PlatformTools/Build.InitPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@
using VirtoCommerce.Platform.DistributedLock;
using VirtoCommerce.Platform.Modules;

internal partial class Build
namespace VirtoCommerce.Build
{
[Parameter("Modules discovery path")]
public static string DiscoveryPath { get; set; }
internal partial class Build
{
[Parameter("Modules discovery path")]
public static string DiscoveryPath { get; set; }

[Parameter("Probing path")]
public static string ProbingPath { get; set; } = RootDirectory / "app_data" / "modules";
[Parameter("Probing path")]
public static string ProbingPath { get; set; } = RootDirectory / "app_data" / "modules";

[Parameter("appsettings.json path")]
public static string AppsettingsPath { get; set; } = RootDirectory / "appsettings.json";
[Parameter("appsettings.json path")]
public static string AppsettingsPath { get; set; } = RootDirectory / "appsettings.json";

public Target InitPlatform => _ => _
.Executes(() =>
{
var configuration = AppSettings.GetConfiguration(RootDirectory, AppsettingsPath);

var moduleCatalogOptions = new LocalStorageModuleCatalogOptions
public Target InitPlatform => _ => _
.Executes(() =>
{
DiscoveryPath = DiscoveryPath.EmptyToNull() ?? configuration.GetModulesDiscoveryPath(),
ProbingPath = ProbingPath,
};
var configuration = AppSettings.GetConfiguration(RootDirectory, AppsettingsPath);

var moduleCatalogOptions = new LocalStorageModuleCatalogOptions
{
DiscoveryPath = DiscoveryPath.EmptyToNull() ?? configuration.GetModulesDiscoveryPath(),
ProbingPath = ProbingPath,
};

var options = Options.Create(moduleCatalogOptions);
var logger = new LoggerFactory().CreateLogger<LocalStorageModuleCatalog>();
var distributedLock = new NoLockDistributedLockProvider(new LoggerFactory().CreateLogger<NoLockDistributedLockProvider>());
var moduleCatalog = new LocalStorageModuleCatalog(options, distributedLock, logger);
moduleCatalog.Load();
});
var options = Options.Create(moduleCatalogOptions);
var logger = new LoggerFactory().CreateLogger<LocalStorageModuleCatalog>();
var distributedLock = new NoLockDistributedLockProvider(new LoggerFactory().CreateLogger<NoLockDistributedLockProvider>());
var moduleCatalog = new LocalStorageModuleCatalog(options, distributedLock, logger);
moduleCatalog.Load();
});
}
}
Loading

0 comments on commit 72d9ac3

Please sign in to comment.