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 · GitHub

My solution for event

https://github.com/mishka-group/mishka_installer

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

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
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement