Search in sources :

Example 1 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class NpmLockfilePackager method parseAndTransform.

public NpmPackagerResult parseAndTransform(@Nullable String packageJsonText, String lockFileText, List<NameVersion> externalDependencies) {
    PackageJson packageJson = Optional.ofNullable(packageJsonText).map(content -> gson.fromJson(content, PackageJson.class)).orElse(null);
    PackageLock packageLock = gson.fromJson(lockFileText, PackageLock.class);
    NpmDependencyConverter dependencyConverter = new NpmDependencyConverter(externalIdFactory);
    NpmProject project = dependencyConverter.convertLockFile(packageLock, packageJson);
    DependencyGraph dependencyGraph = graphTransformer.transform(packageLock, project, externalDependencies);
    ExternalId projectId = projectIdTransformer.transform(packageJson, packageLock);
    CodeLocation codeLocation = new CodeLocation(dependencyGraph, projectId);
    return new NpmPackagerResult(projectId.getName(), projectId.getVersion(), codeLocation);
}
Also used : DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) ArrayList(java.util.ArrayList) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) Nullable(org.jetbrains.annotations.Nullable) NameVersion(com.synopsys.integration.util.NameVersion) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NpmDependencyConverter(com.synopsys.integration.detectable.detectables.npm.lockfile.NpmDependencyConverter) Gson(com.google.gson.Gson) NpmProject(com.synopsys.integration.detectable.detectables.npm.lockfile.model.NpmProject) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Optional(java.util.Optional) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) PackageLock(com.synopsys.integration.detectable.detectables.npm.lockfile.model.PackageLock) PackageLock(com.synopsys.integration.detectable.detectables.npm.lockfile.model.PackageLock) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NpmProject(com.synopsys.integration.detectable.detectables.npm.lockfile.model.NpmProject) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) NpmDependencyConverter(com.synopsys.integration.detectable.detectables.npm.lockfile.NpmDependencyConverter) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)

Example 2 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson 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 PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson 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 PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class NpmOutputParserTest method npmCliDependencyFinder.

@Test
public void npmCliDependencyFinder() {
    NpmCliParser parser = new NpmCliParser(new ExternalIdFactory(), EnumListFilter.excludeNone());
    String testIn = String.join(System.lineSeparator(), Arrays.asList("{", "   \"name\": \"node-js\",", "   \"version\": \"0.2.0\",", "   \"dependencies\": {", "       \"upper-case\": {", "       \"version\": \"1.1.3\",", "       \"from\": \"upper-case@latest\",", "       \"resolved\": \"https://registry.npmjs.org/upper-case/-/upper-case-1.1.3.tgz\"", "       },", "       \"xml2js\": {", "           \"version\": \"0.4.17\",", "           \"from\": \"xml2js@latest\",", "           \"resolved\": \"https://registry.npmjs.org/xml2js/-/xml2js-0.4.17.tgz\",", "            \"dependencies\": {", "               \"sax\": {", "                   \"version\": \"1.2.2\",", "                   \"from\": \"sax@>=0.6.0\",", "                   \"resolved\": \"https://registry.npmjs.org/sax/-/sax-1.2.2.tgz\"", "               },", "               \"xmlbuilder\": {", "                   \"version\": \"4.2.1\",", "                   \"from\": \"xmlbuilder@>=4.1.0 <5.0.0\",", "                   \"resolved\": \"https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-4.2.1.tgz\",", "                   \"dependencies\": {", "                       \"lodash\": {", "                           \"version\": \"4.17.4\",", "                           \"from\": \"lodash@>=4.0.0 <5.0.0\",", "                           \"resolved\": \"https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz\"", "                       }", "                   }", "               }", "           }", "       }", "   }", "}"));
    NpmDependencyTypeFilter npmDependencyTypeFilter = new NpmDependencyTypeFilter(Collections.emptySet(), Collections.emptySet(), true, true);
    PackageJson packageJson = new PackageJson();
    NpmPackagerResult result = parser.convertNpmJsonFileToCodeLocation(testIn, packageJson);
    Assertions.assertEquals("node-js", result.getProjectName());
    Assertions.assertEquals("0.2.0", result.getProjectVersion());
    NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.NPMJS, result.getCodeLocation().getDependencyGraph());
    graphAssert.hasRootSize(2);
    graphAssert.hasRootDependency("xml2js", "0.4.17");
    graphAssert.hasRootDependency("upper-case", "1.1.3");
    graphAssert.hasParentChildRelationship("xml2js", "0.4.17", "xmlbuilder", "4.2.1");
    graphAssert.hasParentChildRelationship("xml2js", "0.4.17", "sax", "1.2.2");
    graphAssert.hasParentChildRelationship("xmlbuilder", "4.2.1", "lodash", "4.17.4");
}
Also used : ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) NpmDependencyTypeFilter(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmDependencyTypeFilter) NameVersionGraphAssert(com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) NpmCliParser(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser) Test(org.junit.jupiter.api.Test)

Example 5 with PackageJson

use of com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson in project synopsys-detect by blackducksoftware.

the class PackageJsonExtractorTest method extractWithNoDevOrPeerDependencies.

@Test
void extractWithNoDevOrPeerDependencies() {
    PackageJson packageJson = createPackageJson();
    Extraction extraction = createExtractor(NpmDependencyType.DEV, NpmDependencyType.PEER).extract(packageJson);
    assertEquals(1, extraction.getCodeLocations().size());
    CodeLocation codeLocation = extraction.getCodeLocations().get(0);
    DependencyGraph dependencyGraph = codeLocation.getDependencyGraph();
    GraphAssert graphAssert = new GraphAssert(Forge.RUBYGEMS, dependencyGraph);
    graphAssert.hasRootDependency(testDep1);
    graphAssert.hasRootDependency(testDep2);
    graphAssert.hasNoDependency(testDevDep1);
    graphAssert.hasNoDependency(testDevDep2);
    graphAssert.hasRootSize(2);
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Aggregations

PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)12 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)4 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)4 NameVersion (com.synopsys.integration.util.NameVersion)4 Test (org.junit.jupiter.api.Test)4 UnitTest (com.synopsys.integration.detectable.annotations.UnitTest)3 NpmPackagerResult (com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)3 NullSafePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson)3 WorkspacePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson)3 Extraction (com.synopsys.integration.detectable.extraction.Extraction)3 Gson (com.google.gson.Gson)2 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 NpmCliParser (com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser)2 YarnLock (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock)2 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