Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotnet try publish #741

Merged
merged 17 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions MLS.Agent.Tools/FileSystemDirectoryAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,10 @@ public FileSystemInfo GetFullyQualifiedPath(RelativePath path)
throw new ArgumentNullException(nameof(path));
}

switch (path)
{
case RelativeFilePath file:
return new FileInfo(
_rootDirectory.Combine(file).FullName);
case RelativeDirectoryPath dir:
return new DirectoryInfo(
_rootDirectory.Combine(dir).FullName);
default:
throw new NotSupportedException($"{path.GetType()} is not supported.");
}
return path.Match<FileSystemInfo>(
directory => new DirectoryInfo(_rootDirectory.Combine(directory).FullName),
file => new FileInfo(_rootDirectory.Combine(file).FullName)
);
}

public IDirectoryAccessor GetDirectoryAccessorForRelativePath(RelativeDirectoryPath relativePath)
Expand Down
2 changes: 1 addition & 1 deletion MLS.Agent.Tools/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class RelativePath
{
private string _value;

protected RelativePath(string value)
protected internal RelativePath(string value)
ax0l0tl marked this conversation as resolved.
Show resolved Hide resolved
{
if (value == null)
{
Expand Down
14 changes: 14 additions & 0 deletions MLS.Agent.Tools/RelativePathExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

namespace MLS.Agent.Tools
Expand Down Expand Up @@ -33,5 +34,18 @@ public static DirectoryInfo Combine(
RelativePath.NormalizeDirectory(directory.FullName),
directoryPath.Value.Replace('/', Path.DirectorySeparatorChar)));
}

public static T Match<T>(this RelativePath path, Func<RelativeDirectoryPath, T> directory, Func<RelativeFilePath, T> file)
{
switch (path)
{
case RelativeDirectoryPath relativeDirectoryPath:
return directory(relativeDirectoryPath);
case RelativeFilePath relativeFilePath:
return file(relativeFilePath);
default:
throw new ArgumentOutOfRangeException($"Unexpected type derived from {nameof(RelativePath)}: {path.GetType().Name}");
}
}
}
}
34 changes: 34 additions & 0 deletions MLS.Agent/CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public delegate Task<int> Verify(
IConsole console,
StartupOptions startupOptions);

public delegate Task<int> Publish(
PublishOptions options,
IConsole console,
StartupOptions startupOptions);

public static Parser Create(
IServiceCollection services,
StartServer startServer = null,
Expand All @@ -61,6 +66,7 @@ public static Parser Create(
TryGitHub tryGithub = null,
Pack pack = null,
Verify verify = null,
Publish publish = null,
ITelemetry telemetry = null,
IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel = null)
{
Expand Down Expand Up @@ -88,6 +94,12 @@ public static Parser Create(
console,
startupOptions));

publish = publish ??
((options, console, startupOptions) =>
PublishCommand.Do(options,
console,
startupOptions));

pack = pack ??
PackCommand.Do;

Expand Down Expand Up @@ -132,6 +144,7 @@ public static Parser Create(
rootCommand.AddCommand(Install());
rootCommand.AddCommand(Pack());
rootCommand.AddCommand(Verify());
rootCommand.AddCommand(Publish());

return new CommandLineBuilder(rootCommand)
.UseDefaults()
Expand Down Expand Up @@ -435,6 +448,27 @@ Command Verify()

return verifyCommand;
}

Command Publish()
{
var publishCommand = new Command("publish", "Publish code from sample projects to Markdown files in the target directory and its children.")
{
new Option("--format", "Format of the files to publish")
{
Argument = new Argument<PublishFormat>()
},
new Option("--target-directory", "Specify the path where the rendered files should go. Can be equal to root directory. In case of Markdown output format and equal root and target directories, original source files will be replaced.")
{
Argument = new Argument<FileSystemDirectoryAccessor>(() => new FileSystemDirectoryAccessor(Directory.GetCurrentDirectory()))
Copy link
Contributor

@jonsequitur jonsequitur Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be Argument<DirectoryInfo>.

},
dirArgument
};

publishCommand.Handler = CommandHandler.Create<PublishOptions, IConsole, StartupOptions>(
(options, console, startupOptions) => publish(options, console, startupOptions));

return publishCommand;
}
}
}
}
46 changes: 46 additions & 0 deletions MLS.Agent/CommandLine/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using MLS.Agent.Tools;

namespace MLS.Agent.CommandLine
{
internal static class DirectoryExtensions
{
private static readonly RelativeDirectoryPath _here = new RelativeDirectoryPath("./");

public static bool IsChildOf(this FileSystemInfo file, IDirectoryAccessor directory)
{
var parent = directory.GetFullyQualifiedPath(_here).FullName;
var child = Path.GetDirectoryName(file.FullName);

child = child.EndsWith('/') || child.EndsWith('\\') ? child : child + "/";
return IsBaseOf(parent, child, selfIsChild: true);
}

public static bool IsSubDirectoryOf(this IDirectoryAccessor potentialChild, IDirectoryAccessor directory)
{
var child = potentialChild.GetFullyQualifiedPath(_here).FullName;
var parent = directory.GetFullyQualifiedPath(_here).FullName;
return IsBaseOf(parent, child, selfIsChild: false);
}

private static bool IsBaseOf(string parent, string child, bool selfIsChild)
{
var parentUri = new Uri(parent);
var childUri = new Uri(child);
return (selfIsChild || parentUri != childUri) && parentUri.IsBaseOf(childUri);
}

public static void EnsureDirectoryExists(this IDirectoryAccessor directoryAccessor, RelativePath path)
{
var relativeDirectoryPath = path.Match(
directory => directory,
file => file.Directory
);
directoryAccessor.EnsureDirectoryExists(relativeDirectoryPath);
}
}
}
126 changes: 126 additions & 0 deletions MLS.Agent/CommandLine/PublishCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
ax0l0tl marked this conversation as resolved.
Show resolved Hide resolved
using System.CommandLine;
using System.CommandLine.IO;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Markdig;
using Markdig.Renderers;
using Markdig.Renderers.Normalize;
using Markdig.Syntax;
using Microsoft.DotNet.Try.Markdown;
using MLS.Agent.Markdown;
using MLS.Agent.Tools;
using WorkspaceServer;

namespace MLS.Agent.CommandLine
{
public static class PublishCommand
{
public static async Task<int> Do(
PublishOptions publishOptions,
IConsole console,
StartupOptions startupOptions = null
)
{
var sourceDirectoryAccessor = publishOptions.RootDirectory;
var packageRegistry = PackageRegistry.CreateForTryMode(sourceDirectoryAccessor);
var markdownProject = new MarkdownProject(
sourceDirectoryAccessor,
packageRegistry,
startupOptions);

var markdownFiles = markdownProject.GetAllMarkdownFiles().ToArray();
if (markdownFiles.Length == 0)
{
console.Error.WriteLine($"No markdown files found under {sourceDirectoryAccessor.GetFullyQualifiedRoot()}");
return -1;
}

var targetDirectoryAccessor = publishOptions.TargetDirectory;
var targetIsSubDirectoryOfSource = targetDirectoryAccessor.IsSubDirectoryOf(sourceDirectoryAccessor);

foreach (var markdownFile in markdownFiles)
{
var markdownFilePath = markdownFile.Path;
var fullSourcePath = sourceDirectoryAccessor.GetFullyQualifiedPath(markdownFilePath);
if (targetIsSubDirectoryOfSource && fullSourcePath.IsChildOf(targetDirectoryAccessor))
continue;

var document = ParseMarkdownDocument(markdownFile);

var rendered = await Render(publishOptions.Format, document);

var targetPath = WriteTargetFile(rendered, markdownFilePath, targetDirectoryAccessor, publishOptions);

console.Out.WriteLine($"Published '{fullSourcePath}' to {targetPath}");
}

return 0;
}

private static string WriteTargetFile(string content, RelativeFilePath relativePath,
IDirectoryAccessor targetDirectoryAccessor, PublishOptions publishOptions)
{
var fullyQualifiedPath = targetDirectoryAccessor.GetFullyQualifiedPath(relativePath);
targetDirectoryAccessor.EnsureDirectoryExists(relativePath);
var targetPath = fullyQualifiedPath.FullName;
if (publishOptions.Format == PublishFormat.HTML)
targetPath = Path.ChangeExtension(targetPath, ".html");
File.WriteAllText(targetPath, content);
return targetPath;
}

private static async Task<string> Render(PublishFormat format, MarkdownDocument document)
{
MarkdownPipeline pipeline;
IMarkdownRenderer renderer;
var writer = new StringWriter();
switch (format)
{
case PublishFormat.Markdown:
pipeline = new MarkdownPipelineBuilder()
.UseNormalizeCodeBlockAnnotations()
.Build();
renderer = new NormalizeRenderer(writer);
break;
case PublishFormat.HTML:
pipeline = new MarkdownPipelineBuilder()
.UseCodeBlockAnnotations(inlineControls: false)
.Build();
renderer = new HtmlRenderer(writer);
break;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}

pipeline.Setup(renderer);

var blocks = document
.OfType<AnnotatedCodeBlock>()
.OrderBy(c => c.Order)
.ToList();

await Task.WhenAll(blocks.Select(b => b.InitializeAsync()));

renderer.Render(document);
writer.Flush();

var rendered = writer.ToString();
return rendered;
}

private static MarkdownDocument ParseMarkdownDocument(MarkdownFile markdownFile)
{
var pipeline = markdownFile.Project.GetMarkdownPipelineFor(markdownFile.Path);

var document = Markdig.Markdown.Parse(
markdownFile.ReadAllText(),
pipeline);
return document;
}
}
}
11 changes: 11 additions & 0 deletions MLS.Agent/CommandLine/PublishFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace MLS.Agent.CommandLine
{
public enum PublishFormat
{
Markdown,
HTML
}
}
23 changes: 23 additions & 0 deletions MLS.Agent/CommandLine/PublishOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using MLS.Agent.Tools;
ax0l0tl marked this conversation as resolved.
Show resolved Hide resolved

namespace MLS.Agent.CommandLine
{
public class PublishOptions
{
public PublishOptions(IDirectoryAccessor rootDirectory, IDirectoryAccessor targetDirectory, PublishFormat format)
{
RootDirectory = rootDirectory ?? throw new System.ArgumentNullException(nameof(rootDirectory));
Format = format;
TargetDirectory = targetDirectory;
}

public IDirectoryAccessor RootDirectory { get; }

public IDirectoryAccessor TargetDirectory { get; }

public PublishFormat Format { get; }
}
}
8 changes: 8 additions & 0 deletions MLS.Agent/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
"Jupyter": {
"commandName": "Project",
"commandLineArgs": "../MLS.Agent.Tests/kernel_connection_file.json"
},
"Verify": {
"commandName": "Project",
"commandLineArgs": "verify \"..\\Samples\""
},
"Publish": {
"commandName": "Project",
"commandLineArgs": "publish \"..\\Samples\" --format Markdown --target-directory \"..\\Samples\\published\""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Markdig;

namespace Microsoft.DotNet.Try.Markdown
{
public static class MarkdownNormalizePipelineBuilderExtensions
{
public static MarkdownPipelineBuilder UseNormalizeCodeBlockAnnotations(
this MarkdownPipelineBuilder pipeline)
{
var extensions = pipeline.Extensions;

if (!extensions.Contains<NormalizeBlockAnnotationExtension>())
{
extensions.Add(new NormalizeBlockAnnotationExtension());
}

if (!extensions.Contains<SkipEmptyLinkReferencesExtension>())
{
extensions.Add(new SkipEmptyLinkReferencesExtension());
}

return pipeline;
}
}
}
Loading