1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-26 09:04:18 +03:00
juvix/app/TopCommand.hs

51 lines
1.7 KiB
Haskell
Raw Normal View History

2022-09-14 17:16:15 +03:00
module TopCommand where
Add `juvix format` command (#1886) This PR adds `juvix format` that can be used to format either a single Juvix file or all files in a Juvix project. ## Usage ``` $ juvix format --help Usage: juvix format JUVIX_FILE_OR_PROJECT [--check] [--in-place] Format a Juvix file or Juvix project When the command is run with an unformatted file it prints the reformatted source to standard output. When the command is run with a project directory it prints a list of unformatted files in the project. Available options: JUVIX_FILE_OR_PROJECT Path to a .juvix file or to a directory containing a Juvix project. --check Do not print reformatted sources or unformatted file paths to standard output. --in-place Do not print reformatted sources to standard output. Overwrite the target's contents with the formatted version if the formatted version differs from the original content. -h,--help Show this help text ``` ## Location of main implementation The implementation is split into two components: * The src API: `format` and `formatProject` https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/src/Juvix/Formatter.hs * The CLI interface: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/app/Commands/Format.hs ## in-place uses polysemy Resource effect The `--in-place` option makes a backup of the target file and restores it if there's an error during processing to avoid data loss. The implementation of this uses the polysemy [Resource effect](https://hackage.haskell.org/package/polysemy-1.9.0.0/docs/Polysemy-Resource.html). The recommended way to interpret the resource effect is to use `resourceToIOFinal` which makes it necessary to change the effects interpretation in main to use `Final IO`: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/app/Main.hs#L15 ## Format input is `FilePath` The format options uses `FilePath` instead of `AppFile f` for the input file/directory used by other commands. This is because we cannot determine if the input string is a file or directory in the CLI parser (we require IO). I discussed some ideas with @janmasrovira on how to improve this in a way that would also solve other issues with CLI input file/parsing but I want to defer this to a separate PR as this one is already quite large. One consequence of Format using `FilePath` as the input option is that the code that changes the working directory to the root of the project containing the CLI input file is changed to work with `FilePath`: https://github.com/anoma/juvix/blob/f715ef6a531f63c40ac3f629dd9cfea7e867507a/app/TopCommand/Options.hs#L33 ## New dependencies This PR adds new dependencies on `temporary` and `polysemy-zoo`. `temporary` is used for `emptySystemTempFile` in the implementation of the TempFile interpreter for IO: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/src/Juvix/Data/Effect/Files/IO.hs#L49 `polysemy-zoo` is used for the `Fresh` effect and `absorbMonadThrow` in the implementation of the pure TempFile interpreter: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/src/Juvix/Data/Effect/Files/Pure.hs#L91 NB: The pure TempFile interpreter is not used, but it seemed a good idea to include it while it's fresh in my mind. * Closes https://github.com/anoma/juvix/issues/1777 --------- Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-03-29 16:51:04 +03:00
import Commands.Base hiding (Format)
import Commands.Clean qualified as Clean
2022-09-14 17:16:15 +03:00
import Commands.Compile qualified as Compile
Add `juvix dependencies update` command (#2419) This PR adds a new command `juvix dependencies update` that fetches all dependencies in a project and updates the project lock file. Currently the only way to update the lock file is to delete it and generate a new one. ## CLI Docs ``` juvix dependencies --help Usage: juvix dependencies COMMAND Subcommands related to dependencies Available options: -h,--help Show this help text Available commands: update Fetch package dependencies and update the lock file ``` ## Example A project containing the following `juvix.yaml` ```yaml dependencies: - .juvix-build/stdlib/ - git: url: https://github.com/anoma/juvix-test ref: v0.6.0 name: test main: Example.juvix name: example version: 1.0.0 ``` compile to generate the lockfile: `juvix compile` ```yaml # This file was autogenerated by Juvix version 0.5.1. # Do not edit this file manually. dependencies: - path: .juvix-build/stdlib/ dependencies: [] - git: name: test ref: a94c61749678ff57556ee6e4cb1f8fbbddbc4ab1 url: https://github.com/anoma/juvix-test dependencies: - git: name: stdlib ref: 4facf14d9b2d06b81ce1be1882aa9050f768cb45 url: https://github.com/anoma/juvix-stdlib dependencies: [] ``` Now update the test dependency version: ```yaml - .juvix-build/stdlib/ - git: url: https://github.com/anoma/juvix-test ref: v0.6.1 name: test main: Example.juvix name: example version: 1.0.0 ``` And run `juvix dependencies update` Now the lockfile has updated to the hash of v0.6.1: ```yaml # This file was autogenerated by Juvix version 0.5.1. # Do not edit this file manually. dependencies: - path: .juvix-build/stdlib/ dependencies: [] - git: name: test ref: a7ac74cac0db92e0b5e349f279d797c3788cdfdd url: https://github.com/anoma/juvix-test dependencies: - git: name: stdlib ref: 4facf14d9b2d06b81ce1be1882aa9050f768cb45 url: https://github.com/anoma/juvix-stdlib dependencies: [] ``` --------- Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-10-03 19:09:13 +03:00
import Commands.Dependencies qualified as Dependencies
2022-09-14 17:16:15 +03:00
import Commands.Dev qualified as Dev
import Commands.Doctor qualified as Doctor
import Commands.Eval qualified as Eval
Add `juvix format` command (#1886) This PR adds `juvix format` that can be used to format either a single Juvix file or all files in a Juvix project. ## Usage ``` $ juvix format --help Usage: juvix format JUVIX_FILE_OR_PROJECT [--check] [--in-place] Format a Juvix file or Juvix project When the command is run with an unformatted file it prints the reformatted source to standard output. When the command is run with a project directory it prints a list of unformatted files in the project. Available options: JUVIX_FILE_OR_PROJECT Path to a .juvix file or to a directory containing a Juvix project. --check Do not print reformatted sources or unformatted file paths to standard output. --in-place Do not print reformatted sources to standard output. Overwrite the target's contents with the formatted version if the formatted version differs from the original content. -h,--help Show this help text ``` ## Location of main implementation The implementation is split into two components: * The src API: `format` and `formatProject` https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/src/Juvix/Formatter.hs * The CLI interface: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/app/Commands/Format.hs ## in-place uses polysemy Resource effect The `--in-place` option makes a backup of the target file and restores it if there's an error during processing to avoid data loss. The implementation of this uses the polysemy [Resource effect](https://hackage.haskell.org/package/polysemy-1.9.0.0/docs/Polysemy-Resource.html). The recommended way to interpret the resource effect is to use `resourceToIOFinal` which makes it necessary to change the effects interpretation in main to use `Final IO`: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/app/Main.hs#L15 ## Format input is `FilePath` The format options uses `FilePath` instead of `AppFile f` for the input file/directory used by other commands. This is because we cannot determine if the input string is a file or directory in the CLI parser (we require IO). I discussed some ideas with @janmasrovira on how to improve this in a way that would also solve other issues with CLI input file/parsing but I want to defer this to a separate PR as this one is already quite large. One consequence of Format using `FilePath` as the input option is that the code that changes the working directory to the root of the project containing the CLI input file is changed to work with `FilePath`: https://github.com/anoma/juvix/blob/f715ef6a531f63c40ac3f629dd9cfea7e867507a/app/TopCommand/Options.hs#L33 ## New dependencies This PR adds new dependencies on `temporary` and `polysemy-zoo`. `temporary` is used for `emptySystemTempFile` in the implementation of the TempFile interpreter for IO: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/src/Juvix/Data/Effect/Files/IO.hs#L49 `polysemy-zoo` is used for the `Fresh` effect and `absorbMonadThrow` in the implementation of the pure TempFile interpreter: https://github.com/anoma/juvix/blob/73952ba15c90f33919c75426cf62ae7e83fd3225/src/Juvix/Data/Effect/Files/Pure.hs#L91 NB: The pure TempFile interpreter is not used, but it seemed a good idea to include it while it's fresh in my mind. * Closes https://github.com/anoma/juvix/issues/1777 --------- Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-03-29 16:51:04 +03:00
import Commands.Format qualified as Format
2022-09-14 17:16:15 +03:00
import Commands.Html qualified as Html
import Commands.Init qualified as Init
import Commands.Isabelle qualified as Isabelle
import Commands.Markdown qualified as Markdown
import Commands.Repl qualified as Repl
2022-09-14 17:16:15 +03:00
import Commands.Typecheck qualified as Typecheck
import Juvix.Extra.Version
import System.Environment qualified as E
2022-09-14 17:16:15 +03:00
import TopCommand.Options
showHelpText :: (MonadIO m) => m ()
2022-09-14 17:16:15 +03:00
showHelpText = do
let p = prefs showHelpOnEmpty
progn <- liftIO E.getProgName
2022-09-14 17:16:15 +03:00
let helpText = parserFailure p descr (ShowHelpText Nothing) []
2023-01-17 11:41:07 +03:00
(msg, _) = renderFailure helpText progn
2022-09-14 17:16:15 +03:00
putStrLn (pack msg)
Make `compile` targets a subcommand instead of a flag (#2700) # Changes The main goal of this pr is to remove the `--target` flag for `juvix compile` and use subcommands instead. The targets that are relevant to normal users are found in `juvix compile --help`. Targets that are relevant only to developers are found in `juvix dev compile --help`. Below I list some of the changes in more detail. ## Compile targets for user-facing languages - `juvix compile native` - `juvix compile wasi`. I wasn't sure how to call this: `wasm`, `wasm32-wasi`, etc. In the end I thought `wasi` was short and accurate, but we can change it. - `juvix compile vampir` - `juvix compile anoma` - `juvix compile cairo` ## *New* compile targets for internal languages See `juvix dev compile --help`. 1. `dev compile core` has the same behaviour as `dev core from-concrete`. The `dev core from-concrete` is redundant at the moment. 2. `dev compile tree` compiles to Tree and prints the InfoTable to the output file wihout any additional checks. 3. `dev compile reg` compiles to Reg and prints the InfoTable to the output file wihout any additional checks. 4. `dev compile asm` compiles to Asm and prints the InfoTable to the output file wihout any additional checks. 5. 4. `dev compile casm` compiles to Asm and prints the Result to the output file wihout any additional checks. TODO: should the Result be printed or something else? At the moment the Result lacks a pretty instance. 6. ## Optional input file 1. The input file for commands that expect a .juvix file as input is now optional. If the argument is ommited, he main file given in the package.yaml will be used. This applies to the following commands: 1. `juvix compile [native|wasi|geb|vampir|anoma|cairo]` 8. `juvix dev compile [core|reg|tree|casm|asm]` 1. `juvix html` 3. `juvix markdown`. 4. `juvix dev internal [typecheck|pretty]`. 5. `juvix dev [parse|scope]` 7. `juvix compile [native|wasi|geb|vampir|anoma|cairo]` 9. note that `juvix format` has not changed its behaviour. ## Refactor some C-like compiler flags Both `juvix compile native` and `juvix compile wasi` support `--only-c` (`-C`), `--only-preprocess` (`-E`), `--only-assemble` (`-S`). I propose to deviate from the `gcc` style and instead use a flag with a single argument: - `--cstage [source|preprocess|assembly|exec(default)]`. I'm open to suggestions. For now, I've kept the legacy flags but marked them as deprecated in the help message. ## Remove code duplication I've tried to reduce code duplication. This is sometimes in tension with code readability so I've tried to find a good balance. I've tried to make it so we don't have to jump to many different files to understand what a single command is doing. I'm sure there is still room for improvement. ## Other refactors I've implemented other small refactors that I considered improved the quality of the code. ## TODO/Future work We should refactor commands (under `compile dev`) which still use `module Commands.Extra.Compile` and remove it.
2024-04-09 14:29:07 +03:00
runTopCommand ::
forall r.
(Members AppEffects r) =>
Make `compile` targets a subcommand instead of a flag (#2700) # Changes The main goal of this pr is to remove the `--target` flag for `juvix compile` and use subcommands instead. The targets that are relevant to normal users are found in `juvix compile --help`. Targets that are relevant only to developers are found in `juvix dev compile --help`. Below I list some of the changes in more detail. ## Compile targets for user-facing languages - `juvix compile native` - `juvix compile wasi`. I wasn't sure how to call this: `wasm`, `wasm32-wasi`, etc. In the end I thought `wasi` was short and accurate, but we can change it. - `juvix compile vampir` - `juvix compile anoma` - `juvix compile cairo` ## *New* compile targets for internal languages See `juvix dev compile --help`. 1. `dev compile core` has the same behaviour as `dev core from-concrete`. The `dev core from-concrete` is redundant at the moment. 2. `dev compile tree` compiles to Tree and prints the InfoTable to the output file wihout any additional checks. 3. `dev compile reg` compiles to Reg and prints the InfoTable to the output file wihout any additional checks. 4. `dev compile asm` compiles to Asm and prints the InfoTable to the output file wihout any additional checks. 5. 4. `dev compile casm` compiles to Asm and prints the Result to the output file wihout any additional checks. TODO: should the Result be printed or something else? At the moment the Result lacks a pretty instance. 6. ## Optional input file 1. The input file for commands that expect a .juvix file as input is now optional. If the argument is ommited, he main file given in the package.yaml will be used. This applies to the following commands: 1. `juvix compile [native|wasi|geb|vampir|anoma|cairo]` 8. `juvix dev compile [core|reg|tree|casm|asm]` 1. `juvix html` 3. `juvix markdown`. 4. `juvix dev internal [typecheck|pretty]`. 5. `juvix dev [parse|scope]` 7. `juvix compile [native|wasi|geb|vampir|anoma|cairo]` 9. note that `juvix format` has not changed its behaviour. ## Refactor some C-like compiler flags Both `juvix compile native` and `juvix compile wasi` support `--only-c` (`-C`), `--only-preprocess` (`-E`), `--only-assemble` (`-S`). I propose to deviate from the `gcc` style and instead use a flag with a single argument: - `--cstage [source|preprocess|assembly|exec(default)]`. I'm open to suggestions. For now, I've kept the legacy flags but marked them as deprecated in the help message. ## Remove code duplication I've tried to reduce code duplication. This is sometimes in tension with code readability so I've tried to find a good balance. I've tried to make it so we don't have to jump to many different files to understand what a single command is doing. I'm sure there is still room for improvement. ## Other refactors I've implemented other small refactors that I considered improved the quality of the code. ## TODO/Future work We should refactor commands (under `compile dev`) which still use `module Commands.Extra.Compile` and remove it.
2024-04-09 14:29:07 +03:00
TopCommand ->
Sem r ()
2022-09-14 17:16:15 +03:00
runTopCommand = \case
DisplayVersion -> runDisplayVersion
DisplayNumericVersion -> runDisplayNumericVersion
DisplayHelp -> showHelpText
2022-09-14 17:16:15 +03:00
Doctor opts -> runLogIO (Doctor.runCommand opts)
Isabelle opts -> Isabelle.runCommand opts
Init opts -> Init.init opts
2022-09-14 17:16:15 +03:00
Dev opts -> Dev.runCommand opts
Typecheck opts -> Typecheck.runCommand opts
Compile opts -> Compile.runCommand opts
Clean opts -> Clean.runCommand opts
Eval opts -> Eval.runCommand opts
2022-09-14 17:16:15 +03:00
Html opts -> Html.runCommand opts
Markdown opts -> Markdown.runCommand opts
JuvixRepl opts -> Repl.runCommand opts
JuvixFormat opts -> Format.runCommand opts
Add `juvix dependencies update` command (#2419) This PR adds a new command `juvix dependencies update` that fetches all dependencies in a project and updates the project lock file. Currently the only way to update the lock file is to delete it and generate a new one. ## CLI Docs ``` juvix dependencies --help Usage: juvix dependencies COMMAND Subcommands related to dependencies Available options: -h,--help Show this help text Available commands: update Fetch package dependencies and update the lock file ``` ## Example A project containing the following `juvix.yaml` ```yaml dependencies: - .juvix-build/stdlib/ - git: url: https://github.com/anoma/juvix-test ref: v0.6.0 name: test main: Example.juvix name: example version: 1.0.0 ``` compile to generate the lockfile: `juvix compile` ```yaml # This file was autogenerated by Juvix version 0.5.1. # Do not edit this file manually. dependencies: - path: .juvix-build/stdlib/ dependencies: [] - git: name: test ref: a94c61749678ff57556ee6e4cb1f8fbbddbc4ab1 url: https://github.com/anoma/juvix-test dependencies: - git: name: stdlib ref: 4facf14d9b2d06b81ce1be1882aa9050f768cb45 url: https://github.com/anoma/juvix-stdlib dependencies: [] ``` Now update the test dependency version: ```yaml - .juvix-build/stdlib/ - git: url: https://github.com/anoma/juvix-test ref: v0.6.1 name: test main: Example.juvix name: example version: 1.0.0 ``` And run `juvix dependencies update` Now the lockfile has updated to the hash of v0.6.1: ```yaml # This file was autogenerated by Juvix version 0.5.1. # Do not edit this file manually. dependencies: - path: .juvix-build/stdlib/ dependencies: [] - git: name: test ref: a7ac74cac0db92e0b5e349f279d797c3788cdfdd url: https://github.com/anoma/juvix-test dependencies: - git: name: stdlib ref: 4facf14d9b2d06b81ce1be1882aa9050f768cb45 url: https://github.com/anoma/juvix-stdlib dependencies: [] ``` --------- Co-authored-by: Jonathan Cubides <jonathan.cubides@uib.no>
2023-10-03 19:09:13 +03:00
Dependencies opts -> Dependencies.runCommand opts