Is there a built-in way to pass command-line arguments to a "shakefile"? I'd like to pass --env production|development|staging
and then use it within my rules to (slightly) alter the build-steps for each environment.
Asked
Active
Viewed 45 times
2

Saurabh Nanda
- 6,373
- 5
- 31
- 60
1 Answers
1
There are two halves to this problem - first getting the flags into Shake, and secondly, using them to influence the behaviour.
You can get arguments into Shake using any Haskell command line parser, but Shake ships with support for one built it, which can often be easier:
data Flags = Production | Dev | Staging deriving Eq
flags = [Option "" ["production"] (NoArg $ Right Production) "Build production."
,Option "" ["dev"] (NoArg $ Right Dev) "Build dev."
,Option "" ["staging"] (NoArg $ Right Staging) "Build staging."
]
main = shakeArgsWith shakeOptionsn flags $ \flags targets -> do
want targets
... do whatever you want with the flags ...
return rules
For using the flags to influence, you might want to:
- Completely isolate build outputs from each flag, in which case changing the directory and setting shakeFiles differently in each case makes each one fully distinct.
- Use the flags to change the output paths, so you always have rules
dev/main.js
andprod/main.js
, and then you consult the flags when doingwant
to pick up the right rules. - Put the flags into an Oracle and have them as tracked settings, so when you flip from prod to dev some things rebuild.
If the builds are 80%+ distinct I'd go for 1. If you change flags very rarely 3 can work. Otherwise, I tend to opt for 2. But they all work, so picking the simplest to start with is also not unreasonable.

Neil Mitchell
- 9,090
- 1
- 27
- 85