feat(manager/sbt): support ThisBuild scoped scalaVersion (#13544)

* fix(manager/sbt): support `ThisBuild` scoped `scalaVersion`

Using `ThisBuild / scalaVersion` to specify a common `scalaVersion`
across subprojects is a well-known technique such as described in
the official documentation of SBT.

<https://www.scala-sbt.org/1.x/docs/Scopes.html#Build-level+settings>

So, we should support to recognize this syntax also.

* fix(manager/sbt): support `ThisBuild / scalaVersion := variable`

Co-authored-by: Rhys Arkins <rhys@arkins.net>
This commit is contained in:
TSUYUSATO Kitsune 2022-01-15 23:47:37 +09:00 committed by GitHub
parent b60996672b
commit e4282ce03a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 4 deletions

View file

@ -428,6 +428,23 @@ Object {
} }
`; `;
exports[`manager/sbt/extract extractPackageFile() extracts deps when scala version is defined in a variable with ThisBuild scope 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "0.0.2",
"datasource": "sbt-package",
"depName": "org.example:bar",
"lookupName": "org.example:bar_2.12",
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
],
"packageFileVersion": undefined,
}
`;
exports[`manager/sbt/extract extractPackageFile() extracts deps when scala version is defined in a variable with a trailing comma 1`] = ` exports[`manager/sbt/extract extractPackageFile() extracts deps when scala version is defined in a variable with a trailing comma 1`] = `
Object { Object {
"deps": Array [ "deps": Array [
@ -445,6 +462,33 @@ Object {
} }
`; `;
exports[`manager/sbt/extract extractPackageFile() extracts deps when scala version is defined with ThisBuild scope 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "2.12.10",
"datasource": "maven",
"depName": "scala",
"lookupName": "org.scala-lang:scala-library",
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
"separateMinorPatch": true,
},
Object {
"currentValue": "0.0.2",
"datasource": "sbt-package",
"depName": "org.example:bar",
"lookupName": "org.example:bar_2.12",
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
],
"packageFileVersion": undefined,
}
`;
exports[`manager/sbt/extract extractPackageFile() extracts deps when scala version is defined with a trailing comma 1`] = ` exports[`manager/sbt/extract extractPackageFile() extracts deps when scala version is defined with a trailing comma 1`] = `
Object { Object {
"deps": Array [ "deps": Array [

View file

@ -154,6 +154,39 @@ describe('manager/sbt/extract', () => {
deps: [{ lookupName: 'org.example:bar_2.12', currentValue: '0.0.2' }], deps: [{ lookupName: 'org.example:bar_2.12', currentValue: '0.0.2' }],
}); });
}); });
it('extracts deps when scala version is defined with ThisBuild scope', () => {
const content = `
ThisBuild / scalaVersion := "2.12.10"
libraryDependencies += "org.example" %% "bar" % "0.0.2"
`;
expect(extractPackageFile(content)).toMatchSnapshot({
deps: [
{
lookupName: 'org.scala-lang:scala-library',
currentValue: '2.12.10',
},
{
lookupName: 'org.example:bar_2.12',
currentValue: '0.0.2',
},
],
});
});
it('extracts deps when scala version is defined in a variable with ThisBuild scope', () => {
const content = `
val ScalaVersion = "2.12.10"
ThisBuild / scalaVersion := ScalaVersion
libraryDependencies += "org.example" %% "bar" % "0.0.2"
`;
expect(extractPackageFile(content)).toMatchSnapshot({
deps: [
{
lookupName: 'org.example:bar_2.12',
currentValue: '0.0.2',
},
],
});
});
it('extract deps from native scala file with private variables', () => { it('extract deps from native scala file with private variables', () => {
expect( expect(
extractPackageFile(sbtPrivateVariableDependencyFile) extractPackageFile(sbtPrivateVariableDependencyFile)

View file

@ -23,11 +23,13 @@ const isPluginDep = (str: string): boolean =>
const isStringLiteral = (str: string): boolean => regEx(/^"[^"]*"$/).test(str); const isStringLiteral = (str: string): boolean => regEx(/^"[^"]*"$/).test(str);
const isScalaVersion = (str: string): boolean => const isScalaVersion = (str: string): boolean =>
regEx(/^\s*scalaVersion\s*:=\s*"[^"]*"[\s,]*$/).test(str); regEx(/^\s*(?:ThisBuild\s*\/\s*)?scalaVersion\s*:=\s*"[^"]*"[\s,]*$/).test(
str
);
const getScalaVersion = (str: string): string => const getScalaVersion = (str: string): string =>
str str
.replace(regEx(/^\s*scalaVersion\s*:=\s*"/), '') .replace(regEx(/^\s*(?:ThisBuild\s*\/\s*)?scalaVersion\s*:=\s*"/), '')
.replace(regEx(/"[\s,]*$/), ''); .replace(regEx(/"[\s,]*$/), '');
const isPackageFileVersion = (str: string): boolean => const isPackageFileVersion = (str: string): boolean =>
@ -66,11 +68,13 @@ const normalizeScalaVersion = (str: string): string => {
}; };
const isScalaVersionVariable = (str: string): boolean => const isScalaVersionVariable = (str: string): boolean =>
regEx(/^\s*scalaVersion\s*:=\s*[_a-zA-Z][_a-zA-Z0-9]*[\s,]*$/).test(str); regEx(
/^\s*(?:ThisBuild\s*\/\s*)?scalaVersion\s*:=\s*[_a-zA-Z][_a-zA-Z0-9]*[\s,]*$/
).test(str);
const getScalaVersionVariable = (str: string): string => const getScalaVersionVariable = (str: string): string =>
str str
.replace(regEx(/^\s*scalaVersion\s*:=\s*/), '') .replace(regEx(/^\s*(?:ThisBuild\s*\/\s*)?scalaVersion\s*:=\s*/), '')
.replace(regEx(/[\s,]*$/), ''); .replace(regEx(/[\s,]*$/), '');
const isResolver = (str: string): boolean => const isResolver = (str: string): boolean =>