shahryarjb

shahryarjb

Create and replace mix.exs file

Hi my friends, Imaging you have a mix file which you want to change part of it and replace after doing your changes.

For example

defmodule Dvote.MixProject do
  use Mix.Project

  def project do
    [  ..  ]
  end

  def application do
    [..... ]
  end

  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

  defp deps do
    [
      {:phoenix, "~> 1.6.6"},
      {:mishka_installer, git: "https://github.com/mishka-group/mishka_installer.git"}
    ]
  end

  defp aliases do
    [.... ]
  end
end

Now I just want to change deps function and keep the other same as the orginal file and create a new mix.exs file.

  defp deps do
    [
      {:phoenix, "~> 1.6.6"},
      {:mishka_installer, git: "https://github.com/mishka-group/mishka_installer.git"},
      {:new_plug, "~> 1.0.0"}
    ]
  end

So I need suggestions how to do this! Regex? Or the other way, you can offer me?!
After creating this file in runtime!! So we need to compile whole the project because it is exs file? Or we can hot reload with a way?!

Why I need something like this?

I let my user add a dep from hex site in runtime, but I need to change the mix file auto with code and do not make user get involved to changing this file manually

By the way, I load my new deps from a json file

Thank you

The simple code for this: https://github.com/mishka-group/mishka_installer/blob/master/lib/installer/run_time_sourcing.ex

Most Liked

lud

lud

Remember that your mix.exs file is code. That means that the deps function can return anything from anywhere, including using anything that is present in the erlang distribution like xmerl to read XML files for instance.

You could then define the dependencies in an xml file, which could allow more control of the data.

So you want to provide an app that can be added new dependencies to, at runtime? What is is exactly that you are trying to do? If the code added that way is not needed for the project to run then it is not an actual dependency ; maybe you need to implement a plugin solution for instance?

shahryarjb

shahryarjb

Hi dear @lud, I talked about this way I want to do in these post, I am open to receive any suggestion

and

and

in this video, you can see a simple demo

with these line I add a dep or delete it from my source
https://github.com/mishka-group/mishka_installer/blob/master/lib/installer/run_time_sourcing.ex#L10-L30

So, my user need to add his package into deps function and I create this for him/her

https://github.com/mishka-group/mishka_installer/blob/master/lib/installer/dep_handler.ex#L116-L127

So all the plugins are called from a JSON file which is created from my database

for example

  %MishkaInstaller.Installer.DepHandler{
    app: "mishka_social",
    version: "0.0.2 ",
    type: "hex",
    url: "https://hex.pm/packages/mishka_social",
    git_tag: nil,
    custom_command: nil,
    dependency_type: "force_update",
    update_server: nil,
    dependencies: [
      %{app: :phoenix, min: "1.6"},
      %{app: :phoenix_live_view, max: "0.17.7", min: "0.17.7"},
      %{app: :ueberauth, max: "0.17.7", min: "0.17.7"},
      %{app: :ueberauth_github, min: "0.8.1"},
      %{app: :ueberauth_google, min: "0.10.1"},
    ]
  }

So the only problem exist here that is keeping state when a dep was installed before. So my library let users register an event; before compiling I send a name of app and some information, if an app needs to save something, it prevents to force_update, if not it is going to be run the bottom function.

  # If you have a Task in your project you can load it in a list like [{:task_one, "ecto.migrate"}], it should be a list
  @spec deps(list()) :: list
  def deps(custom_command \\ []) when is_list(custom_command) do
    [{:get, "deps.get"}, {:compile, "deps.compile"}] ++ custom_command
    |> Enum.map(fn {operation, command} ->
      {stream, status} = System.cmd("mix", [command], into: IO.stream(), stderr_to_stdout: true)
      %{operation: operation, output: stream, status: status}
    end)
  end

In this function, when the lib gets an answer, it sends a pubsu to used admin dashboard

https://github.com/mishka-group/mishka_installer/blob/master/lib/installer/dep_changes_protector.ex#L83-L97

A simple dashboard I’m developing it: https://github.com/mishka-group/mishka_installer/blob/master/lib/installer/live/dep_getter.ex after that I add it in my CMS: GitHub - shahryarjb/mishka-cms: MishkaCms an open source and real time API base CMS Powered by Elixir and Phoenix

My solution for event

lud

lud

Ok so it looks like it is working, based on your demo.

I would still add special code to mix.exs to fetch deps from another file instead of modifying mix.exs itself. It can be an XML or it can be a binary file generater with :erlang.term_to_binary(new_deps).

create a new mix.exs file.

If you absolutely need to generate a new file then you can just change the path of the xml (or whatever format) in the mix.exs source. With the following code:

@xml_path "path/to/file.xml"

def deps do
    [
        {:a, "~> 1.2"},
        {:b, "~> 0.1.1"}
    ]
    ++ plugin_deps(@xml_path)
end

…a regex to update @xml_path "path/to/file.xml" is easy.

mayel

mayel

You may be interested in reading the code we use for something similar: GitHub - bonfire-networks/mess: Simple, file-based dependency management with git and local overrides.

Where Next?

Popular in Questions Top

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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement