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 1 commit
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
70 changes: 70 additions & 0 deletions MLS.Agent.Tests/CommandLine/PublishCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.CommandLine.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Options;
using MLS.Agent.CommandLine;
using MLS.Agent.Tools;
using MLS.Agent.Tools.Tests;
using WorkspaceServer.Tests;
using Xunit;
using Xunit.Abstractions;

namespace MLS.Agent.Tests.CommandLine
{
public class PublishCommandTests
{
private const string CsprojContents = @"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>
";

public class WithMarkdownOutputFormat
{
private static PublishOptions Options(IDirectoryAccessor source, IDirectoryAccessor target = null) => new PublishOptions(source, target ?? source, PublishFormat.Markdown);

private readonly ITestOutputHelper _output;

public WithMarkdownOutputFormat(ITestOutputHelper output) => _output = output;

[Theory]
[InlineData("##Title")]
[InlineData("markdown with line \r\n break")]
[InlineData("markdown with linux line \n break")]
[InlineData("[link](https://try.dot.net/)")]
public async Task When_there_are_no_code_fence_annotations_markdown_is_unchanged(string markdown)
{
var rootDirectory = Create.EmptyWorkspace(isRebuildablePackage: true).Directory;

var directoryAccessor = new InMemoryDirectoryAccessor(rootDirectory)
{
("doc.md", markdown)
}.CreateFiles();


var targetDirectory = await DoPublish(directoryAccessor);

var files = targetDirectory.GetAllFilesRecursively().ToList();
ax0l0tl marked this conversation as resolved.
Show resolved Hide resolved
}

private async Task<InMemoryDirectoryAccessor> DoPublish(IDirectoryAccessor directoryAccessor, IDirectoryAccessor target = null)
ax0l0tl marked this conversation as resolved.
Show resolved Hide resolved
{
var console = new TestConsole();
var targetDirectory = new InMemoryDirectoryAccessor();

void WriteOutput(string path, string content) => targetDirectory.Add((path, content));

var resultCode = await PublishCommand.Do(Options(directoryAccessor), console, writeOutput: WriteOutput);

resultCode.Should().Be(0);

_output.WriteLine(console.Out.ToString());
return targetDirectory;
}
}

}
}
13 changes: 9 additions & 4 deletions MLS.Agent/CommandLine/PublishCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ namespace MLS.Agent.CommandLine
{
public static class PublishCommand
{
public delegate void WriteOutput(string path, string content);

public static async Task<int> Do(
PublishOptions publishOptions,
IConsole console,
StartupOptions startupOptions = null
StartupOptions startupOptions = null,
WriteOutput writeOutput = null
)
{
writeOutput ??= (path, content) => File.WriteAllText(path, content);

var sourceDirectoryAccessor = publishOptions.RootDirectory;
var packageRegistry = PackageRegistry.CreateForTryMode(sourceDirectoryAccessor);
var markdownProject = new MarkdownProject(
Expand Down Expand Up @@ -54,7 +59,7 @@ public static async Task<int> Do(

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

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

console.Out.WriteLine($"Published '{fullSourcePath}' to {targetPath}");
}
Expand All @@ -63,14 +68,14 @@ public static async Task<int> Do(
}

private static string WriteTargetFile(string content, RelativeFilePath relativePath,
IDirectoryAccessor targetDirectoryAccessor, PublishOptions publishOptions)
IDirectoryAccessor targetDirectoryAccessor, PublishOptions publishOptions, WriteOutput writeOutput)
{
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);
writeOutput(targetPath, content);
return targetPath;
}

Expand Down
6 changes: 6 additions & 0 deletions dotnet-try.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=4F05FBA2B784B84FA04B3CFF2DA88B3E/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=4F05FBA2B784B84FA04B3CFF2DA88B3E/AbsolutePath/@EntryValue">G:\Projects\try\dotnet-try.sln.DotSettings</s:String>
<s:String x:Key="/Default/Environment/InjectedLayers/FileInjectedLayer/=4F05FBA2B784B84FA04B3CFF2DA88B3E/RelativePath/@EntryValue"></s:String>
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File4F05FBA2B784B84FA04B3CFF2DA88B3E/@KeyIndexDefined">True</s:Boolean>
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=File4F05FBA2B784B84FA04B3CFF2DA88B3E/RelativePriority/@EntryValue">1</s:Double>
<s:Boolean x:Key="/Default/UserDictionary/Words/=blazor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bnet/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jupyter/@EntryIndexedValue">True</s:Boolean>
Expand Down