smon

smon

Hi everyone,

I was thinking of resources like images, XML files or documents in other text formats. Is there something similar to the resources folder in Java (“src/main/resources”)? The goal would be to add files to my package that can be accessed from code on runtime.

Or is this somehow a very un-Elixir-y way of thinking? Elixir is my first functional language - so what would be the “right” way to do it?

Cheers,
smon

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey!

That is precisely what the /priv folder is for in your project root. It’ll get packaged up along with your application when you build a release. If you have a data.xml file in your /priv directory you can get to it in your code by doing

Path.join(:code.priv_dir(:my_app), "data.xml")
13
Post #1
smon

smon OP

Awesome!

Thank you very much. priv is an abbreviation of what exactly? “private”? Or am I missing some more obvious term it stands for?

Cheers,
smon

david

david

Erlang OTP Design Principles User’s Guide at Directory Structure

priv - Used for application specific files. For example, C executables are placed here. The function code:priv_dir/1 should be used to access this directory.

Per Jose:

“priv” is like “OTP” where its name made sense at the beginning but today it has grown beyond that. All it matters now is that we put in the “priv” directory any artifact that you need in production alongside your code.

Elixir Lang Talk - What is Priv?

smon

smon OP

I am unable to open a File.stream! for the file in my /priv folder when running my escript-build. Do I have to fallback to Erlang? Using a path generated the way you described only works when starting the application in iex.

Edit

From the Mix.Tasks.Escript.Build:

Note: escripts do not support projects and dependencies
that need to store or read artifacts from the priv directory.

Damn…

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I have it on good authority that http://elixir-lang.org/docs/master/elixir/Application.html#app_dir/1 is an option here.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Edited my previous reply in light of new information.

smon

smon OP

I am not quite sure if I understood correctly.

Path.join(:code.priv_dir(:my_app), "data.xml")

produced the path /path/to/my_app/my_app/priv/data.xml, where the first my_app corresponds the escript build file.

By replacing :code.priv_dir(:my_app) with app_dir(:my_app) , I can get rid off the priv sub-folder, but that still does not allow me to open a valid file stream.

sentient-wendy

sentient-wendy

Hi @smon and @benwilson512, I’d also like to know if there are options to access the /priv folder for a command line utility built with escript. Please update if you’ve found anything. Thank you in advance!

smon

smon OP

Hey!

Sorry I did not check the forum for quite some time. The answer seems to be the thing I already mentioned:

I basically gave up on the idea of adding the file to the build file.

josevalim

josevalim

Creator of Elixir

To confirm: although priv is the appropriate place, we can’t load files from priv inside escripts. What I usually do is to load the file at compilation time and store it as a module attribute:

@external_resource "priv/foo/bar"
@bar_contents File.read! "priv/foo/bar"
def bar_contents, do: @bar_contents

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews