Cake Build - Update NuGet package to the newest version - c#

I have a solution with 5 projects in it. Every project is deployed via a nuget-push. Some projects reference other projects via nuget. In order to work properly these nuget-packages have to be updated before they are pushed.
For this we use Cake-Build but the nuget update is not working on core/standard projects. Instead it is necessary to use remove --> add, which is not working for me?
How can I handle this?
Example:
Project A v1.0.0
Project B v1.0.0
Project C v1.0.0
Reference to A v1.0.0
Reference to B v1.0.0
Now the Build-Script would compile A and B, increment the version to v1.0.1, and push the nuget-package. Before C is build the nuget packages to A & B needs to be updated.
Example:
Project A v1.0.1
Project B v1.0.1
Project C v1.0.1
Reference to A v1.0.1
Reference to B v1.0.1
How I'm able to update the packages via Cake-Build?!?

If you use project references and build as part of same solution, you should be able to get everything referenced correctly. That's how Cake itself is built.
Cake.exe / dll depends on
Cake.Core
Cake.Common which depends on
Cake.Core
When we i.e. build 0.30.0 we pass that version as common MSBuildSettings to Restore. Build and Pack. Rough example
string configuration = "Release",
version = "0.30.0",
semVersion = "0.30.0"; // for pre-release this is suffixed i.e. -alpha-001
DotNetCoreMSBuildSettings msBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Version", semVersion)
.WithProperty("AssemblyVersion", version)
.WithProperty("FileVersion", version);
DotNetCoreRestore("./src/Cake.sln", new DotNetCoreRestoreSettings
{
Verbosity = DotNetCoreVerbosity.Minimal,
Sources = new [] { "https://api.nuget.org/v3/index.json" },
MSBuildSettings = msBuildSettings
});
DotNetCoreBuild("./src/Cake.sln", new DotNetCoreBuildSettings()
{
Configuration = configuration,
NoRestore = true,
MSBuildSettings = msBuildSettings
});
var projects = GetFiles("./src/**/*.csproj");
foreach(var project in projects)
{
DotNetCorePack(project.FullPath, new DotNetCorePackSettings {
Configuration = configuration,
OutputDirectory = "./nuget,
NoBuild = true,
NoRestore = true,
IncludeSymbols = true,
MSBuildSettings = msBuildSettings
});
}
A project reference in .NET Core csproj looks like
<ProjectReference Include="..\Cake.Core\Cake.Core.csproj" />

Related

How to convert between TFM (Target framework moniker) and FrameworkName?

E.g.
netstandard2.0 (from Supported target frameworks) which is used as <TargetFramework> in *.csproj files or as folder name in NuGet packages internal structure,
and
.NETStandard,Version=v2.0 which is accepted by System.Runtime.Versioning.FrameworkName class's constructor or can be a value of TargetFrameworkAttribute.FrameworkName.
How to convert those strings from one form to another? At least one (any) direction.
You can use the source code of NuGet.Frameworks:
Here is the method that converts TFM to FrameworkName:
https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Frameworks/NuGetFrameworkFactory.cs#L575
(e.g. netstandard2.0 to .NETStandard,Version=v2.0)
UPDATE #1
The good news is that it is available as a NuGet package:
https://www.nuget.org/packages/NuGet.Frameworks/
Here is a .NET 6 Console Application:
using NuGet.Frameworks;
using System.Runtime.Versioning;
var tfmNetstandard20 = NuGetFramework.ParseFolder("netstandard20");
var fwNetstandard20 = new FrameworkName(tfmNetstandard20.DotNetFrameworkName);
Console.WriteLine(tfmNetstandard20);
Console.WriteLine(fwNetstandard20);
The output will be:
.NETStandard,Version=v2.0
.NETStandard,Version=v2.0

Using ValueTuple with PackageReference on Azure Cloud Services

I'm trying to convert my c# projects from old to new csproj style, but this breaks my Azure cloud service at runtime because the ReferenceAssembly of System.ValueTuple.dll is copied instead of the implementing assembly.
This is the same problem as described in this closed/abandoned issue.
As my projects are currently targeting .NET 4.6.2, the problem is "solvable" by targeting .NET 4.7+, as that comes with System.ValueTuple and hence does not need to reference it as a NuGet package.
I would like to avoid this situation, if possible, as:
This requires an additional deploy step to install .net 4.7+ runtimes on the worker roles, as they come with .net 4.6.2 installed. https://learn.microsoft.com/en-us/azure/cloud-services/cloud-services-guestos-update-matrix#family-5-releases
This seems as "the easy way out", and I would like to know if the problem can be solved otherwise.
Additional description of the issue:
I'll use:
refDLL for: packages\system.valuetuple\4.5.0\ref\net461\System.ValueTuple.dll, and
libDLL for packages\system.valuetuple\4.5.0\lib\net461\System.ValueTuple.dll.
They are easily distinguishable, as refDLL is 40 kb and libDLL is 78 kb.
The actual code and complete build log file is found here: https://www.dropbox.com/s/kquv5voa19jfonz/AzureCloudService1.zip?dl=0
I have a solution struture as follows:
AzureCloudService1
WorkerRole1 (old csproj)
WorkerRole2 (new csproj)
After building the cloud service the
WorkerRole1\bin\Debug has libDLL.
WorkerRole2\bin\Debug\net461 has libDLL
AzureCloudService1\obj\Debug\WorkerRole1 has libDLL
AzureCloudService1\obj\Debug\WorkerRole2 has refDLL
From the logs, I noticed the following difference between WorkerRole1 and WorkerRole2.
WorkerRole1:
C:\Users\jonas\source\repos\AzureCloudService1\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll
CopyLocal = true
FusionName = System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
HintPath = ..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll
ImageRuntime = v4.0.30319
MSBuildSourceProjectFile = C:\Users\jonas\source\repos\AzureCloudService1\WorkerRole1\WorkerRole1.csproj
MSBuildSourceTargetName = BuiltProjectOutputGroupDependencies
OriginalItemSpec = System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL
ReferenceAssembly = C:\Users\jonas\source\repos\AzureCloudService1\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll
ReferenceSourceTarget = ResolveAssemblyReference
ResolvedFrom = {HintPathFromItem}
Version = 4.0.3.0
WorkerRole2:
C:\Users\jonas\.nuget\packages\system.valuetuple\4.5.0\ref\net461\System.ValueTuple.dll
CopyLocal = false
ExternallyResolved = true
FusionName = System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
HintPath = C:\Users\jonas\.nuget\packages\system.valuetuple\4.5.0\ref\net461\System.ValueTuple.dll
ImageRuntime = v4.0.30319
MSBuildSourceProjectFile = C:\Users\jonas\source\repos\AzureCloudService1\WorkerRole2\WorkerRole2.csproj
MSBuildSourceTargetName = BuiltProjectOutputGroupDependencies
NuGetPackageId = System.ValueTuple
NuGetPackageVersion = 4.5.0
NuGetSourceType = Package
OriginalItemSpec = C:\Users\jonas\.nuget\packages\system.valuetuple\4.5.0\ref\net461\System.ValueTuple.dll
Private = false
ReferenceAssembly = C:\Users\jonas\.nuget\packages\system.valuetuple\4.5.0\ref\net461\System.ValueTuple.dll
ReferenceSourceTarget = ResolveAssemblyReference
ResolvedFrom = {HintPathFromItem}
Version = 4.0.3.0
After searching for other related issues on various Microsoft issue trackers, I found this one, which seems related: https://github.com/dotnet/sdk/issues/1738.

Cake Build set full version with DotNetCoreBuild alias

I'm using the ParseReleaseNotes alias in Cake to manage my versioning, worked fine with a project where I patch the assembly info with CreateAssemblyInfo alias.
Now with project not using csproj but project.json I want to achieve the same and assembly info isn't an real option with project.json.
Checking out the DotNetCoreBuild(string, ​DotNetCoreBuildSettings)​ and it's DotNetCoreBuildSettings there only seems to be a way to set parts of the version via it's VersionSuffix property.
Is there an Cake alias / setting to achieve this or is it possible to patch the project.json from Cake?
There is no built in Cake Alias to provide this functionality, but you can make use of a 3rd Party Addin for the MagicChunks project. You can add this into your Cake script by simply doing:
#addin "MagicChunks"
And from there, you can do something like:
var projectToPackagePackageJson = $"{projectToPackage}/project.json";
Information("Updating {0} version -> {1}", projectToPackagePackageJson, nugetVersion);
TransformConfig(projectToPackagePackageJson, projectToPackagePackageJson, new TransformationCollection {
{ "version", nugetVersion }
});
Where TransformConfig is the Method Alias that is added by the MagicChunks addin.
NOTE: This sample was taken from the following project.
There's no built in alias to patch project.json version or parameter for dotnet build to set full version that I know of.
That said as project.json is just "JSON" it's fully possible to patch project.json using a JSON serializer i.e. JSON.Net.
Below I've created an example that references JSON.Net as an addin and then created an UpdateProjectJsonVersion utility function that I can use to patch my project.json using parsed the ReleaseNotes (in this case I've hard coded it for simplicity).
#addin "Newtonsoft.Json"
// fake a release note
ReleaseNotes releaseNotes = new ReleaseNotes(
new Version("3.0.0"),
new [] {"3rd release"},
"3.0.-beta"
);
// project.json to patch
FilePath filePaths = File("./project.json");
// patch project.json
UpdateProjectJsonVersion(releaseNotes.RawVersionLine, filePaths);
// utility function that patches project.json using json.net
public static void UpdateProjectJsonVersion(string version, FilePath projectPath)
{
var project = Newtonsoft.Json.Linq.JObject.Parse(
System.IO.File.ReadAllText(projectPath.FullPath, Encoding.UTF8));
project["version"].Replace(version);
System.IO.File.WriteAllText(projectPath.FullPath, project.ToString(), Encoding.UTF8);
}
So basically just call UpdateProjectJsonVersion before you call the DotNetCoreBuild alias and it'll result in same version as your release notes.

How to use Roslyn Code Analysis API with MVC 6 projects

I intend to use the new Roslyn Code Analysis API with an MVC 6 project.
However, when I run this code:
string pathToSolution = #"..\..\..\WebApplicationComplex.sln";
const string projectName = "RoslynWebAPIProject";
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
Solution solutionToAnalyze = workspace.OpenSolutionAsync(pathToSolution).Result;
Project sampleProjectToAnalyze = solutionToAnalyze.Projects.Where((proj) => proj.Name == projectName).FirstOrDefault();
Compilation sampleToAnalyzeCompilation = sampleProjectToAnalyze.GetCompilationAsync().Result;
I only get the analyzer project and nothing of the MVC 6 project, so the sampleProjectToAnalyze remains null.
How should I analyze that type of projects?
You need to use the DNX Workspace.
Add a reference to that package, then create a new ProjectJsonWorkspace(jsonPath)

ServiceStack client on a Xamarin.iOS project

I am trying to use the ServiceStack clients on a Xamarin iOS project and when debugging it I have the following exception:
“System.ArgumentException: PclExport.Instance needs to be
initialized”.
The code that produces the exception is the following:
try
{
string strReadParam = this.xmlParser.GetString("SyncUrl");
CommonStatic.SyncWSUrl = strReadParam;
var client = new JsonServiceClient(CommonStatic.SyncWSUrl);
client.Timeout = new TimeSpan(0, 0, 0, 5, 0);
var response = client.Get(new mSalesCheckConnectionRequest { DBSource = CommonStatic.DBSource, DBSourceInstance = CommonStatic.DBSourceInstance, DBName = CommonStatic.DBName, DBUsername = CommonStatic.DBUsername, DBPassword = CommonStatic.DBPassword });
return;
}
catch (System.Net.WebException wex)
{
}
I am using ServiceStack.Interfaces, ServiceStack.Client.Pcl and ServiceStack.Text.Pcl all having version 4.0.34. Additionally I referenced Newtonsoft.Json at version 6.0.7.
After some research I realized that the PCL provider for iOS is not registered automatically, so I added “IosPclExportClient.Configure();” before instantiating the new Json Service Client and a I referenced ServiceStack.Pcl.iOS.dll at version 4.0.0.0.
The result is the following error:
“Cannot include both 'monotouch.dll' and 'Xamarin.iOS.dll' in the same Xamarin.iOS project - 'Xamarin.iOS.dll' is referenced explicitly, while 'monotouch.dll' is referenced by 'ServiceStack.Pcl.iOS, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null'.”
Is there is any suggestion of resolving this problem?
Thank you in advance
You need to call IosPclExportClient.Configure(); when you application starts to initialise the PCL Export Client before use in iOS applications.
So in your Main method:
static void Main (string[] args)
{
// Configure ServiceStack Client
IosPclExportClient.Configure();
// Set AppDelegate
UIApplication.Main (args, null, "AppDelegate");
}
and a I referenced ServiceStack.Pcl.iOS.dll at version 4.0.0.0.
The PCL specific NuGet packages of ServiceStack are no longer maintained, as they have been merged into the main NuGet package using specific profile.
You should only be including the ServiceStack.Client package in your project. So remove all references to ServiceStack in your project, clean the build, and add just ServiceStack.Client.
If you reference ServiceStack.Client.Pcl was well as ServiceStack.Client you will get a conflict.

Categories

Resources