Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active March 16, 2024 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/39206b7d47a1eacac35cbd1b98f6f5d3 to your computer and use it in GitHub Desktop.
Save juanfal/39206b7d47a1eacac35cbd1b98f6f5d3 to your computer and use it in GitHub Desktop.
Create .dmg disk image from folder(s) in a smart way
#!/usr/bin/env ruby -wU
# juanfc 2024-03-16
# https://gist.github.com/juanfal/39206b7d47a1eacac35cbd1b98f6f5d3
if ARGV.length < 1 or (ARGV.length == 1 and ARGV[0] == '-h') then
puts "Usage:
#$0 [destdmg] sourceFolder [sourceFolders..]
"
exit 0
end
if ARGV.length == 1 then
folders = ARGV
thedmg = ARGV[0]
else
if File.directory?(ARGV[0]) then
folders = ARGV
else
folders = ARGV[1..]
end
thedmg = ARGV[0]
end
if not thedmg.end_with? ".dmg"
thedmg = thedmg.gsub(/\/+$/, '') + ".dmg"
end
if (File.exist?(thedmg)) then
$stderr.puts "File '#{thedmg}' already exists! Not touched"
exit 1
end
folders.each{ |f|
if not File.exist?(f) then
$stderr.puts "Folder '#{f}' doesn't exists!"
exit 1
end
}
folders = "-srcfolder " + folders.map{|i| "'#{i}'"}.join(" -srcfolder ")
puts("hdiutil create '#{thedmg}' #{folders}")
system("hdiutil create '#{thedmg}' #{folders}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment