View Source Rewrite (rewrite v0.10.5)

Rewrite provides a struct that contains all resources that could be handeld by Rewrite.

Summary

Functions

Counts the sources with the given extname in the rewrite project.

Deletes the source for the given path from the rewrite. The file on disk is not removed.

Drops the sources with the given paths from the rewrite project.

Creates a %Rewrite{} from the given sources.

Same as from_sources/2, but raises a Rewrite.Error exception in case of failure.

Returns true when the %Rewrite{} contains a %Source{} with the given path.

Returns true if any source has one or more issues.

Invokes fun for each source in the rewrite project and updates the rewirte project with the result of fun.

Return a rewrite project where each source is the result of invoking fun on each source of the given rewrite project.

Creates an empty project.

Creates a %Rewrite{} from the given inputs.

Returns a sorted list of all paths in the rewrite project.

Puts the given source to the given rewrite project.

Same as put/2, but raises a Rewrite.Error exception in case of failure.

Reads the given input/inputs and adds the source/sources to the project when not already readed.

Tries to delete the source file and removes the source from the rewrite project.

Same as source/2, but raises a Rewrite.Error exception in case of failure.

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

Same as source/2, but raises a Rewrite.Error exception in case of failure.

Returns all sources sorted by path.

Updates the given source in the rewrite project.

Updates a source for the given path in the rewrite project.

The same as update/2 but raises a Rewrite.Error exception in case of an error.

The same as update/3 but raises a Rewrite.Error exception in case of an error.

Returns true if any source in the rewrite project returns true for Source.updated?/1.

Writes a source to disk.

Writes all sources in the rewrite project to disk.

Types

@type input() :: Path.t() | wildcard() | GlobEx.t()
@type opts() :: keyword()
@type t() :: %Rewrite{
  extensions: term(),
  sources: %{required(Path.t()) => Rewrite.Source.t()}
}
@type wildcard() :: IO.chardata()

Functions

@spec count(t(), String.t()) :: non_neg_integer()

Counts the sources with the given extname in the rewrite project.

@spec delete(t(), Path.t()) :: t()

Deletes the source for the given path from the rewrite. The file on disk is not removed.

If the source is not part of the rewrite project the unchanged rewrite is returned.

Examples

iex> {:ok, project} = Rewrite.from_sources([
...>   Source.from_string(":a", "a.exs"),
...>   Source.from_string(":b", "b.exs"),
...>   Source.from_string(":a", "c.exs")
...> ])
iex> Rewrite.paths(project)
["a.exs", "b.exs", "c.exs"]
iex> project = Rewrite.delete(project, "a.exs")
iex> Rewrite.paths(project)
["b.exs", "c.exs"]
iex> project = Rewrite.delete(project, "b.exs")
iex> Rewrite.paths(project)
["c.exs"]
iex> project = Rewrite.delete(project, "b.exs")
iex> Rewrite.paths(project)
["c.exs"]
@spec drop(t(), [Path.t()]) :: t()

Drops the sources with the given paths from the rewrite project.

The files for the dropped sources are not removed from disk.

If paths contains paths that are not in rewrite, they're simply ignored.

Examples

iex> {:ok, project} = Rewrite.from_sources([
...>   Source.from_string(":a", "a.exs"),
...>   Source.from_string(":b", "b.exs"),
...>   Source.from_string(":a", "c.exs")
...> ])
iex> project = Rewrite.drop(project, ["a.exs", "b.exs", "z.exs"])
iex> Rewrite.paths(project)
["c.exs"]
Link to this function

from_sources(sources, filetypes \\ [Source.Ex])

View Source
@spec from_sources([Rewrite.Source.t()], [module()]) ::
  {:ok, t()} | {:error, Rewrite.Error.t()}

Creates a %Rewrite{} from the given sources.

Returns {:ok, rewrite} for a list of regular sources.

Returns {:error, error} for sources with a missing path and/or duplicated paths.

Link to this function

from_sources!(sources, filetypes \\ [Source.Ex])

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

Same as from_sources/2, but raises a Rewrite.Error exception in case of failure.

Link to this function

has_source?(rewrite, path)

View Source
@spec has_source?(t(), Path.t()) :: boolean()

Returns true when the %Rewrite{} contains a %Source{} with the given path.

Examples

iex> {:ok, project} = Rewrite.from_sources([
...>   Source.from_string(":a", "a.exs")
...> ])
iex> Rewrite.has_source?(project, "a.exs")
true
iex> Rewrite.has_source?(project, "b.exs")
false
@spec issues?(t()) :: boolean()

Returns true if any source has one or more issues.

@spec map(t(), (Rewrite.Source.t() -> Rewrite.Source.t())) ::
  {:ok, t()}
  | {:error, [{:nosource | :overwrites | :nopath, Rewrite.Source.t()}]}

Invokes fun for each source in the rewrite project and updates the rewirte project with the result of fun.

Returns a {:ok, rewrite} if any update is successful.

Returns {:error, errors, rewrite} where rewrite is updated for all sources that are updated successful. The errors are the errors of update/3.

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

Return a rewrite project where each source is the result of invoking fun on each source of the given rewrite project.

Link to this function

new(filetypes \\ [Source, Source.Ex])

View Source
@spec new([module() | {module(), keyword()}]) :: t()

Creates an empty project.

The optional argument is a list of modules implementing the behavior Rewrite.Filetye. This list is used to add the filetype to the sources of the corresponding files. The list can contain modules representing a file type or a tuple of {module(), keyword()}. Rewrite uses the keyword list from the tuple as the options argument when a file is reading.

Examples

iex> project = Rewrite.new()
%Rewrite{
  sources: %{},
  extensions: %{
    "default" => Source,
    ".ex" => Source.Ex,
    ".exs" => Source.Ex,
  }
}
iex> path = "test/fixtures/source/hello.txt"
iex> project = Rewrite.read!(project, path)
iex> project |> Rewrite.source!(path) |> Source.get(:content)
"hello\n"
iex> project |> Rewrite.source!(path) |> Source.owner()
Rewrite

iex> project = Rewrite.new([{Rewrite.Source, owner: MyApp}])
%Rewrite{
  sources: %{},
  extensions: %{
    "default" => {Source, owner: MyApp}
  }
}
iex> path = "test/fixtures/source/hello.txt"
iex> project = Rewrite.read!(project, path)
iex> project |> Rewrite.source!(path) |> Source.owner()
MyApp
Link to this function

new!(inputs, filetypes \\ [Source, Source.Ex])

View Source
@spec new!(input() | [input()], [module() | {module(), keyword()}]) :: t()

Creates a %Rewrite{} from the given inputs.

The optional second argument is a list of modules implementing the behavior Rewrite.Filetye. For more info, see new/1.

@spec paths(t()) :: [Path.t()]

Returns a sorted list of all paths in the rewrite project.

@spec put(t(), Rewrite.Source.t()) :: {:ok, t()} | {:error, Rewrite.Error.t()}

Puts the given source to the given rewrite project.

Returns {:ok, rewrite} if successful, {:error, reason} otherwise.

Examples

iex> project = Rewrite.new()
iex> {:ok, project} = Rewrite.put(project, Source.from_string(":a", "a.exs"))
iex> map_size(project.sources)
1
iex> Rewrite.put(project, Source.from_string(":b"))
{:error, %Rewrite.Error{reason: :nopath}}
iex> Rewrite.put(project, Source.from_string(":a", "a.exs"))
{:error, %Rewrite.Error{reason: :overwrites, path: "a.exs"}}
@spec put!(t(), Rewrite.Source.t()) :: t()

Same as put/2, but raises a Rewrite.Error exception in case of failure.

Link to this function

read!(rewrite, inputs, opts \\ [])

View Source
@spec read!(t(), input() | [input()], opts()) :: t()

Reads the given input/inputs and adds the source/sources to the project when not already readed.

Options

  • :force, default: false - forces the reading of sources. With force: true updates and issues for an already existing source are deleted.
@spec rm(t(), Path.t()) ::
  {:ok, t()} | {:error, Rewrite.Error.t() | Rewrite.SourceError.t()}

Tries to delete the source file and removes the source from the rewrite project.

Returns {:ok, rewrite} if successful, or {:error, error} if an error occurs.

Note the file is deleted even if in read-only mode.

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

Same as source/2, but raises a Rewrite.Error exception in case of failure.

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

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

Returns an :ok tuple with the found source, if not exactly one source is available an :error is returned.

See also sources/2 to get a list of sources for a given path.

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

Same as source/2, but raises a Rewrite.Error exception in case of failure.

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

Returns all sources sorted by path.

@spec update(t(), Rewrite.Source.t()) :: {:ok, t()} | {:error, Rewrite.Error.t()}

Updates the given source in the rewrite project.

This function will be usually used if the path for the source has not changed.

Returns {:ok, rewrite} if successful, {:error, error} otherwise.

Link to this function

update(rewrite, path, source)

View Source
@spec update(t(), Path.t(), Rewrite.Source.t() | function()) ::
  {:ok, t()} | {:error, Rewrite.Error.t() | Rewrite.UpdateError.t()}

Updates a source for the given path in the rewrite project.

If source a Rewrite.Source struct the struct is used to update the rewrite project.

If source a function the source for the given path is passed to the function and the result is used to update the rewrite project.

Returns {:ok, rewrite} if the update was successful, {:error, error} otherwise.

Examples

iex> a = Source.Ex.from_string(":a", "a.exs")
iex> b = Source.Ex.from_string(":b", "b.exs")
iex> {:ok, project} = Rewrite.from_sources([a, b])
iex> {:ok, project} = Rewrite.update(project, "a.exs", Source.Ex.from_string(":foo", "a.exs"))
iex> project |> Rewrite.source!("a.exs") |> Source.get(:content)
":foo"
iex> {:ok, project} = Rewrite.update(project, "a.exs", fn s -> Source.update(s, :content, ":baz") end)
iex> project |> Rewrite.source!("a.exs") |> Source.get(:content)
":baz"
iex> {:ok, project} = Rewrite.update(project, "a.exs", fn s -> Source.update(s, :path, "c.exs") end)
iex> Rewrite.paths(project)
["b.exs", "c.exs"]
iex> Rewrite.update(project, "no.exs", Source.from_string(":foo", "x.exs"))
{:error, %Rewrite.Error{reason: :nosource, path: "no.exs"}}
iex> Rewrite.update(project, "c.exs", Source.from_string(":foo"))
{:error, %Rewrite.UpdateError{reason: :nopath, source: "c.exs"}}
iex> Rewrite.update(project, "c.exs", fn _ -> b end)
{:error, %Rewrite.UpdateError{reason: :overwrites, path: "b.exs", source: "c.exs"}}
Link to this function

update!(rewrite, source)

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

The same as update/2 but raises a Rewrite.Error exception in case of an error.

Link to this function

update!(rewrite, path, new)

View Source
@spec update!(t(), Path.t(), Rewrite.Source.t() | function()) :: t()

The same as update/3 but raises a Rewrite.Error exception in case of an error.

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

Returns true if any source in the rewrite project returns true for Source.updated?/1.

Examples

iex> {:ok, project} = Rewrite.from_sources([
...>   Source.Ex.from_string(":a", "a.exs"),
...>   Source.Ex.from_string(":b", "b.exs"),
...>   Source.Ex.from_string("c", "c.txt")
...> ])
iex> Rewrite.updated?(project)
false
iex> project = Rewrite.update!(project, "a.exs", fn source ->
...>   Source.update(source, :quoted, ":z")
...> end)
iex> Rewrite.updated?(project)
true
Link to this function

write(rewrite, path, force \\ nil)

View Source
@spec write(t(), Path.t() | Rewrite.Source.t(), nil | :force) ::
  {:ok, t()} | {:error, Rewrite.Error.t() | Rewrite.SourceError.t()}

Writes a source to disk.

The function expects a path or a %Source{} as first argument.

Returns {:ok, rewrite} if the file was written successful. See also Source.write/2.

If the given source is not part of the rewrite project then it is added.

Link to this function

write_all(rewrite, opts \\ [])

View Source
@spec write_all(t(), opts()) :: {:ok, t()} | {:error, [Rewrite.SourceError.t()], t()}

Writes all sources in the rewrite project to disk.

This function calls Rewrite.Source.write/1 on all sources in the rewrite project.

Returns {:ok, rewrite} if all sources are written successfully.

Returns {:error, reasons, rewrite} where rewrite is updated for all sources that are written successfully.

Options

  • exclude - a list paths to exclude form writting.
  • force, default: false - forces the writting of unchanged files.