Commit b4536fc735b77f8986fcd16b76d3bc0a37009fdf

Michael Crumm 2021-07-30T15:29:57

Fix installation path/unzip on windows (#3) * Fix path to windows .zip archive * Fix unpacking .zip archive

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01a3d99..3c6ad17 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## v0.1.1 (Unreleased)
 
+  * Fix installation path/unzip on windows
   * Add wrapper script to address zombie processes
 
 ## v0.1.0 (2021-07-25)
diff --git a/lib/dart_sass.ex b/lib/dart_sass.ex
index db2bca7..2088d1f 100644
--- a/lib/dart_sass.ex
+++ b/lib/dart_sass.ex
@@ -210,9 +210,9 @@ defmodule DartSass do
 
     name = "dart-sass-#{version}-#{target()}"
     url = "https://github.com/sass/dart-sass/releases/download/#{version}/#{name}"
-    tar = fetch_body!(url)
+    archive = fetch_body!(url)
 
-    case :erl_tar.extract({:binary, tar}, [:compressed, cwd: tmp_dir]) do
+    case unpack_archive(Path.extname(name), archive, tmp_dir) do
       :ok -> :ok
       other -> raise "couldn't unpack archive: #{inspect(other)}"
     end
@@ -235,11 +235,22 @@ defmodule DartSass do
     end
   end
 
+  defp unpack_archive(".zip", zip, cwd) do
+    with {:ok, _} <- :zip.unzip(zip, cwd: cwd), do: :ok
+  end
+
+  defp unpack_archive(_, tar, cwd) do
+    :erl_tar.extract({:binary, tar}, [:compressed, cwd: cwd])
+  end
+
   # Available targets: https://github.com/sass/dart-sass/releases
   defp target do
     case :os.type() do
       {:win32, _} ->
-        "windows-#{:erlang.system_info(:wordsize) * 8}.zip"
+        case :erlang.system_info(:wordsize) * 8 do
+          32 -> "windows-ia32.zip"
+          64 -> "windows-x64.zip"
+        end
 
       {:unix, osname} ->
         arch_str = :erlang.system_info(:system_architecture)