4
votes

My .net core application is getting amazing, and all the bros want to use it. I need to give them a fail safe way to get the project running, as well as a strategy for adding publish tasks down the road.

Right now, thanks to help I received in this question and this question, I run my application using the following command.

dotnet watch run --environment "Development"

I'd love it if I can get my devs using something more straightforward, maybe a custom task ala npm scripts:

dotnet run dev

I understand there is infrastructure to support this kind of thing via msbuild in a .csproj file, but the docs haven't worked out for me.

3
if these docs answer my question, apparently all of it. do you know the answer? - Matthew James Davis
It depends on which one of the options makes the most sense to you. I don't see why I should repeat the docs here. - svick
thanks for your help @svick - Matthew James Davis
it seems even with a bounty i was unable to get an answer, perhaps this simply cannot be done with dotnet core. disappointing. - Matthew James Davis

3 Answers

1
votes

Nodejs has amazing cli and scripting features, thats what we use in our project for devops tooling.

simply add a package.json with scripts:

{
  scripts : {
    start: "dotnet watch run --environment Development",
  }
}

Now you can start your app either using: npm start or: npm run start, you can add tons of scripts if you want.

If you want more freedom for developers to choose look at how to make a cli tool yourself. its very easy and i feel using a scripting language gives more freedom then using any msbuild/C# code tooling. and cli arguments parsing npm package.

This ofcourse needs every developer to have node installed though.

we did something like this in the commandline: ourAppName start --watch --environment Development --config Debug (ofc these are default flags). With nodejs you can spawn new processes and giving these processes new Environment Variables so its easy to overwrite, even appsettings this way.

2
votes

One option is to use flubu (fluent builder). It's an open source C# library that we wrote for building projects and executing deployment scripts using C# code.

More about flubu can be found here: choice-for-build-tool-msbuild-nant-or-something-else

For your particular case you'd have to write something like this:

public class BuildScript : DefaultBuildScript
{
    protected override void ConfigureTargets(ITaskContext session)
    {
        context.CreateTarget("run.dev")
                .AddCoreTask(x => x.Restore()) //// Optional to restore cli tool watch
                .AddCoreTask(x => x.ExecuteDotnetTask("watch")
                    .WithArguments("run")
                    .WithArguments("--enviroment", "Development"));
    }
}

And then to run the script you would execute command: dotnet flubu run.dev

Update:

You could also add to target: .SetAsDefault()

Then you could execute script with the command dotnet flubu

0
votes

You can invoke MSBuild using the dotnet CLI. See the example task CopyTestJS below. I am using dotnet cli version 5.0.301 on ubuntu.

    <Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
    </ItemGroup>

    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
    </ItemGroup>

    <ItemGroup>
        <Folder Include="wwwroot" />
    </ItemGroup>

    <!-- Example Custom Target -->
    <Target Name="CopyTestJS">
        <Copy SourceFiles="test.js" DestinationFolder="wwwroot" />
    </Target>

    </Project>

You may run the task using the dotnet CLI.

dotnet msbuild -target:CopyTestJS