|
| 1 | +using Zafiro.DivineBytes; |
1 | 2 | using System.Text; |
2 | | -using System.Linq; |
3 | | -using DiscUtils.Iso9660; |
| 3 | +using DotnetPackaging.Formats.Dmg.Iso; |
| 4 | +using DotnetPackaging.Formats.Dmg.Udif; |
| 5 | +using Path = System.IO.Path; |
4 | 6 |
|
5 | 7 | namespace DotnetPackaging.Dmg; |
6 | 8 |
|
7 | 9 | /// <summary> |
8 | | -/// Minimal cross-platform DMG builder using an ISO/UDF (UDTO) payload. |
9 | | -/// This produces a .dmg that mounts fine on macOS for simple drag & drop installs. |
10 | | -/// Follow-ups can add true UDIF/UDZO wrapping while keeping the same public API. |
| 10 | +/// Cross-platform DMG builder using UDIF format with Rock Ridge support. |
| 11 | +/// Produces proper .dmg files that mount on macOS with full metadata preservation. |
11 | 12 | /// </summary> |
12 | 13 | public static class DmgIsoBuilder |
13 | 14 | { |
14 | | - public static Task Create(string sourceFolder, string outputPath, string volumeName) |
| 15 | + public static Task Create(string sourceFolder, string outputPath, string volumeName, bool compress = false, bool addApplicationsSymlink = false) |
15 | 16 | { |
16 | | - // Build a Joliet-enabled ISO so long filenames/casing are preserved reasonably. |
17 | | - using var fs = File.Create(outputPath); |
18 | | - var builder = new CDBuilder |
| 17 | + var builder = new IsoBuilder(SanitizeVolumeName(volumeName)); |
| 18 | + |
| 19 | + if (addApplicationsSymlink) |
19 | 20 | { |
20 | | - UseJoliet = true, |
21 | | - VolumeIdentifier = SanitizeVolumeName(volumeName), |
22 | | - }; |
| 21 | + builder.Root.AddChild(new IsoSymlink("Applications", "/Applications")); |
| 22 | + } |
23 | 23 |
|
24 | 24 | var appBundles = Directory.EnumerateDirectories(sourceFolder, "*.app", SearchOption.TopDirectoryOnly).ToList(); |
| 25 | + |
25 | 26 | if (appBundles.Any()) |
26 | 27 | { |
27 | | - // If pre-built .app bundles exist, copy only those (plus DMG adornments) to the image root |
| 28 | + // Copy pre-built .app bundles to image root |
28 | 29 | foreach (var bundle in appBundles) |
29 | 30 | { |
30 | | - var name = Path.GetFileName(bundle); |
31 | | - if (name == null) |
32 | | - { |
33 | | - continue; |
34 | | - } |
35 | | - |
36 | | - AddDirectoryRecursive(builder, sourceFolder, name, prefix: null); |
| 31 | + var bundleName = Path.GetFileName(bundle); |
| 32 | + if (bundleName == null) continue; |
| 33 | + |
| 34 | + var bundleDir = builder.Root.AddDirectory(bundleName); |
| 35 | + AddDirectoryRecursive(bundleDir, bundle); |
37 | 36 | } |
38 | 37 |
|
39 | | - AddDmgAdornments(builder, sourceFolder); |
| 38 | + AddDmgAdornments(builder.Root, sourceFolder); |
40 | 39 | } |
41 | 40 | else |
42 | 41 | { |
43 | | - var bundle = SanitizeBundleName(volumeName) + ".app"; |
44 | | - builder.AddDirectory(bundle); |
45 | | - builder.AddDirectory($"{bundle}/Contents"); |
46 | | - builder.AddDirectory($"{bundle}/Contents/MacOS"); |
47 | | - builder.AddDirectory($"{bundle}/Contents/Resources"); |
| 42 | + // Create .app bundle structure from publish output |
| 43 | + var bundleName = SanitizeBundleName(volumeName) + ".app"; |
| 44 | + var appBundle = builder.Root.AddDirectory(bundleName); |
| 45 | + var contents = appBundle.AddDirectory("Contents"); |
| 46 | + var macOs = contents.AddDirectory("MacOS"); |
| 47 | + var resources = contents.AddDirectory("Resources"); |
48 | 48 |
|
49 | 49 | // Copy application payload under Contents/MacOS |
50 | | - AddDirectoryRecursive( |
51 | | - builder, |
| 50 | + AddDirectoryContents( |
| 51 | + macOs, |
52 | 52 | sourceFolder, |
53 | | - ".", |
54 | | - prefix: $"{bundle}/Contents/MacOS", |
55 | | - shouldSkip: rel => rel.EndsWith(".app", StringComparison.OrdinalIgnoreCase)); |
| 53 | + shouldSkip: path => path.EndsWith(".app", StringComparison.OrdinalIgnoreCase) || |
| 54 | + path.EndsWith(".icns", StringComparison.OrdinalIgnoreCase)); |
56 | 55 |
|
| 56 | + // Copy .icns files to Resources |
57 | 57 | var appIcon = FindIcnsIcon(sourceFolder); |
58 | 58 | if (appIcon != null) |
59 | 59 | { |
60 | 60 | var iconName = Path.GetFileName(appIcon); |
61 | | - var iconBytes = File.ReadAllBytes(appIcon); |
62 | | - builder.AddFile($"{bundle}/Contents/Resources/{iconName}", new MemoryStream(iconBytes, writable: false)); |
| 61 | + resources.AddChild(new IsoFile(iconName) |
| 62 | + { |
| 63 | + ContentSource = () => ByteSource.FromStreamFactory(() => File.OpenRead(appIcon)), |
| 64 | + SourcePath = appIcon |
| 65 | + }); |
63 | 66 | } |
64 | 67 |
|
65 | | - AddDmgAdornments(builder, sourceFolder); |
| 68 | + AddDmgAdornments(builder.Root, sourceFolder); |
66 | 69 |
|
67 | | - // Add a minimal Info.plist |
| 70 | + // Generate Info.plist |
68 | 71 | var exeName = GuessExecutableName(sourceFolder, volumeName); |
69 | 72 | var plist = GenerateMinimalPlist(volumeName, exeName, appIcon == null ? null : Path.GetFileNameWithoutExtension(appIcon)); |
70 | | - builder.AddFile($"{bundle}/Contents/Info.plist", new MemoryStream(Encoding.UTF8.GetBytes(plist), writable: false)); |
| 73 | + contents.AddChild(new IsoFile("Info.plist") |
| 74 | + { |
| 75 | + ContentSource = () => ByteSource.FromBytes(Encoding.UTF8.GetBytes(plist)) |
| 76 | + }); |
| 77 | + |
| 78 | + // Add PkgInfo |
| 79 | + contents.AddChild(new IsoFile("PkgInfo") |
| 80 | + { |
| 81 | + ContentSource = () => ByteSource.FromBytes(Encoding.ASCII.GetBytes("APPL????")) |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + // Build ISO |
| 86 | + if (!compress) |
| 87 | + { |
| 88 | + // For uncompressed, output raw ISO for backward compatibility with tests |
| 89 | + using var dmgStream = File.Create(outputPath); |
| 90 | + builder.Build(dmgStream); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + // For compressed, wrap in UDIF with Bzip2 |
| 95 | + using var isoStream = new MemoryStream(); |
| 96 | + builder.Build(isoStream); |
| 97 | + isoStream.Position = 0; |
| 98 | + |
| 99 | + using var dmgStream = File.Create(outputPath); |
| 100 | + var writer = new UdifWriter { CompressionType = CompressionType.Bzip2 }; |
| 101 | + writer.Create(isoStream, dmgStream); |
71 | 102 | } |
72 | 103 |
|
73 | | - builder.Build(fs); |
74 | 104 | return Task.CompletedTask; |
75 | 105 | } |
76 | 106 |
|
77 | | - private static void AddDirectoryRecursive( |
78 | | - CDBuilder builder, |
79 | | - string root, |
80 | | - string rel, |
81 | | - string? prefix, |
82 | | - Func<string, bool>? shouldSkip = null) |
| 107 | + private static void AddDirectoryRecursive(IsoDirectory target, string sourcePath) |
83 | 108 | { |
84 | | - var abs = Path.Combine(root, rel); |
85 | | - var targetDir = prefix == null || rel == "." ? rel : Path.Combine(prefix, rel); |
86 | | - if (rel != ".") |
| 109 | + foreach (var dir in Directory.EnumerateDirectories(sourcePath)) |
87 | 110 | { |
88 | | - builder.AddDirectory(targetDir.Replace('\\','/')); |
| 111 | + var dirName = Path.GetFileName(dir); |
| 112 | + if (dirName == null) continue; |
| 113 | + |
| 114 | + var subDir = target.AddDirectory(dirName); |
| 115 | + AddDirectoryRecursive(subDir, dir); |
89 | 116 | } |
90 | 117 |
|
91 | | - foreach (var dir in Directory.EnumerateDirectories(abs)) |
| 118 | + foreach (var file in Directory.EnumerateFiles(sourcePath)) |
92 | 119 | { |
93 | | - var name = Path.GetFileName(dir); |
94 | | - if (name == null) continue; |
95 | | - var nextRel = rel == "." ? name : Path.Combine(rel, name); |
96 | | - if (shouldSkip?.Invoke(nextRel) == true) |
| 120 | + var fileName = Path.GetFileName(file); |
| 121 | + if (fileName == null) continue; |
| 122 | + |
| 123 | + target.AddChild(new IsoFile(fileName) |
97 | 124 | { |
98 | | - continue; |
99 | | - } |
100 | | - AddDirectoryRecursive(builder, root, nextRel, prefix); |
| 125 | + ContentSource = () => ByteSource.FromStreamFactory(() => File.OpenRead(file)), |
| 126 | + SourcePath = file |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static void AddDirectoryContents(IsoDirectory target, string sourcePath, Func<string, bool>? shouldSkip = null) |
| 132 | + { |
| 133 | + foreach (var dir in Directory.EnumerateDirectories(sourcePath)) |
| 134 | + { |
| 135 | + var dirName = Path.GetFileName(dir); |
| 136 | + if (dirName == null) continue; |
| 137 | + |
| 138 | + if (shouldSkip?.Invoke(dir) == true) continue; |
| 139 | + |
| 140 | + var subDir = target.AddDirectory(dirName); |
| 141 | + AddDirectoryRecursive(subDir, dir); |
101 | 142 | } |
102 | 143 |
|
103 | | - foreach (var file in Directory.EnumerateFiles(abs)) |
| 144 | + foreach (var file in Directory.EnumerateFiles(sourcePath)) |
104 | 145 | { |
105 | | - var name = Path.GetFileName(file); |
106 | | - if (name == null) continue; |
107 | | - var relPath = rel == "." ? name : Path.Combine(rel, name); |
108 | | - var finalPath = prefix == null ? relPath : Path.Combine(prefix, relPath); |
109 | | - var bytes = File.ReadAllBytes(file); |
110 | | - builder.AddFile(finalPath.Replace('\\','/'), new MemoryStream(bytes, writable: false)); |
| 146 | + var fileName = Path.GetFileName(file); |
| 147 | + if (fileName == null) continue; |
| 148 | + |
| 149 | + if (shouldSkip?.Invoke(file) == true) continue; |
| 150 | + |
| 151 | + target.AddChild(new IsoFile(fileName) |
| 152 | + { |
| 153 | + ContentSource = () => ByteSource.FromStreamFactory(() => File.OpenRead(file)), |
| 154 | + SourcePath = file |
| 155 | + }); |
111 | 156 | } |
112 | 157 | } |
113 | 158 |
|
114 | | - private static void AddDmgAdornments(CDBuilder builder, string sourceFolder) |
| 159 | + private static void AddDmgAdornments(IsoDirectory root, string sourceFolder) |
115 | 160 | { |
116 | | - // Hoist DMG adornments (if present) at image root for macOS Finder niceties |
| 161 | + // Add .VolumeIcon.icns if present |
117 | 162 | var volIcon = Path.Combine(sourceFolder, ".VolumeIcon.icns"); |
118 | 163 | if (File.Exists(volIcon)) |
119 | 164 | { |
120 | | - var bytes = File.ReadAllBytes(volIcon); |
121 | | - builder.AddFile(".VolumeIcon.icns", new MemoryStream(bytes, writable: false)); |
| 165 | + root.AddChild(new IsoFile(".VolumeIcon.icns") |
| 166 | + { |
| 167 | + ContentSource = () => ByteSource.FromStreamFactory(() => File.OpenRead(volIcon)), |
| 168 | + SourcePath = volIcon |
| 169 | + }); |
122 | 170 | } |
123 | 171 |
|
| 172 | + // Add .background directory if present |
124 | 173 | var backgroundDir = Path.Combine(sourceFolder, ".background"); |
125 | 174 | if (Directory.Exists(backgroundDir)) |
126 | 175 | { |
127 | | - AddDirectoryRecursive(builder, sourceFolder, ".background", prefix: null); |
| 176 | + var bgDir = root.AddDirectory(".background"); |
| 177 | + AddDirectoryRecursive(bgDir, backgroundDir); |
128 | 178 | } |
129 | 179 | } |
130 | 180 |
|
|
0 commit comments