Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sbt): Support private variable version resolving #6565

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,15 @@
import sbt._

object Dependencies {
val moreSettings = Seq(
scalaVersion := "2.13.0-RC5"
)

private val abcVersion = "1.2.3"

private lazy val ujson = "com.example" %% "foo" % "0.7.1"

lazy val abc = "com.abc" % "abc" % abcVersion

lazy val dependentLibraries = Seq(ujson, abc)
}
25 changes: 25 additions & 0 deletions lib/manager/sbt/__snapshots__/extract.spec.ts.snap
@@ -1,5 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lib/manager/sbt/extract extractPackageFile() extract deps from native scala file with private variables 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "0.7.1",
"datasource": "sbt-package",
"depName": "com.example:foo",
"lookupName": "com.example:foo_2.13.0-RC5",
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
Object {
"currentValue": "1.2.3",
"datasource": "sbt-package",
"depName": "com.abc:abc",
"lookupName": "com.abc:abc",
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
],
}
`;

exports[`lib/manager/sbt/extract extractPackageFile() extract deps from native scala file with variables 1`] = `
Object {
"deps": Array [
Expand Down
10 changes: 10 additions & 0 deletions lib/manager/sbt/extract.spec.ts
Expand Up @@ -20,6 +20,11 @@ const sbtDependencyFile = readFileSync(
'utf8'
);

const sbtPrivateVariableDependencyFile = readFileSync(
resolve(__dirname, `./__fixtures__/private-variable-dependency-file.scala`),
'utf8'
);

describe('lib/manager/sbt/extract', () => {
describe('extractPackageFile()', () => {
it('returns null for empty', () => {
Expand Down Expand Up @@ -78,5 +83,10 @@ describe('lib/manager/sbt/extract', () => {
`;
expect(extractPackageFile(content)).toMatchSnapshot();
});
it('extract deps from native scala file with private variables', () => {
expect(
extractPackageFile(sbtPrivateVariableDependencyFile)
).toMatchSnapshot();
});
});
});
16 changes: 11 additions & 5 deletions lib/manager/sbt/extract.ts
Expand Up @@ -66,20 +66,26 @@ const getResolverUrl = (str: string): string =>
.replace(/"[\s,)]*$/, '');

const isVarDependency = (str: string): boolean =>
/^\s*(lazy\s*)?val\s[_a-zA-Z][_a-zA-Z0-9]*\s*=.*(%%?).*%.*/.test(str);
/^\s*(private\s*)?(lazy\s*)?val\s[_a-zA-Z][_a-zA-Z0-9]*\s*=.*(%%?).*%.*/.test(
str
);

const isVarDef = (str: string): boolean =>
/^\s*(lazy\s*)?val\s+[_a-zA-Z][_a-zA-Z0-9]*\s*=\s*"[^"]*"\s*$/.test(str);
/^\s*(private\s*)?(lazy\s*)?val\s+[_a-zA-Z][_a-zA-Z0-9]*\s*=\s*"[^"]*"\s*$/.test(
str
);

const getVarName = (str: string): string =>
str.replace(/^\s*(lazy\s*)?val\s+/, '').replace(/\s*=\s*"[^"]*"\s*$/, '');
str
.replace(/^\s*(private\s*)?(lazy\s*)?val\s+/, '')
.replace(/\s*=\s*"[^"]*"\s*$/, '');

const isVarName = (str: string): boolean =>
/^[_a-zA-Z][_a-zA-Z0-9]*$/.test(str);

const getVarInfo = (str: string, ctx: ParseContext): { val: string } => {
const rightPart = str.replace(
/^\s*(lazy\s*)?val\s+[_a-zA-Z][_a-zA-Z0-9]*\s*=\s*"/,
/^\s*(private\s*)?(lazy\s*)?val\s+[_a-zA-Z][_a-zA-Z0-9]*\s*=\s*"/,
''
);
const val = rightPart.replace(/"\s*$/, '');
Expand Down Expand Up @@ -210,7 +216,7 @@ function parseSbtLine(
} else if (isVarDependency(line)) {
isMultiDeps = false;
const depExpr = line.replace(
/^\s*(lazy\s*)?val\s[_a-zA-Z][_a-zA-Z0-9]*\s*=\s*/,
/^\s*(private\s*)?(lazy\s*)?val\s[_a-zA-Z][_a-zA-Z0-9]*\s*=\s*/,
''
);
dep = parseDepExpr(depExpr, {
Expand Down