aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/objc/PlMergeControlBytes.java
blob: 80eecdf3db679f0f393ffe9c6ab3fabb94e8ac43 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2015 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.lib.rules.objc;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteSource;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.xcode.plmerge.proto.PlMergeProtos;
import com.google.devtools.build.xcode.plmerge.proto.PlMergeProtos.Control;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import javax.annotation.Nullable;

/**
 * A byte source that can be used the generate a control file for the tool plmerge.
 */
public final class PlMergeControlBytes extends ByteSource {

  private final NestedSet<Artifact> inputPlists;
  private final NestedSet<Artifact> immutableInputPlists;
  @Nullable private final String primaryBundleId;
  @Nullable private final String fallbackBundleId;
  @Nullable private final Map<String, String> variableSubstitutions;
  @Nullable private final String executableName;
  private final Artifact mergedPlist;
  private final OutputFormat outputFormat;

  /**
   * Creates a control based on the passed bundling's plists and values.
   *
   * @param bundling bundle for which to create a merged plist
   * @param mergedPlist the plist that should be created as an output
   */
  static PlMergeControlBytes fromBundling(Bundling bundling, Artifact mergedPlist) {

    NestedSetBuilder<Artifact> immutableInputPlists = NestedSetBuilder.stableOrder();
    if (bundling.getAutomaticInfoPlist() != null) {
      immutableInputPlists.add(bundling.getAutomaticInfoPlist());
    }

    return new PlMergeControlBytes(
        NestedSetBuilder.<Artifact>stableOrder()
            .addTransitive(bundling.getBundleInfoplistInputs())
            .build(),
        immutableInputPlists.build(),
        bundling.getPrimaryBundleId(),
        bundling.getFallbackBundleId(),
        bundling.variableSubstitutions(),
        bundling.getExecutableName(),
        mergedPlist,
        OutputFormat.BINARY);
  }

  /**
   * Creates a control that merges the given plists into the merged plist.
   */
  static PlMergeControlBytes fromPlists(
      NestedSet<Artifact> inputPlists,
      Artifact mergedPlist,
      OutputFormat outputFormat) {
    return new PlMergeControlBytes(
        inputPlists,
        NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER),
        null,
        null,
        ImmutableMap.<String, String>of(),
        null,
        mergedPlist,
        outputFormat);
  }

  private PlMergeControlBytes(
      NestedSet<Artifact> inputPlists,
      NestedSet<Artifact> immutableInputPlists,
      @Nullable String primaryBundleId,
      @Nullable String fallbackBundleId,
      @Nullable Map<String, String> variableSubstitutions,
      @Nullable String executableName,
      Artifact mergedPlist,
      OutputFormat outputFormat) {
    this.inputPlists = inputPlists;
    this.immutableInputPlists = immutableInputPlists;
    this.primaryBundleId = primaryBundleId;
    this.fallbackBundleId = fallbackBundleId;
    this.variableSubstitutions = variableSubstitutions;
    this.executableName = executableName;
    this.mergedPlist = mergedPlist;
    this.outputFormat = Preconditions.checkNotNull(outputFormat);
  }

  @Override
  public InputStream openStream() throws IOException {
    return control().toByteString().newInput();
  }

  private Control control() {
    PlMergeProtos.Control.Builder control =
        PlMergeProtos.Control.newBuilder()
            .addAllSourceFile(Artifact.toExecPaths(inputPlists))
            .addAllImmutableSourceFile(Artifact.toExecPaths(immutableInputPlists))
            .putAllVariableSubstitutionMap(variableSubstitutions)
            .setOutFile(mergedPlist.getExecPathString());

    if (primaryBundleId != null) {
      control.setPrimaryBundleId(primaryBundleId);
    }

    if (fallbackBundleId != null) {
      control.setFallbackBundleId(fallbackBundleId);
    }

    if (executableName != null) {
      control.setExecutableName(executableName);
    }

    control.setOutputFormat(outputFormat.getProtoOutputFormat());

    return control.build();
  }

  /**
   * Plist output formats.
   */
  public enum OutputFormat {
    BINARY(Control.OutputFormat.BINARY),
    XML(Control.OutputFormat.XML);

    private final Control.OutputFormat protoOutputFormat;

    private OutputFormat(Control.OutputFormat protoOutputFormat) {
      this.protoOutputFormat = protoOutputFormat;
    }

    Control.OutputFormat getProtoOutputFormat() {
      return protoOutputFormat;
    }
  }
}