Skip to content

Commit ccd99d9

Browse files
papauorgviceice
andauthored
feat(manager/cake): Support extracting nuget packages from InstallTools helper methods (#40070)
* Support InstallTools methods for cake build files * Allow dotnet tool extraction in single file cake sdk build scripts * Fix dependency parsing for InstallTools methods * Remove unused import * Fix readme for cake manager * Add test case to improve coverage * Update lib/modules/manager/cake/index.ts Co-authored-by: Michael Kriese <michael.kriese@gmx.de> --------- Co-authored-by: Michael Kriese <michael.kriese@visualon.de> Co-authored-by: Michael Kriese <michael.kriese@gmx.de>
1 parent ddc4622 commit ccd99d9

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

lib/modules/manager/cake/index.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { codeBlock } from 'common-tags';
12
import { Fixtures } from '~test/fixtures.ts';
23
import { extractPackageFile } from './index.ts';
34

@@ -19,4 +20,79 @@ describe('modules/manager/cake/index', () => {
1920
],
2021
});
2122
});
23+
24+
it('extracts dotnet tools from single sdk style build file', () => {
25+
const content = codeBlock`
26+
#:sdk Cake.Sdk
27+
28+
// Install single tool
29+
InstallTool("dotnet:https://api.nuget.org/v3/index.json?package=SingleTool.Install.First&version=1.0.0");
30+
InstallTool("dotnet:?package=SingleTool.Install.Second&version=1.2.0");
31+
32+
// Install multiple tools at once
33+
InstallTools(
34+
"dotnet:https://api.nuget.org/v3/index.json?package=MultipleTools.Install.First&version=2.0.0",
35+
"dotnet:?package=MultipleTools.Install.Second&version=2.1.1"
36+
);
37+
38+
var target = Argument("target", "Default");
39+
40+
Task("Default")
41+
.Does(() =>
42+
{
43+
Information("Hello from Cake.Sdk!");
44+
});
45+
46+
var installTools = "dotnet:?Should.Not.Match&version=1.0.0";
47+
48+
RunTarget(target);
49+
`;
50+
expect(extractPackageFile(content)).toMatchObject({
51+
deps: [
52+
{
53+
depName: 'SingleTool.Install.First',
54+
currentValue: '1.0.0',
55+
datasource: 'nuget',
56+
registryUrls: ['https://api.nuget.org/v3/index.json'],
57+
},
58+
{
59+
depName: 'SingleTool.Install.Second',
60+
currentValue: '1.2.0',
61+
datasource: 'nuget',
62+
},
63+
{
64+
depName: 'MultipleTools.Install.First',
65+
currentValue: '2.0.0',
66+
datasource: 'nuget',
67+
registryUrls: ['https://api.nuget.org/v3/index.json'],
68+
},
69+
{
70+
depName: 'MultipleTools.Install.Second',
71+
currentValue: '2.1.1',
72+
datasource: 'nuget',
73+
},
74+
],
75+
});
76+
});
77+
78+
it('skips invalid entries in InstallTools', () => {
79+
const content = codeBlock`
80+
#:sdk Cake.Sdk
81+
82+
// One invalid and one valid tool entry
83+
InstallTools(
84+
"dotnet:bad uri",
85+
"dotnet:?package=Good.Tool&version=1.2.3"
86+
);
87+
`;
88+
expect(extractPackageFile(content)).toMatchObject({
89+
deps: [
90+
{
91+
depName: 'Good.Tool',
92+
currentValue: '1.2.3',
93+
datasource: 'nuget',
94+
},
95+
],
96+
});
97+
});
2298
});

lib/modules/manager/cake/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const lexer = moo.states({
2525
match: /^#(?:addin|tool|module|load|l)\s+"(?:nuget|dotnet):[^"]+"\s*$/, // TODO #12870
2626
value: (s: string) => s.trim().slice(1, -1),
2727
},
28+
dependencyFromInstallTools: {
29+
match: /(?:InstallTools?\s*\()[^)]+(?:\s*\)\s*;)/,
30+
lineBreaks: true,
31+
},
2832
unknown: moo.fallback,
2933
},
3034
});
@@ -74,6 +78,15 @@ export function extractPackageFile(content: string): PackageFileContent {
7478
if (dep) {
7579
deps.push(dep);
7680
}
81+
} else if (type === 'dependencyFromInstallTools') {
82+
const matches = value.matchAll(regEx(/"dotnet:[^"]+"/g));
83+
for (const match of matches) {
84+
const withoutQuote = match.toString().slice(1, -1);
85+
const dep = parseDependencyLine(withoutQuote);
86+
if (dep) {
87+
deps.push(dep);
88+
}
89+
}
7790
}
7891
token = lexer.next();
7992
}

lib/modules/manager/cake/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
Extracts dependencies from `*.cake` files.
2+
3+
It can also extract `dotnet:` tool packages when used in .NET single file builds, when they are used with the `InstallTool` or `InstallTools` method. Keep in mind that those files are usually not `*.cake` files but C# code files. Those are not included in the `managerFilePatterns` per default - make sure to include them manually in your configuration if you want to use this feature.`#:package`or`#:sdk` directives are not handled by the cake manager as this is a dotnet feature. Those are separately handled by the nuget manager.

0 commit comments

Comments
 (0)