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
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
0.3.1
0.3.0
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
Productive. Reliable. Fast. A productive web framework that does not compromise speed or maintainability.
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/mix/phoenix.ex
defmodule Mix.Phoenix do
# Conveniences for Phoenix tasks.
@moduledoc false
@doc """
Copies files from source dir to target dir
according to the given map.
Files are evaluated against EEx according to
the given binding.
"""
def copy_from(apps, source_dir, target_dir, binding, mapping) when is_list(mapping) do
roots = Enum.map(apps, &to_app_source(&1, source_dir))
for {format, source_file_path, target_file_path} <- mapping do
source =
Enum.find_value(roots, fn root ->
source = Path.join(root, source_file_path)
if File.exists?(source), do: source
end) || raise "could not find #{source_file_path} in any of the sources"
target = Path.join(target_dir, target_file_path)
contents =
case format do
:text -> File.read!(source)
:eex -> EEx.eval_file(source, binding)
end
Mix.Generator.create_file(target, contents)
end
end
defp to_app_source(path, source_dir) when is_binary(path),
do: Path.join(path, source_dir)
defp to_app_source(app, source_dir) when is_atom(app),
do: Application.app_dir(app, source_dir)
@doc """
Inflect path, scope, alias and more from the given name.
iex> Mix.Phoenix.inflect("user")
[alias: "User",
human: "User",
base: "Phoenix",
module: "Phoenix.User",
scoped: "User",
singular: "user",
path: "user"]
iex> Mix.Phoenix.inflect("Admin.User")
[alias: "User",
human: "User",
base: "Phoenix",
module: "Phoenix.Admin.User",
scoped: "Admin.User",
singular: "user",
path: "admin/user"]
iex> Mix.Phoenix.inflect("Admin.SuperUser")
[alias: "SuperUser",
human: "Super user",
base: "Phoenix",
module: "Phoenix.Admin.SuperUser",
scoped: "Admin.SuperUser",
singular: "super_user",
path: "admin/super_user"]
"""
def inflect(singular) do
base = Mix.Phoenix.base
scoped = Phoenix.Naming.camelize(singular)
path = Phoenix.Naming.underscore(scoped)
singular = String.split(path, "/") |> List.last
module = Module.concat(base, scoped) |> inspect
alias = String.split(module, ".") |> List.last
human = Phoenix.Naming.humanize(singular)
[alias: alias,
human: human,
base: base,
module: module,
scoped: scoped,
singular: singular,
path: path]
end
@doc """
Parses the attrs as received by generators.
"""
def attrs(attrs) do
Enum.map attrs, fn attr ->
case String.split(attr, ":", parts: 3) do
[key, comp, value] -> {String.to_atom(key), {String.to_atom(comp), String.to_atom(value)}}
[key, value] -> {String.to_atom(key), String.to_atom(value)}
[key] -> {String.to_atom(key), :string}
end
end
end
@doc """
Generates some sample params based on the parsed attributes.
"""
def params(attrs) do
attrs
|> Enum.reject(fn
{_, {:references, _}} -> true
{_, _} -> false
end)
|> Enum.into(%{}, fn
{k, {:array, _}} -> {k, []}
{k, :integer} -> {k, 42}
{k, :float} -> {k, "120.5"}
{k, :decimal} -> {k, "120.5"}
{k, :boolean} -> {k, true}
{k, :map} -> {k, %{}}
{k, :text} -> {k, "some content"}
{k, :date} -> {k, "2010-04-17"}
{k, :time} -> {k, "14:00:00"}
{k, :datetime} -> {k, "2010-04-17 14:00:00"}
{k, :uuid} -> {k, "7488a646-e31f-11e4-aace-600308960662"}
{k, _} -> {k, "some content"}
end)
end
@doc """
Checks the availability of a given module name.
"""
def check_module_name_availability!(name) do
name = Module.concat(Elixir, name)
if Code.ensure_loaded?(name) do
Mix.raise "Module name #{inspect name} is already taken, please choose another name"
end
end
@doc """
Returns the module base name based on the configuration value.
config :my_app
app_namespace: My.App
"""
def base do
app = Mix.Project.config |> Keyword.fetch!(:app)
case Application.get_env(app, :app_namespace, app) do
^app -> app |> to_string |> Phoenix.Naming.camelize
mod -> mod |> inspect
end
end
@doc """
Returns all compiled modules in a project.
"""
def modules do
Mix.Project.compile_path
|> Path.join("*.beam")
|> Path.wildcard
|> Enum.map(&beam_to_module/1)
end
defp beam_to_module(path) do
path |> Path.basename(".beam") |> String.to_atom()
end
end