Search in sources :

Example 1 with XCVersionGroup

use of com.facebook.buck.apple.xcode.xcodeproj.XCVersionGroup in project buck by facebook.

the class ProjectGenerator method addCoreDataModelBuildPhase.

private void addCoreDataModelBuildPhase(PBXGroup targetGroup, Iterable<AppleWrapperResourceArg> dataModels) throws IOException {
    for (final AppleWrapperResourceArg dataModel : dataModels) {
        // Core data models go in the resources group also.
        PBXGroup resourcesGroup = targetGroup.getOrCreateChildGroupByName("Resources");
        if (CoreDataModelDescription.isVersionedDataModel(dataModel)) {
            // It's safe to do I/O here to figure out the current version because we're returning all
            // the versions and the file pointing to the current version from
            // getInputsToCompareToOutput(), so the rule will be correctly detected as stale if any of
            // them change.
            final String currentVersionFileName = ".xccurrentversion";
            final String currentVersionKey = "_XCCurrentVersionName";
            final XCVersionGroup versionGroup = resourcesGroup.getOrCreateChildVersionGroupsBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
            projectFilesystem.walkRelativeFileTree(dataModel.path, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                    if (dir.equals(dataModel.path)) {
                        return FileVisitResult.CONTINUE;
                    }
                    versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dir), Optional.empty()));
                    return FileVisitResult.SKIP_SUBTREE;
                }
            });
            Path currentVersionPath = dataModel.path.resolve(currentVersionFileName);
            try (InputStream in = projectFilesystem.newFileInputStream(currentVersionPath)) {
                NSObject rootObject;
                try {
                    rootObject = PropertyListParser.parse(in);
                } catch (IOException e) {
                    throw e;
                } catch (Exception e) {
                    rootObject = null;
                }
                if (!(rootObject instanceof NSDictionary)) {
                    throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
                }
                NSDictionary rootDictionary = (NSDictionary) rootObject;
                NSObject currentVersionName = rootDictionary.objectForKey(currentVersionKey);
                if (!(currentVersionName instanceof NSString)) {
                    throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
                }
                PBXFileReference ref = versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path.resolve(currentVersionName.toString())), Optional.empty()));
                versionGroup.setCurrentVersion(Optional.of(ref));
            } catch (NoSuchFileException e) {
                if (versionGroup.getChildren().size() == 1) {
                    versionGroup.setCurrentVersion(Optional.of(Iterables.get(versionGroup.getChildren(), 0)));
                }
            }
        } else {
            resourcesGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
        }
    }
}
Also used : SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) NSObject(com.dd.plist.NSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NSDictionary(com.dd.plist.NSDictionary) NoSuchFileException(java.nio.file.NoSuchFileException) FileVisitResult(java.nio.file.FileVisitResult) NSString(com.dd.plist.NSString) IOException(java.io.IOException) NSString(com.dd.plist.NSString) MacroException(com.facebook.buck.model.MacroException) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) HumanReadableException(com.facebook.buck.util.HumanReadableException) XCVersionGroup(com.facebook.buck.apple.xcode.xcodeproj.XCVersionGroup) HumanReadableException(com.facebook.buck.util.HumanReadableException) PBXGroup(com.facebook.buck.apple.xcode.xcodeproj.PBXGroup) AppleWrapperResourceArg(com.facebook.buck.apple.AppleWrapperResourceArg) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PBXFileReference(com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)

Aggregations

NSDictionary (com.dd.plist.NSDictionary)1 NSObject (com.dd.plist.NSObject)1 NSString (com.dd.plist.NSString)1 AppleWrapperResourceArg (com.facebook.buck.apple.AppleWrapperResourceArg)1 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)1 PBXGroup (com.facebook.buck.apple.xcode.xcodeproj.PBXGroup)1 SourceTreePath (com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath)1 XCVersionGroup (com.facebook.buck.apple.xcode.xcodeproj.XCVersionGroup)1 MacroException (com.facebook.buck.model.MacroException)1 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)1 BuildTargetSourcePath (com.facebook.buck.rules.BuildTargetSourcePath)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 FrameworkPath (com.facebook.buck.rules.coercer.FrameworkPath)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 FileVisitResult (java.nio.file.FileVisitResult)1