BartOtten

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

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

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

zachdaniel

Creator of Ash

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
sodapopcan

sodapopcan

@BartOtten already mentioned the env variable approach which I do like better. I’m certainly aware mix.exs is just code which I thought was implied by my saying I already considered @Nicodemus’s approach (well, same goes for checking an env var, really).

BartOtten

BartOtten

So far my top pick: Demo code in same repo (excluded from Hex package)

Pre:

  • use demo app for integration test
  • (major) 1 clone and easy hacking in both example as lib
  • no split commits; no sync issues

——-

Also on the table: dotfile + env var

  • Presence check a dotfile which is .gitignored. (Default). If available, use path: …/my_lib
  • If env set and path exists, use path of env var. (Override)
  • If env set and false: use Hex (Override)

Pre over only env variable:

  • no need for an env-setter.
  • no clobbering ~.profile etc.
  • shell and terminal agnostic.
  • Easy (un)set: touch .local / rm .local.

Pre over only path check:

  • can set set non-default path.
  • can force Hex.

no env, no file == no possible path execution

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement