aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/AggregateReferenceType.java
blob: 3a123e0d5f1f5827428c45226b105c371d2c5a33 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2014 The Bazel Authors. 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;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.SetMultimap;

import com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference;
import com.facebook.buck.apple.xcode.xcodeproj.PBXReference;
import com.facebook.buck.apple.xcode.xcodeproj.PBXReference.SourceTree;
import com.facebook.buck.apple.xcode.xcodeproj.PBXVariantGroup;
import com.facebook.buck.apple.xcode.xcodeproj.XCVersionGroup;

import java.nio.file.Path;

/**
 * An aggregate reference is a kind of PBXReference that contains one or more files, grouped by some
 * criteria, and appearing as a group in the Xcode project navigator, and often handled as a single
 * file during the build phase and in other situations.
 */
public enum AggregateReferenceType {
  /**
   * A group which contains multiple .xcdatamodel directories where each is a different version of
   * the same schema. We may have to support other files besides .xcdatamodel in the future.
   * Instances of this group are represented by {@link XCVersionGroup} and are grouped by the
   * relative path of the containing .xcdatamodeld directory.
   */
  XCVersionGroup {
    @Override
    public PBXReference create(AggregateKey key, Iterable<PBXFileReference> children) {
      XCVersionGroup result = new XCVersionGroup(
          key.name().orNull(),
          key.path().isPresent() ? key.path().get().toString() : null,
          SourceTree.GROUP);
      Iterables.addAll(result.getChildren(), children);
      return result;
    }

    @Override
    public AggregateKey aggregateKey(Path path) {
      Path parent = path.getParent();
      if (parent.getFileName().toString().endsWith(".xcdatamodeld")) {
        return new AggregateKey(
            Optional.of(parent.getFileName().toString()), Optional.of(parent));
      } else {
        return AggregateKey.standalone();
      }
    }

    @Override
    public Path pathInAggregate(Path path) {
      return path.getFileName();
    }
  },

  /**
   * A group which contains the same content in multiple languages, each language belonging to a
   * different file. Instances of this group are represented by {@link PBXVariantGroup} and are
   * grouped by the base name of the file (e.g. "foo" in "/usr/bar/foo").
   */
  PBXVariantGroup {
    @Override
    public PBXReference create(AggregateKey key, Iterable<PBXFileReference> children) {
      PBXVariantGroup result = new PBXVariantGroup(
          key.name().orNull(),
          key.path().isPresent() ? key.path().get().toString() : null,
          SourceTree.GROUP);
      Iterables.addAll(result.getChildren(), children);
      return result;
    }

    @Override
    public AggregateKey aggregateKey(Path path) {
      if (Resources.languageOfLprojDir(path).isPresent()) {
        return new AggregateKey(
            Optional.of(path.getFileName().toString()), Optional.<Path>absent());
      } else {
        return AggregateKey.standalone();
      }
    }

    @Override
    public Path pathInAggregate(Path path) {
      return path;
    }
  };

  /**
   * Creates a new instance of this group with the group information and children.
   */
  public abstract PBXReference create(AggregateKey key, Iterable<PBXFileReference> children);

  /**
   * Returns the value by which this item should be grouped. All items sharing the same key should
   * belong to the same group. An {@link AggregateKey#standalone()} return here indicates that the
   * item should not belong to a group and should be built and treated as a standalone file.
   */
  public abstract AggregateKey aggregateKey(Path path);

  public abstract Path pathInAggregate(Path path);

  /**
   * Groups a sequence of items according to their {@link #aggregateKey(Path)}.
   */
  public SetMultimap<AggregateKey, Path> aggregates(Iterable<Path> paths) {
    ImmutableSetMultimap.Builder<AggregateKey, Path> result =
        new ImmutableSetMultimap.Builder<>();
    for (Path path : paths) {
      AggregateKey key = aggregateKey(path);
      Path referencePath = key.isStandalone() ? path : pathInAggregate(path);
      result.put(key, referencePath);
    }
    return result.build();
  }
}