Code examples of plugins

131 views
Skip to first unread message

Booker Bense

unread,
Apr 13, 2015, 8:32:19 PM4/13/15
to elixir-l...@googlegroups.com
My project elixgrep has the ability to use plugins, but I'm not very happy with the way it works. 

It basically reads the plugin name from the cmd line and recompiles and swaps in a new version 
of an existing module. 

I'm pretty sure Behaviours is where I want to start, but I was wondering if there are code examples out
there of how to load code at run time and call function X from that module.  

thanks 

- Booker C. Bense 

Vincent Siliakus

unread,
Apr 14, 2015, 5:49:28 AM4/14/15
to elixir-l...@googlegroups.com
Not an actual example, but assuming that a plugin is a single file, I think Code.load_file/2 would be best suited for this.

I'm not sure if behaviours will help ensuring that a plugin/module exports the expected functions
, because when a module fails to correctly implement a behaviour, the compiler just emits a warning, that you, afaik, can't catch when calling Code.load_file/2. An alternative would be using MyPlugin.__info__(:functions) and check if the module exports the expected functions.

-vincent

Booker Bense

unread,
Apr 14, 2015, 5:01:47 PM4/14/15
to elixir-l...@googlegroups.com


On Tuesday, April 14, 2015 at 2:49:28 AM UTC-7, Vincent Siliakus wrote:
Not an actual example, but assuming that a plugin is a single file, I think Code.load_file/2 would be best suited for this

I'm currently using Code.require_file. 

 
I'm not sure if behaviours will help ensuring that a plugin/module exports the expected functions, because when a module fails to correctly implement a behaviour, the compiler just emits a warning, that you, afaik, can't catch when calling Code.load_file/2. An alternative would be using MyPlugin.__info__(:functions) and check if the module exports the expected functions.

I think Mix.Tasks provides most of what I'm looking for as an example. I was mostly interested in how you go from 
a string to a Module that you can query. What I do currently is have each plugin simply reimplement the same Module. 

- Booker C. Bense 

José Valim

unread,
Apr 14, 2015, 5:10:54 PM4/14/15
to elixir-l...@googlegroups.com
Doesn't Code.require_file returns the modules that were loaded in the file? If so, you can use this information to find out which modules were defined. Then you could traverse them looking up for some module that implements your particular function with function_exported?/3 and fail if you got none or more than one.



José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/2eaee920-7a36-4d7d-a2a5-b889fb0faa81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Booker Bense

unread,
Apr 15, 2015, 2:49:14 PM4/15/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br


On Tuesday, April 14, 2015 at 2:10:54 PM UTC-7, José Valim wrote:
Doesn't Code.require_file returns the modules that were loaded in the file? If so, you can use this information to find out which modules were defined. Then you could traverse them looking up for some module that implements your particular function with function_exported?/3 and fail if you got none or more than one.

Yes, that's exactly what I need to do. I was just hoping that someone had already done the dirty work for me... 

- Booker C. Bense 

Booker Bense

unread,
Apr 19, 2015, 1:14:51 PM4/19/15
to elixir-l...@googlegroups.com
I have implemented a first pass at this in 


Comments are welcome. 

- Booker C. Bense

Gabriele Lana

unread,
Apr 20, 2015, 11:50:28 AM4/20/15
to elixir-l...@googlegroups.com
On Sun, Apr 19, 2015 at 7:14 PM, Booker Bense <bbe...@gmail.com> wrote:
> I have implemented a first pass at this in
>
> https://github.com/bbense/pluginator

That’s cool +1

> Comments are welcome.

Would be nice to have a function that loads every module that is
compliant with a signature in a directory

--
Gabriele Lana
https://github.com/gabrielelana
https://twitter.com/gabrielelana

Booker Bense

unread,
Apr 20, 2015, 2:22:48 PM4/20/15
to elixir-l...@googlegroups.com


On Monday, April 20, 2015 at 8:50:28 AM UTC-7, Gabriele Lana wrote:


Would be nice to have a function that loads every module that is
compliant with a signature in a directory



I can do load every .exs file in a directory and of those files return a list of the modules that
match a signature. However all the modules would be loaded, not just those that match. 


- Booker C. Bense 

Gabriele Lana

unread,
Apr 23, 2015, 10:15:07 AM4/23/15
to elixir-l...@googlegroups.com
You can unload them :-) see code:delete and code:purge (if I remember correctly) in Erlang docs
 
- Booker C. Bense 

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/cf2d83b6-ca0a-4066-8867-12cba8b23567%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages