From 909b00bfbc9bba19507683aafc48fc10ad77f3eb Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 20 Jan 2022 09:58:26 +0100 Subject: [PATCH] Clean up building Info.plist (#900) --- app_builder/lib/app_builder/macos.ex | 174 ++++++++++++++------------- app_builder/mix.exs | 2 +- 2 files changed, 90 insertions(+), 86 deletions(-) diff --git a/app_builder/lib/app_builder/macos.ex b/app_builder/lib/app_builder/macos.ex index 8fafb2e27..026846d79 100644 --- a/app_builder/lib/app_builder/macos.ex +++ b/app_builder/lib/app_builder/macos.ex @@ -31,22 +31,7 @@ defmodule AppBuilder.MacOS do if codesign do entitlements_path = "tmp/entitlements.plist" - - File.write!(entitlements_path, """ - - - - - com.apple.security.cs.allow-jit - - com.apple.security.cs.allow-unsigned-executable-memory - - com.apple.security.cs.allow-dyld-environment-variables - - - - """) - + File.write!(entitlements_path, entitlements()) codesign(to_sign, "--options=runtime --entitlements=#{entitlements_path}", codesign) end @@ -132,7 +117,7 @@ defmodule AppBuilder.MacOS do logo_path = options[:logo_path] || Application.app_dir(:wx, "examples/demo/erlang.png") create_logo(app_bundle_path, logo_path) - info_plist = options[:info_plist] || build_info_plist(options) + info_plist = options[:info_plist] || info_plist(options) File.write!(Path.join([app_bundle_path, "Contents", "Info.plist"]), info_plist) release @@ -149,74 +134,6 @@ defmodule AppBuilder.MacOS do """ end - defp build_info_plist(options) do - app_name = Keyword.fetch!(options, :name) - app_version = Keyword.fetch!(options, :version) - - url_schemes = - """ - \nCFBundleURLTypes - - """ <> - for scheme <- options[:url_schemes] || [], into: "" do - """ - - CFBundleURLName - #{app_name} - CFBundleURLSchemes - - #{scheme} - - - """ - end <> - "" - - document_types = - """ - \nCFBundleDocumentTypes - - """ <> - for type <- options[:document_types] || [], into: "" do - """ - - CFBundleTypeName - #{type.name} - CFBundleTypeRole - #{type.role} - CFBundleTypeExtensions - - #{for ext <- type.extensions, do: "#{ext}"} - - - """ - end <> - "" - - """ - - - - - CFBundlePackageType - APPL - CFBundleName - #{app_name} - CFBundleDisplayName - #{app_name} - CFBundleShortVersionString - #{app_version} - CFBundleVersion - #{app_version} - CFBundleIconFile - AppIcon - CFBundleIconName - AppIcon#{url_schemes}#{document_types} - - - """ - end - defp create_logo(app_bundle_path, logo_source_path) do logo_dest_path = Path.join([app_bundle_path, "Contents", "Resources", "AppIcon.icns"]) @@ -245,4 +162,91 @@ defmodule AppBuilder.MacOS do File.rm_rf!(logo_dest_tmp_path) end end + + ## Templates + + require EEx + + defp entitlements do + """ + + + + + com.apple.security.cs.allow-jit + + com.apple.security.cs.allow-unsigned-executable-memory + + com.apple.security.cs.allow-dyld-environment-variables + + + + """ + end + + code = """ + <% + app_name = Keyword.fetch!(options, :name) + app_version = Keyword.fetch!(options, :version) + %> + + + + + CFBundlePackageType + APPL + CFBundleName + <%= app_name %> + CFBundleDisplayName + <%= app_name %> + CFBundleShortVersionString + <%= app_version %>on} + CFBundleVersion + <%= app_version %>on} + CFBundleIconFile + AppIcon + CFBundleIconName + AppIcon + + <%= if schemes = options[:url_schemes] do %> + CFBundleURLTypes + + <%= for scheme <- schemes do %> + + CFBundleURLName + <%= app_name %> + CFBundleURLSchemes + + <%= scheme %> + + + <% end %> + + <% end %> + + <%= if types = options[:document_types] do %> + CFBundleDocumentTypes + + <%= for type <- types do %> + + CFBundleTypeName + <%= type.name %> + CFBundleTypeRole + <%= type.role %> + CFBundleTypeExtensions + + <%= for ext <- type.extensions do %> + <%= ext %> + <% end %> + + + <% end %> + + <% end %> + + + + """ + + EEx.function_from_string(:defp, :info_plist, code, [:options], trim: true) end diff --git a/app_builder/mix.exs b/app_builder/mix.exs index 27c8b19ff..c07a9f0b0 100644 --- a/app_builder/mix.exs +++ b/app_builder/mix.exs @@ -13,7 +13,7 @@ defmodule AppBuilder.MixProject do def application do [ - extra_applications: [:logger] + extra_applications: [:logger, :eex] ] end