aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/testing/PbxTypes.java
blob: 8d0a2ee514c6a8858aa42b0a304958af40442d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.xcode.xcodegen.testing;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.xcode.xcodegen.FileReference;

import com.facebook.buck.apple.xcode.xcodeproj.PBXBuildFile;
import com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference;
import com.facebook.buck.apple.xcode.xcodeproj.PBXFrameworksBuildPhase;
import com.facebook.buck.apple.xcode.xcodeproj.PBXReference;

/**
 * Collection of static utility methods for PBX type transformations, either to other
 * representations of the same or our domain objects.
 */
public class PbxTypes {

  private PbxTypes() {}

  /**
   * Returns all file references the {@code phase} depends on in
   * {@link PBXFrameworksBuildPhase#getFiles()}.
   */
  public static ImmutableList<FileReference> fileReferences(PBXFrameworksBuildPhase phase) {
    return fileReferences(pbxFileReferences(phase));
  }

  /**
   * Transforms the given list of PBX references to file references.
   */
  public static ImmutableList<FileReference> fileReferences(
      Iterable<? extends PBXReference> references) {
    ImmutableList.Builder<FileReference> fileReferences = ImmutableList.builder();
    for (PBXReference reference : references) {
      fileReferences.add(fileReference(reference));
    }
    return fileReferences.build();
  }

  /**
   * Extracts the list of PBX references {@code phase} depends on through
   * {@link PBXFrameworksBuildPhase#getFiles()}.
   */
  public static ImmutableList<PBXReference> pbxFileReferences(PBXFrameworksBuildPhase phase) {
    ImmutableList.Builder<PBXReference> phaseFileReferences = ImmutableList.builder();
    for (PBXBuildFile buildFile : phase.getFiles()) {
      phaseFileReferences.add(buildFile.getFileRef());
    }
    return phaseFileReferences.build();
  }

  /**
   * Converts a PBX file reference to its domain equivalent.
   */
  public static FileReference fileReference(PBXReference reference) {
    FileReference fileReference =
        FileReference.of(reference.getName(), reference.getPath(), reference.getSourceTree());
    if (reference instanceof PBXFileReference) {
      Optional<String> explicitFileType = ((PBXFileReference) reference).getExplicitFileType();
      if (explicitFileType.isPresent()) {
        return fileReference.withExplicitFileType(explicitFileType.get());
      }
    }
    return fileReference;
  }

  /**
   * Returns the string representation of all references the {@code phase} depends on in
   * {@link PBXFrameworksBuildPhase#getFiles()}.
   *
   */
  public static ImmutableList<String> referencePaths(PBXFrameworksBuildPhase phase) {
    return paths(pbxFileReferences(phase));
  }

  /**
   * Transforms the given list of references into their string path representations.
   */
  public static ImmutableList<String> paths(Iterable<? extends PBXReference> references) {
    ImmutableList.Builder<String> paths = ImmutableList.builder();
    for (PBXReference reference : references) {
      paths.add(reference.getPath());
    }
    return paths.build();
  }
}