Built a file parser, how to include it in my Phoenix app/deps?

HOW? Is there an outline I can follow? I have build a file parser I need to pipe into phoenix. I have included it in the deps but its not found in the namespace… I have zero idea what Im doing wrong so any thing would be helpful.

A tutorial would be nice!

How did You do that?

{:parser, path: "./lib/parser"}

In Phx mix.exs

Is there a better way? How can I bring a module (set of modules) into the namespace?

It’s not a dependency if it is already in lib…
It should be a simple module.

I put them outside the project tree, like this

{:parser, path: "../plugins/parser"}
1 Like

How then do I access the modules? Can you give me an outline of the expected project structure so that when the dep is loaded its is available in the name space?

You did not say how… did You use mix new? is it a simple file?

Or show the code…

1 Like

its multi module. I started with a simple file / Code.require_file(path) model. Im some where between there and post mix new. I created a new project with mix new then just moved the files into lib. I literally have no idea how this works…

If it’s in the lib directory then it’s already a part of your project and you should remove that entry from the deps part of the mix.exs file. Everything inside lib gets automatically compiled, you don’t need to lift a finger about it.

If you want to use it as an external dependency then you either put it outside of your project’s directory entirely (as @kokolegorille is showing you) or put it in a separate GitHub repo and refer to it in mix.exs like that:

  {:parser, github: "your_name/your_project"}
2 Likes

I dont understand… Its in lib > parser when I try to parse I get

function LogParser.route/1 is undefined (module LogParser is not available)

That’s likely because you need to alias it to use it.

Show us the first line definition of LogParser, please?

Example: it might be e.g. defmodule SomeLibrary.LogParser do ... in which case you need to have alias SomeLibrary.LogParser at the top of the Elixir file in which you want to use it.

defmodule LogParser do

Ive used import and alias where I intend to use it… and get:

Should work. The only way we can help you is if you show us the exact file tree (not all of it, just a few relevant files) and show us the using code.

Technically, if you have this:

lib/log_parser/log_parser.ex:

defmodule LogParser do
  def route(something) do
    # ...
  end
end

and then f.ex. lib/my_app/some_module.ex:

defmodule MyApp.SomeModule do
  def whatever() do
    LogParser.route("something")
  end
end

Then it all should work fine.

lib > parser > [parser.exs, jobs.exs, json.exs, results.exs, …etc]

Im calling the parser from a controller. Might that have something to do with it?

EDIT: the controller works fine w/o calling the LogParser.route

Ah, here’s the problem: the .exs files are scripts and they are not compiled. They are only meant to be run directly.

Rename all these files from .exs to .ex and it should work.

2 Likes

It’s completely possible, but using exs extension does not work

1 Like

For future reference: Modules and functions - The Elixir programming language

And: Elixir: When to use .ex and when .exs files - Stack Overflow

1 Like

Because Im curious and wish for this post to be more useful. What are the necessary components of a dep, assuming I use mix new?

deps are like libraries in other languages. Usually it’s code you didn’t write, or code that you might want to share across multiple projects and repos.

In Elixir they come in two flavours

  1. A “library” - a bundle of modules and functions and data OR
  2. An “application” - which is something that starts its own supervision tree, or which runs under a supervisor and so must be “started” when your app starts.

An example of 1. might be decimal
An example of 2 might be prometheus

Most of the time deps are imported from Hex which is Elixir’s package manager. So adding a dep (usually) instructs mix (the build tool) to fetch the dep from Hex and compile it as part of your application.

If instead you specify a dep like this:

{:parser, github: "your_name/your_project"}

You are telling mix to get the dep from github. You can also do this:

{:parser, path: "local/path/to/repo"}

But that is usually only for local debugging.

So in general, you would expect code you write to first just exist in lib. If you need to share across multiple projects you can create a library and publish it on Hex.

3 Likes