Modkit.Mod (modkit v0.10.0)

Copy Markdown View Source

Functions to inspect the modules of an application: list them, locate their source files, and derive related module names.

Summary

Functions

Returns the conventional test module name for the given module.

Returns the path of the source file that defines the given module.

Groups the given modules by the source file that defines them.

Returns all modules of the given OTP application.

Returns the module that is a local root of all given modules. That is a common prefix of all given modules that is also a module from the list.

Given two module names, returns the module name that is a prefix of the other, or nil if the two names are disjoint.

Returns the top module namespaces that are prefix to all other modules and that is also a module from the list.

Functions

as_test(module)

Returns the conventional test module name for the given module.

Examples

iex> as_test(MyApp.Worker)
MyApp.WorkerTest

current_path(module, relative_to \\ File.cwd!())

Returns the path of the source file that defines the given module.

The path is returned relative to relative_to, which defaults to the current working directory. Raises an ArgumentError when the module cannot be loaded.

group_by_file(modules, relative_to \\ File.cwd!())

Groups the given modules by the source file that defines them.

Returns a map with file paths as keys and lists of modules as values. Paths are relative to relative_to, which defaults to the current working directory.

list_all(otp_app)

Returns all modules of the given OTP application.

The application must be loaded, which is always the case for the current application in a Mix project. Raises an ArgumentError otherwise.

local_root(mods)

@spec local_root([module()]) :: module() | nil

Returns the module that is a local root of all given modules. That is a common prefix of all given modules that is also a module from the list.

For instance, modules A and A.B have a common prefix that is A, and A is provided as an argument, so it will be returned.

But if only A.B and A.C are provided, the common prefix A will not be returned since it is not one of the arguments.

Examples

iex> local_root([A, A.B])
A

iex> local_root([A.B, A.C, A])
A

iex> local_root([A.B, A.C])
nil

iex> local_root([])
nil

local_root(a, b)

@spec local_root(module(), module()) :: module() | nil

Given two module names, returns the module name that is a prefix of the other, or nil if the two names are disjoint.

See local_root/1.

Examples

iex> local_root(A, A.B)
A

iex> local_root(A.B, A.C)
nil

local_roots(mods)

Returns the top module namespaces that are prefix to all other modules and that is also a module from the list.

See local_root/1

Examples

iex> local_roots([A, A.B, X.Y, X])
[A, X]

iex> local_roots([A.B, A.C, A, X, X, X.Y, X.Y.Z])
[A,X]

iex> local_roots([A.B, A.C])
[A.B, A.C]

iex> local_roots([A.B.C1, A.B.C2])
[A.B.C1, A.B.C2]

iex> local_roots([A.B.C1, A.B.C2, A.B])
[A.B]