Today I Learned

A Hashrocket project

2 posts by codyroberts

Mix xref and Elixir compiler warnings

The release of Elixir v1.3 added a mix task called xref for performing cross reference checks of your code. mix xref gives us some handy tools!

Find unreachable code:

❯ mix xref unreachable
lib/exonk8s/foo.ex:16: SomeModule.non_existant/0

Find callers of modules and functions:

❯ mix xref callers SomeModule
lib/exonk8s/foo.ex:16: SomeModule.non_existant/0

Generate a dependency graph:

> mix xref graph
├── lib/exonk8s/repo.ex
└── lib/router.ex
    └── lib/exonk8s/musician_controller.ex (compile)
        ├── lib/exonk8s/foo.ex
        ├── lib/exonk8s/musician.ex
        └── lib/exonk8s/repo.ex
...snipped for brevity

The release of Elixir v1.6 added the module attribute @deprecated to enable library authors to warn users of deprecated functions.

❯ mix xref deprecated
Compiling 2 files (.ex)
Foo Loaded
warning: ExOnK8s.Foo.lazy/0 is deprecated. You should be more eager.

lib/exonk8s/musician_controller.ex:10: ExOnK8s.Foo.lazy/0

Lastly I should note that xref deprecated and xref unreachable are included in the compiler warnings, so you you’ve likely seen these at work even if you didn’t know they were there.

Learn more

Relative Dates with GNU date

GNU date ships with the ability to add and subtract dates. Using the -d flag one can add add or subtract years, months, days, weeks, and seconds. As an example here’s a future date helper:

in() {
  if [ "$(uname)" == "Darwin" ]; then
    gdate -d"$(gdate) +$1 $2" "+%Y-%m-%d"
    date -d"$(date) +$1 $2" "+%Y-%m-%d"
~❯ in 7 days
~❯ in 2 months

This can be handy for some CLI utilities like Todo.txt.

For example:

~❯ t add Post first TIL due:$(in 3 days)
10 Post first TIL due:2018-03-20
TODO: 10 added.

Note: If you’re on a Mac you’ll need to install the GNU coreutils. By default all commands will be prepended with g, hence gdate above.

brew install coreutils

Check the man pages for more info.