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

(GH-1) Add ability to reset the ScriptHost #2

Merged

Conversation

augustoproiete
Copy link
Sponsor Contributor

@augustoproiete augustoproiete commented Jan 23, 2021

I took an initial stab at this one and ended up going back to my initial idea of having a method that allows the dev to reset the ScriptHost.

There are two scenarios I'm thinking:

Scenario 1: (Not covered by this PR) The host app is controlled by the Cake dev, and the app is aware that the code being run is a Cake script and knows about Cake.Bridge, so it could potentially use something like ExecuteScoped, etc.

Scenario 2: (This PR) The host app is not controlled by the Cake dev, and the app is not aware that it's running a Cake script (all it knows is how to run C# code, and because it's referencing Cake.Bridge, everything magically works).

The goal in both scenarios is the same: Run a Cake script multiple times in the same process. Example of a Cake Script that would work after this PR:

build.cake:

var target = Context.Argument("target", "Test");
var configuration = Context.Argument("configuration", "Release");

Task("Test")
    .Does(() =>
    {
        Context.Information($"Hello from App Host ({configuration})");
    });

RunTarget(target);

#if MYCAKEHOST
ResetScriptHost();
#endif

We expect that the host app defines a constant MYCAKEHOST which tells us that CakeBridge is loaded and therefore we can call this method, otherwise running the .cake script normally via dotnet cake and other runners would just ignore this instruction.

Let me know what you think.

This seemed the cleanest way to call into Cake.Bridge whilst still being able to run the same code via Cake, and without changing the original Cake script code too much.


Closes #1

Copy link
Owner

@devlead devlead left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@devlead devlead merged commit 91a58d4 into devlead:master Feb 1, 2021
@devlead
Copy link
Owner

devlead commented Feb 1, 2021

@augustoproiete your changes have been merged, thanks for your contribution 👍

@devlead
Copy link
Owner

devlead commented Feb 1, 2021

@augustoproiete augustoproiete deleted the ability-to-reset-script-host branch February 1, 2021 16:09
@augustoproiete
Copy link
Sponsor Contributor Author

Awesome. Cheers @devlead ! this opens a very interesting door!

Just a small (but important) correction on my own sample script now that I had more coffee:

#if MYCAKEHOST
ResetScriptHost();
#endif

var target = Context.Argument("target", "Test");
var configuration = Context.Argument("configuration", "Release");

Task("Test")
    .Does(() =>
    {
        Context.Information($"Hello from App Host ({configuration})");
    });

RunTarget(target);

The reset needs to be at the top to make sure we always get a new host before anything else runs, otherwise an exception stops the execution flow and we're back to the original problem in #1 with the duplicate tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to reset the ScriptHost for allowing multiple runs on the same process
2 participants