2022-07-19 20:48:07 +08:00
|
|
|
defmodule AppBundler do
|
2022-06-02 04:29:54 +08:00
|
|
|
def bundle(release) do
|
2022-07-18 17:51:17 +08:00
|
|
|
options = validate_options(release.options[:app] || [])
|
2022-01-18 00:34:38 +08:00
|
|
|
|
2022-06-03 03:33:23 +08:00
|
|
|
case os() do
|
2022-06-02 04:29:54 +08:00
|
|
|
:macos ->
|
2022-07-19 20:48:07 +08:00
|
|
|
AppBundler.MacOS.bundle(release, options)
|
2022-06-02 04:29:54 +08:00
|
|
|
|
|
|
|
:windows ->
|
2022-07-19 20:48:07 +08:00
|
|
|
AppBundler.Windows.bundle(release, options)
|
2022-06-02 04:29:54 +08:00
|
|
|
end
|
|
|
|
end
|
2022-05-27 20:45:20 +08:00
|
|
|
|
2022-07-20 17:05:28 +08:00
|
|
|
def target do
|
|
|
|
case :erlang.system_info(:system_architecture) do
|
|
|
|
'x86_64-apple-' ++ _ -> "macos-x86_64"
|
|
|
|
'aarch64-apple-' ++ _ -> "macos-aarch64"
|
|
|
|
'win32' -> "windows-x86_64"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-27 20:45:20 +08:00
|
|
|
def os do
|
|
|
|
case :os.type() do
|
|
|
|
{:unix, :darwin} -> :macos
|
|
|
|
{:win32, _} -> :windows
|
|
|
|
end
|
|
|
|
end
|
2022-06-03 03:33:23 +08:00
|
|
|
|
2022-07-07 01:27:06 +08:00
|
|
|
def init do
|
2022-07-19 20:48:07 +08:00
|
|
|
{:ok, _} = Registry.register(AppBundler.Registry, "app_event_subscribers", [])
|
2022-07-18 17:51:17 +08:00
|
|
|
|
2022-07-07 01:27:06 +08:00
|
|
|
if input = System.get_env("APP_BUILDER_INPUT") do
|
2022-07-18 17:51:17 +08:00
|
|
|
__rpc__(input)
|
2022-07-07 01:27:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
def __rpc__ do
|
|
|
|
IO.read(:line)
|
|
|
|
|> String.trim()
|
|
|
|
|> __rpc__()
|
2022-07-07 01:27:06 +08:00
|
|
|
end
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
def __rpc__("open_app") do
|
|
|
|
dispatch(:open_app)
|
2022-07-07 01:27:06 +08:00
|
|
|
end
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
def __rpc__("open_url:" <> url) do
|
|
|
|
dispatch({:open_url, url})
|
2022-07-07 01:27:06 +08:00
|
|
|
end
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
def __rpc__("open_file:" <> path) do
|
2022-07-07 01:27:06 +08:00
|
|
|
path =
|
|
|
|
if os() == :windows do
|
|
|
|
String.replace(path, "\\", "/")
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
dispatch({:open_file, path})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp dispatch(message) do
|
2022-07-19 20:48:07 +08:00
|
|
|
Registry.dispatch(AppBundler.Registry, "app_event_subscribers", fn entries ->
|
2022-07-18 17:51:17 +08:00
|
|
|
for {pid, _} <- entries, do: send(pid, message)
|
|
|
|
end)
|
2022-07-07 01:27:06 +08:00
|
|
|
end
|
|
|
|
|
2022-06-03 03:33:23 +08:00
|
|
|
defp validate_options(options) do
|
|
|
|
os = os()
|
|
|
|
|
|
|
|
root_allowed_options = %{
|
|
|
|
all: [
|
|
|
|
:name,
|
|
|
|
:icon_path,
|
|
|
|
url_schemes: [],
|
|
|
|
document_types: [],
|
|
|
|
additional_paths: []
|
|
|
|
],
|
|
|
|
macos: [
|
|
|
|
app_type: :regular,
|
|
|
|
build_dmg: false,
|
|
|
|
notarization: nil
|
|
|
|
],
|
|
|
|
windows: [
|
|
|
|
:server,
|
|
|
|
build_installer: false
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
document_type_allowed_options = %{
|
|
|
|
all: [
|
|
|
|
:name,
|
|
|
|
:extensions,
|
|
|
|
:icon_path
|
|
|
|
],
|
|
|
|
macos: [
|
|
|
|
:role
|
|
|
|
],
|
|
|
|
windows: []
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
options
|
|
|
|
|> validate_options(root_allowed_options, os)
|
|
|
|
|> Keyword.put_new_lazy(:name, &default_name/0)
|
|
|
|
|> Keyword.update!(:document_types, fn document_types ->
|
2022-06-03 03:33:23 +08:00
|
|
|
Enum.map(document_types, fn options ->
|
|
|
|
validate_options(options, document_type_allowed_options, os)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-07-18 17:51:17 +08:00
|
|
|
defp default_name do
|
|
|
|
Mix.Project.config()[:app] |> to_string |> Macro.camelize()
|
|
|
|
end
|
|
|
|
|
2022-06-03 03:33:23 +08:00
|
|
|
defp validate_options(options, allowed, os) do
|
|
|
|
{macos_options, options} = Keyword.pop(options, :macos, [])
|
|
|
|
{windows_options, options} = Keyword.pop(options, :windows, [])
|
|
|
|
|
|
|
|
options_per_os = %{
|
|
|
|
macos: macos_options,
|
|
|
|
windows: windows_options
|
|
|
|
}
|
|
|
|
|
|
|
|
options = Keyword.validate!(options, allowed.all)
|
|
|
|
options_for_os = Map.fetch!(options_per_os, os)
|
|
|
|
|
|
|
|
allowed_without_defaults =
|
|
|
|
for option <- allowed.all do
|
|
|
|
case option do
|
|
|
|
atom when is_atom(atom) -> atom
|
|
|
|
{atom, _default} when is_atom(atom) -> atom
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
allowed_for_os = allowed_without_defaults ++ Map.fetch!(allowed, os)
|
|
|
|
os_options = Keyword.validate!(options_for_os, allowed_for_os)
|
|
|
|
Keyword.merge(options, os_options)
|
|
|
|
end
|
2022-01-18 00:34:38 +08:00
|
|
|
end
|