View Source Rewrite.Project (rewrite v0.5.0)

The %Project{} contains all %Rewrite.Sources{} of a project.

Link to this section Summary

Functions

Returns conflicts between sources.

Counts the items of the given type in the project.

Creates a %Project{} from the given sources.

Returns true if any source has one or more issues.

Return a %Project{} where each source is the result of invoking fun on each source of the given project.

Creates a %Project{} from the given inputs.

Saves all sources in the project to disk.

Returns the %Rewrite.Source{} for the given path.

Same as source/2 but raises a ProjectError.

Returns the %Rewrite.source{} for the given module.

Same as source_by_module/2 but raises a ProjectError.

Returns all sources sorted by path.

Returns a list of %Source{} for the given path.

Returns a list of %Rewrite.Source{} with an implementation for the given module.

Returns the unreferenced sources.

Updates the project with the given source.

Link to this section Types

@type id() :: reference()
@type t() :: %Rewrite.Project{sources: %{required(id()) => Rewrite.Source.t()}}
@type wildcard() :: IO.chardata()

Link to this section Functions

@spec conflicts(t()) :: %{required(Path.t()) => [Rewrite.Source.t()]}

Returns conflicts between sources.

Sources with the same path have a conflict.

@spec count(t(), type :: :sources | :scripts) :: non_neg_integer()

Counts the items of the given type in the project.

The type :sources returns the count for all sources in the project, including scripts.

The type :scripts returns the count of all sources with a path that ends with ".exs".

@spec from_sources([Rewrite.Source.t()]) :: t()

Creates a %Project{} from the given sources.

@spec issues?(t()) :: boolean()

Returns true if any source has one or more issues.

@spec map(t(), (Rewrite.Source.t() -> Rewrite.Source.t())) :: t()

Return a %Project{} where each source is the result of invoking fun on each source of the given project.

@spec read!(input | [input]) :: t() when input: Path.t() | wildcard() | GlobEx.t()

Creates a %Project{} from the given inputs.

Link to this function

save(project, exclude \\ [])

View Source
@spec save(t(), [Path.t()]) :: :ok | {:error, :conflicts | {Path.t(), File.posix()}}

Saves all sources in the project to disk.

This function call Rewrite.Source.save/1 on all sources in the project.

The optional second argument accepts a list of paths for files to be excluded.

@spec source(t(), Path.t()) :: {:ok, Rewrite.Source.t()} | :error

Returns the %Rewrite.Source{} for the given path.

Returns an :ok tuple with the found source, if no or multiple sources are available an :error is returned.

@spec source!(t(), Path.t()) :: Rewrite.Source.t()

Same as source/2 but raises a ProjectError.

Link to this function

source_by_module(project, module)

View Source
@spec source_by_module(t(), module()) :: {:ok, Rewrite.Source.t()} | :error

Returns the %Rewrite.source{} for the given module.

Returns an :ok tuple with the found source, if no or multiple sources are available an :error is returned.

Link to this function

source_by_module!(project, module)

View Source
@spec source_by_module!(t(), module()) :: Rewrite.Source.t()

Same as source_by_module/2 but raises a ProjectError.

@spec sources(t()) :: [Rewrite.Source.t()]

Returns all sources sorted by path.

@spec sources(t(), Path.t()) :: [Rewrite.Source.t()]

Returns a list of %Source{} for the given path.

It is possible that the project contains multiple sources with the same path. The function conflicts/1 returns all conflicts in a project and the function save/2 returns an error tuple when trying to save a project with conflicts. It is up to the user of rewrite to handle conflicts.

examples

Examples

iex> source = Source.from_string(
...>    """
...>    defmodule MyApp.Mode do
...>    end
...>    """,
...>    "my_app/mode.ex"
...> )
iex> project = Project.from_sources([source])
iex> Project.sources(project, "my_app/mode.ex")
[source]
iex> Project.sources(project, "foo")
[]

iex> a = Source.from_string(":a", "a.ex")
iex> b = Source.from_string(":b", "b.ex")
iex> project = Project.from_sources([a, b])
iex> update = Source.update(a, :test, path: "b.ex")
iex> project = Project.update(project, update)
iex> Project.sources(project, "a.ex")
[]
iex> Project.sources(project, "b.ex")
[b, update]
Link to this function

sources_by_module(project, module)

View Source
@spec sources_by_module(t(), module()) :: [Rewrite.Source.t()]

Returns a list of %Rewrite.Source{} with an implementation for the given module.

@spec unreferenced(t()) :: [Rewrite.Source.t()]

Returns the unreferenced sources.

Unreferenced source are sources whose original path is no longer part of the project.

@spec update(t(), Rewrite.Source.t() | [Rewrite.Source.t()]) :: t()

Updates the project with the given source.

If the source is part of the project the source will be replaced, otherwise the source will be added.