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
Trending in Questions
Other Trending 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
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hst337
Code.eval_stringis not safe. I’d suggestCode.string_to_quotedshahryarjb
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@versionhst337
You can just travese AST with functions provided in macro. You should look for field
versioninprojectfuncion. If it contains,@version, than look for@versionattirbute. If it contains something else, than there’s no safe way to fetch a version without executing code defined in this moduleshahryarjb
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:
for example, I want to get the values of
versionandelixirandapp, so I usedMacro.postwalkand I do not know it is the right way to do this:When I print this, I can see something like this:
After that, I tried to get
projectfunction like this:It is hard code and I do not know what is my user
mix.exsfile is? So I tried to delete some lanes like:but the pattern is not matched, and always the
vpattern is runI 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 exampleThank you in advance
shahryarjb
I could be able to get values with this:
But how can access just this tuple?
{Keyword.get(fun, :app), Keyword.get(fun, :version)}and skip the others?LostKobrakai
The AST of a keyword list is not a keyword list by itself, so you cannot use
Keywordto 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:
However you’d want to be more cautious as currently any later keyword list with a
:versionkey 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
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.getLostKobrakai
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
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