aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objc_tools/xcodegen/java/com/google/devtools/build/xcode/xcodegen/Resources.java
blob: 170d39e2609f10efd42e8e0e2dac5e25d4255149 (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
// 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;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.xcode.util.Equaling;
import com.google.devtools.build.xcode.util.Value;
import com.google.devtools.build.xcode.xcodegen.proto.XcodeGenProtos.TargetControl;

import com.facebook.buck.apple.xcode.xcodeproj.PBXBuildFile;
import com.facebook.buck.apple.xcode.xcodeproj.PBXReference.SourceTree;
import com.facebook.buck.apple.xcode.xcodeproj.PBXResourcesBuildPhase;
import com.facebook.buck.apple.xcode.xcodeproj.PBXTarget.ProductType;

import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * Contains information about resources in an Xcode project.
 */
public class Resources extends Value<Resources> {

  private final ImmutableSetMultimap<TargetControl, PBXBuildFile> buildFiles;

  private Resources(ImmutableSetMultimap<TargetControl, PBXBuildFile> buildFiles) {
    super(buildFiles);
    this.buildFiles = buildFiles;
  }

  /**
   * Build files that should be added to the PBXResourcesBuildPhase for the given target.
   */
  public ImmutableSetMultimap<TargetControl, PBXBuildFile> buildFiles() {
    return buildFiles;
  }

  /**
   * Returns the PBXResourcesBuildPhase for the given target, if applicable. It will return an
   * absent {@code Optional} if the target is a library or there are no resources to compile.
   */
  public PBXResourcesBuildPhase resourcesBuildPhase(TargetControl targetControl) {
    PBXResourcesBuildPhase resourcesPhase = new PBXResourcesBuildPhase();
    resourcesPhase.getFiles().addAll(buildFiles().get(targetControl));
    return resourcesPhase;
  }

  public static Optional<String> languageOfLprojDir(Path child) {
    Path parent = child.getParent();
    if (parent == null) {
      return Optional.absent();
    }
    String dirName = parent.getFileName().toString();
    String lprojSuffix = ".lproj";
    if (dirName.endsWith(lprojSuffix)) {
      return Optional.of(dirName.substring(0, dirName.length() - lprojSuffix.length()));
    } else {
      return Optional.absent();
    }
  }

  public static Resources fromTargetControls(
      FileSystem fileSystem, PBXBuildFiles pbxBuildFiles, Iterable<TargetControl> targetControls) {
    ImmutableSetMultimap.Builder<TargetControl, PBXBuildFile> buildFiles =
        new ImmutableSetMultimap.Builder<>();

    for (TargetControl targetControl : targetControls) {
      List<PBXBuildFile> targetBuildFiles = new ArrayList<>();

      Iterable<String> simpleImports =
          Iterables.concat(targetControl.getXcassetsDirList(), targetControl.getBundleImportList());
      // Add .bundle, .xcassets directories to the Project Navigator so they are visible from within
      // Xcode.
      // Bundle imports are handled very similarly to asset catalogs, so we just add them with the
      // same logic. Xcode's automatic file type detection logic is smart enough to see it is a
      // bundle and link it properly, and add the {@code lastKnownFileType} property.
      for (String simpleImport : simpleImports) {
        targetBuildFiles.add(
            pbxBuildFiles.getStandalone(FileReference.of(simpleImport, SourceTree.GROUP)));
      }

      Iterables.addAll(
          targetBuildFiles,
          pbxBuildFiles.get(
              AggregateReferenceType.PBXVariantGroup,
              RelativePaths.fromStrings(fileSystem, targetControl.getGeneralResourceFileList())));

      // If this target is a binary, save the build files. Otherwise, we don't need them. The file
      // references we generated with fileObjects will be added to the main group later.
      if (!Equaling.of(
          ProductType.STATIC_LIBRARY, XcodeprojGeneration.productType(targetControl))) {
        buildFiles.putAll(targetControl, targetBuildFiles);
      }
    }

    return new Resources(buildFiles.build());
  }
}