|
| 1 | +using System.CommandLine; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using CSharpFunctionalExtensions; |
| 4 | +using DotnetPackaging.Deb.Archives.Deb; |
| 5 | +using Serilog; |
| 6 | +using DotnetPackaging.Tool; |
| 7 | +using Zafiro.FileSystem.Core; |
| 8 | +using Zafiro.DivineBytes; |
| 9 | + |
| 10 | +namespace DotnetPackaging.Tool.Commands; |
| 11 | + |
| 12 | +public static class DebCommand |
| 13 | +{ |
| 14 | + public static Command GetCommand() |
| 15 | + { |
| 16 | + var command = CommandFactory.CreateCommand( |
| 17 | + "deb", |
| 18 | + "Debian package", |
| 19 | + ".deb", |
| 20 | + CreateDeb, |
| 21 | + "Create a Debian (.deb) installer for Debian and Ubuntu based distributions.", |
| 22 | + "pack-deb", |
| 23 | + "debian"); |
| 24 | + |
| 25 | + AddFromProjectSubcommand(command); |
| 26 | + return command; |
| 27 | + } |
| 28 | + |
| 29 | + private static Task CreateDeb(DirectoryInfo inputDir, FileInfo outputFile, Options options, ILogger logger) |
| 30 | + { |
| 31 | + logger.Debug("Packaging Debian artifact from {Directory}", inputDir.FullName); |
| 32 | + var fs = new System.IO.Abstractions.FileSystem(); |
| 33 | + return new Zafiro.FileSystem.Local.Directory(fs.DirectoryInfo.New(inputDir.FullName)) |
| 34 | + .ToDirectory() |
| 35 | + .Bind(directory => DotnetPackaging.Deb.DebFile.From() |
| 36 | + .Directory(directory) |
| 37 | + .Configure(configuration => configuration.From(options)) |
| 38 | + .Build() |
| 39 | + .Map(DebMixin.ToData) |
| 40 | + .Bind(async data => |
| 41 | + { |
| 42 | + await using var fileSystemStream = outputFile.Open(FileMode.Create); |
| 43 | + return await data.DumpTo(fileSystemStream); |
| 44 | + })) |
| 45 | + .WriteResult(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void AddFromProjectSubcommand(Command debCommand) |
| 49 | + { |
| 50 | + var project = new Option<FileInfo>("--project") { Description = "Path to the .csproj file", Required = true }; |
| 51 | + var rid = new Option<string?>("--rid") { Description = "Runtime identifier (e.g. linux-x64, linux-arm64)" }; |
| 52 | + var selfContained = new Option<bool>("--self-contained") { Description = "Publish self-contained" }; |
| 53 | + selfContained.DefaultValueFactory = _ => true; |
| 54 | + var configuration = new Option<string>("--configuration") { Description = "Build configuration" }; |
| 55 | + configuration.DefaultValueFactory = _ => "Release"; |
| 56 | + var singleFile = new Option<bool>("--single-file") { Description = "Publish single-file" }; |
| 57 | + var trimmed = new Option<bool>("--trimmed") { Description = "Enable trimming" }; |
| 58 | + var output = new Option<FileInfo>("--output") { Description = "Destination path for the generated .deb", Required = true }; |
| 59 | + |
| 60 | + var appName = new Option<string>("--application-name") { Description = "Application name", Required = false }; |
| 61 | + var startupWmClass = new Option<string>("--wm-class") { Description = "Startup WM Class", Required = false }; |
| 62 | + var mainCategory = new Option<MainCategory?>("--main-category") { Description = "Main category", Required = false, Arity = ArgumentArity.ZeroOrOne, }; |
| 63 | + var additionalCategories = new Option<IEnumerable<AdditionalCategory>>("--additional-categories") { Description = "Additional categories", Required = false, Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true }; |
| 64 | + var keywords = new Option<IEnumerable<string>>("--keywords") { Description = "Keywords", Required = false, Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true }; |
| 65 | + var comment = new Option<string>("--comment") { Description = "Comment", Required = false }; |
| 66 | + var version = new Option<string>("--version") { Description = "Version", Required = false }; |
| 67 | + var homePage = new Option<Uri>("--homepage") { Description = "Home page of the application", Required = false }; |
| 68 | + var license = new Option<string>("--license") { Description = "License of the application", Required = false }; |
| 69 | + var screenshotUrls = new Option<IEnumerable<Uri>>("--screenshot-urls") { Description = "Screenshot URLs", Required = false }; |
| 70 | + var summary = new Option<string>("--summary") { Description = "Summary. Short description that should not end in a dot.", Required = false }; |
| 71 | + var appId = new Option<string>("--appId") { Description = "Application Id. Usually a Reverse DNS name like com.SomeCompany.SomeApplication", Required = false }; |
| 72 | + var executableName = new Option<string>("--executable-name") { Description = "Name of your application's executable", Required = false }; |
| 73 | + var isTerminal = new Option<bool>("--is-terminal") { Description = "Indicates whether your application is a terminal application", Required = false }; |
| 74 | + var iconOption = new Option<IIcon?>("--icon") { Required = false, Description = "Path to the application icon" }; |
| 75 | + iconOption.CustomParser = OptionsBinder.GetIcon; |
| 76 | + |
| 77 | + var optionsBinder = new OptionsBinder(appName, startupWmClass, keywords, comment, mainCategory, additionalCategories, iconOption, version, homePage, license, screenshotUrls, summary, appId, executableName, isTerminal); |
| 78 | + |
| 79 | + var fromProject = new Command("from-project") { Description = "Publish a .NET project and build a Debian .deb from the published output." }; |
| 80 | + fromProject.Add(project); |
| 81 | + fromProject.Add(rid); |
| 82 | + fromProject.Add(selfContained); |
| 83 | + fromProject.Add(configuration); |
| 84 | + fromProject.Add(singleFile); |
| 85 | + fromProject.Add(trimmed); |
| 86 | + fromProject.Add(output); |
| 87 | + fromProject.Add(appName); |
| 88 | + fromProject.Add(startupWmClass); |
| 89 | + fromProject.Add(mainCategory); |
| 90 | + fromProject.Add(additionalCategories); |
| 91 | + fromProject.Add(keywords); |
| 92 | + fromProject.Add(comment); |
| 93 | + fromProject.Add(version); |
| 94 | + fromProject.Add(homePage); |
| 95 | + fromProject.Add(license); |
| 96 | + fromProject.Add(screenshotUrls); |
| 97 | + fromProject.Add(summary); |
| 98 | + fromProject.Add(appId); |
| 99 | + fromProject.Add(executableName); |
| 100 | + fromProject.Add(isTerminal); |
| 101 | + fromProject.Add(iconOption); |
| 102 | + |
| 103 | + fromProject.SetAction(async parseResult => |
| 104 | + { |
| 105 | + var prj = parseResult.GetValue(project)!; |
| 106 | + var sc = parseResult.GetValue(selfContained); |
| 107 | + var cfg = parseResult.GetValue(configuration)!; |
| 108 | + var sf = parseResult.GetValue(singleFile); |
| 109 | + var tr = parseResult.GetValue(trimmed); |
| 110 | + var outFile = parseResult.GetValue(output)!; |
| 111 | + var opt = optionsBinder.Bind(parseResult); |
| 112 | + var ridVal = parseResult.GetValue(rid); |
| 113 | + |
| 114 | + await ExecutionWrapper.ExecuteWithLogging("deb-from-project", outFile.FullName, async logger => |
| 115 | + { |
| 116 | + if (string.IsNullOrWhiteSpace(ridVal) && !RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 117 | + { |
| 118 | + logger.Error("--rid is required when building DEB from-project on non-Linux hosts (e.g., linux-x64/linux-arm64)."); |
| 119 | + Environment.ExitCode = 1; |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + var publisher = new DotnetPackaging.Publish.DotnetPublisher(); |
| 124 | + var req = new DotnetPackaging.Publish.ProjectPublishRequest(prj.FullName) |
| 125 | + { |
| 126 | + Rid = string.IsNullOrWhiteSpace(ridVal) ? Maybe<string>.None : Maybe<string>.From(ridVal!), |
| 127 | + SelfContained = sc, |
| 128 | + Configuration = cfg, |
| 129 | + SingleFile = sf, |
| 130 | + Trimmed = tr |
| 131 | + }; |
| 132 | + |
| 133 | + var pub = await publisher.Publish(req); |
| 134 | + if (pub.IsFailure) |
| 135 | + { |
| 136 | + logger.Error("Publish failed: {Error}", pub.Error); |
| 137 | + Environment.ExitCode = 1; |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + var container = pub.Value.Container; |
| 142 | + var name = pub.Value.Name.Match(value => value, () => (string?)null); |
| 143 | + var built = await DotnetPackaging.Deb.DebFile.From().Container(container, name).Configure(o => o.From(opt)).Build(); |
| 144 | + if (built.IsFailure) |
| 145 | + { |
| 146 | + logger.Error("Deb creation failed: {Error}", built.Error); |
| 147 | + Environment.ExitCode = 1; |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + var data = DebMixin.ToData(built.Value); |
| 152 | + await using var fs = outFile.Open(FileMode.Create); |
| 153 | + var dumpRes = await data.DumpTo(fs); |
| 154 | + if (dumpRes.IsFailure) |
| 155 | + { |
| 156 | + logger.Error("Failed writing Deb file: {Error}", dumpRes.Error); |
| 157 | + Environment.ExitCode = 1; |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + logger.Information("{OutputFile}", outFile.FullName); |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + debCommand.Add(fromProject); |
| 167 | + } |
| 168 | +} |
0 commit comments