Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 10 additions & 31 deletions gradle.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,23 @@ export const getGradleDependenciesWithVersions = async (parsedGradle) => {
let allDependencies = [];
// if it's not an array, a single dependency has been declared and it doesn't apply
if (Array.isArray(parsedGradle.dependencies)) {
allDependencies = allDependencies.concat(
parsedGradle.dependencies.filter((dep) => dep.version),
);
allDependencies = allDependencies.concat(parsedGradle.dependencies.filter((dep) => dep.version));
}
if (Array.isArray(parsedGradle?.subprojects?.[0]?.dependencies)) {
allDependencies = allDependencies.concat(
parsedGradle.subprojects[0].dependencies.filter((dep) => dep.version),
);
allDependencies = allDependencies.concat(parsedGradle.subprojects[0].dependencies.filter((dep) => dep.version));
}
return allDependencies;
};

export const getGradleSpringBootVersion = async (parsedGradle) => {
const springBootPlugin = parsedGradle?.plugins?.filter(
(plugin) => plugin.id === 'org.springframework.boot',
);
if (
Array.isArray(springBootPlugin) &&
springBootPlugin.length &&
springBootPlugin[0].version
) {
const springBootPlugin = parsedGradle?.plugins?.filter((plugin) => plugin.id === 'org.springframework.boot');
if (Array.isArray(springBootPlugin) && springBootPlugin.length && springBootPlugin[0].version) {
// Handle build.gradle, which allows 3.2.+ format
return springBootPlugin[0].version.replace('+', 'x');
}
// there are likely more places we could search for this variable, but we'll start here
if (parsedGradle?.buildscript?.[0]?.ext?.springBootVersion) {
return String(
parsedGradle?.buildscript?.[0]?.ext?.springBootVersion,
).replace('+', 'x');
return String(parsedGradle?.buildscript?.[0]?.ext?.springBootVersion).replace('+', 'x');
}
if (parsedGradle.buildscript?.[0]?.["ext['springBootVersion']"]) {
return parsedGradle.buildscript[0]["ext['springBootVersion']"];
Expand All @@ -85,28 +73,19 @@ export const getGradleSpringBootVersion = async (parsedGradle) => {
return '';
};

export const retrieveSimilarGradlePackages = async (
parsedGradle,
springBootVersion,
) => {
const gradleDependenciesWithVersions =
await getGradleDependenciesWithVersions(parsedGradle);
export const retrieveSimilarGradlePackages = async (parsedGradle, springBootVersion) => {
const gradleDependenciesWithVersions = await getGradleDependenciesWithVersions(parsedGradle);
if (springBootVersion) {
const defaultVersions =
await getDefaultSpringBootVersions(springBootVersion);
const defaultVersions = await getDefaultSpringBootVersions(springBootVersion);

if (defaultVersions.length) {
const declaredPackages = [];
gradleDependenciesWithVersions.forEach((gradleDependency) =>
defaultVersions.forEach((bootPackage) => {
if (
gradleDependency.group === bootPackage.group &&
gradleDependency.name === bootPackage.name
) {
if (gradleDependency.group === bootPackage.group && gradleDependency.name === bootPackage.name) {
const existingMatches = declaredPackages.find(
(declaredPackage) =>
declaredPackage.group === gradleDependency.group &&
declaredPackage.name === gradleDependency.name,
declaredPackage.group === gradleDependency.group && declaredPackage.name === gradleDependency.name,
);
if (!existingMatches) {
declaredPackages.push(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"prettier": {
"trailingComma": "all",
"printWidth": 120,
"semi": true,
"singleQuote": true
},
Expand Down
97 changes: 24 additions & 73 deletions pom.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import { parse } from 'node-html-parser';
import {
cachePath,
ensureDirExists,
getDefaultSpringBootVersions,
getJsonFromFile,
Package,
} from './shared.js';
import { cachePath, ensureDirExists, getDefaultSpringBootVersions, getJsonFromFile, Package } from './shared.js';

export const getXMLFromFile = async (filename) => {
try {
Expand All @@ -32,9 +26,7 @@ export const getXMLFromFile = async (filename) => {
parsedPomFiles.forEach((pom) => {
properties = properties.concat(pom.project.properties ?? []);
dependencies = dependencies.concat(pom.project.dependencies ?? []);
dependencyManagement = dependencyManagement.concat(
pom.project.dependencyManagement ?? [],
);
dependencyManagement = dependencyManagement.concat(pom.project.dependencyManagement ?? []);
parent = parent.concat(pom.project.parent ?? []);
});

Expand Down Expand Up @@ -83,45 +75,32 @@ export const getPomDependenciesWithVersions = async (parsedPom) => {

export const getPomSpringBootVersion = async (parsedPom) => {
let bootVersion = parsedPom.project.parent.filter(
(pom) =>
pom.groupId === 'org.springframework.boot' &&
pom.artifactId === 'spring-boot-starter-parent',
(pom) => pom.groupId === 'org.springframework.boot' && pom.artifactId === 'spring-boot-starter-parent',
);
if (bootVersion.length) {
return bootVersion[0].version;
}

bootVersion =
parsedPom.project.dependencyManagement?.dependencies?.dependency.filter(
(pom) =>
pom.groupId === 'org.springframework.boot' &&
pom.artifactId === 'spring-boot-starter-parent',
);
bootVersion = parsedPom.project.dependencyManagement?.dependencies?.dependency.filter(
(pom) => pom.groupId === 'org.springframework.boot' && pom.artifactId === 'spring-boot-starter-parent',
);
if (bootVersion) {
const tempBootVersion = bootVersion.version;
return replaceVariable(parsedPom.project.properties, tempBootVersion);
}

if (
Array.isArray(
parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency,
)
) {
const bootVersion =
parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.find(
(dependency) =>
dependency.groupId === 'org.springframework.boot' &&
dependency.artifactId === 'spring-boot-dependencies',
)?.version;
if (Array.isArray(parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency)) {
const bootVersion = parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.find(
(dependency) =>
dependency.groupId === 'org.springframework.boot' && dependency.artifactId === 'spring-boot-dependencies',
)?.version;
if (bootVersion) {
return replaceVariable(parsedPom.project.properties, bootVersion);
}
}
if (
parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency
.groupId === 'org.springframework.boot' &&
parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency
.artifactId === 'spring-boot-dependencies'
parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.groupId === 'org.springframework.boot' &&
parsedPom.project?.dependencyManagement?.[0]?.dependencies.dependency.artifactId === 'spring-boot-dependencies'
) {
return replaceVariable(
parsedPom.project.properties,
Expand All @@ -138,41 +117,24 @@ export const getPomSpringBootVersion = async (parsedPom) => {
return '';
};

export const retrieveSimilarPomPackages = async (
parsedPom,
springBootVersion,
) => {
const pomDependenciesWithVersions =
await getPomDependenciesWithVersions(parsedPom);
export const retrieveSimilarPomPackages = async (parsedPom, springBootVersion) => {
const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom);
if (springBootVersion) {
const defaultVersions =
await getDefaultSpringBootVersions(springBootVersion);
const defaultVersions = await getDefaultSpringBootVersions(springBootVersion);

if (defaultVersions.length) {
const declaredPackages = [];
pomDependenciesWithVersions.forEach((pomDependency) =>
defaultVersions.forEach((bootPackage) => {
if (
pomDependency.groupId === bootPackage.group &&
pomDependency.artifactId === bootPackage.name
) {
const pomVersion = replaceVariable(
parsedPom.project.properties,
pomDependency.version,
);
if (pomDependency.groupId === bootPackage.group && pomDependency.artifactId === bootPackage.name) {
const pomVersion = replaceVariable(parsedPom.project.properties, pomDependency.version);
const existingMatches = declaredPackages.find(
(declaredPackage) =>
declaredPackage.group === pomDependency.groupId &&
declaredPackage.name === pomDependency.artifactId,
declaredPackage.group === pomDependency.groupId && declaredPackage.name === pomDependency.artifactId,
);
if (!existingMatches) {
declaredPackages.push(
new Package(
pomDependency.groupId,
pomDependency.artifactId,
pomVersion,
bootPackage.version,
),
new Package(pomDependency.groupId, pomDependency.artifactId, pomVersion, bootPackage.version),
);
}
}
Expand All @@ -192,10 +154,7 @@ export const retrieveSimilarPomPackages = async (
return [];
};

export const retrieveSimilarPomProperties = async (
parsedPom,
springBootVersion,
) => {
export const retrieveSimilarPomProperties = async (parsedPom, springBootVersion) => {
const pomProperties = await getPomProperties(parsedPom);
if (springBootVersion) {
const defaultProperties = await getSpringBootProperties(springBootVersion);
Expand Down Expand Up @@ -274,17 +233,9 @@ const downloadSpringVersionProperties = async (springBootVersion) => {
}),
);
}
await writeFileSync(
`${cachePath}/properties_${springBootVersion}.json`,
JSON.stringify(versions, null, 2),
);
await writeFileSync(`${cachePath}/properties_${springBootVersion}.json`, JSON.stringify(versions, null, 2));
} else {
await writeFileSync(
`${cachePath}/properties_${springBootVersion}.json`,
JSON.stringify(versions, null, 2),
);
console.log(
'URL not found - Spring Boot default versions URL no longer exists.',
);
await writeFileSync(`${cachePath}/properties_${springBootVersion}.json`, JSON.stringify(versions, null, 2));
console.log('URL not found - Spring Boot default versions URL no longer exists.');
}
};
14 changes: 3 additions & 11 deletions shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,10 @@ const downloadSpringDefaultVersions = async (springBootVersion) => {
version: child.childNodes[5].rawText,
}),
);
await writeFileSync(
`${cachePath}/dependencies_${springBootVersion}.json`,
JSON.stringify(versions, null, 2),
);
await writeFileSync(`${cachePath}/dependencies_${springBootVersion}.json`, JSON.stringify(versions, null, 2));
} else {
await writeFileSync(
`${cachePath}/dependencies_${springBootVersion}.json`,
JSON.stringify(versions, null, 2),
);
console.log(
'URL not found - Spring Boot default versions URL no longer exists.',
);
await writeFileSync(`${cachePath}/dependencies_${springBootVersion}.json`, JSON.stringify(versions, null, 2));
console.log('URL not found - Spring Boot default versions URL no longer exists.');
}
};

Expand Down
31 changes: 7 additions & 24 deletions test/test-pom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,18 @@ describe('test pom parsing', () => {

const xmlData = await getXMLFromFile(filename);

strictEqual(
xmlData.project.parent[0].artifactId,
'spring-boot-starter-parent',
);
strictEqual(xmlData.project.parent[0].artifactId, 'spring-boot-starter-parent');
strictEqual(xmlData.project.parent[0].groupId, 'org.springframework.boot');
strictEqual(xmlData.project.parent[0].version, '3.1.0');

strictEqual(xmlData.project.properties[0]['java.version'], 1.8);
strictEqual(xmlData.project.properties[0]['jackson.version'], '2.10.2');

strictEqual(xmlData.project.dependencies[0].dependency.length, 2);
strictEqual(
xmlData.project.dependencies[0].dependency[0].groupId,
'org.apache.httpcomponents',
);
strictEqual(
xmlData.project.dependencies[0].dependency[0].artifactId,
'httpclient',
);
strictEqual(
xmlData.project.dependencies[0].dependency[1].groupId,
'org.java-websocket',
);
strictEqual(
xmlData.project.dependencies[0].dependency[1].artifactId,
'Java-WebSocket',
);
strictEqual(xmlData.project.dependencies[0].dependency[0].groupId, 'org.apache.httpcomponents');
strictEqual(xmlData.project.dependencies[0].dependency[0].artifactId, 'httpclient');
strictEqual(xmlData.project.dependencies[0].dependency[1].groupId, 'org.java-websocket');
strictEqual(xmlData.project.dependencies[0].dependency[1].artifactId, 'Java-WebSocket');
strictEqual(xmlData.project.dependencies[0].dependency[1].version, '2.3.1');
});

Expand Down Expand Up @@ -119,8 +104,7 @@ describe('test pom parsing', () => {
},
};

const pomDependenciesWithVersions =
await getPomDependenciesWithVersions(parsedPom);
const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom);

strictEqual(pomDependenciesWithVersions.length, 1);
strictEqual(pomDependenciesWithVersions[0].artifactId, 'Java-WebSocket');
Expand All @@ -133,8 +117,7 @@ describe('test pom parsing', () => {
project: [],
};

const pomDependenciesWithVersions =
await getPomDependenciesWithVersions(parsedPom);
const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom);

strictEqual(pomDependenciesWithVersions.length, 0);
});
Expand Down