dkuku

dkuku

How to run mix task without compilation?

I’m trying to run a task before the app is compiled so I can patch the source of dependency.
mix deps.get runs this way and I can’t figure out how to do this?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you need to change the source code of a dependency, I would copy the source code to another directory or git repo that you manage, edit the source code there, and then point the mix.exs deps entry to that new source location.

I think trying to edit it in the deps directory directly with a mix task is going to be swimming upstream from how it’s designed to be used.

mayel

mayel

I like to make my mix tasks double up as escripts for this purpose:

# lib/mix/tasks/secrets/mix.exs
defmodule Bonfire.Secrets do
  use Mix.Project

  def project do
    [
      app: :secrets,
      version: "0.1.0-alpha.1",
      elixir: "~> 1.11",
      escript: [main_module: Mix.Tasks.Bonfire.Secrets]
    ]
  end
end

# lib/mix/tasks/secrets/lib/secrets.ex
defmodule Mix.Tasks.Bonfire.Secrets do
  @shortdoc "Generates some secrets"

  @moduledoc """
  Generates random secrets and prints to the terminal.
  > mix bonfire.secrets [length]

  Can also compile/run it as an escript (without having to first compile the app):
  > cd lib/mix/tasks/secrets && mix escript.build && ./secrets [length]
  """
  use Mix.Task

  # for running as escript
  def main(args) do
    run(args)
  end

  @doc false
  def run([]), do: run(["64"])

  def run([int]),
    do: int |> parse!() |> random_string() |> Kernel.<>("\r\n") |> IO.puts()

  def run([int, iterate]), do: for(_ <- 1..parse!(iterate), do: run([int]))
  def run(args), do: nil

  defp parse!(int) do
    case Integer.parse(int) do
      {int, ""} -> int
      _ -> 64
    end
  end

  defp random_string(length) when length > 31 do
    :crypto.strong_rand_bytes(length)
    |> Base.encode64()
    |> binary_part(0, length)
  end
end
dkuku

dkuku

thanks, It’s useful

Where Next?

Popular in Discussions Top

vans163
So useless benchmarks aside, Its possible to write a webserver that can serve 300k requests per second (perhaps more with optimizations)....
New
andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 13924 124
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement