Temporary directories that are monitored and automatically removed in Elixir.
The package can be installed by adding tmp to your list of dependencies in mix.exs:
defdepsdo[{:tmp,"~> 0.3.0"}]endDefine your Tmp module:
defmoduleMyApp.TmpdouseTmpend# Or with a custom base directorydefmoduleMyApp.CustomTmpdouseTmp,base_dir: "/tmp/my_dir"endAdd it to your supervision tree:
children=[{MyApp.Tmp,name: MyApp.Tmp}]Use it in your code:
MyApp.Tmp.dir(fntmp_dir_path->file_path=Path.join(tmp_dir_path,"my_file")# do work with file_path...# then return a valueEnum.sum([1,2,3])end)# => 6When calling MyApp.Tmp.dir/2, you can pass the following options:
:prefix(optional) - Prefix for the temporary directory name, defaults tonil:base_dir(optional) - Base directory for the temporary directory, defaults toSystem.tmp_dir()or the value set inuse Tmp:timeout(optional) - Timeout in milliseconds, defaults to:infinity
MyApp.Tmp.dir(fntmp_dir_path->File.touch(Path.join(tmp_dir_path,"file_one"))# ... other important work2+2end,prefix: "my_app",base_dir: "/tmp/custom_base")# => 4Documentation can be found at https://hexdocs.pm/tmp.
Tmp is MIT licensed.