adamralph / bau

The C# task runner
MIT License
152 stars 17 forks source link

Ability to pass parameters to the script being executed #233

Open karthik25 opened 8 years ago

karthik25 commented 8 years ago

Hello!

This pull request provides a way to pass parameters to a bau script file - I based this off of the issue #212. I wanted this for what I am currently trying to achieve with bau!

Here is how you could possibly use it:

C:\bau>scriptcs script_with_params.csx -- process_parameters -p FirstName=John -p LastName=Doe

Corresponding bau script file with details on how the context is retrieved and used.

var bau = Require<Bau>();
var parameters = bau.GetParameterContext();

bau
    .Task("process_parameters")
    .Do(() =>
    {
        Console.WriteLine("Parameters count: " + parameters.Count);
        Console.WriteLine("Parameter 'FirstName': " + parameters["FirstName"]);
        Console.WriteLine("Parameter 'LastName': " + parameters["LastName"]);
    })
.Run();

What follows is the output when running this script

C:\bau>scriptcs script_with_params.csx -- process_parameters -p FirstName=John -p LastName=Doe
[Bau] Bau 0.1.0 Copyright (c) Bau contributors (baubuildch@gmail.com)
[Bau] Running 'process_parameters' and dependencies...
[Bau] Starting 'process_parameters'...
Parameters count: 2
Parameter 'FirstName': John
Parameter 'LastName': Doe
[Bau] Finished 'process_parameters' after 8.58 ms.
[Bau] Completed 'process_parameters' and dependencies in 23.8 ms.

Please check it out!

karthik25 commented 8 years ago

By the way, this is new fork of bau and I am not using the previous fork I used for my previous contribution!

karthik25 commented 8 years ago

Just wanted to add one more thing - The changes I made also supports accessing the parameters using dynamic, like

var bau = Require<Bau>();
dynamic parameters = bau.GetParameterContext();

bau
    .Task("process_parameters")
    .Do(() =>
    {
        Console.WriteLine("Parameters count: " + parameters.Count);
        Console.WriteLine("Parameter 'FirstName': " + parameters.FirstName);
        Console.WriteLine("Parameter 'LastName': " + parameters.LastName);
    })
.Run();

But this would fail as of since it looks like dynamic is not yet implemented in Roslyn. For example I got the following error:

C:\bau>scriptcs script_with_parameters.csx -- process_parameters -p FirstName=John -p LastNa
me=Doe
ERROR: Script compilation failed. [CompilationErrorException] D:\bau\parameters.
csx(2,1): error CS8000: This language feature ('dynamic') is not yet implemented
 in Roslyn.
adamralph commented 8 years ago

Hi @karthik25 thanks for raising this, I'll take a look ASAP.

karthik25 commented 8 years ago

@adamralph You are welcome and thanks for the opportunity!!

adamralph commented 7 years ago

@karthik25 after careful consideration I've decided to mothball this project. If you're interested in taking it over, please let me know.