I'm just getting started with FAKE. I really like the idea. In the tutorials the set the build and deploy directories.
// Directories let buildDir = "./build/" let testDir = "./test/" let deployDir = "./deploy/"
and then later reference those which is fine, but is it possible to pass that as a parameter? Maybe to as Task where I can use it later?
Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.
In JavaScript, you cannot pass parameters by reference; that is, if you pass a variable to a function, its value is copied and handed to the function (pass by value). Therefore, the function can't change the variable. If you need to do so, you must wrap the value of the variable (e.g., in an array).
Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.
Something like this should work.
From the command-line:
"..\packages\Fake.3.7.5\tools\Fake.exe" "build.fsx" dir=my/custom/dir/
In the build.fsx script:
let buildDir = getBuildParamOrDefault "dir" "./build/"
That will look for the dir parameter being passed in, and use it if it's assigned, otherwise it will default to ./build/
Non of the above fully worked for me. Below an adjusted solution.
The console command needs to be run with the -e
flag:
"..\fake.exe" "build.fsx" -e dir=sample/directory
The build script is using Environment.environVarOrDefault "dir" "default\directory"
:
let buildDir = Environment.environVarOrDefault "dir" "default\directory"
Environment.environVar
can also be used if no alternative is provided.
Finally the build script has to run with Target.runOrDefaultWithArguments
. It will fail if it's executed with Target.runOrDefault
:
// start build Target.runOrDefaultWithArguments "DefaultBuildTarget"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With