Fix installed? check
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
diff --git a/lib/dart_sass.ex b/lib/dart_sass.ex
index 5c1aeca..4c4d5b3 100644
--- a/lib/dart_sass.ex
+++ b/lib/dart_sass.ex
@@ -55,7 +55,7 @@ defmodule DartSass do
end
@doc """
- Returns the configured dart_sass version.
+ Returns the configured dart-sass version.
"""
def configured_version do
Application.get_env(:dart_sass, :version, latest_version())
@@ -80,28 +80,54 @@ defmodule DartSass do
end
@doc """
+ Checks whether or not dart-sass is installed.
+ """
+ def installed? do
+ case bin_paths() do
+ {sass, nil} -> File.exists?(sass)
+ {vm, snapshot} -> File.exists?(vm) and File.exists?(snapshot)
+ end
+ end
+
+ @doc """
Returns the path to the executable.
The executable may not be available if it was not yet installed.
"""
def bin_path do
- Path.join(Path.dirname(Mix.Project.build_path()), "sass")
+ {path, _snapshot} = bin_paths()
+ path
end
- def vm_path do
- Path.join(Path.dirname(Mix.Project.build_path()), "dart")
+ @doc """
+ Returns the path to the executable and optional snapshot.
+
+ Depending on your environment, sass may be invoked through a
+ portable instance of the Dart VM. In such case, this function
+ will return a tuple of `{Dart, Snapshot}`, otherwise it will
+ return `{Sass, Nil}`.
+ """
+ def bin_paths do
+ case :os.type() do
+ {:unix, :darwin} -> {vm_path(), snapshot_path()}
+ {:win32, _} -> {vm_path(), snapshot_path()}
+ _ -> {sass_path(), nil}
+ end
+ end
+
+ @doc false
+ def sass_path() do
+ Path.join(Path.dirname(Mix.Project.build_path()), "sass")
end
+ @doc false
def snapshot_path do
Path.join(Path.dirname(Mix.Project.build_path()), "sass.snapshot")
end
- def sass_cmd do
- case :os.type() do
- {:unix, :darwin} -> {vm_path(), [snapshot_path()]}
- {:win32, _} -> {vm_path(), [snapshot_path()]}
- _ -> {bin_path(), []}
- end
+ @doc false
+ def vm_path do
+ Path.join(Path.dirname(Mix.Project.build_path()), "dart")
end
@doc """
@@ -111,16 +137,23 @@ defmodule DartSass do
is not available.
"""
def bin_version do
- {path, args} = sass_cmd()
+ {path, args} = sass(["--version"])
with true <- File.exists?(path),
- {result, 0} <- System.cmd(path, args ++ ["--version"]) do
+ {result, 0} <- System.cmd(path, args) do
{:ok, String.trim(result)}
else
_ -> :error
end
end
+ defp sass(args) do
+ case bin_paths() do
+ {sass, nil} -> {sass, args}
+ {vm, snapshot} -> {vm, [snapshot] ++ args}
+ end
+ end
+
@doc """
Runs the given command with `args`.
@@ -139,22 +172,20 @@ defmodule DartSass do
stderr_to_stdout: true
]
- {bin_path, bin_args} = sass_cmd()
+ {sass_path, args} = sass(args ++ extra_args)
- bin_path
- |> System.cmd(bin_args ++ args ++ extra_args, opts)
+ sass_path
+ |> System.cmd(args, opts)
|> elem(1)
end
@doc """
- Installs, if not available, and then runs `dart_sass`.
+ Installs, if not available, and then runs `sass`.
Returns the same as `run/2`.
"""
def install_and_run(profile, args) do
- bin_path = DartSass.bin_path()
-
- unless File.exists?(bin_path) do
+ unless installed?() do
install()
end
@@ -162,7 +193,7 @@ defmodule DartSass do
end
@doc """
- Installs dart_sass with `configured_version/0`.
+ Installs dart-sass with `configured_version/0`.
"""
def install do
version = DartSass.configured_version()
diff --git a/lib/mix/tasks/sass.install.ex b/lib/mix/tasks/sass.install.ex
index 6de5950..073d7ca 100644
--- a/lib/mix/tasks/sass.install.ex
+++ b/lib/mix/tasks/sass.install.ex
@@ -23,7 +23,7 @@ defmodule Mix.Tasks.Sass.Install do
def run(args) do
case OptionParser.parse_head!(args, strict: [if_missing: :boolean]) do
{opts, []} ->
- if opts[:if_missing] && File.exists?(DartSass.bin_path()) do
+ if opts[:if_missing] && DartSass.installed?() do
:ok
else
if Code.ensure_loaded?(Mix.Tasks.App.Config) do