dotnet-script icon indicating copy to clipboard operation
dotnet-script copied to clipboard

Consider adding watch support

Open TomasHubelbauer opened this issue 6 years ago • 6 comments

Would it be possible to add watch support? I tried to use dotnet watch script main.csx to see if the .NET CLI native watch functionality could be hijacked, but that doesn't work:

Could not find a MSBuild project file

Hopefully it's possible to tap into that, because implementing watch infrastructure is an undertaking. In any case, I hope this can be delivered, as it would be quite useful when developing servers in scripts or tweaking scripts which produces some sort of a set (say a CSV - which is then watched by some other program).

Thoughts?

TomasHubelbauer avatar May 14 '19 15:05 TomasHubelbauer

This is not something we have discussed, but that isn't to say that it is a bad idea. I don't think we can use dotnet watch here since that expects a project file and we don't have a project file for scripts.

Just for fun I threw something together as a script (watch.csx)

#!/usr/bin/env dotnet-script
#r "nuget:SimpleExec, 5.0.1"

using static SimpleExec.Command;

string pathToScript = Args[0];

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Directory.GetCurrentDirectory();
watcher.Filter = "*.csx";
watcher.IncludeSubdirectories = true;
watcher.Changed += (sender, args) => ExecuteScript();
watcher.EnableRaisingEvents = true;

//Wait for the user to quit the program.
WriteLine("Press 'q' to quit");

while (Console.Read() != 'q') ;
private void ExecuteScript()
{
    Run("dotnet", "script " + pathToScript, Directory.GetCurrentDirectory());
}

If you are on *nix you can pretty easily make watch.csx a global command with a symlink.

Then you can do something like the following in any folder containing script files.

watch myscript.csx

seesharper avatar May 14 '19 20:05 seesharper

This will come in handy, thanks! I'll leave this open as a feature request for an integrated watch with this as a workaround if that's cool.

TomasHubelbauer avatar May 15 '19 04:05 TomasHubelbauer

dotnet watch explicitly looks for csproj - it doesn't really support 3rd party dotnet tools, it was designed to support the built-in commands of the CLI (run, test, exec etc). in script cs we had watch built into the tool as a command, we might want to consider it here too, although I think the better way would be to PR into dotnet-watch and allow it to interop with 3rd party tools instead

filipw avatar May 15 '19 07:05 filipw

I like the idea of extending dotnet watch because it would be really cool if any .NET CLI tool could benefit from it - good idea. It would make the watching more consistent and predictable no matter the tool being watched. It's kinda funny that the dotnet watch implementation is in the AspNetCore repository, gave me a pause because I don't think I ever used it with ASP .NET Core.

TomasHubelbauer avatar May 15 '19 07:05 TomasHubelbauer

I think the better way would be to PR into dotnet-watch and allow it to interop with 3rd party tools instead

That's the intention but it's been moved out of the 3.0 milestone recently.

I'm the only one who's :+1: the issue and the fact that it has a glaring bug (doesn't watch new files) in its current form makes me wonder sometimes if anyone uses it at all. 😕

atifaziz avatar May 15 '19 13:05 atifaziz

I think the bug link you posted is broken, it 404s for me.

Sorry, my bad. I've fixed the link. It was indeed aspnet/AspNetCore#8321.

atifaziz avatar May 15 '19 13:05 atifaziz