Skip to content

Commit c298730

Browse files
committed
PS: Simplify execution by automatically including the default spec
1 parent c2a2d86 commit c298730

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

purescript/spago.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ workspace:
66
- argonaut
77
- argonaut-generic
88
- console
9+
- debug
910
- effect
1011
- node-process
1112
- prelude
@@ -31,6 +32,7 @@ workspace:
3132
- contravariant
3233
- control
3334
- datetime
35+
- debug
3436
- distributive
3537
- effect
3638
- either
@@ -763,6 +765,13 @@ packages:
763765
- partial
764766
- prelude
765767
- tuples
768+
debug:
769+
type: registry
770+
version: 6.0.2
771+
integrity: sha256-vmkYFuXYuELBzeauvgHG6E6Kf/Hp1dAnxwE9ByHfwSg=
772+
dependencies:
773+
- functions
774+
- prelude
766775
distributive:
767776
type: registry
768777
version: 6.0.0

purescript/spago.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package:
44
- argonaut
55
- argonaut-generic
66
- console
7+
- debug
78
- effect
89
- node-process
910
- prelude

purescript/src/Oclis/Executor.purs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44

55
module Oclis where
66

7-
import Oclis.Types
8-
97
import Prelude (Unit, bind, discard, pure, unit, (#), ($), (-), (<>), (>), (||))
108

119
import Ansi.Codes (Color(..))
1210
import Ansi.Output (withGraphics, foreground)
13-
import Oclis.Parser (tokensToCliArguments)
14-
import Oclis.Tokenizer (tokenizeCliArguments)
1511
import Data.Argonaut.Decode (decodeJson)
1612
import Data.Argonaut.Decode.Error (printJsonDecodeError)
1713
import Data.Argonaut.Parser (jsonParser)
@@ -25,6 +21,11 @@ import Effect (Effect)
2521
import Effect.Class.Console (log, error)
2622
import Node.Process (argv, setExitCode)
2723

24+
import Oclis.Parser (tokensToCliArguments)
25+
import Oclis.SpecEmbed (fileContent)
26+
import Oclis.Tokenizer (tokenizeCliArguments)
27+
import Oclis.Types
28+
2829
-- TODO: Automatically disable colors if not supported
2930
makeRed :: String -> String
3031
makeRed str =
@@ -144,11 +145,34 @@ repeatString :: String -> Int -> String
144145
repeatString str n =
145146
fold $ replicate n str
146147

147-
callCliApp
148+
-- | Convenience function to call the CLI app with the default spec and args.
149+
-- | Use `callCliAppWith`` if you want to provide your own values.
150+
callCliApp :: (ExecutorContext -> Effect (Result String Unit)) -> Effect Unit
151+
callCliApp executor =
152+
case parseCliSpec fileContent of
153+
Error errMsg -> do
154+
error $
155+
"ERROR:\n"
156+
<> "The auto-generated CLI specification in SpecEmbed.purs "
157+
<> "could not be parsed.\n"
158+
<> "This should not be possible!\n"
159+
<> "Please make sure you didn't accidentally modify any Oclis files\n"
160+
<> "and report following error at "
161+
<> "https://github.com/Airsequel/Oclis/issues/new:\n"
162+
<> "\n"
163+
<> errMsg
164+
setExitCode 1
165+
Ok cliSpec -> do
166+
arguments <- argv
167+
_ <- callCliAppWith cliSpec executor arguments
168+
pure unit
169+
170+
callCliAppWith
148171
:: Oclis
149172
-> (ExecutorContext -> Effect (Result String Unit))
173+
-> Array String
150174
-> Effect (Result String Unit)
151-
callCliApp cliSpec@(Oclis cliSpecRaw) executor = do
175+
callCliAppWith cliSpec@(Oclis cliSpecRaw) executor arguments = do
152176
let
153177
lengthLongestCmd :: Int
154178
lengthLongestCmd =
@@ -184,8 +208,6 @@ callCliApp cliSpec@(Oclis cliSpecRaw) executor = do
184208
)
185209
)
186210

187-
arguments <- argv
188-
189211
let
190212
argsNoInterpreter = arguments # drop 1 -- Drop "node"
191213
cliArgsMb =

purescript/src/Oclis/SpecEmbed.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- | This file exists only for testing purposes.
2+
-- | The actual implementation in the user's project
3+
-- | is generated by the `oclis build` command.
4+
5+
module Oclis.SpecEmbed where
6+
7+
fileContent :: String
8+
fileContent =
9+
"""
10+
{
11+
"name": "placeholder",
12+
"description": "This is a placeholder command"
13+
}
14+
"""

purescript/test/Executor.purs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ import Effect.Class (liftEffect)
99
import Test.Spec (Spec, describe, it)
1010
import Test.Spec.Assertions (fail, shouldEqual, shouldReturn)
1111

12-
import Oclis (callCommand)
12+
import Oclis (callCliApp, callCommand)
1313
import Oclis.Parser (tokensToCliArguments)
1414
import Oclis.Tokenizer (tokenizeCliArguments)
1515
import Oclis.Types (CliArgPrim(..), CliArgument(..), Oclis(..), emptyCliSpecRaw)
1616

1717
tests :: Spec Unit
1818
tests =
1919
describe "Execution" do
20+
it "executes a command with included spec" do
21+
let
22+
executor context = do
23+
context.command `shouldEqual` Nothing
24+
context.usageString `shouldEqual` "xxx"
25+
context.arguments `shouldEqual` []
26+
pure $ Ok unit
27+
28+
liftEffect (callCliApp executor) `shouldReturn` unit
29+
2030
describe "Help" do
2131
let
2232
cliSpec = Oclis emptyCliSpecRaw
@@ -65,12 +75,12 @@ tests =
6575
cliSpec = Oclis emptyCliSpecRaw
6676
usageString = "Irrelevant"
6777
executor context = do
68-
context.command `shouldEqual` Just "help"
78+
context.command `shouldEqual` Just "version"
6979
context.usageString `shouldEqual` usageString
7080
context.arguments `shouldEqual` []
7181
pure $ Ok unit
7282

73-
it "shows help output for -v" do
83+
it "shows version output for -v" do
7484
let
7585
toolArgs = [ "git", "-v" ]
7686
tokens = tokenizeCliArguments toolArgs
@@ -81,7 +91,7 @@ tests =
8191
liftEffect (callCommand cliSpec usageString cliArgs executor)
8292
`shouldReturn` (Ok unit)
8393

84-
it "shows help output for --version" do
94+
it "shows version output for --version" do
8595
let
8696
toolArgs = [ "git", "--version" ]
8797
tokens = tokenizeCliArguments toolArgs
@@ -92,9 +102,9 @@ tests =
92102
liftEffect (callCommand cliSpec usageString cliArgs executor)
93103
`shouldReturn` (Ok unit)
94104

95-
it "shows help output for `help`" do
105+
it "shows version output for `version`" do
96106
let
97-
toolArgs = [ "git", "help" ]
107+
toolArgs = [ "git", "version" ]
98108
tokens = tokenizeCliArguments toolArgs
99109

100110
case tokensToCliArguments cliSpec tokens of

0 commit comments

Comments
 (0)