Search in sources :

Example 1 with NullSafePackageJson

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();
    }
}
Also used : YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) ExcludedIncludedWildcardFilter(com.synopsys.integration.util.ExcludedIncludedWildcardFilter) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) YarnWorkspaces(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException)

Example 2 with NullSafePackageJson

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);
}
Also used : WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace)

Example 3 with NullSafePackageJson

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);
}
Also used : NameVersion(com.synopsys.integration.util.NameVersion) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) LinkedList(java.util.LinkedList) YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) YarnLockResult(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult) YarnLockEntryId(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency) YarnWorkspaces(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with NullSafePackageJson

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;
    }
}
Also used : NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) IOException(java.io.IOException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with NullSafePackageJson

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());
}
Also used : NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) Test(org.junit.jupiter.api.Test)

Aggregations

NullSafePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson)6 PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)3 WorkspacePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson)3 YarnLock (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock)3 YarnLockDependency (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency)2 YarnLockResult (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult)2 YarnLockEntry (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry)2 YarnLockEntryId (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId)2 YarnWorkspace (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace)2 YarnWorkspaces (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces)2 IOException (java.io.IOException)2 Test (org.junit.jupiter.api.Test)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)1 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)1 UnitTest (com.synopsys.integration.detectable.annotations.UnitTest)1 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)1 Extraction (com.synopsys.integration.detectable.extraction.Extraction)1 ExcludedIncludedWildcardFilter (com.synopsys.integration.util.ExcludedIncludedWildcardFilter)1 NameVersion (com.synopsys.integration.util.NameVersion)1 File (java.io.File)1