use of com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson in project synopsys-detect by blackducksoftware.
the class YarnLockExtractor method extract.
public Extraction extract(File projectDir, File yarnLockFile, File rootPackageJsonFile) {
try {
NullSafePackageJson rootPackageJson = packageJsonFiles.read(rootPackageJsonFile);
String projectName = rootPackageJson.getName().orElse("null");
logger.debug("Extracting Yarn project {} in {}", projectName, projectDir.getAbsolutePath());
YarnLock yarnLock = readYarnLock(yarnLockFile);
YarnWorkspaces workspaceData = collectWorkspaceData(projectDir);
ExcludedIncludedWildcardFilter workspacesFilter = deriveExcludedIncludedWildcardFilter();
YarnResult yarnResult = yarnPackager.generateCodeLocation(rootPackageJson, workspaceData, yarnLock, new ArrayList<>(), workspacesFilter);
Optional<Exception> yarnException = yarnResult.getException();
if (yarnException.isPresent()) {
throw yarnException.get();
}
return new Extraction.Builder().projectName(yarnResult.getProjectName()).projectVersion(yarnResult.getProjectVersionName()).success(yarnResult.getCodeLocations()).build();
} catch (Exception e) {
return new Extraction.Builder().exception(e).build();
}
}
use of com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson in project synopsys-detect by blackducksoftware.
the class YarnTransformerTest method addWorkspacePackageJson.
private void addWorkspacePackageJson(Collection<YarnWorkspace> workspacesByName, NameVersion workspaceNameVersion, String workspaceDepName, String workspaceDevDepName) {
PackageJson rawWorkspacePackageJson = new PackageJson();
rawWorkspacePackageJson.name = workspaceNameVersion.getName();
rawWorkspacePackageJson.version = workspaceNameVersion.getVersion();
rawWorkspacePackageJson.dependencies = new HashMap<>();
rawWorkspacePackageJson.dependencies.put(workspaceDepName, workspaceNameVersion.getVersion());
rawWorkspacePackageJson.devDependencies.put(workspaceDevDepName, workspaceNameVersion.getVersion());
NullSafePackageJson workspacePackageJson = new NullSafePackageJson(rawWorkspacePackageJson);
WorkspacePackageJson locatedWorkspacePackageJson = new WorkspacePackageJson(null, workspacePackageJson, "packages/" + workspaceNameVersion.getName());
YarnWorkspace workspace = new YarnWorkspace(locatedWorkspacePackageJson);
workspacesByName.add(workspace);
}
use of com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson in project synopsys-detect by blackducksoftware.
the class YarnTransformerTest method buildTestYarnLockResult.
@NotNull
private YarnLockResult buildTestYarnLockResult(List<NameVersion> workspacesThatAreDependencies, List<NameVersion> workspacesThatAreNotDependencies, boolean yarn1project) {
PackageJson rawPackageJson = new PackageJson();
rawPackageJson.dependencies = new HashMap<>();
rawPackageJson.dependencies.put("foo", "fooFuzzyVersion-1.0");
for (NameVersion workspace : workspacesThatAreDependencies) {
rawPackageJson.dependencies.put(workspace.getName(), workspace.getVersion());
}
rawPackageJson.devDependencies.put("bar", "barFuzzyVersion-1.0");
NullSafePackageJson packageJson = new NullSafePackageJson(rawPackageJson);
// yarn.lock: foo and bar both depend on yarn
List<YarnLockEntryId> yarnLockEntryIdsFoo = Collections.singletonList(new YarnLockEntryId("foo", "fooFuzzyVersion-1.0"));
List<YarnLockEntryId> yarnLockEntryIdsBar = Collections.singletonList(new YarnLockEntryId("bar", "barFuzzyVersion-1.0"));
List<YarnLockEntryId> yarnLockEntryIdsYarn = Collections.singletonList(new YarnLockEntryId("yarn", "^1.22.4"));
List<YarnLockDependency> dependencyRefToYarn = Collections.singletonList(new YarnLockDependency("yarn", "^1.22.4", false));
List<YarnLockEntry> yarnLockEntries = new LinkedList<>();
if (!yarn1project) {
List<YarnLockEntryId> projectEntryIds = Collections.singletonList(new YarnLockEntryId("project", "1.0.0"));
List<YarnLockDependency> projectDependencies = new LinkedList<>();
projectDependencies.add(new YarnLockDependency("foo", "fooFuzzyVersion-1.0", false));
projectDependencies.add(new YarnLockDependency("bar", "barFuzzyVersion-1.0", false));
for (NameVersion workspaceThatIsDependency : workspacesThatAreDependencies) {
projectDependencies.add(new YarnLockDependency(workspaceThatIsDependency.getName(), workspaceThatIsDependency.getVersion(), false));
}
yarnLockEntries.add(new YarnLockEntry(false, projectEntryIds, "1.0.0", projectDependencies));
}
Collection<YarnWorkspace> workspacesByName = new LinkedList<>();
List<NameVersion> allWorkspaces = new LinkedList<>(workspacesThatAreDependencies);
allWorkspaces.addAll(workspacesThatAreNotDependencies);
for (NameVersion workspace : allWorkspaces) {
String workspaceDepName = workspace.getName() + WORKSPACE_DEP_SUFFIX;
String workspaceDevDepName = workspace.getName() + "-dev" + WORKSPACE_DEP_SUFFIX;
addWorkspacePackageJson(workspacesByName, workspace, workspaceDepName, workspaceDevDepName);
if (!yarn1project) {
addWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDepName);
}
addDependencyOfWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDepName);
addDependencyOfWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDevDepName);
}
yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsFoo, "1.0", dependencyRefToYarn));
yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsBar, "1.0", dependencyRefToYarn));
yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsYarn, "1.22.5", new LinkedList<>()));
String yarnLockVersion = null;
if (!yarn1project) {
yarnLockVersion = "4";
}
YarnLock yarnLock = new YarnLock(yarnLockVersion, yarn1project, yarnLockEntries);
YarnWorkspaces workspaceData = new YarnWorkspaces(workspacesByName);
return new YarnLockResult(packageJson, workspaceData, yarnLock);
}
use of com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson in project synopsys-detect by blackducksoftware.
the class PnpmLinkedPackageResolver method resolveVersionOfLinkedPackage.
@Nullable
public String resolveVersionOfLinkedPackage(@Nullable String reportingProjectPackagePath, String relativePathToLinkedPackage) {
File reportingPackage;
if (reportingProjectPackagePath != null) {
reportingPackage = new File(projectRoot, reportingProjectPackagePath);
} else {
reportingPackage = projectRoot;
}
File linkedPackage = new File(reportingPackage, relativePathToLinkedPackage);
File packageJsonFile = new File(linkedPackage, "package.json");
if (!packageJsonFile.exists()) {
logger.debug(String.format("Unable to resolve version for linked package: %s", linkedPackage));
return null;
}
try {
NullSafePackageJson packageJson = packageJsonFiles.read(packageJsonFile);
return packageJson.getVersionString();
} catch (IOException e) {
logger.debug(String.format("Unable to parse package.json: %s", packageJsonFile.getAbsolutePath()));
return null;
}
}
use of com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson in project synopsys-detect by blackducksoftware.
the class PackageJsonReaderTest method testSimpleRead.
@Test
void testSimpleRead() {
NullSafePackageJson packageJson = packageJsonReader.read(getPackageJsonTextNewFormat());
assertEquals("simple", packageJson.getName().orElse(null));
assertEquals("1.0.0", packageJson.getVersion().orElse(null));
assertEquals(2, packageJson.getDependencies().size());
}
Aggregations