Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies •...
Transcript of Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies •...
![Page 1: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/1.jpg)
Elixir ToolingExploring Beyond the Language
@emjii
![Page 2: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/2.jpg)
What’s happening?• 1.0
• ElixirConf
• Hex
![Page 3: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/3.jpg)
Topics• Mix
• Hex
• IEx
• ExUnit
• Standard Library
![Page 4: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/4.jpg)
Mix
![Page 5: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/5.jpg)
Mix• Generate new projects
• Compile
• Run tests
• Handle dependencies
• Whatever else you can think of
![Page 6: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/6.jpg)
mix new
![Page 7: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/7.jpg)
mix new
![Page 8: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/8.jpg)
mix.exs!defmodule MyProject.Mixfile do use Mix.Project def project do [ app: :my_project, version: "0.1.0", elixir: "~> 0.14.0" ] end end
![Page 9: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/9.jpg)
Compilation• Generates .beams and .app
• Compiles erlang code
• And also .leex & .yecc files
![Page 10: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/10.jpg)
Dependencies!defp deps do [ { :poolboy, github: "devinus/poolboy" }, { :ecto, "~> 0.2.0" } ] end
![Page 11: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/11.jpg)
Dependencies• $ mix deps
• Converger
• Repeatable builds
• Rebar dependencies
![Page 12: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/12.jpg)
Extending Mixdefmodule Mix.Tasks.MyTask do use Mix.Task def run(args) do IO.puts "Hello world!" end end $ mix my_task Hello world!
![Page 13: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/13.jpg)
Extending Mix• ecto (github.com/elixir-lang/ecto)
- $ mix ecto.gen.migration
- $ mix ecto.migrate
• exrm (github.com/bitwalker/exrm)
- $ mix release
![Page 14: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/14.jpg)
Umbrella projects• apps/*
• Isolated applications
• Recursive tasks
![Page 15: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/15.jpg)
Hex
![Page 16: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/16.jpg)
![Page 17: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/17.jpg)
Hex tasks
![Page 18: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/18.jpg)
Dependency resolution• Find the latest version that satisfies
all requirements
• Use the lockfile
• Honour overrides
![Page 19: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/19.jpg)
Future work• Erlang support
• Installing executables
![Page 20: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/20.jpg)
IEx
![Page 21: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/21.jpg)
iex -S mix• $ mix run
• Loads configs
• Loads and starts dependencies
![Page 22: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/22.jpg)
pry• Hook into a running process
• Inspired by ruby’s pry
![Page 23: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/23.jpg)
pry
![Page 24: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/24.jpg)
First class docs
![Page 25: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/25.jpg)
ExUnit
![Page 26: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/26.jpg)
The assert macro!defmodule SampleTest do use ExUnit.Case test "the truth" do assert {:ok, _} = foo() end defp foo do :nope end end
![Page 27: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/27.jpg)
The assert macro!quote do assert {:ok, _} = foo() end {:assert, [], [ {:=, [], [ {:ok, {:_, [], nil}}, {:foo, [], []} ]} ]}
![Page 28: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/28.jpg)
Beautiful failures
![Page 29: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/29.jpg)
Tags & Filters!defmodule SampleTest do use ExUnit.Case @tag :integration test "the truth" do # call some expensive service end end
![Page 30: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/30.jpg)
Tags & Filters• —only / —include / —exclude
• $ mix test test/sample_test.exs:5
![Page 31: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/31.jpg)
Doctests@doc """ Returns a new collection, where each item is the result of invoking `fun` on each corresponding item of `collection`. For dicts, the function expects a key-‐value tuple. ## Examples iex> Enum.map([1, 2, 3], fn(x) -‐> x * 2 end) [2, 4, 6] iex> Enum.map([a: 1, b: 2], fn({k, v}) -‐> {k, -‐v} end) [a: -‐1, b: -‐2] """ @spec map(t, (element -‐> any)) :: list def map(collection, fun) do
![Page 32: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/32.jpg)
Standard library
![Page 33: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/33.jpg)
Stream• Composable, lazy collections
• Implements Enumerable protocol
![Page 34: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/34.jpg)
Read file by linedef read(filename) do read_device(File.open!(filename)) end def read_device(device) do case IO.read(device) do :eof -‐> :ok line -‐> operation(line) read_device(device) end end
![Page 35: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/35.jpg)
Streaming IO!File.stream!(filename) |> Enum.each(&operation/1)
![Page 36: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/36.jpg)
Streaming GenEvent!stream = GenEvent.stream(pid) # Take the next 10 events Enum.take(stream, 10) # Print all remaining events for event <-‐ stream do IO.inspect event end
![Page 37: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/37.jpg)
Extending OTP• OTP’s great
• No high-level abstractions
• What exists in other languages?
![Page 38: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/38.jpg)
Agent• Abstraction around state
• Inspired by Clojure
• Builds on GenServer
![Page 39: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/39.jpg)
Agent!defmodule Cache do def start_link do Agent.start_link(fn -‐> HashDict.new end) end def put(pid, key, value) do Agent.update(pid, &Dict.put(&1, key, value)) end def get(pid, key) do Agent.get(pid, &Dict.get(&1, key)) end end
![Page 40: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/40.jpg)
Agent-‐module(sample). -‐export([start_link/0, put/3, get/2]). -‐define(Agent, 'Elixir.Agent'). -‐define(HashDict, 'Elixir.HashDict'). start_link() -‐> ?Agent:start_link(fun() -‐> ?HashDict:new() end). put(Pid, Key, Value) -‐> ?Agent:update(Pid, fun(Dict) -‐> ?HashDict:put(Dict, Key, Value) end). get(Pid, Key) -‐> ?Agent:get(Pid, fun(Dict) -‐> ?HashDict:get(Dict, Key) end).
![Page 41: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/41.jpg)
Task• Asynchronous tasks
• Small, single action
![Page 42: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/42.jpg)
Task.async & Task.await!task = Task.async(&do_some_work/1) res = do_some_other_work() res + Task.await(task)
![Page 43: Elixir Tooling...• Generate new projects • Compile • Run tests • Handle dependencies • Whatever else you can think of mix new mix new ...](https://reader033.fdocuments.in/reader033/viewer/2022043017/5f39b241bf7ecc6d7e0fa6af/html5/thumbnails/43.jpg)
?@emjii