-
-
Notifications
You must be signed in to change notification settings - Fork 69
feat: add engine subcommands to expert #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rmand97
wants to merge
7
commits into
elixir-lang:main
Choose a base branch
from
rmand97:feat/expert-engine-subcommands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+557
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
019339d
feat: add engine subcommands to expert
rma97 06e860c
fix credo and dialyzer errors
rma97 ac6c030
let Engine return 1 if file deletion fails
rma97 d478a52
use with for parsing options
rma97 6ea5ccd
use parse_head/2 to parse options
rma97 846812a
adjust tests
rma97 c843deb
handle unknown and no commands to Egine
rma97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| defmodule Expert.Engine do | ||
| @moduledoc """ | ||
| Utilities for managing Expert engine builds. | ||
|
|
||
| When Expert builds the engine for a project using Mix.install, it caches | ||
| the build in the user data directory. If engine dependencies change (e.g., | ||
| in nightly builds), Mix.install may not know to rebuild, causing errors. | ||
|
|
||
| This module provides functions to inspect and clean these cached builds. | ||
| """ | ||
|
|
||
| @doc """ | ||
| Runs engine management commands based on parsed arguments. | ||
|
|
||
| Returns the exit code for the command. Clean operations will stop at the | ||
| first deletion error and return exit code 1. | ||
| """ | ||
|
|
||
| @success_code 0 | ||
| @error_code 1 | ||
|
|
||
| @spec run([String.t()]) :: non_neg_integer() | ||
| def run(args) do | ||
| {opts, subcommand, _invalid} = | ||
| OptionParser.parse_head(args, | ||
| strict: [help: :boolean], | ||
| aliases: [h: :help] | ||
| ) | ||
|
|
||
| if opts[:help] do | ||
| print_help() | ||
| else | ||
| case subcommand do | ||
| ["ls" | ls_opts] -> list_engines(ls_opts) | ||
| ["clean" | clean_opts] -> clean_engines(clean_opts) | ||
| [unknown | _] -> print_unknown_subcommand(unknown) | ||
| [] -> print_help() | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @spec list_engines([String.t()]) :: non_neg_integer() | ||
| defp list_engines(ls_options) do | ||
| {opts, _rest, _invalid} = | ||
| OptionParser.parse_head(ls_options, | ||
| strict: [help: :boolean], | ||
| aliases: [h: :help] | ||
| ) | ||
|
|
||
| if opts[:help] do | ||
| print_ls_help() | ||
| else | ||
| print_engine_dirs() | ||
| end | ||
| end | ||
|
|
||
| @spec print_engine_dirs() :: non_neg_integer() | ||
| defp print_engine_dirs do | ||
| dirs = get_engine_dirs() | ||
|
|
||
| case dirs do | ||
| [] -> | ||
| print_no_engines_found() | ||
|
|
||
| dirs -> | ||
| Enum.each(dirs, &IO.puts/1) | ||
| end | ||
|
|
||
| @success_code | ||
| end | ||
|
|
||
| @spec clean_engines([String.t()]) :: non_neg_integer() | ||
| defp clean_engines(clean_options) do | ||
| {opts, _rest, _invalid} = | ||
| OptionParser.parse_head(clean_options, | ||
| strict: [force: :boolean, help: :boolean], | ||
| aliases: [f: :force, h: :help] | ||
| ) | ||
|
|
||
| dirs = get_engine_dirs() | ||
|
|
||
| cond do | ||
| opts[:help] -> | ||
| print_clean_help() | ||
|
|
||
| dirs == [] -> | ||
| print_no_engines_found() | ||
|
|
||
| opts[:force] -> | ||
| clean_all_force(dirs) | ||
|
|
||
| true -> | ||
| clean_interactive(dirs) | ||
| end | ||
| end | ||
|
|
||
| @spec base_dir() :: String.t() | ||
| defp base_dir do | ||
| base = :filename.basedir(:user_data, ~c"Expert") | ||
| to_string(base) | ||
| end | ||
|
|
||
| @spec get_engine_dirs() :: [String.t()] | ||
| defp get_engine_dirs do | ||
| base = base_dir() | ||
|
|
||
| if File.exists?(base) do | ||
| base | ||
| |> File.ls!() | ||
| |> Enum.map(&Path.join(base, &1)) | ||
| |> Enum.filter(&File.dir?/1) | ||
| |> Enum.sort() | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| @spec clean_all_force([String.t()]) :: non_neg_integer() | ||
| # Deletes all directories without prompting. Stops on first error and returns 1. | ||
| defp clean_all_force(dirs) do | ||
| result = | ||
| Enum.reduce_while(dirs, :ok, fn dir, _acc -> | ||
| case File.rm_rf(dir) do | ||
| {:ok, _} -> | ||
| IO.puts("Deleted #{dir}") | ||
| {:cont, :ok} | ||
|
|
||
| {:error, reason, file} -> | ||
| IO.puts(:stderr, "Error deleting #{file}: #{inspect(reason)}") | ||
| {:halt, :error} | ||
| end | ||
| end) | ||
|
|
||
| case result do | ||
| :ok -> @success_code | ||
| :error -> @error_code | ||
| end | ||
| end | ||
|
|
||
| @spec clean_interactive([String.t()]) :: non_neg_integer() | ||
| # Prompts the user for each directory deletion. Stops on first error and returns 1. | ||
| defp clean_interactive(dirs) do | ||
| result = | ||
| Enum.reduce_while(dirs, :ok, fn dir, _acc -> | ||
| answer = prompt_delete(dir) | ||
|
|
||
| if answer do | ||
| case File.rm_rf(dir) do | ||
| {:ok, _} -> | ||
| {:cont, :ok} | ||
|
|
||
| {:error, reason, file} -> | ||
| IO.puts(:stderr, "Error deleting #{file}: #{inspect(reason)}") | ||
| {:halt, :error} | ||
| end | ||
| else | ||
| {:cont, :ok} | ||
| end | ||
| end) | ||
|
|
||
| case result do | ||
| :ok -> @success_code | ||
| :error -> @error_code | ||
| end | ||
| end | ||
|
|
||
| @spec prompt_delete(dir :: [String.t()]) :: boolean() | ||
| defp prompt_delete(dir) do | ||
| IO.puts(["Delete #{dir}", IO.ANSI.red(), "?", IO.ANSI.reset(), " [Yn] "]) | ||
|
|
||
| input = | ||
| "" | ||
| |> IO.gets() | ||
| |> String.trim() | ||
| |> String.downcase() | ||
|
|
||
| case input do | ||
| "" -> true | ||
| "y" -> true | ||
| "yes" -> true | ||
| _ -> false | ||
| end | ||
| end | ||
|
|
||
| @spec print_no_engines_found() :: non_neg_integer() | ||
| defp print_no_engines_found do | ||
| IO.puts("No engine builds found.") | ||
| IO.puts("\nEngine builds are stored in: #{base_dir()}") | ||
|
|
||
| @success_code | ||
| end | ||
|
|
||
| @spec print_unknown_subcommand(String.t()) :: non_neg_integer() | ||
| defp print_unknown_subcommand(subcommand) do | ||
| IO.puts(:stderr, """ | ||
| Error: Unknown subcommand '#{subcommand}' | ||
|
|
||
| Run 'expert engine --help' for usage information. | ||
| """) | ||
|
|
||
| @error_code | ||
| end | ||
|
|
||
| @spec print_help() :: non_neg_integer() | ||
| defp print_help do | ||
| IO.puts(""" | ||
| Expert Engine Management | ||
|
|
||
| Manage cached engine builds created by Mix.install. Use these commands | ||
| to resolve dependency errors or free up disk space. | ||
|
|
||
| USAGE: | ||
| expert engine <subcommand> | ||
|
|
||
| SUBCOMMANDS: | ||
| ls List all engine build directories | ||
| clean Interactively delete engine build directories | ||
|
|
||
| Use 'expert engine <subcommand> --help' for more information on a specific command. | ||
|
|
||
| EXAMPLES: | ||
| expert engine ls | ||
| expert engine clean | ||
| """) | ||
|
|
||
| @success_code | ||
| end | ||
|
|
||
| @spec print_ls_help() :: non_neg_integer() | ||
| defp print_ls_help do | ||
| IO.puts(""" | ||
| List Engine Builds | ||
|
|
||
| List all cached engine build directories. | ||
|
|
||
| USAGE: | ||
| expert engine ls | ||
|
|
||
| EXAMPLES: | ||
| expert engine ls | ||
| """) | ||
|
|
||
| @success_code | ||
| end | ||
|
|
||
| @spec print_clean_help() :: non_neg_integer() | ||
| defp print_clean_help do | ||
| IO.puts(""" | ||
| Clean Engine Builds | ||
|
|
||
| Interactively delete cached engine build directories. By default, you will | ||
| be prompted to confirm deletion of each build. Use --force to skip prompts. | ||
|
|
||
| USAGE: | ||
| expert engine clean [options] | ||
|
|
||
| OPTIONS: | ||
| -f, --force Delete all builds without prompting | ||
|
|
||
| EXAMPLES: | ||
| expert engine clean | ||
| expert engine clean --force | ||
| """) | ||
|
|
||
| @success_code | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add help text to each subcommand, and the options can be presented for the relevant subcommand.
So in the existing help text here, we won't have any options, and the
--forceoption will show up in the help text for thecleansubcommand