Skip to content
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

Improve cli and help #147

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 73 additions & 52 deletions cli.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
racket/match
racket/path
racket/string
racket/vector
rebellion/base/option
rebellion/collection/entry
rebellion/collection/hash
Expand All @@ -21,7 +22,8 @@
resyntax/file-group
resyntax/refactoring-result
resyntax/refactoring-suite
resyntax/source)
resyntax/source
syntax/parse/define)


;@----------------------------------------------------------------------------------------------------
Expand All @@ -30,31 +32,50 @@
(define-record-type resyntax-options (targets suite))


;; Remove the first argument of the command line arguments
(define-syntax-parse-rule (shift-command-line-arguments body ...)
(λ args
(parameterize ([current-command-line-arguments (vector-copy (current-command-line-arguments) 1)])
body ...)))


;; If the command line arguments are empty, re-parameterize it to
;; default to #("--help")
(define-syntax-parse-rule (parameterize-help-if-empty-ccla body ...)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we ought to try to add this to command-line as an opt-in feature. So that we could write something like:

(command-line
  #:program-name "foo"
  #:print-help-by-default
  flags ...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would certainly be useful.

(let ([ccla (current-command-line-arguments)])
(parameterize ([current-command-line-arguments
(if (vector-empty? ccla)
#("--help")
ccla)])
body ...)))


(define (resyntax-analyze-parse-command-line)
(define targets (box (make-vector-builder)))
(define suite (box default-recommendations))
(define (add-target! target)
(set-box! targets (vector-builder-add (unbox targets) target)))
(command-line
#:program "resyntax analyze"
#:multi
("--file" filepath "A file to analyze." (add-target! (single-file-group filepath)))
("--directory"
dirpath
"A directory to anaylze, including subdirectories."
(add-target! (directory-file-group dirpath)))
("--package"
pkgname
"An installed package to analyze."
(add-target! (package-file-group pkgname)))
#:once-each
("--refactoring-suite"
modpath
suite-name
"The refactoring suite to analyze code with."
(define parsed-modpath (read (open-input-string modpath)))
(define parsed-suite-name (read (open-input-string suite-name)))
(set-box! suite (dynamic-require parsed-modpath parsed-suite-name))))
(parameterize-help-if-empty-ccla
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this line has changed in this function. The rest is indentation.

(command-line
#:program "resyntax --analyze"
#:multi
("--file" filepath "A file to analyze." (add-target! (single-file-group filepath)))
("--directory"
dirpath
"A directory to anaylze, including subdirectories."
(add-target! (directory-file-group dirpath)))
("--package"
pkgname
"An installed package to analyze."
(add-target! (package-file-group pkgname)))
#:once-each
("--refactoring-suite"
modpath
suite-name
"The refactoring suite to analyze code with."
(define parsed-modpath (read (open-input-string modpath)))
(define parsed-suite-name (read (open-input-string suite-name)))
(set-box! suite (dynamic-require parsed-modpath parsed-suite-name)))))
(resyntax-options #:targets (build-vector (unbox targets)) #:suite (unbox suite)))


Expand All @@ -63,41 +84,41 @@
(define suite (box default-recommendations))
(define (add-target! target)
(set-box! targets (vector-builder-add (unbox targets) target)))
(command-line
#:program "resyntax fix"
#:multi
("--file" filepath "A file to fix." (add-target! (single-file-group filepath)))
("--directory"
dirpath
"A directory to fix, including subdirectories."
(add-target! (directory-file-group dirpath)))
("--package"
pkgname
"An installed package to fix."
(add-target! (package-file-group pkgname)))
#:once-each
("--refactoring-suite"
modpath
suite-name
"The refactoring suite to analyze code with."
(define parsed-modpath (read (open-input-string modpath)))
(define parsed-suite-name (read (open-input-string suite-name)))
(set-box! suite (dynamic-require parsed-modpath parsed-suite-name))))
(parameterize-help-if-empty-ccla
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this line has changed n this function. The rest is indentation.

(command-line
#:program "resyntax --fix"
#:multi
("--file" filepath "A file to fix." (add-target! (single-file-group filepath)))
("--directory"
dirpath
"A directory to fix, including subdirectories."
(add-target! (directory-file-group dirpath)))
("--package"
pkgname
"An installed package to fix."
(add-target! (package-file-group pkgname)))
#:once-each
("--refactoring-suite"
modpath
suite-name
"The refactoring suite to analyze code with."
(define parsed-modpath (read (open-input-string modpath)))
(define parsed-suite-name (read (open-input-string suite-name)))
(set-box! suite (dynamic-require parsed-modpath parsed-suite-name)))))
(resyntax-options #:targets (build-vector (unbox targets)) #:suite (unbox suite)))


(define (resyntax-run)
(command-line
#:program "resyntax"
#:args (command . leftover-args)
(define leftover-arg-vector (vector->immutable-vector (list->vector leftover-args)))
(match command
["analyze"
(parameterize ([current-command-line-arguments leftover-arg-vector])
(resyntax-analyze-run))]
["fix"
(parameterize ([current-command-line-arguments leftover-arg-vector])
(resyntax-fix-run))])))
(parameterize-help-if-empty-ccla
(command-line
#:program "resyntax"
#:once-any
["--analyze" => (shift-command-line-arguments
(resyntax-analyze-run))
'("Analyze source code without applying changes")]
["--fix" => (shift-command-line-arguments
(resyntax-fix-run))
'("Analyze source code and apply changes")])))


(define (resyntax-analyze-run)
Expand Down
3 changes: 3 additions & 0 deletions main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@
#:into into-hash))
(for ([(path replacement) (in-hash results-by-path)])
(file-apply-string-replacement! path replacement)))

(module+ main
(displayln "Try `racket -l- resyntax/cli --help` instead."))
26 changes: 15 additions & 11 deletions main.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ consider the following program:

This program uses @racket[let] unnecessarily. The @racket[let] expression can be replaced with a
@racket[define] form, reducing the indentation of the code. Resyntax is capable of detecting and
automatically fixing this issue. Running @exec{resyntax fix --file my-program.rkt} rewrites the above
to the following:
automatically fixing this issue. Running @exec{resyntax --fix --file my-program.rkt} rewrites the
above to the following:

@(racketmod
#:file "my-program.rkt"
Expand All @@ -39,8 +39,8 @@ to the following:
(set-box! x (unbox y))
(set-box! y t)))

To see a list of suggestions that Resyntax would apply, use @exec{resyntax analyze} instead of
@exec{resyntax fix}. Each suggestion includes an explanation of why the change is being recommended.
To see a list of suggestions that Resyntax would apply, use @exec{resyntax --analyze} instead of
@exec{resyntax --fix}. Each suggestion includes an explanation of why the change is being recommended.

@bold{This tool is extremely experimental.} Do not attempt to incorporate it into your projects yet.
For now, the refactoring suggestions produced by @racketmodname[resyntax] are best viewed as glimpses
Expand All @@ -58,17 +58,21 @@ greatly appreciated and are best directed at the @hyperlink[github-repository-ur


Resyntax provides a command-line @exec{resyntax} tool for analyzing and refactoring code. The tool has
two commands: @exec{resyntax analyze} for analyzing code without changing it, and @exec{resyntax fix}
for fixing code by applying Resyntax's suggestions.
two commands: @exec{resyntax --analyze} for analyzing code without changing it, and
@exec{resyntax --fix} for fixing code by applying Resyntax's suggestions.

Note that at present, Resyntax is limited in what files it can fix. Resyntax only analyzes files with
the @exec{.rkt} extension where @tt{#lang racket/base} is the first line in file.

If @exec{resyntax} is not installed as a program but only as a Racket package, it can be run from
the command line with
@exec{racket -l- resyntax/cli --help}.

@subsection{Running @exec{resyntax analyze}}

@subsection{Running @exec{resyntax --analyze}}

The @exec{resyntax analyze} command accepts flags for specifying what modules to analyze. After

The @exec{resyntax --analyze} command accepts flags for specifying what modules to analyze. After
analysis, suggestions are printed in the console. Any of the following flags can be specified any
number of times:

Expand All @@ -83,11 +87,11 @@ number of times:
@item{@exec{--package} @nonterm{package-name} --- An installed package to analyze.}]


@subsection{Running @exec{resyntax fix}}
@subsection{Running @exec{resyntax --fix}}


The @exec{resyntax fix} command accepts the same flags as @exec{resyntax analyze} for specifying what
modules to fix. After analysis, fixes are applied and a summary is printed.
The @exec{resyntax --fix} command accepts the same flags as @exec{resyntax --analyze} for specifying
what modules to fix. After analysis, fixes are applied and a summary is printed.


@itemlist[
Expand Down