shahryarjb

shahryarjb

Hi, Please consider you have a project in GitHub and before running and getting it with mix deps task you want to get some basic information like what is the version and name of this library from mix.exs file.

So regex is not a good way because some people put version as global variable like @version, hence I need to run whole the mix.exs and get the version and name from it.

What is your suggesting to get mix.exs as string from GitHub and compile it? For example, and get the name and version from it or another information before installing it.


I do not know Code.eval_string(code) is useful for me and safe? All the examples in the document is about a function or operation, but not about a full module:

{:module, MishkaDeveloperTools.MixProject,
 <<70, 79, 82, 49, 0, 0, 11, 156, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 5,
   0, 0, 0, 25, 38, 69, 108, 105, 120, 105, 114, 46, 77, 105, 115, 104, 107, 97,
   68, 101, 118, 101, 108, 111, 112, 101, 114, ...>>, {:package, 0}}

After evaluating the module, I do not know how to get version for example:

MishkaDeveloperTools.MixProject.project()

Thank you in advance

Showing Posts 1 to 10

hst337

hst337

Code.eval_string is not safe. I’d suggest Code.string_to_quoted

shahryarjb

shahryarjb OP

Thank you, it converts AST, right? So after that, how can I run this module with a random name because it is possible there is a module same name.

Or use something Macro.postwalk ?! To get version and name? But without running this module how can get version for example, the developer load it with some names as global variable like @version

hst337

hst337

You can just travese AST with functions provided in macro. You should look for field version in project funcion. If it contains, @version, than look for @version attirbute. If it contains something else, than there’s no safe way to fetch a version without executing code defined in this module

shahryarjb

shahryarjb OP

Hi, I want to get some value from a module which is AST, and why I want to do this you can see this post Getting basic information of an elixir project from GitHub.

for example I have something like this:

code = """
defmodule MishkaDeveloperTools.MixProject do
  use Mix.Project
  @version "0.0.7"

  def project do
    [
      app: :mishka_developer_tools,
      version: @version,
      elixir: "~> 1.13",
      name: "Mishka developer tools",
      elixirc_paths: elixirc_paths(Mix.env()),
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      description: description(),
      package: package(),
      homepage_url: "https://github.com/mishka-group",
      source_url: "https://github.com/mishka-group/mishka_developer_tools",
    ]
  end
end
"""

for example, I want to get the values of version and elixir and app, so I used Macro.postwalk and I do not know it is the right way to do this:

{:ok, ast} = Code.string_to_quoted(code)
ast
|> Macro.postwalk(fn
......
end)

When I print this, I can see something like this:

{:defmodule, [line: 1],
 [
   {:__aliases__, [line: 1], [:MishkaDeveloperTools, :MixProject]},
   [
     do: {:__block__, [],
      [
        {:use, [line: 2], [{:__aliases__, [line: 2], [:Mix, :Project]}]},
        {:@, [line: 3], [{:version, [line: 3], ["0.0.7"]}]},
        {:def, [line: 5],
         [
           {:project, [line: 5], nil},
           [
             do: [
               app: :mishka_developer_tools,
               version: {:@, [line: 8], [{:version, [line: 8], nil}]},
               elixir: "~> 1.13",
               name: "Mishka developer tools",
               elixirc_paths: {:elixirc_paths, [line: 11],
                [
                  {{:., [line: 11], [{:__aliases__, [line: 11], [:Mix]}, :env]},
                   [line: 11], []}
                ]},
               start_permanent: {:==, [line: 12],
                [
                  {{:., [line: 12], [{:__aliases__, [line: 12], [:Mix]}, :env]},
                   [line: 12], []},
                  :prod
                ]},
               deps: {:deps, [line: 13], []},
               description: {:description, [line: 14], []},
               package: {:package, [line: 15], []},
               homepage_url: "https://github.com/mishka-group",
               source_url: "https://github.com/mishka-group/mishka_developer_tools"
             ]
           ]
         ]}
      ]}
   ]
 ]}

After that, I tried to get project function like this:

Macro.postwalk(ast, fn
  {:defmodule, _line ,[_aliases,[do: {:__block__, _, [
    {:use, [line: 2], [{:__aliases__, [line: 2], [:Mix, :Project]}]},
    {:@, [line: 3], [{:version, [line: 3], ["0.0.7"]}]},
    {:def, [line: 5], block}
  ]}]]} -> IO.inspect block
  v -> v
end)

It is hard code and I do not know what is my user mix.exs file is? So I tried to delete some lanes like:

{:use, [line: 2], [{:__aliases__, [line: 2], [:Mix, :Project]}]},
{:@, [line: 3], [{:version, [line: 3], ["0.0.7"]}]},

but the pattern is not matched, and always the v pattern is run

Macro.postwalk(ast, fn
  {:defmodule, _line ,[_aliases,[do: {:__block__, _, [{:def, [line: 5], block}]}]]} -> IO.inspect block
  v -> v
end)

I think I don’t have enough understanding about the AST, how can I do it and get another lane in my source because if I have something like this: version: @version, I need to back and get @version "0.0.7" for example

Thank you in advance

shahryarjb

shahryarjb OP

I could be able to get values with this:

{:ok, ast} = Code.string_to_quoted(code)
Macro.postwalk(ast, fn
  {:def, _line, [{:project, _, _}, [do: fun]]} -> {Keyword.get(fun, :app), Keyword.get(fun, :version)}
  v -> v
end)

But how can access just this tuple? {Keyword.get(fun, :app), Keyword.get(fun, :version)} and skip the others?

LostKobrakai

LostKobrakai

The AST of a keyword list is not a keyword list by itself, so you cannot use Keyword to access things. AST is an abstraction of written code, and not of the data represented by that code. You’ll need more manual approaches for filtering out the information you need.

A rather naive appraoch to getting to the information you seek would be like this:

{_ast, acc} =
  Macro.postwalk(ast, %{version: nil, attributes: %{}}, fn
    {:@, _, [{name, _, value}]} = ast, acc when is_atom(name) and not is_nil(value) ->
      {ast, put_in(acc.attributes[name], value)}

    {:version, {:@, _, [{name, _, nil}]}} = ast, acc ->
      {ast, Map.put(acc, :version, {:attribute, name})}

    {:version, value} = ast, acc ->
      {ast, Map.put(acc, :version, value)}

    ast, acc ->
      {ast, acc}
  end)

acc
# %{attributes: %{version: ["0.0.7"]}, version: {:attribute, :version}}

However you’d want to be more cautious as currently any later keyword list with a :version key would overwrite the returned result.

What you’re doing here is very brittle, as you’re trying to interpret code without running it, which can easily break by someone not sticking to conventions closely.

shahryarjb

shahryarjb OP

Thank you, but do you have any suggestion? Because I need to get some basic information about the dep they want to install and after that I pass this operation to Mix deps.get

LostKobrakai

LostKobrakai

No. There’s no safe way to do that for all possible mix.exs files. Mix itself doesn’t use versions for git dependencies at all, it uses branches or git sha’s.

shahryarjb

shahryarjb OP

Dear @LostKobrakai, do you have any tutorial especially in elixir to learn work with AST, I am thinking if I had no access to internet I could not solve this problem! I have read Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!) by Chris McCord book

LostKobrakai

LostKobrakai

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews