Skip to content

Commit

Permalink
feat(manager/sbt): Improve scala 3 dependencies handling and meta-bui…
Browse files Browse the repository at this point in the history
…ld classes (#29155)
  • Loading branch information
ArtyomGabeev committed Jun 1, 2024
1 parent 6797e01 commit 5c472e4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
24 changes: 24 additions & 0 deletions lib/modules/manager/sbt/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extract deps from nati
"variableName": "abcVersion",
},
],
"managerData": {
"scalaVersion": "2.13.0-RC5",
},
"packageFileVersion": undefined,
}
`;
Expand Down Expand Up @@ -87,6 +90,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extract deps from nati
"variableName": "abcVersion",
},
],
"managerData": {
"scalaVersion": "2.13.0-RC5",
},
"packageFileVersion": undefined,
}
`;
Expand Down Expand Up @@ -273,6 +279,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extracts deps for gene
],
},
],
"managerData": {
"scalaVersion": "2.9.10",
},
"packageFileVersion": "1.0",
}
`;
Expand Down Expand Up @@ -418,6 +427,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extracts deps when sca
],
},
],
"managerData": {
"scalaVersion": "2.12",
},
"packageFileVersion": "3.2.1",
}
`;
Expand All @@ -441,6 +453,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extracts deps when sca
"registryUrls": [],
},
],
"managerData": {
"scalaVersion": "2.12",
},
"packageFileVersion": undefined,
}
`;
Expand All @@ -464,6 +479,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extracts deps when sca
"registryUrls": [],
},
],
"managerData": {
"scalaVersion": "2.12",
},
"packageFileVersion": undefined,
}
`;
Expand All @@ -487,6 +505,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extracts deps when sca
"registryUrls": [],
},
],
"managerData": {
"scalaVersion": "2.12",
},
"packageFileVersion": undefined,
}
`;
Expand All @@ -510,6 +531,9 @@ exports[`modules/manager/sbt/extract extractPackageFile() extracts deps when sca
"registryUrls": [],
},
],
"managerData": {
"scalaVersion": "2.12",
},
"packageFileVersion": undefined,
}
`;
7 changes: 5 additions & 2 deletions lib/modules/manager/sbt/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ describe('modules/manager/sbt/extract', () => {
variableName: 'sbtReleaseVersion',
},
],
managerData: {
scalaVersion: undefined,
},
packageFileVersion: '1.0.1',
});
});
Expand Down Expand Up @@ -472,8 +475,8 @@ describe('modules/manager/sbt/extract', () => {
maven-central
`;
fs.readLocalFile
.mockResolvedValueOnce(repositoryContent)
.mockResolvedValueOnce(sbtDependencyFile);
.mockResolvedValueOnce(sbtDependencyFile)
.mockResolvedValueOnce(repositoryContent);
const packages = await extractAllPackageFiles({}, [
'repositories',
'build.sbt',
Expand Down
29 changes: 24 additions & 5 deletions lib/modules/manager/sbt/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
PackageFile,
PackageFileContent,
} from '../types';
import { normalizeScalaVersion } from './util';
import { normalizeScalaVersion, sortPackageFiles } from './util';

type Vars = Record<string, string>;

Expand Down Expand Up @@ -321,6 +321,14 @@ export function extractProxyUrls(
export function extractPackageFile(
content: string,
packageFile: string,
): PackageFileContent | null {
return extractPackageFileInternal(content, packageFile);
}

function extractPackageFileInternal(
content: string,
packageFile: string,
ctxScalaVersion?: string,
): PackageFileContent | null {
if (
packageFile === 'project/build.properties' ||
Expand Down Expand Up @@ -356,6 +364,7 @@ export function extractPackageFile(
vars: {},
deps: [],
registryUrls: [],
scalaVersion: ctxScalaVersion,
});
} catch (err) /* istanbul ignore next */ {
logger.debug({ err, packageFile }, 'Sbt parsing error');
Expand All @@ -365,13 +374,13 @@ export function extractPackageFile(
return null;
}

const { deps, packageFileVersion } = parsedResult;
const { deps, scalaVersion, packageFileVersion } = parsedResult;

if (!deps.length) {
return null;
}

return { deps, packageFileVersion };
return { deps, packageFileVersion, managerData: { scalaVersion } };
}

export async function extractAllPackageFiles(
Expand All @@ -380,8 +389,11 @@ export async function extractAllPackageFiles(
): Promise<PackageFile[]> {
const packages: PackageFile[] = [];
const proxyUrls: string[] = [];
let ctxScalaVersion: string | undefined;

const sortedPackageFiles = sortPackageFiles(packageFiles);

for (const packageFile of packageFiles) {
for (const packageFile of sortedPackageFiles) {
const content = await readLocalFile(packageFile, 'utf8');
if (!content) {
logger.debug({ packageFile }, 'packageFile has no content');
Expand All @@ -391,9 +403,16 @@ export async function extractAllPackageFiles(
const urls = extractProxyUrls(content, packageFile);
proxyUrls.push(...urls);
} else {
const pkg = extractPackageFile(content, packageFile);
const pkg = extractPackageFileInternal(
content,
packageFile,
ctxScalaVersion,
);
if (pkg) {
packages.push({ deps: pkg.deps, packageFile });
if (pkg.managerData?.scalaVersion) {
ctxScalaVersion = pkg.managerData.scalaVersion;
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions lib/modules/manager/sbt/util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { sortPackageFiles } from './util';

describe('modules/manager/sbt/util', () => {
describe('sortPackageFiles()', () => {
it('places build.sbt first', () => {
const packageFiles = [
'project/build.properties',
'project/Dependencies.scala',
'build.sbt',
];
expect(sortPackageFiles(packageFiles)).toEqual([
'build.sbt',
'project/build.properties',
'project/Dependencies.scala',
]);
});
});
});
13 changes: 13 additions & 0 deletions lib/modules/manager/sbt/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,16 @@ export function normalizeScalaVersion(str: string): string {
// istanbul ignore next
return str;
}

export function sortPackageFiles(packageFiles: string[]): string[] {
// process build.sbt first
const sortedPackageFiles = [...packageFiles];
const buildSbtIndex = sortedPackageFiles.findIndex((file) =>
file.endsWith('build.sbt'),
);
if (buildSbtIndex !== -1) {
const buildSbt = sortedPackageFiles.splice(buildSbtIndex, 1)[0];
sortedPackageFiles.unshift(buildSbt);
}
return sortedPackageFiles;
}

0 comments on commit 5c472e4

Please sign in to comment.