27
votes

Assuming this folder structure

SampleApp
        global.json
        Src  
            Web             
                project.json
                Startup.cs
                ...
            Model
                project.json
                Startup.cs
                ...

how does one compile both projects using dotnet? (from command line, not in visual studio)

If you run dotnet build at the root folder level you get

Could not find file .. project.json

I can see there is this outstanding enhancement on the CLI repo but that is from Feb2.

Any script would have to take dependencies into account before just blindly calling dotnet on all src sub-folders.

5

5 Answers

27
votes

The dotnet build command accepts glob patterns. So you can do this:

dotnet build Src/**/project.json
3
votes

There's no such a tool yet. Even KoreBuild, the tool that the ASP.NET team uses, goes blindly in each folder and invokes dotnet build/pack.

The nice thing is that dotnet build is now smart enough to not recompile the dependencies if they haven't changed, so that's not a problem anymore.

2
votes

I had a similar requirement. This is my workaround:

@echo off
for /D %%d in (*) do (
    cd %%d  
    cd  
    dotnet restore
    dotnet build
    cd ..
)
exit /b
2
votes

Use GNU Make. I use it to build my projects. all you have to do create a Makefile in your project root folder. You can nest Makefiles in directories and have a Top Level Makefile that runs the subdirectories. then you set up Makefiles for each of your "Sub Projects" folders and run any comandline tool. with dotnet core is is dotnet .

Wait... GNU - "GNU is not Unix" that's a Unix/Linux application... I run windows. Well the good news is you can do this is in windows. I'm using make.exe through my git-bash installation (git for windows). You will have to go find the cygwin port of make. (google: "make for git-bash") Then install it to your bin directory under the cygwin folder. You could also just install cygwin if you really wanted to.

The nice thing about using Gnu-Make is it is universal. Since dotnet core is platform agnostic, every environment Mac/FreeBSD/Linux have "make" most likely already installed. Adding it to your Windows machine and projects to me makes a lot of sense. Since you project can now be built by everyone the same way.

some of my projects need to build docker containers with dockerfiles, or snap packages, deploy to test, etc... Make (pardon the pun) makes it easy.

Here is a sample of simple projects Makefile. Running 'make' by itself is like saying 'make all' you could set up a command like 'cd ./subdir; make' as one of your .phoney directives. (Google: "Makefile documentation")

project_drive?=/c/prj
nuget_repo_name?=Local_Nuget_Packages
local_nuget_dir?=$(project_drive)/$(nuget_repo_name)

RELEASE_VERSION:= `grep "<Version>" *.csproj | cut -d '>' -f 2 | cut -d '<' -f 1`

.PHONEY: clean release test doc nuget install debug_nuget debug_install

all: doc MSBuild

test:
  ./test.sh

MSBuild: 
   dotnet build

clean:
   dotnet clean; dotnet restore

release: 
   dotnet build -c Release

doc:
   doxygen ./Doxyfile.config

nuget: release
   dotnet pack -c Release

install: 
   cp ./bin/Release/*.$(RELEASE_VERSION).nupkg $(local_nuget_dir)

debug_nuget: MSBuild
   dotnet pack 

debug_install: 
   cp ./bin/debug/*.$(RELEASE_VERSION).nupkg $(local_nuget_dir)
0
votes

What's missing is that you can also use the commands on project.sln files if you do not have project.json

dotnet build src/**/project.json
-- or --
dotnet build src/project.sln

same goes for dotnet test