-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
2
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,163 @@ | ||
| 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 :ok and halts the system after executing the command. | ||
| """ | ||
| @spec run([String.t()]) :: no_return() | ||
| def run(args) do | ||
| {opts, subcommand, _invalid} = | ||
| OptionParser.parse(args, | ||
| strict: [force: :boolean], | ||
| aliases: [f: :force] | ||
| ) | ||
|
|
||
| case subcommand do | ||
| ["ls"] -> list_engines() | ||
| ["clean"] -> clean_engines(opts) | ||
| _ -> print_help() | ||
| end | ||
| end | ||
|
|
||
| @spec list_engines() :: no_return() | ||
| defp list_engines do | ||
| case get_engine_dirs() do | ||
| [] -> | ||
| IO.puts("No engine builds found.") | ||
| print_location_info() | ||
|
|
||
| dirs -> | ||
| Enum.each(dirs, &IO.puts/1) | ||
| end | ||
|
|
||
| System.halt(0) | ||
| end | ||
|
|
||
| @spec clean_engines(keyword()) :: no_return() | ||
| defp clean_engines(opts) do | ||
| case get_engine_dirs() do | ||
| [] -> | ||
| IO.puts("No engine builds found.") | ||
| print_location_info() | ||
| System.halt(0) | ||
|
|
||
| dirs -> | ||
| if opts[:force] do | ||
| clean_all_force(dirs) | ||
| else | ||
| clean_interactive(dirs) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| defp base_dir do | ||
| base = :filename.basedir(:user_data, ~c"Expert") | ||
| to_string(base) | ||
| end | ||
|
|
||
| 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()]) :: no_return() | ||
| defp clean_all_force(dirs) do | ||
| Enum.each(dirs, fn dir -> | ||
| case File.rm_rf(dir) do | ||
| {:ok, _} -> | ||
| IO.puts("Deleted #{dir}") | ||
|
|
||
| {:error, reason, file} -> | ||
| IO.puts(:stderr, "Error deleting #{file}: #{inspect(reason)}") | ||
| end | ||
| end) | ||
|
|
||
| System.halt(0) | ||
| end | ||
|
|
||
| @spec clean_interactive([String.t()]) :: no_return() | ||
| defp clean_interactive(dirs) do | ||
| Enum.each(dirs, fn dir -> | ||
| answer = prompt_delete(dir) | ||
|
|
||
| if answer do | ||
| case File.rm_rf(dir) do | ||
| {:ok, _} -> | ||
| :ok | ||
|
|
||
| {:error, reason, file} -> | ||
| IO.puts(:stderr, "Error deleting #{file}: #{inspect(reason)}") | ||
| end | ||
| end | ||
| end) | ||
|
|
||
| System.halt(0) | ||
| end | ||
|
|
||
| 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 | ||
|
|
||
| defp print_location_info do | ||
| IO.puts("\nEngine builds are stored in: #{base_dir()}") | ||
| end | ||
|
|
||
| @spec print_help() :: no_return() | ||
| defp print_help do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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> [options] | ||
| SUBCOMMANDS: | ||
| ls List all engine build directories | ||
| clean Interactively delete engine build directories | ||
| OPTIONS: | ||
| -f, --force Delete all builds without prompting (clean only) | ||
| EXAMPLES: | ||
| expert engine ls | ||
| expert engine clean | ||
| expert engine clean --force | ||
| """) | ||
|
|
||
| System.halt(0) | ||
| 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.
Can you make this return the exit code, and put the
System.halt(exit_code)part here?That'll make the unit tests easier to write, as well as pull the halting part up to the top level.
This way it's very clear that the program stops after this code runs.
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.
Will do.
Just a quick question. Would you like it to always return success (
0), event though it fails deleting a file?like if its fails the middle one of 3 files in
expert clean --forceit will continue after failing, logging something went wrong, and delete the last one and return0. Or would like it to stop after failing and return1?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.
If it fails any, lets return a non zero exit code (1).