BartOtten
How to switch between local or Hex dependency?
For my public libs I have created demo apps. During development the demo app should depend on the development version of the library; so I use path: ../my_lib. However, when the demo app is cloned by a user it should depend on the Hex version instead.
Currently I set an environment flag which toggles the location of the dependency, but it keeps nagging in my head: this should be easier. Maybe detecting the existence of a file not committed to Git..?
Anyone having a better idea?
Ps. Detecting :dev runtime environment is not gonna cut it, as the user will probably run the demo in :dev too.
Most Liked
Nicodemus
I do this in the demo for one of my projects, in “mix.exs”:
defp deps do
[
{:phoenix, "~> 1.7.14"},
...,
my_lib_dep()
]
end
defp my_lib_dep() do
if File.exists?(Path.join(__DIR__, "../my_lib/mix.exs")) do
{:my_lib, path: "../my_lib"}
else
{:my_lib, "~> 0.1.0"}
end
end
Or you can append an entire list to the end if you have multiple deps. The nice thing about “mix.exs” is that it’s code, and is executed every time you run mix.
BartOtten
Current implementation is like this. I did commit an .envrc file which sets the path. It seems checking path existence is the only ‘fully native’ solution. Combining it with env override and a warning might hit the sweet spot.
defp routex_dep() do
if path = System.get_env("ROUTEX_PATH") do
IO.puts(">> !! USING LOCAL ROUTEX PATH !! <<")
{:routex, path: path}
else
{:routex, ">= 0.0.0"}
end
end
zachdaniel
Something that might be useful is that Mix.env() returns :prod when your package is compiled for use in a user’s application.
So
@dependency if Mix.env() == :prod do
{:package, "~> ..."}
else
{:package, path: "..."}
end
Last Post!
sodapopcan
Ya, I wasn’t clear on this—I actually have multiple in mine (two) and it’s only annoying as it adds to the grepping madness. Of course it’s simple to mitigate in different ways and you get used to it pretty quickly, it just adds another a thing you have to think about.
EDIT: I meant “I wasn’t clear on this.” Fixed.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









