From af5511582ef45b1e89c7aa106a9b350f1b0ea189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@gmail.com> Date: Thu, 10 Aug 2017 17:08:12 +0200 Subject: [PATCH] Compile Mac artifacts during Mix compilation (#37) * Compile artifacts at compile time * Check for staleness --- lib/file_system/backends/fs_mac.ex | 36 ++++---------------------- lib/file_system/backends/fs_windows.ex | 8 +----- lib/mix/tasks/fs_mac.ex | 17 ------------ mix.exs | 28 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 55 deletions(-) delete mode 100644 lib/mix/tasks/fs_mac.ex diff --git a/lib/file_system/backends/fs_mac.ex b/lib/file_system/backends/fs_mac.ex index 68a8476..ff0fc6b 100644 --- a/lib/file_system/backends/fs_mac.ex +++ b/lib/file_system/backends/fs_mac.ex @@ -40,32 +40,12 @@ defmodule FileSystem.Backends.FSMac do def bootstrap do exec_file = executable_path() - cond do - is_nil(exec_file) -> {:error, :fs_mac_bootstrap_error} - File.exists?(exec_file) -> :ok - true -> compile_executable_file(exec_file) - end - end - - defp compile_executable_file(exec_file) do - Logger.info "Compiling executable file..." - src_dir = - case Mix.Project.config[:app] do - :file_system -> "." - _ -> Mix.Project.deps_paths[:file_system] - end - cmd = "clang -framework CoreFoundation -framework CoreServices -Wno-deprecated-declarations #{src_dir}/c_src/mac/*.c -o #{exec_file}" - if Mix.shell.cmd(cmd) > 0 do - Logger.error ~s|Compile executable file error, try to run "#{cmd}" manually.| - {:error, :fs_mac_bootstrap_error} - else - Logger.info "Compile executable file, Done." + if not is_nil(exec_file) and File.exists?(exec_file) do :ok - end - rescue - _ -> - Logger.error ~s|Automatic compile executable file failed, run "mix file_system.fs_mac init" to compile it manually.| + else + Logger.error "Can't find executable `mac_listener`" {:error, :fs_mac_bootstrap_error} + end end def supported_systems do @@ -84,13 +64,7 @@ defmodule FileSystem.Backends.FSMac do end defp executable_path(:config) do - with config when is_list(config) <- Application.get_env(:file_system, :fs_mac), - executable_file when not is_nil(executable_file) <- Keyword.get(config, :executable_file) - do - executable_file |> to_string - else - _ -> nil - end + Application.get_env(:file_system, :fs_mac)[:executable_file] end defp executable_path(:system_env) do diff --git a/lib/file_system/backends/fs_windows.ex b/lib/file_system/backends/fs_windows.ex index fbfcadb..3fbe04a 100644 --- a/lib/file_system/backends/fs_windows.ex +++ b/lib/file_system/backends/fs_windows.ex @@ -51,13 +51,7 @@ defmodule FileSystem.Backends.FSWindows do end defp executable_path(:config) do - with config when is_list(config) <- Application.get_env(:file_system, :fs_windows), - executable_file when not is_nil(executable_file) <- Keyword.get(config, :executable_file) - do - executable_file |> to_string - else - _ -> nil - end + Application.get_env(:file_system, :fs_windows)[:executable_file] end defp executable_path(:system_env) do diff --git a/lib/mix/tasks/fs_mac.ex b/lib/mix/tasks/fs_mac.ex deleted file mode 100644 index 7d966f9..0000000 --- a/lib/mix/tasks/fs_mac.ex +++ /dev/null @@ -1,17 +0,0 @@ -defmodule Mix.Tasks.FileSystem.FsMac do - use Mix.Task - - @doc false - def run(["init"]) do - case FileSystem.Backends.FSMac.bootstrap do - :ok -> - IO.puts "Initialize fs_mac backend successfully." - {:error, reason} -> - IO.puts :stderr, "Initialize fs_mac backend error, reason: #{reason}." - end - end - - def run(args) do - IO.puts :stderr, "unknown command `#{args}`" - end -end diff --git a/mix.exs b/mix.exs index 74d5f2c..d57ba9d 100644 --- a/mix.exs +++ b/mix.exs @@ -9,6 +9,8 @@ defmodule FileSystem.Mixfile do description: "A file system change watcher wrapper based on [fs](https://github.com/synrc/fs)", source_url: "https://github.com/falood/file_system", package: package(), + compilers: [:file_system | Mix.compilers], + aliases: ["compile.file_system": &file_system/1], docs: [ extras: ["README.md"], main: "readme", @@ -28,6 +30,32 @@ defmodule FileSystem.Mixfile do ] end + defp file_system(_args) do + case :os.type do + {:unix, :darwin} -> compile_mac() + _ -> :ok + end + end + + defp compile_mac do + require Logger + source = "c_src/mac/*.c" + target = "priv/mac_listener" + + if Mix.Utils.stale?(Path.wildcard(source), [target]) do + Logger.info "Compiling file system watcher for Mac..." + cmd = "clang -framework CoreFoundation -framework CoreServices -Wno-deprecated-declarations #{source} -o #{target}" + if Mix.shell.cmd(cmd) > 0 do + Logger.error "Could not compile file system watcher for Mac, try to run #{inspect cmd} manually inside the dependnecy." + else + Logger.info "Done." + end + :ok + else + :noop + end + end + defp package do %{ maintainers: ["Xiangrong Hao", "Max Veytsman"], files: [ -- GitLab