Skip to content

Commit

Permalink
Merge #4174 Stabilize SourceForge mirror, Cmdline export fixes and mo…
Browse files Browse the repository at this point in the history
…dpack format
  • Loading branch information
HebaruSan committed Sep 5, 2024
2 parents fde0845 + 107303c commit 3f578cc
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 226 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ All notable changes to this project will be documented in this file.
- [Infra] Trigger mod installer deploy after APT repo update (#4158 by: HebaruSan)
- [CLI] Ability to update repos without a game instance (#4161 by: HebaruSan)
- [Multiple] Nullable references, net8.0, blend registry alert dot, netkan fixes (#4171 by: HebaruSan)
- [Netkan] SourceForge kref (#4172 by: HebaruSan)
- [Netkan] SourceForge kref (#4172, #4174 by: HebaruSan)

## v1.34.4 (Niven)

Expand Down
28 changes: 14 additions & 14 deletions Cmdline/Action/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
}
else
{
var stream = Console.OpenStandardOutput();
new Exporter(exportFileType.Value).Export(registry, stream);
stream.Flush();
using (var stream = Console.OpenStandardOutput())
{
new Exporter(exportFileType.Value).Export(regMgr, registry, stream);
}
}

if (!(options.porcelain) && exportFileType == null)
Expand All @@ -163,17 +164,16 @@ public int RunCommand(CKAN.GameInstance instance, object raw_options)
}

private static ExportFileType? GetExportFileType(string? export)
{
switch (export?.ToLowerInvariant())
=> export?.ToLowerInvariant() switch
{
case "text": return ExportFileType.PlainText;
case "markdown": return ExportFileType.Markdown;
case "bbcode": return ExportFileType.BbCode;
case "csv": return ExportFileType.Csv;
case "tsv": return ExportFileType.Tsv;
default: return null;
}
}
"ckan" => ExportFileType.Ckan,
"text" => ExportFileType.PlainText,
"markdown" => ExportFileType.Markdown,
"bbcode" => ExportFileType.BbCode,
"csv" => ExportFileType.Csv,
"tsv" => ExportFileType.Tsv,
_ => null,
};

private readonly RepositoryDataManager repoData;
private readonly IUser user;
Expand All @@ -186,7 +186,7 @@ internal class ListOptions : InstanceSpecificOptions
[Option("porcelain", HelpText = "Dump raw list of modules, good for shell scripting")]
public bool porcelain { get; set; }

[Option("export", HelpText = "Export list of modules in specified format to stdout")]
[Option("export", HelpText = "Format of module list: ckan, text, markdown, bbcode, csv, tsv")]
public string? export { get; set; }
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Exporters/BbCodeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CKAN.Exporters
{
public sealed class BbCodeExporter : IExporter
{
public void Export(IRegistryQuerier registry, Stream stream)
public void Export(RegistryManager manager, IRegistryQuerier registry, Stream stream)
{
using (var writer = new StreamWriter(stream))
{
Expand Down
17 changes: 17 additions & 0 deletions Core/Exporters/CkanExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.IO;

namespace CKAN.Exporters
{
public sealed class CkanExporter : IExporter
{
public void Export(RegistryManager manager,
IRegistryQuerier registry,
Stream stream)
{
using (var writer = new StreamWriter(stream))
{
writer.Write(CkanModule.ToJson(manager.GenerateModpack(false, false)));
}
}
}
}
134 changes: 0 additions & 134 deletions Core/Exporters/DelimeterSeparatedValueExporter.cs

This file was deleted.

123 changes: 123 additions & 0 deletions Core/Exporters/DelimiterSeparatedValueExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.IO;
using System.Linq;

namespace CKAN.Exporters
{
public sealed class DelimiterSeparatedValueExporter : IExporter
{
public DelimiterSeparatedValueExporter(Delimiter delimiter)
{
_delimiter = delimiter switch
{
Delimiter.Comma => ",",
Delimiter.Tab => "\t",
_ => throw new ArgumentOutOfRangeException(nameof(delimiter),
delimiter.ToString()),
};
}

public void Export(RegistryManager manager, IRegistryQuerier registry, Stream stream)
{
using (var writer = new StreamWriter(stream))
{
writer.WriteLine(string.Join(_delimiter,
"identifier",
"version",
"name",
"abstract",
"description",
"author",
"kind",
"download",
"download_size",
"ksp_version",
"ksp_version_min",
"ksp_version_max",
"license",
"release_status",
"repository",
"homepage",
"bugtracker",
"discussions",
"spacedock",
"curse"));

foreach (var mod in registry.InstalledModules.OrderBy(i => i.Module.name))
{
writer.WriteLine(string.Join(_delimiter,
mod.Module.identifier,
mod.Module.version,
QuoteIfNecessary(mod.Module.name),
QuoteIfNecessary(mod.Module.@abstract),
QuoteIfNecessary(mod.Module.description),
QuoteIfNecessary(mod.Module.author == null ? "" : string.Join(";", mod.Module.author)),
QuoteIfNecessary(mod.Module.kind),
WriteUri(mod.Module.download?[0]),
mod.Module.download_size,
mod.Module.ksp_version,
mod.Module.ksp_version_min,
mod.Module.ksp_version_max,
QuoteIfNecessary(string.Join(";",mod.Module.license)),
mod.Module.release_status,
WriteRepository(mod.Module.resources),
WriteHomepage(mod.Module.resources),
WriteBugtracker(mod.Module.resources),
WriteDiscussions(mod.Module.resources),
WriteSpaceDock(mod.Module.resources),
WriteCurse(mod.Module.resources)));
}
}
}

private string WriteUri(Uri? uri)
=> uri != null
? QuoteIfNecessary(uri.ToString())
: string.Empty;

private string WriteRepository(ResourcesDescriptor? resources)
=> resources != null && resources.repository != null
? QuoteIfNecessary(resources.repository.ToString())
: string.Empty;

private string WriteHomepage(ResourcesDescriptor? resources)
=> resources != null && resources.homepage != null
? QuoteIfNecessary(resources.homepage.ToString())
: string.Empty;

private string WriteBugtracker(ResourcesDescriptor? resources)
=> resources != null && resources.bugtracker != null
? QuoteIfNecessary(resources.bugtracker.ToString())
: string.Empty;

private string WriteDiscussions(ResourcesDescriptor? resources)
=> resources != null && resources.discussions != null
? QuoteIfNecessary(resources.discussions.ToString())
: string.Empty;

private string WriteSpaceDock(ResourcesDescriptor? resources)
=> resources != null && resources.spacedock != null
? QuoteIfNecessary(resources.spacedock.ToString())
: string.Empty;

private string WriteCurse(ResourcesDescriptor? resources)
=> resources != null && resources.curse != null
? QuoteIfNecessary(resources.curse.ToString())
: string.Empty;

private string QuoteIfNecessary(string? value)
=> value == null
? ""
: value.IndexOf(_delimiter, StringComparison.Ordinal) >= 0
? "\"" + value + "\""
: value;

public enum Delimiter
{
Comma,
Tab
}

private readonly string _delimiter;
}
}
36 changes: 15 additions & 21 deletions Core/Exporters/Exporter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;

using CKAN.Types;

namespace CKAN.Exporters
Expand All @@ -18,31 +19,24 @@ public sealed class Exporter : IExporter

public Exporter(ExportFileType exportFileType)
{
switch (exportFileType)
_exporter = exportFileType switch
{
case ExportFileType.PlainText:
_exporter = new PlainTextExporter();
break;
case ExportFileType.Markdown:
_exporter = new MarkdownExporter();
break;
case ExportFileType.BbCode:
_exporter = new BbCodeExporter();
break;
case ExportFileType.Csv:
_exporter = new DelimeterSeparatedValueExporter(DelimeterSeparatedValueExporter.Delimeter.Comma);
break;
case ExportFileType.Tsv:
_exporter = new DelimeterSeparatedValueExporter(DelimeterSeparatedValueExporter.Delimeter.Tab);
break;
default:
throw new ArgumentOutOfRangeException();
}
ExportFileType.Ckan => new CkanExporter(),
ExportFileType.PlainText => new PlainTextExporter(),
ExportFileType.Markdown => new MarkdownExporter(),
ExportFileType.BbCode => new BbCodeExporter(),
ExportFileType.Csv => new DelimiterSeparatedValueExporter(DelimiterSeparatedValueExporter.Delimiter.Comma),
ExportFileType.Tsv => new DelimiterSeparatedValueExporter(DelimiterSeparatedValueExporter.Delimiter.Tab),
_ => throw new ArgumentOutOfRangeException(nameof(exportFileType),
exportFileType.ToString()),
};
}

public void Export(IRegistryQuerier registry, Stream stream)
public void Export(RegistryManager manager,
IRegistryQuerier registry,
Stream stream)
{
_exporter.Export(registry, stream);
_exporter.Export(manager, registry, stream);
}
}
}
Loading

0 comments on commit 3f578cc

Please sign in to comment.