Skip to content

Commit

Permalink
Dry option setup, error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hissssst committed Mar 30, 2024
1 parent 1c1bcb4 commit b6a97e9
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 183 deletions.
39 changes: 23 additions & 16 deletions lib/mix/tasks/compile.tria.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
defmodule Mix.Tasks.Compile.Tria do

@moduledoc """
Mix task for running Tria optimizing compiler
Available command-line options are:
* `--dry` — (not implemented yet) to check how compilation works without storing compilation results on disk
* `--force` — forces full compilation from scratch (opposite to incremental recompilation)
"""

use Mix.Task.Compiler
Expand All @@ -15,16 +19,19 @@ defmodule Mix.Tasks.Compile.Tria do
defmodule Options do
# Internal representation `mix compile.tria` command
@moduledoc false
defstruct [force: false]
defstruct force: false, dry: false
def parse(["--force" | tail]), do: %__MODULE__{parse(tail) | force: true}
def parse(["--dry" | tail]), do: %__MODULE__{parse(tail) | dry: true}
def parse(_), do: %__MODULE__{}
end

def run(args) do
options = Options.parse(args)
Project.get!() # Just to make sure that project exists
# Just to make sure that project exists
Project.get!()
mix_config = Project.config()
Project.ensure_structure(mix_config)

manifest_path =
mix_config
|> Project.manifest_path()
Expand All @@ -40,7 +47,7 @@ defmodule Mix.Tasks.Compile.Tria do
tags ->
tags
|> String.split(",")
|> Enum.map(& String.to_atom String.trim &1)
|> Enum.map(&String.to_atom(String.trim(&1)))
|> Debug.flag_debug()
end

Expand All @@ -51,19 +58,20 @@ defmodule Mix.Tasks.Compile.Tria do
|> Enum.each(&Tracer.trace(&1, only: :all))
end

root = Path.dirname Project.project_file()
root = Path.dirname(Project.project_file())

elixirc_paths =
mix_config
|> Keyword.fetch!(:elixirc_paths)
|> Enum.map(fn path -> Path.join(root, path) end)

build_path = Path.join [
Project.build_path(mix_config),
"lib",
to_string(mix_config[:project] || mix_config[:app]),
"ebin"
]
build_path =
Path.join([
Project.build_path(mix_config),
"lib",
to_string(mix_config[:project] || mix_config[:app]),
"ebin"
])

File.mkdir_p!(build_path)

Expand Down Expand Up @@ -109,7 +117,7 @@ defmodule Mix.Tasks.Compile.Tria do
end

%{type: other} ->
IO.warn "Unrecognized filetype #{inspect other} for #{path}"
IO.warn("Unrecognized filetype #{inspect(other)} for #{path}")
[]
end
end)
Expand Down Expand Up @@ -143,10 +151,9 @@ defmodule Mix.Tasks.Compile.Tria do
ignore_module_conflict: true
]
|> Tria.Compiler.ElixirCompiler.with_compiler_options(fn ->
:code.add_path to_charlist Path.dirname filename
:code.purge module
:code.load_file module
:code.add_path(to_charlist(Path.dirname(filename)))
:code.purge(module)
:code.load_file(module)
end)
end

end
32 changes: 18 additions & 14 deletions lib/mix/tasks/tria.clean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,41 @@ defmodule Mix.Tasks.Tria.Clean do
if String.ends_with?(filename, "_cache.ets") do
priv
|> Path.join(filename)
|> info()
|> info("Cache file: ")
|> List.wrap()
else
[]
end
end)

confirmed? =
with false <- args in [["-y"], ["--yes"]] do
result = IO.gets("Confirm? [Y(yes); n(no)] ") in ["y\n", "Y\n", "\n"]
unless result do
IO.write [IO.ANSI.red(), "Denied. Nothing cleaned\n", IO.ANSI.reset()]
end
result
"-y" in args or
"--yes" in args or
IO.gets("Confirm? [Y(yes); n(no)] ") in ["y\n", "Y\n", "\n"]

if confirmed? and to_delete != [] do
Enum.each(to_delete, fn file ->
info(file, "Deleting: ")
File.rm!(file)
end)
else
if to_delete == [] do
IO.write([IO.ANSI.yellow(), "No cache files found!\n", IO.ANSI.reset()])
end

if confirmed? do
Enum.each(to_delete, &File.rm/1)
IO.write([IO.ANSI.red(), "Nothing cleaned\n", IO.ANSI.reset()])
end
end

defp info(cache) do
IO.write [
"Deleting file: ",
defp info(cache, prefix) do
IO.write([
prefix,
IO.ANSI.green(),
cache,
IO.ANSI.reset(),
"\n"
]
])

cache
end

end
Loading

0 comments on commit b6a97e9

Please sign in to comment.