OdeToCode IC Logo

.NET Core Opinion #4 - Increase Productivity with Dev Scripts

Friday, September 21, 2018

In a previous post I mentioned that a scripts directory can be a welcome addition to any source code repository. What goes into scripts? Anything you can automate to make a developer’s life easier!

Examples for Inspiration

Here’s a script I’ve used to simplify adding an EF migration. All I need to do from the command line is addmigration [migration_name].

pushd src\beaverleague.data
dotnet ef migrations add %1
dotnet ef database update
popd

I also have a recreatedb script I can use to start fresh after pulling changes.

pushd src\beaverleague.web 
dotnet run dropdb migratedb seeddb stop 
popd

More on how the parameters above work in a future post.

The EF repo itself uses a tools folder instead of a scripts folder, but the idea is the same. Inside you’ll find scripts to clean up test environments by dropping and shrinking databases, like this one that uses a combination of sqlcmd and sqllocaldb command line tools, as well as a script to query for all the non-system databases in a DB instance.

@echo off 
sqlcmd -S "(localdb)\mssqllocaldb" -i "DropAllDatabases.sql" -o "DropAll.sql" 

sqlcmd -S "(localdb)\mssqllocaldb" -i "DropAll.sql" 

del "DropAll.sql" 

sqllocaldb stop mssqllocaldb 
sqllocaldb delete mssqllocaldb 

ShrinkLocalDBModel.cmd

For more examples and ideas, checkout the TypeScript repo with scripts for everything from running tests to automating GitHub issues with OctoKit. There’s the vscode repo with scripts to setup an environment. The repo to build the official .NET Docker images includes Powershell scripts to execute docker pull with retries.

These are all examples where 5 or 6 lines of script code can not only save time for the entire team in the long run, but also codify a common operation.

dotnet

I specifically want to call out special capabilities of the dotnet CLI tool. We’ve always had the ability to build, publish, and package from the command line, but the new global tools feature gives us an npm-ishly easy path to installing new tools and use them from anywhere.

Here are some of the tools I use.

Nate McMaster maintains a more complete list of global tools.

Summary

Take advantage of the command line renaissance in .NET Core to speed up a repeatable development process.