Niku Hysa
Niku Hysa

Reputation: 76

Have a set of scripts run in C# Projects

I am a web developer moving to the C#/.NET world. In npm I usually write the scripts I will need for development (run unit tests/local server/compilation) in the package.json file. Its easy to access, and it is very helpful for CI/CD pipelines.

My question is, are there any alternatives similar to this in C#/.NET, or are there any industry standards for where and how to store these scripts needed for development/deployment.

For dependencies management, paket (https://fsprojects.github.io/Paket/) is used.

Upvotes: 2

Views: 484

Answers (1)

sommmen
sommmen

Reputation: 7628

I do not work at a company has web technologies as its core business, so there are maybe better solutions.

In .NET however project files are built with MSBuild (the build engine). You can directly edit the project file(s) and add commands, copy files, etc.

For example in project.csproj i added a section to automate api documentation (swagger/openapi3) :

  <!-- Enriches the swagger/openapi specification with documentation from xml -->
  <PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
   </PropertyGroup>

  <!-- The following target generates the files according to the Nswag configuration file(s?) present in the project. -->
  <!-- NOTE do not use the project file in the nswag configuration when using NSwag.Build, this causes a Build loop, spawning a bunch of dotnet processes. Use the assembly! See: https://github.com/RicoSuter/NSwag/issues/1445-->
  <Target Name="NSwag" AfterTargets="Build">
    <Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />
    <Message Text="Performing cmd: $(NSwagExe_Core30) run /variables:Configuration=$(Configuration),OutDir=$(OutDir) /runtime:NetCore30 /nobuild:true" />
    <Exec Command="$(NSwagExe_Core30) run /variables:Configuration=$(Configuration),OutDir=$(OutDir) /runtime:NetCore30 /nobuild:true" ConsoleToMSBuild="true" />
    <RemoveDir Directories="$(OutDir)References" />
  </Target>
</Project>

Now this gets ran each time I build. Maybe you only want certain operations to run when you actually publish the project? well you can do so with publish profiles:

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-3.1#run-a-target-before-or-after-publishing

Additionally we also have a build server that does a lot of this for us. We for example don't package the application on each build, but the buildserver does this with a post-build script (in addition to various other tasks).

We use devops for this; https://learn.microsoft.com/en-us/aspnet/core/azure/devops/cicd?view=aspnetcore-3.1

I think it would be helpful to get a good idea of what you want to do when, and pick a strategy accordingly. Once again - There may be other ways but this is how we do it.

Upvotes: 1

Related Questions