How to add all projects to a single solution with dotnet sln? - c#

Following examples from here I'm trying to execute
dotnet sln AllProjects.sln add **/*.csproj
But I get this error:
Could not find project or directory **/*.csproj.
Looks like wildcards are not working. What am I doing wrong?

For Windows, open PowerShell and run this command to add all projects to the solution file:
dotnet sln add (ls -r **/*.csproj)

I've missed this statement:
Globbing patterns are supported on Unix/Linux based terminals
My Windows PowerShell solution looks like this:
$projects = Get-ChildItem -Recurse | Where-Object { $_.Name -match '^.+\.(csproj|vbproj)$' }
$uniqueProjects = $projects | Group-Object -Property Name | Where Count -EQ 1 | select -ExpandProperty Group | % { $_.FullName }
Invoke-Expression -Command "dotnet new sln -n AllProjects"
$uniqueProjects | % { Invoke-Expression -Command "dotnet sln AllProjects.sln add ""$_""" }

On Windows you could also use the following command to recursively add all the projects in sub-directories to a pre-existing solution file:
FOR /R %i IN (*.csproj) DO dotnet sln add "%i"
Alternatively, if you need to (re)create solution files often then you could create a batch file with the following content, and then just run it whenever you need to:
dotnet new sln
FOR /R %%i IN (*.csproj) DO dotnet sln add "%%i"
Please note that you need the extra % when trying to do this inside the batch file.

I tried ls -r on git bash
dotnet sln add (ls -r **/*.csproj)
but it gives me
$ dotnet sln add (ls -r **\*.csproj)
bash: syntax error near unexpected token `('
And then i tried
dotnet sln add **/*.csproj
it worked for me on git bash(windows)

Related

How to reproduce Visual Studio ClickOnce publish from command line

I have been publishing my app by clicking the Publish button in the VS2022 project Publish tool. I want to recreate the same behaviour from the command line.
My C# project targets .NET 6.0, and my .pubxml has <PublishProtocol>ClickOnce</PublishProtocol>.
These docs sound like they should document what I need, but the suggested variations on msbuild /target:publish absolutely do not produce the same result as clicking the button in VS:
The <PublishDir> and <PublishUrl> from the .pubxml are ignored.
The <ApplicationRevision> is not updated even when <IsRevisionIncremented> is true.
Most importantly, the output is missing the main MyApp.application file and the Application Files folder.
Update:
A script to achieve your requirements:
#find the .csproj file
$csproj = Get-ChildItem -Path . -Filter *.csproj
# #return the name of the project
$projectname = $csproj.Name -replace ".csproj",""
$publishdir = "bin\publish"
$publishXML = "Properties\PublishProfiles\ClickOnceProfile.pubxml"
#remove all content of the "$publishdir/Application Files" directory
Get-ChildItem -Path $publishdir"\Application Files" | Remove-Item -Recurse
MSBuild.exe /target:publish /p:COnfiguration=publish /p:PublishProfile=Properties\PublishProfiles\ClickOnceProfile.pubxml /p:PublishDir=$publishdir
#remove all files or directories except setup.exe, projectname.application and the directory "Application Files"
Get-ChildItem -Path $publishdir | Where-Object {$_.Name -ne "setup.exe" -and $_.Name -ne $projectname+".application" -and $_.Name -ne "Application Files"} | Remove-Item -Recurse
#get the content of Project.PropertyGroup.ApplicationRevision in the .pubxml file
$pubxmldata = [xml](Get-Content $publishXML)
$ApplicationRevision = $pubxmldata.Project.PropertyGroup.ApplicationRevision
#increment the ApplicationRevision value
$ApplicationRevision = [int]$ApplicationRevision + 1
#replace the ApplicationRevision value in the .pubxml file
$pubxmldata.Project.PropertyGroup.ApplicationRevision = [string]$ApplicationRevision
#save the .pubxml file
$pubxmldata.Save($publishXML)
Original Answer:
The <PublishDir> and <PublishUrl> from the .pubxml are ignored.
The <ApplicationRevision> is not updated even when
is true.
Most importantly, the output is missing the main
MyApp.application file and the Application Files folder.
For the first and the third requirement, using the below command will solve the issue:
msbuild -t:restore
msbuild /target:publish /p:Configuration=publish /p:PublishProfile=Properties\PublishProfiles\ClickOnceProfile.pubxml /p:PublishDir=bin\Release\net6.0-windows\app.publish\ /p:PublishUrl="bin\publish\"
Result:
For the second requirement, the value didn't increase is totally expected, because it is not automatically incremented for builds performed at the command-line. See this official document:
Publish Properties
Increase this value need based on IDE, pure msbuild command can't achieve this.

Use TextTransform (tt files) into the Azure Devops pipeline

Is it possible to transform the **/*.tt file into a *.cs file.
Using Azure Devops pipeline?
Otherwise is there a CLI command available for Dotnet core using TextTransform ?
I already test : T5.TextTransform.Tool but is don't work (and deprecated)
Thanks for your help
How i solve this problem using Devops pipeline + script:
As mention #Leo Liu-MSFT Install dotnet-t4
install global -g
Create powershell script and find tt file
Seach all *.tt file and convert them with the t4 command
Get-ChildItem -Path .\ -Filter *.tt -Recurse -File -Name| ForEach-Object {
$file = [System.IO.Path]::GetFileName($_);
$directory = [System.IO.Path]::GetDirectoryName($_)
"Conversion file : " + $file
t4 "$directory\$file" -I="$directory"
}
NOTE : It is important to place the T4.ps1 file in the parent directory of your *.tt files
Is it possible to transform the **/*.tt file into a *.cs file. Using Azure Devops pipeline?
The answer is yes.
According to the state of the package T5.TextTransform.Tool:
T5 was a stopgap measure for a time when Mono.TextTemplating was not
available for .NET Core. Now that that is no longer the case, T5 is
not needed and no longer being maintained. Use Mono.TextTemplating's
dotnet-t4 instead.
So, we could use the Mono.TextTemplating instead of T5.TextTransform.Tool.
Besides, there is also an implementation of the TextTransform.exe command-line tool, we could use the command line to transform the .tt file into .cs file:
"%CommonProgramFiles%\Microsoft Shared\TextTemplating\1.2\texttransform.exe" -out %1.cs -P %2 -P "%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.5" %1.tt
Check this thread for some more details.
Hope this helps.

Weired COMMAND in container for ASP.NET Core applications

I developed a ASP.NET Core service in Visual Studio 2019 with "Docker Support" (Linux Container).
The solution contains three projects, with one service and two librarys.
Starting and debugging works as expected. I can see "tail -f /dev/null" in the COMMAND column of
docker container ps
, what make sense while debugging in my opinion.
I added UseKestrel() to the webhost configuration, suggested somewhere...
If I try to build the Dockerfile via context menu inside Visual Studio, a "COPY failed" error appears. After reading the last days about docker and .NET Core, I can fix this by using PowerShell and
docker build -f .\fooWebService\Dockerfile -t foowebservice --target base --label "com.microsoft.created_by_visual_studio_2019" .
The Dockerfile looks like (generated from creating project):
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["fooWebService/fooWebService.csproj", "fooWebService/"]
COPY ["IfooInterface/fooInterface.csproj", "IfooInterface/"]
COPY ["fooService/fooService.csproj", "fooService/"]
RUN dotnet restore "fooWebService/fooWebService.csproj"
COPY . .
WORKDIR "/src/fooWebService"
RUN dotnet build "fooWebService.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "fooWebService.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "fooWebService.dll"]
This will end up in the foowebservice:latest image.
If I try to run this image (after fiddeling with certificates, Kestrel, parameter, options, aaaaarg), it won't start. The container stops immediately.
docker run -d -p 63173:80 -p 44304:443 -e ASPNETCORE_ENVIRONMENT=Production -e ASPNETCORE_URLS="https://+:443;http://+:80" -e ASPNETCORE_HTTPS_PORT=44304 Kestrel__Certificates__Default__Path=/root/.dotnet/https/aspnetapp.pfx -e Kestrel__Certificates__Default__Password=crypticpassword -v $env:USERPROFILE/.aspnet/https/:/root/.dotnet/https foowebservice
The thing is, now in the COMMAND column appears "bash"! What about the ENTRYPOINT in Dockerfile? Complete ignored?
In a 'I-dont-remember-anymore'-combination, I can go interactive into the container and see that the /app-directory is available, but empty.
I don't really know, if there must be something inside? The line
:
WORKDIR /app
:
suggest that behavior.
The whole bunch of steps I do with the simplest project template for ASP.NET Core webservice, and this one shows "dotnet TestWebApp.dll" in COMMAND as expected?!?
PM> docker ps -a
aa2f9a8b0512 test "dotnet test.dll" 3 seconds ago Up 1 second 0.0.0.0:63173->80/tcp, 0.0.0.0:44304->443/tcp mystifying_mclean
d635bfac782b mdtdbsinoswebservice:dev "bash" About a minute ago Exited (0) About a minute ago awesome_lamarr
1e14dc686250 mdtdbsinoswebservice:dev "tail -f /dev/null" 2 minutes ago Up 2 minutes 0.0.0.0:63164->80/tcp, 0.0.0.0:44395->443/tcp sad_shockley
aa2f9a8b0512 is the simple webapp, only "UseKestrel"-change
d635bfac782b is from my docker build.... line
1e14dc686250 is the one from Visual Studio --> Run Docker
I can build, push, pull and run the simple one successfully on a linux server.
I have no clue for resolving this COMMAND behavior issue nor to understand, what happend on build my "More-Than-One-Project"-Solution and why it doesnt work as a standalone image/container besides IDE?
So my questions, at least, are:
What is the correct workflow for....
creating a ASP.NET Core Solution in Visual Studio 2019 with dependency projects inside?
modifying the DockerFile or using a docker-compose'ing for production like behavior?
build a "standalone"-image and run the container successfully?
New knowledge
First, now I know that
... --target build ...
cannot result in a working image. The stages are not complete.
But I can't build an image in case of COPY failed any time.
In Dockerfile I try relative paths, absolute paths, form solution directory, from project directory, same in combination of the
docker build -f ...
syntax, but nothing works!
I'm not able to do a simple COPY ["thisfile",""] without a
COPY failed: stat /var/lib/docker/tmp/docker-builder849213770/usless.file: no such file or directory
Error.
The tipp from .net core docker is working via VS2019, but image build is getting error and not working doesn't solve my problem.
The most confusing thing is, that the simple test project works.
...one step further
It was the last parameter of
docker build -f ".\fooWebService\Dockerfile" -t foowebservice:dev --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=fooWebService" "."
, that change the behavior of Build-/Source-/Contextpath problems.
But now I can't get external references within the
dotnet build
process.
The build fails with
warning MSB3245: Could not resolve this reference. Could not locate the assembly "MyUsefulLibrary". Check to make sure the assembly exists on disk. If this reference is required by your code, y
ou may get compilation errors. [/src/fooService/fooService.csproj]
Q: Don't know at the time, where to put these libraries while building in image...?
I try also to use a local NuGet-repository and build packages from my libraries, but the build process searchs the packages in 'nuget.org'... !
Q: No idea where I can add these packages into build process?
Solution
Hello everybody,
For anyone who has the same problems docking an ASP.NET solution with projects that are not in the same directory, I think there is a workaround.
Since there is no automatism, some steps have to be solved manually.
First you have to add 'Container Orchestration Support'.
After that you got (Example solution directory structure):
Somewhere
+- Library1
| +- Library1 Project
| +- Library1.csproj
+- Library2
| +- Library2 Project
| +- Library2.csproj
+- Main
| +- Main Project
| | +- Main.csproj
| | +- Dockerfile
| +- docker-compose.yml
| +- .dockerignore
In the resulting docker-compose.yml change the context path to .. and the path to the Dockerfile.
context: .
dockerfile: Main Project/Dockerfile
to
context: ..
dockerfile: Main/Main Project/Dockerfile
This moves the root directory from the project directory to your development directory 'Somewhere'.
In order for the contents of the .dockerignore file to work, you have to deposit a copy in the directory, the context points now.
Somewhere
+- Library1
| :
+- Library2
| :
+- Main
| :
+- .dockerignore
Now the paths in the Dockerfile have to be adjusted.
For each COPY, the path must be entered for pointing into the project directory in the src part.
The relative information ../ can be removed.
Before build, the WORKDIR must now be extended by the working directory.
Example Dockerfile (VS generated):
:
WORKDIR /src
COPY ["Main Project/Main.csproj", "Main Project/"]
COPY ["../Library1/Library1 Project/Library1.csproj", "../Library1/Library1 Project/"]
COPY ["../Library2/Library2 Project/Library2.csproj", "../Library2/Library2 Project/"]
RUN dotnet restore "Main Project/Main.csproj"
COPY . .
WORKDIR "/src/Main Project"
RUN dotnet build "Main.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Main.csproj" -c Release -o /app
:
...change to:
:
WORKDIR /src
COPY ["Main/Main Project/Main.csproj", "Main/Main Project/"]
COPY ["Library1/Library1 Project/Library1.csproj", "Library1/Library1 Project/"]
COPY ["Library2/Library2 Project/Library2.csproj", "Library2/Library2 Project/"]
RUN dotnet restore "Main/Main Project/Main.csproj"
COPY . .
WORKDIR "/src/Main/Main Project"
RUN dotnet build "Main.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Main.csproj" -c Release -o /app
:
If you create the solution now, the process runs smoothly and successfully!
Hope someone finds these hints useful.

.NET Core Unit tests not shown in AppVeyor Tests window (and badge)

Follow up from this question, I'm currently setting up AppVeyor for my project (here) and my .NET Core tests are only shown in the console output but not in the Tests window.
This is the link for the AppVeyor project: ci.appveyor.com/project/Sergio0694/neuralnetwork-net
If some tests fail, the console correctly shows an error and the build is marked as failing, but the Tests window is empty anyways. Same goes for the badge from shields.io which shows 0 total tests, even if I can see many of them being executed from the console output.
Here's the console output:
And here's the Tests window:
Is there something else I have to setup in order for them to be reported correctly outside the console window?
Please add https://www.nuget.org/packages/Appveyor.TestLogger to your test projects.
An arguably cleaner alternative to adding an otherwise unused reference to your test project is to do this in your test script:
cd <test_project_dir>
nuget install Appveyor.TestLogger -Version 2.0.0
cd ..
dotnet test --no-build --no-restore --test-adapter-path:. --logger:Appveyor <test_project_dir>
This has the same effect as adding the reference, in that it makes the testlogger binary available to the test framework, but it doesn't actually change the test project, and therefore doesn't require someone who's not using Appveyor to install the package when they clone and build your repo.
The slight advantage of this solution over outputting and subsequently uploading .trx files (as in the PS script above) is that you should get the test results in real-time, rather than all at the end.
Example appveyor.yml:
version: 0.0.{build}
build_script:
- cmd: dotnet build MySolution.sln
test_script:
- cmd: cd Test
- cmd: nuget install Appveyor.TestLogger -Version 2.0.0
- cmd: cd ..
- cmd: dotnet test --no-build --no-restore --test-adapter-path:. --logger:Appveyor Test
You can add the AppVeyor.TestLogger package to your project, but it can be done without changing your code. You need to output your tests results into an xml file format that AppVeyor understands and then upload it to their HTTP API. The following powershell snippet will iterate through your solution and find each test project, call dotnet test on the csproj and log the output to test-result.trx and then upload the file to AppVeyor.
$config = "release"
# Find each test project and run tests and upload results to AppVeyor
Get-ChildItem .\**\*.csproj -Recurse |
Where-Object { $_.Name -match ".*Test(s)?.csproj$"} |
ForEach-Object {
# Run dotnet test on the project and output the results in mstest format (also works for other frameworks like nunit)
& dotnet test $_.FullName --configuration $config --no-build --no-restore --logger "trx;LogFileName=..\..\test-result.trx"
# if on build server upload results to AppVeyor
if ("${ENV:APPVEYOR_JOB_ID}" -ne "") {
$wc = New-Object 'System.Net.WebClient'
$wc.UploadFile("https://ci.appveyor.com/api/testresults/mstest/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\test-result.trx))
}
# don't leave the test results lying around
Remove-Item .\test-result.trx -ErrorAction SilentlyContinue
}

c# makefile on windows

What do I need to install/configure to run the following makefile on windows 7:
MONO = mono
MONOC = gmcs
MONOCFLAGS = -optimize -reference:${PARSERREF}
GPLEX = ${MONO} gplex.exe
GPPG = ${MONO} gppg.exe
PARSERREF = bin/ShiftReduceParser.dll
CSFILES = Absyn.cs Parser.cs Printer.cs Scanner.cs Test.cs VisitSkeleton.cs AbstractVisitSkeleton.cs
all: test
clean:
rm -f test.pdf test
distclean: clean
rm -f ${CSFILES}
rm -f test.l test.y test.tex
rm -f Makefile
test: Parser.cs Scanner.cs
#echo "Compiling test..."
${MONOC} ${MONOCFLAGS} -out:bin/test.exe ${CSFILES}
Scanner.cs: test.l
${GPLEX} /out:$# test.l
Parser.cs: test.y
${GPPG} /gplex test.y > $#
I have always used Visual Studio, however, this time, this is generated by BNFC (Parser generator). I don't see the csc.exe (CSharp compiler) in this makefile. Can someone please explain this to me?
Note: I have got gplex.exe and gppg.exe and I have .NET installed on my machine.
You need to download and install GNU make for Windows
http://gnuwin32.sourceforge.net/packages/make.htm
Afterwards you open cmd.exe and cd to the directory where the makefile is in.
Then you type make and press enter, or make test in this case.

Categories

Resources