Skip to content
Snippets Groups Projects
Commit af551158 authored by José Valim's avatar José Valim Committed by Xiangrong Hao
Browse files

Compile Mac artifacts during Mix compilation (#37)

* Compile artifacts at compile time

* Check for staleness
parent debee097
No related branches found
No related tags found
No related merge requests found
...@@ -40,32 +40,12 @@ defmodule FileSystem.Backends.FSMac do ...@@ -40,32 +40,12 @@ defmodule FileSystem.Backends.FSMac do
def bootstrap do def bootstrap do
exec_file = executable_path() exec_file = executable_path()
cond do if not is_nil(exec_file) and File.exists?(exec_file) 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."
:ok :ok
end else
rescue Logger.error "Can't find executable `mac_listener`"
_ ->
Logger.error ~s|Automatic compile executable file failed, run "mix file_system.fs_mac init" to compile it manually.|
{:error, :fs_mac_bootstrap_error} {:error, :fs_mac_bootstrap_error}
end
end end
def supported_systems do def supported_systems do
...@@ -84,13 +64,7 @@ defmodule FileSystem.Backends.FSMac do ...@@ -84,13 +64,7 @@ defmodule FileSystem.Backends.FSMac do
end end
defp executable_path(:config) do defp executable_path(:config) do
with config when is_list(config) <- Application.get_env(:file_system, :fs_mac), Application.get_env(:file_system, :fs_mac)[:executable_file]
executable_file when not is_nil(executable_file) <- Keyword.get(config, :executable_file)
do
executable_file |> to_string
else
_ -> nil
end
end end
defp executable_path(:system_env) do defp executable_path(:system_env) do
......
...@@ -51,13 +51,7 @@ defmodule FileSystem.Backends.FSWindows do ...@@ -51,13 +51,7 @@ defmodule FileSystem.Backends.FSWindows do
end end
defp executable_path(:config) do defp executable_path(:config) do
with config when is_list(config) <- Application.get_env(:file_system, :fs_windows), Application.get_env(:file_system, :fs_windows)[:executable_file]
executable_file when not is_nil(executable_file) <- Keyword.get(config, :executable_file)
do
executable_file |> to_string
else
_ -> nil
end
end end
defp executable_path(:system_env) do defp executable_path(:system_env) do
......
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
...@@ -9,6 +9,8 @@ defmodule FileSystem.Mixfile do ...@@ -9,6 +9,8 @@ defmodule FileSystem.Mixfile do
description: "A file system change watcher wrapper based on [fs](https://github.com/synrc/fs)", description: "A file system change watcher wrapper based on [fs](https://github.com/synrc/fs)",
source_url: "https://github.com/falood/file_system", source_url: "https://github.com/falood/file_system",
package: package(), package: package(),
compilers: [:file_system | Mix.compilers],
aliases: ["compile.file_system": &file_system/1],
docs: [ docs: [
extras: ["README.md"], extras: ["README.md"],
main: "readme", main: "readme",
...@@ -28,6 +30,32 @@ defmodule FileSystem.Mixfile do ...@@ -28,6 +30,32 @@ defmodule FileSystem.Mixfile do
] ]
end 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 defp package do
%{ maintainers: ["Xiangrong Hao", "Max Veytsman"], %{ maintainers: ["Xiangrong Hao", "Max Veytsman"],
files: [ files: [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment