-
-
Save nivertech/7242965 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule CommandSigil do | |
| # 'x' is for 'execute'. | |
| defmacro sigil_x({ :<<>>, _line, [string] }, []) when is_binary(string) do | |
| string | |
| |> Macro.unescape_string() | |
| |> String.to_char_list!() | |
| |> :os.cmd() | |
| end | |
| defmacro sigil_x({ :<<>>, line, pieces }, []) do | |
| binary = { :<<>>, line, Macro.unescape_tokens(pieces) } | |
| quote do | |
| unquote(binary) | |
| |> String.to_char_list!() | |
| |> :os.cmd() | |
| end | |
| end | |
| defmacro sigil_X({ :<<>>, _line, [string] }, []) when is_binary(string) do | |
| string | |
| |> String.to_char_list!() | |
| |> :os.cmd() | |
| end | |
| end | |
| defmodule CommandSigil.Test do | |
| import CommandSigil | |
| def test_sigil_with_var do | |
| dir = "~/" | |
| %x"ls -al #{dir}/*" | |
| end | |
| def test_sigil_interpolated do | |
| %x"echo Hello \x45\x6c\x69\x78\x69\x72\x21" # => 'Hello Elixir!\n' | |
| end | |
| def test_sigil_non_interpolated do | |
| %X"echo Hello \x45\x6c\x69\x78\x69\x72\x21" # => 'Hello x45x6cx69x78x69x72x21\n' | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment