Packages
membrane_core
1.3.1
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.7
1.2.6
1.2.5
retired
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc1
1.1.2
1.1.1
1.1.0
1.1.0-rc1
1.1.0-rc0
1.0.1
1.0.0
1.0.0-rc1
1.0.0-rc0
0.12.9
0.12.8
0.12.7
0.12.6
0.12.5
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
retired
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.2
0.10.1
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
0.7.0
0.6.1
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
Membrane Multimedia Framework (Core)
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/membrane.gen.component.ex
[Filter, Endpoint, Sink, Source, Bin, Pipeline]
|> Enum.map(fn component_type ->
component_name = inspect(component_type)
defmodule Module.concat(Mix.Tasks.Membrane.Gen, component_type) do
@shortdoc "Generates a template for a Membrane #{component_name}"
@moduledoc """
Generates a template for a Membrane #{component_name} with the provided module name.
$ mix membrane.gen.#{String.downcase(component_name)} module_name [-l target_location]
## Options
* `-l, --location` - If a target location is provided, the #{component_name} will be created there, relative to the `lib` directory.
The filename must also be present and have an `.ex` extension. If location is not provided, then it will be
inferred from the provided module name - it will be converted to lowercase and `.` separators will be interpreted
as directory separators. For example, a #{component_name} with module name `Foo.Bar` will be created at `lib/foo/bar.ex`.
"""
use Mix.Task
@switches [location: :string]
@aliases [l: :location]
@impl true
def run(argv) do
do_run("lib", argv)
end
@spec do_run(binary(), [binary()]) :: any()
def do_run(base_dir, argv) do
{option, argv} = OptionParser.parse!(argv, aliases: @aliases, strict: @switches)
module_name =
case argv do
[] ->
Mix.raise("""
Module name not provided.
This task expects a module name for the newly created #{unquote(component_type)}:
$ mix membrane.gen.#{String.downcase(unquote(component_name))} My#{unquote(component_name)}
""")
[module_name | _rest] ->
module_name
end
if not valid_module_name?(module_name) do
Mix.raise("""
Invalid module name, please provide a valid one.
(no other special characters than dots are allowed and the module name as well as dot-separated segments in it must start with uppercase letters).
""")
end
component_path =
case option do
[] -> Macro.underscore(module_name) <> ".ex"
[{:location, path} | _rest] -> path
end
|> then(&Path.join(base_dir, &1))
component_path |> Path.dirname() |> File.mkdir_p!()
File.write!(component_path, get_component(module_name))
end
defp get_component(module_name) do
template =
"../../../templates"
|> Path.expand(__DIR__)
|> Path.join(Macro.underscore(unquote(component_type)) <> ".ex")
|> File.read!()
template
|> String.split("\n")
|> List.replace_at(0, "defmodule #{module_name} do")
|> Enum.join("\n")
end
@spec valid_module_name?(String.t()) :: boolean()
defp valid_module_name?(name) do
case Code.string_to_quoted(name) do
{:ok, {:__aliases__, _metadata, _name}} -> true
_other -> false
end
end
end
end)