|
| 1 | +#!/usr/bin/dotnet -- |
| 2 | +#:package CliFx |
| 3 | + |
| 4 | +using CliFx; |
| 5 | +using CliFx.Attributes; |
| 6 | +using CliFx.Infrastructure; |
| 7 | + |
| 8 | +return await new CliApplicationBuilder() |
| 9 | + .AddCommand<PublishMacOSBundleCommand>() |
| 10 | + .Build() |
| 11 | + .RunAsync(args); |
| 12 | + |
| 13 | +[Command(Description = "Publishes the GUI app as a macOS .app bundle.")] |
| 14 | +public class PublishMacOSBundleCommand : ICommand |
| 15 | +{ |
| 16 | + private const string BundleName = "YoutubeDownloader.app"; |
| 17 | + private const string AppName = "YoutubeDownloader"; |
| 18 | + private const string AppCopyright = "© Oleksii Holub"; |
| 19 | + private const string AppIdentifier = "me.Tyrrrz.YoutubeDownloader"; |
| 20 | + private const string AppSpokenName = "YoutubeDownloader"; |
| 21 | + private const string AppIconName = "AppIcon"; |
| 22 | + |
| 23 | + [CommandOption("publish-dir", Description = "Path to the publish output directory.")] |
| 24 | + public required string PublishDirPath { get; init; } |
| 25 | + |
| 26 | + [CommandOption("icons-file", Description = "Path to the .icns icons file.")] |
| 27 | + public required string IconsFilePath { get; init; } |
| 28 | + |
| 29 | + [CommandOption("full-version", Description = "Full version string (e.g. '1.2.3.4').")] |
| 30 | + public required string FullVersion { get; init; } |
| 31 | + |
| 32 | + [CommandOption("short-version", Description = "Short version string (e.g. '1.2.3').")] |
| 33 | + public required string ShortVersion { get; init; } |
| 34 | + |
| 35 | + public async ValueTask ExecuteAsync(IConsole console) |
| 36 | + { |
| 37 | + // Set up paths |
| 38 | + var publishDirPath = Path.GetFullPath(PublishDirPath); |
| 39 | + var tempDirPath = Path.GetFullPath( |
| 40 | + Path.Combine(publishDirPath, "../publish-macos-app-temp") |
| 41 | + ); |
| 42 | + |
| 43 | + // Ensure the temporary directory is clean before use in case a previous run crashed |
| 44 | + if (Directory.Exists(tempDirPath)) |
| 45 | + Directory.Delete(tempDirPath, true); |
| 46 | + |
| 47 | + var bundleDirPath = Path.Combine(tempDirPath, BundleName); |
| 48 | + var contentsDirPath = Path.Combine(bundleDirPath, "Contents"); |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + // Copy icons into the .app's Resources folder |
| 53 | + Directory.CreateDirectory(Path.Combine(contentsDirPath, "Resources")); |
| 54 | + File.Copy( |
| 55 | + IconsFilePath, |
| 56 | + Path.Combine(contentsDirPath, "Resources", "AppIcon.icns"), |
| 57 | + true |
| 58 | + ); |
| 59 | + |
| 60 | + // Generate the Info.plist metadata file with the app information |
| 61 | + // lang=xml |
| 62 | + var plistContent = $""" |
| 63 | + <?xml version="1.0" encoding="UTF-8"?> |
| 64 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 65 | + <plist version="1.0"> |
| 66 | + <dict> |
| 67 | + <key>CFBundleDisplayName</key> |
| 68 | + <string>{AppName}</string> |
| 69 | + <key>CFBundleName</key> |
| 70 | + <string>{AppName}</string> |
| 71 | + <key>CFBundleExecutable</key> |
| 72 | + <string>{AppName}</string> |
| 73 | + <key>NSHumanReadableCopyright</key> |
| 74 | + <string>{AppCopyright}</string> |
| 75 | + <key>CFBundleIdentifier</key> |
| 76 | + <string>{AppIdentifier}</string> |
| 77 | + <key>CFBundleSpokenName</key> |
| 78 | + <string>{AppSpokenName}</string> |
| 79 | + <key>CFBundleIconFile</key> |
| 80 | + <string>{AppIconName}</string> |
| 81 | + <key>CFBundleIconName</key> |
| 82 | + <string>{AppIconName}</string> |
| 83 | + <key>CFBundleVersion</key> |
| 84 | + <string>{FullVersion}</string> |
| 85 | + <key>CFBundleShortVersionString</key> |
| 86 | + <string>{ShortVersion}</string> |
| 87 | + <key>NSHighResolutionCapable</key> |
| 88 | + <true /> |
| 89 | + <key>CFBundlePackageType</key> |
| 90 | + <string>APPL</string> |
| 91 | + </dict> |
| 92 | + </plist> |
| 93 | + """; |
| 94 | + |
| 95 | + await File.WriteAllTextAsync(Path.Combine(contentsDirPath, "Info.plist"), plistContent); |
| 96 | + |
| 97 | + // Delete the previous bundle if it exists |
| 98 | + var existingBundlePath = Path.Combine(publishDirPath, BundleName); |
| 99 | + if (Directory.Exists(existingBundlePath)) |
| 100 | + Directory.Delete(existingBundlePath, true); |
| 101 | + |
| 102 | + // Move all files from the publish directory into the MacOS directory |
| 103 | + Directory.CreateDirectory(Path.Combine(contentsDirPath, "MacOS")); |
| 104 | + foreach (var entryPath in Directory.GetFileSystemEntries(publishDirPath)) |
| 105 | + { |
| 106 | + var destinationPath = Path.Combine( |
| 107 | + contentsDirPath, |
| 108 | + "MacOS", |
| 109 | + Path.GetFileName(entryPath) |
| 110 | + ); |
| 111 | + |
| 112 | + if (Directory.Exists(entryPath)) |
| 113 | + Directory.Move(entryPath, destinationPath); |
| 114 | + else |
| 115 | + File.Move(entryPath, destinationPath); |
| 116 | + } |
| 117 | + |
| 118 | + // Move the final bundle into the publish directory for upload |
| 119 | + Directory.Move(bundleDirPath, Path.Combine(publishDirPath, BundleName)); |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + // Clean up the temporary directory |
| 124 | + if (Directory.Exists(tempDirPath)) |
| 125 | + Directory.Delete(tempDirPath, true); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments