Skip to content

Commit

Permalink
refactor: Extract gitree configuration from Project Model (#126)
Browse files Browse the repository at this point in the history
* refactor: Remove ProjectConfiguration, use C#9 'record'

- cleanup ProjectModel by removing all ProjectConfiguration
elements and moving those necessary to Gitree project
- use C#9 records across ProjectModel and Workspaces
- remove unncessary package references
- remove Gitree support from Publish command in CliTool

* fix: Suppress weird warnings

* style: Fix whitespace issue
  • Loading branch information
amis92 committed Oct 12, 2020
1 parent 7cb8145 commit 7d62279
Show file tree
Hide file tree
Showing 40 changed files with 331 additions and 575 deletions.
2 changes: 0 additions & 2 deletions Packages.props
Expand Up @@ -2,7 +2,6 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemGroup>
<PackageReference Update="Amadevus.RecordGenerator" Version="[0.6.0]" PrivateAssets="all" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-3.final" PrivateAssets="all" />
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" />
<PackageReference Update="morelinq" Version="3.2.0" />
Expand Down Expand Up @@ -31,7 +30,6 @@
Condition=" !$(IsTestingOnlyProject) " />
<GlobalPackageReference Include="Nerdbank.GitVersioning"
Version="3.2.31" />
<GlobalPackageReference Include="Nullable" Version="1.2.1" IncludeAssets="contentFiles" />
</ItemGroup>

</Project>
32 changes: 12 additions & 20 deletions src/WarHub.ArmouryModel.CliTool/Commands/ConvertXmlCommand.cs
@@ -1,7 +1,6 @@
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
using WarHub.ArmouryModel.ProjectModel;
using WarHub.ArmouryModel.Source;
using WarHub.ArmouryModel.Workspaces.BattleScribe;
using WarHub.ArmouryModel.Workspaces.Gitree;
Expand All @@ -25,14 +24,14 @@ public async Task RunAsync(DirectoryInfo source, DirectoryInfo output, string ve
await ConvertFilesAsync(configInfo, workspace);
}

private async Task ConvertFilesAsync(ProjectConfigurationInfo configInfo, XmlWorkspace workspace)
private async Task ConvertFilesAsync(GitreeWorkspaceOptions gitreeOptions, XmlWorkspace workspace)
{
var treeWriter = new GitreeWriter();
foreach (var document in workspace.GetDocuments(SourceKind.Gamesystem, SourceKind.Catalogue))
{
var sourceKind = document.Kind.GetSourceKindOrUnknown();
var filenameNoExt = Path.GetFileNameWithoutExtension(document.Filepath);
var folderPath = Path.Combine(configInfo.GetFullPath(sourceKind), filenameNoExt);
var folderPath = Path.Combine(gitreeOptions.GetFullPath(sourceKind), filenameNoExt);
var folder = Directory.CreateDirectory(folderPath);
Log.Information("Converting file {Name} into {Folder}", filenameNoExt, folder);
Log.Verbose("- Reading...");
Expand All @@ -45,11 +44,17 @@ private async Task ConvertFilesAsync(ProjectConfigurationInfo configInfo, XmlWor
}
}

private static ProjectConfigurationInfo CreateDestinationProjectConfig(DirectoryInfo sourceDir, DirectoryInfo destDir)
private static GitreeWorkspaceOptions CreateDestinationProjectConfig(DirectoryInfo sourceDir, DirectoryInfo destDir)
{
var configInfo = new ConvertedGitreeProjectConfigurationProvider().Create(sourceDir.FullName);
var destFilepath = Path.Combine(destDir.FullName, Path.GetFileName(configInfo.Filepath));
return configInfo.WithFilepath(destFilepath);
var options = GitreeWorkspaceOptions.Create(sourceDir.FullName);
var destFilepath = Path.Combine(destDir.FullName, Path.GetFileName(options.Filepath));
return options with
{
Filepath = destFilepath,
SourceDirectories = ImmutableArray.Create(
new GitreeSourceFolder(GitreeSourceFolderKind.Catalogues, "src/catalogues"),
new GitreeSourceFolder(GitreeSourceFolderKind.Gamesystems, "src/gamesystems"))
};
}

private XmlWorkspace CreateXmlWorkspace(DirectoryInfo sourceDir)
Expand All @@ -67,18 +72,5 @@ private XmlWorkspace CreateXmlWorkspace(DirectoryInfo sourceDir)

return workspace;
}

private class ConvertedGitreeProjectConfigurationProvider : GitreeProjectConfigurationProvider
{
protected override ImmutableArray<SourceFolder> DefaultDirectoryReferences { get; } =
ImmutableArray.Create(
new SourceFolder(SourceFolderKind.Catalogues, "src/catalogues"),
new SourceFolder(SourceFolderKind.Gamesystems, "src/gamesystems"));

protected override ProjectConfiguration CreateDefaultCore(string path)
{
return base.CreateDefaultCore(path).WithSourceDirectories(DefaultDirectoryReferences);
}
}
}
}
50 changes: 14 additions & 36 deletions src/WarHub.ArmouryModel.CliTool/Commands/PublishCommand.cs
Expand Up @@ -4,31 +4,27 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Amadevus.RecordGenerator;
using WarHub.ArmouryModel.CliTool.Utilities;
using WarHub.ArmouryModel.ProjectModel;
using WarHub.ArmouryModel.Source;
using WarHub.ArmouryModel.Source.XmlFormat;
using WarHub.ArmouryModel.Workspaces.BattleScribe;
using WarHub.ArmouryModel.Workspaces.Gitree;

namespace WarHub.ArmouryModel.CliTool.Commands
{
public partial class PublishCommand : CommandBase
{
internal static readonly string[] ArtifactNames = new[] { "xml", "zip", "index", "bsi", "bsr" };

[Record]
private partial class Options
private record Options
{
public ImmutableArray<ArtifactType> Artifacts { get; }
public DirectoryInfo Source { get; }
public DirectoryInfo Output { get; }
public Uri Url { get; }
public ImmutableArray<Uri> AdditionalUrls { get; }
public bool UrlOnlyIndex { get; }
public string RepoName { get; }
public string Filename { get; }
public ImmutableArray<ArtifactType> Artifacts { get; init; }
public DirectoryInfo Source { get; init; }
public DirectoryInfo Output { get; init; }
public Uri Url { get; init; }
public ImmutableArray<Uri> AdditionalUrls { get; init; }
public bool UrlOnlyIndex { get; init; }
public string RepoName { get; init; }
public string Filename { get; init; }
}

public enum ArtifactType
Expand All @@ -53,12 +49,6 @@ public enum ArtifactType
string verbosity)
{
SetupLogger(verbosity);
var configInfo = new AutoProjectConfigurationProvider().Create(source.FullName);
Log.Debug("Using configuration: {@Config}", configInfo);
if (configInfo.Configuration.FormatProvider == ProjectFormatProviderType.Gitree)
{
Log.Warning("Gitree feature is a Work In Progress. It may not work as expected, or at all.");
}

var artifactTypes = artifacts
.Distinct()
Expand All @@ -70,12 +60,12 @@ public enum ArtifactType
Log.Information("Nothing to do.");
return;
}
output ??= new DirectoryInfo(configInfo.Configuration.OutputPath);
output ??= new DirectoryInfo("artifacts");
Log.Debug("Writing artifacts to: {Destination}", output);
output.Create();

Log.Debug("Loading workspace...");
var workspace = ReadWorkspaceFromConfig(configInfo);
var workspace = XmlWorkspace.CreateFromDirectory(source.FullName);
Log.Debug(
"Workspace loaded. {DatafileCount} datafiles discovered.",
workspace.Datafiles.Count(x => x.DataKind.IsDataCatalogueKind()));
Expand All @@ -87,7 +77,7 @@ public enum ArtifactType

var resolvedFilename = string.IsNullOrWhiteSpace(filename) ? source.Name : filename;

var options = new Options.Builder
var options = new Options
{
Artifacts = artifactTypes,
Source = source,
Expand All @@ -97,7 +87,7 @@ public enum ArtifactType
RepoName = resolvedRepoName,
Filename = resolvedFilename,
UrlOnlyIndex = urlOnlyIndex
}.ToImmutable();
};

foreach (var artifactType in options.Artifacts)
{
Expand Down Expand Up @@ -156,7 +146,7 @@ private static async Task<string> GetRepoNameFallbackAsync(IWorkspace workspace)
var gst = (GamesystemNode)await workspace.Datafiles
.FirstOrDefault(x => x.DataKind == SourceKind.Gamesystem)
?.GetDataAsync();
return gst?.Name ?? workspace.Info.GetDirectoryInfo().Name;
return gst?.Name ?? new DirectoryInfo(workspace.RootPath).Name;
}

private async Task PublishArtifactRepoDistributionAsync(IWorkspace workspace, Options options)
Expand Down Expand Up @@ -242,18 +232,6 @@ private async Task PublishXmlAsync(IWorkspace workspace, Options options)
}
}

private static IWorkspace ReadWorkspaceFromConfig(ProjectConfigurationInfo info)
{
return info.Configuration.FormatProvider switch
{
ProjectFormatProviderType.Gitree => GitreeWorkspace.CreateFromConfigurationInfo(info),
ProjectFormatProviderType.BattleScribeXml => XmlWorkspace.CreateFromConfigurationInfo(info),
_ => throw new InvalidOperationException(
$"Unknown {nameof(ProjectConfiguration.FormatProvider)}:" +
$" {info.Configuration.FormatProvider}"),
};
}

private static ArtifactType ParseArtifactType(string name)
{
return name switch
Expand Down

This file was deleted.

Expand Up @@ -13,10 +13,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amadevus.RecordGenerator" />
<PackageReference Include="morelinq" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Optional" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="System.CommandLine" />
Expand Down

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions src/WarHub.ArmouryModel.ProjectModel/Configuration/SourceFolder.cs

This file was deleted.

10 changes: 1 addition & 9 deletions src/WarHub.ArmouryModel.ProjectModel/DatafileInfo`1.cs
@@ -1,20 +1,12 @@
using System.IO;
using System.Threading.Tasks;
using Amadevus.RecordGenerator;
using WarHub.ArmouryModel.Source;

namespace WarHub.ArmouryModel.ProjectModel
{
[Record]
public sealed partial class DatafileInfo<TData> : IDatafileInfo<TData>
public sealed record DatafileInfo<TData>(string Filepath, TData Data) : IDatafileInfo<TData>
where TData : SourceNode
{
// TODO internal ctor

public string Filepath { get; }

public TData Data { get; }

public SourceKind DataKind => Data.Kind;

public string GetStorageName() => Path.GetFileNameWithoutExtension(Filepath);
Expand Down

This file was deleted.

0 comments on commit 7d62279

Please sign in to comment.