5

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.

Community
  • 1
  • 1
Matthew James Davis
  • 12,134
  • 7
  • 61
  • 90

3 Answers3

2

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.

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • 1
    haha I like this answer, but I'm really disappointed. Did the .net core team really build an incomplete cli? – Matthew James Davis May 01 '17 at 11:22
  • 2
    @MatthewJamesDavis The thing with big projects is that you want to do things with your tooling that you can't simply do with configuration, so you make your own tools (programming). But if you want to make something simple just create a run.bat and add your command in there, tell people to double-click that file. (better let your programmers learn how the dotnet CLI tool works) – Joel Harkes May 01 '17 at 12:28
2

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

Stan88
  • 252
  • 3
  • 12
0

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
mageeeeep
  • 13
  • 1
  • 2