Search in sources :

Example 1 with XcodeFileReference

use of com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference in project synopsys-detect by blackducksoftware.

the class XcodeWorkspaceExtractor method extract.

public XcodeWorkspaceResult extract(File workspaceDataFile, File workspaceDirectory) throws IOException, ParserConfigurationException, SAXException {
    String workspaceFileContents = FileUtils.readFileToString(workspaceDataFile, Charset.defaultCharset());
    XcodeWorkspace xcodeWorkspace = xcodeWorkspaceParser.parse(workspaceFileContents);
    xcodeWorkspaceFormatChecker.checkForVersionCompatibility(xcodeWorkspace);
    List<CodeLocation> codeLocations = new LinkedList<>();
    List<FailedDetectableResult> failedDetectableResults = new LinkedList<>();
    for (XcodeFileReference fileReference : xcodeWorkspace.getFileReferences()) {
        File workspaceReferencedDirectory = workspaceDirectory.getParentFile().toPath().resolve(fileReference.getRelativeLocation()).toFile();
        if (!workspaceReferencedDirectory.exists()) {
            logger.warn("Failed to find subproject '{}' as defined in the workspace at '{}'", workspaceReferencedDirectory, workspaceDataFile.getParentFile().getAbsolutePath());
            continue;
        }
        switch(fileReference.getFileReferenceType()) {
            case DIRECTORY:
                PackageResolvedResult swiftProjectResult = extractStandalonePackageResolved(workspaceReferencedDirectory);
                swiftProjectResult.getFailedDetectableResult().ifPresent(failedDetectableResults::add);
                codeLocations.add(new CodeLocation(swiftProjectResult.getDependencyGraph(), workspaceReferencedDirectory));
                break;
            case XCODE_PROJECT:
                PackageResolvedResult xcodeProjectResult = extractFromXcodeProject(workspaceReferencedDirectory);
                xcodeProjectResult.getFailedDetectableResult().ifPresent(failedDetectableResults::add);
                codeLocations.add(new CodeLocation(xcodeProjectResult.getDependencyGraph(), workspaceReferencedDirectory));
                break;
            default:
                throw new UnsupportedOperationException(String.format("Unrecognized FileReferenceType: %s", fileReference.getFileReferenceType()));
        }
    }
    if (CollectionUtils.isNotEmpty(failedDetectableResults)) {
        return XcodeWorkspaceResult.failure(failedDetectableResults);
    }
    return XcodeWorkspaceResult.success(codeLocations);
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) FailedDetectableResult(com.synopsys.integration.detectable.detectable.result.FailedDetectableResult) XcodeFileReference(com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference) PackageResolvedResult(com.synopsys.integration.detectable.detectables.swift.lock.model.PackageResolvedResult) XcodeWorkspace(com.synopsys.integration.detectable.detectables.xcode.model.XcodeWorkspace) File(java.io.File) LinkedList(java.util.LinkedList)

Example 2 with XcodeFileReference

use of com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference in project synopsys-detect by blackducksoftware.

the class XcodeWorkspaceParser method parseReference.

private XcodeFileReference parseReference(String locationString) {
    String location = StringUtils.removeStart(locationString, "group:");
    FileReferenceType fileReferenceType = FileReferenceType.DIRECTORY;
    if (StringUtils.endsWith(location, ".xcodeproj")) {
        fileReferenceType = FileReferenceType.XCODE_PROJECT;
    }
    return new XcodeFileReference(Paths.get(location), fileReferenceType);
}
Also used : XcodeFileReference(com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference) FileReferenceType(com.synopsys.integration.detectable.detectables.xcode.model.FileReferenceType)

Example 3 with XcodeFileReference

use of com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference in project synopsys-detect by blackducksoftware.

the class XcodeWorkspaceParser method parse.

public XcodeWorkspace parse(String workspaceDataXmlContents) throws IOException, SAXException, ParserConfigurationException, DOMException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource workspaceInputSource = new InputSource(new StringReader(workspaceDataXmlContents.trim()));
    Document workspaceDocument = builder.parse(workspaceInputSource);
    Node workspaceNode = XmlUtil.getNode("Workspace", workspaceDocument);
    Optional<String> formatVersion = XmlUtil.getAttributeSafetly("version", workspaceNode, logger);
    List<Node> fileRefs = XmlUtil.getNodeList("FileRef", workspaceNode);
    List<XcodeFileReference> xcodeFileReferences = fileRefs.stream().map(fileRefsNode -> XmlUtil.getAttributeSafetly("location", fileRefsNode, logger)).filter(Optional::isPresent).map(Optional::get).map(this::parseReference).collect(Collectors.toList());
    return new XcodeWorkspace(formatVersion.orElse("UNKNOWN"), xcodeFileReferences);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Optional(java.util.Optional) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) XcodeFileReference(com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) XcodeWorkspace(com.synopsys.integration.detectable.detectables.xcode.model.XcodeWorkspace)

Example 4 with XcodeFileReference

use of com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference in project synopsys-detect by blackducksoftware.

the class XcodeWorkspaceParserTest method parse.

@Test
void parse() throws IOException, ParserConfigurationException, SAXException {
    Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS);
    String workspaceContents = StringUtils.joinWith(System.lineSeparator(), " <?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<Workspace", "   version = \"1.0\">", "   <FileRef", "      location = \"group:ENA/ENASecurity\">", "   </FileRef>", "   <FileRef", "      location = \"group:ENA/HealthCertificateToolkit\">", "   </FileRef>", "   <FileRef", "      location = \"group:ENA/ENA.xcodeproj\">", "   </FileRef>", "</Workspace>");
    XcodeWorkspaceParser parser = new XcodeWorkspaceParser();
    XcodeWorkspace xcodeWorkspace = parser.parse(workspaceContents);
    XcodeFileReference enaSecurity = assertReferenceExists(xcodeWorkspace, "ENA/ENASecurity");
    assertEquals(FileReferenceType.DIRECTORY, enaSecurity.getFileReferenceType());
    XcodeFileReference healthToolkit = assertReferenceExists(xcodeWorkspace, "ENA/HealthCertificateToolkit");
    assertEquals(FileReferenceType.DIRECTORY, healthToolkit.getFileReferenceType());
    XcodeFileReference xcodeProject = assertReferenceExists(xcodeWorkspace, "ENA/ENA.xcodeproj");
    assertEquals(FileReferenceType.XCODE_PROJECT, xcodeProject.getFileReferenceType());
    assertEquals(3, xcodeWorkspace.getFileReferences().size(), "There are more file references than are being asserted against");
}
Also used : XcodeFileReference(com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference) XcodeWorkspace(com.synopsys.integration.detectable.detectables.xcode.model.XcodeWorkspace) Test(org.junit.jupiter.api.Test)

Aggregations

XcodeFileReference (com.synopsys.integration.detectable.detectables.xcode.model.XcodeFileReference)4 XcodeWorkspace (com.synopsys.integration.detectable.detectables.xcode.model.XcodeWorkspace)3 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)1 FailedDetectableResult (com.synopsys.integration.detectable.detectable.result.FailedDetectableResult)1 PackageResolvedResult (com.synopsys.integration.detectable.detectables.swift.lock.model.PackageResolvedResult)1 FileReferenceType (com.synopsys.integration.detectable.detectables.xcode.model.FileReferenceType)1 File (java.io.File)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 Optional (java.util.Optional)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Test (org.junit.jupiter.api.Test)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 InputSource (org.xml.sax.InputSource)1