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.
Related
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.
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)
Trying to build a Windows 8.1 store app with msbuild but getting an error. This is my first battle with msbuild and I haven't had much luck with the documentation as it all appears to leverage UWP specific things. The error I'm getting reads
C:\git\adr\win8app\src\AppDataRoom.WinRT.Adr\AppDataRoom.WinRT.Adr.csproj" (default target) (1) ->
(_GenerateAppxPackageRecipeFile target) ->
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\AppxPackage\Microsoft.AppXPackage.Targets(2156,5): error
APPX0702: Payload file 'C:\app\bin\x64\Release\WinRTXamlToolkit\WinRTXamlToolkit.xr.xml' do
es not exist.
There are 25 other errors following all related to WinRTXamlToolkit missing inside of the bin\x64\Release folder
My msbuild command I'm running is:
msbuild .\app.csproj /p:Configuration="Release" /p:Platform="x64
I understand WinRTXamlToolKit is a nuget package and I can see the dll inside of the release folder but how what do I do to solve this error? What am I missing?
I ended up finding a solution (although it still feels a little hacky). I ended up wrapping everything in an a powershell script. The script first restores the projects nuget packages, just in case it's not there for some reason. Then the script copies the WinRTXamlToolKit folder from packages into the bin/x64/Release folder and then I run the MSBuild command and now everything appears to build correctly. Here is what the script looks like (I don't write powershell scripts very often so my conventions may not be the best)
#create some alias
$nugetPath = $sourceControlRoot + ".nuget/nuget.exe";
$nugetPackagesPath = $sourceControlRoot + "/packages/";
$projectPath = $sourceControlRoot + "/TestingProject/"
Set-Alias nuget $nugetPath
#Nuget Restore
$solutionPath = $sourceControlRoot + "/TestingProject.sln"
nuget restore $solutionPath
#To Help MSBuild we need to copy the WinRTXamlToolkit into the bin/Release folders
$winRtXamlToolkitPath = $nugetPackagesPath + "WinRTXamlToolkit.1.6.1.3/lib/netcore451/WinRTXamlToolkit"
$copyOutput64 = $projectPath + "bin/x64/Release/WinRTXamlToolkit"
$copyOutput86 = $projectPath + "bin/x86/Release/WinRTXamlToolkit"
$testPath = $copyOutput64
if (!(Test-Path $testPath )) {
Copy-Item $winRtXamlToolkitPath $copyOutput64 -recurse
Copy-Item $winRtXamlToolkitPath $copyOutput86 -recurse
Write-Output "WinRTXamlToolkit copied into bin folders"
}
#build the project
$buildPath = $projectPath + "TestingProject.csproj"
msbuild $buildPath /p:Configuration="Release" /p:Platform="x64"
I would like to backup data of client computers with PowerShell. To do so I create a ZIP archive of the requested data which basically is no problem at all. But when it comes to special characters which use Unicode or the users build very long path names I run into trouble. I have tried different things now but haven’t found a solution yet. My client computers run Windows 10 (Build 1511). Installing the anniversary update (Build 1607) isn’t a solution due to other dependencies. Also the usage of 3rd party software to create ZIP files can’t be one.
Below are three methods I’ve found and tried already. They all have the same problems in common: when it comes to long path names they immediately stop the execution or they just skip the rest of the folder structure.
1. Create a ZIP file with PowerShell V5 CmdLet Expand-Archive
$Target = "C:\Temp\Test.zip"
$Source = "C:\Test"
Compress-Archive -Path $Source -DestinationPath $Target
2. Create a ZIP file with .net class
Add-Type -assembly "system.io.compression.filesystem"
$Target = "C:\Temp\Test.zip"
$Source = "C:\Test"
[io.compression.zipfile]::CreateFromDirectory($Source, $Target);
3. Create a ZIP file with Windows Explorer (Compressed Folders)
$Source = Get-ChildItem "C:\Test" -Recurse
$Target = "C:\Temp\Test.zip"
if (-not (Test-Path $Target)) {
Set-Content $Target ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $Target).IsReadOnly = $false
}
$objShell = New-Object -Com Shell.Application
$objZIP = $objShell.NameSpace($Target)
foreach($File in $Source) {
$objZIP.CopyHere($File.FullName)
Start-sleep -milliseconds 500
}
Then I found out, that it should be possible to access the local drives via UNC namespace which could look like \\?\C:\Test. But this doesn’t work with my Windows 10 build 1511 version. With build 1607 it is possible. What I don’t understand: why? I tried it with installing the latest .net version 4.6.2 on build 1511 but the problem still exists.
Can anybody help me with the access to (local) UNC namespaces or creating ZIP archives?
to access UNC path, use the $ characters to design drive
like \\computername\c$\path\path\file.txt
i use the .Net function because of compatibility with powershell v2
[IO.Compression.ZipFile]::CreateFromDirectory($SourcesFolder, $zipTempPath, $CompressionLevel, $False)
i use it with long UNC path with no problems.
I am trying to add a script (possibly a PowerShell script) to my Visual Studio C# project. The goal of this script is to re-create a file every time I build my project. Ideally this script would run before Visual Studio's build process every time I want to build my project.
I am completely new to this process and I don't seem to find a way (or understand) a way of doing this.
Thank you in advance for your help!
EDIT : This is what my PowerShell script looks like.
$file_path = 'C:\Users\<username>\Desktop\TestScripts'
$file_name = 'test.xml'
$file = $file_path +'\'+ $file_name
$file_exist = Test-Path $file
If($file_exist)
{
Remove-Item $file
}
$file_header_comment = '<!-- This file was auto generated during the build process -->'
New-Item -Path $file_path -Name $file_name -Value $file_header_comment -ItemType file -force
In your project's properties, on the Build Events tab, invoke your script on the Pre-build event command line. Something like:
Powershell.exe -File myScript.ps1