aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/GenericBuildInfoPropertiesTranslator.java
blob: c51a209ade1ba45c1786293cd42a9cab77881aed (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
// 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.lib.rules.java;

import com.google.devtools.build.lib.util.Fingerprint;

import java.util.Map;
import java.util.Properties;

/** The generic implementation of {@link BuildInfoPropertiesTranslator} */
public class GenericBuildInfoPropertiesTranslator implements
    BuildInfoPropertiesTranslator {

  private static final String GUID = "e71fe4a8-11af-4ec0-9b38-1d3e7f542f51";

  // syntax is %ID% for a property that depends on the ID key, %ID|default% to
  // always add the property with the "default" key, %% is to add a percent sign
  private final Map<String, String> translationKeys;
  
  /**
   * Create a generic translator, for each key,value pair in {@code translationKeys}, the key
   * represents the property key that will be written and the value, its value. Inside value every
   * %ID% is replaced by the corresponding build information with the same ID key. The property
   * won't be added if it's depends on an unresolved build information. Adding a property can
   * be forced even if a build information is missing by specifying a default value using the
   * %ID|default% syntax. Finally to add a percent sign, just use the %% syntax.
   */
  public GenericBuildInfoPropertiesTranslator(Map<String, String> translationKeys) {
    this.translationKeys = translationKeys;
  }

  @Override
  public String computeKey() {
    Fingerprint f = new Fingerprint();
    f.addString(GUID);
    f.addStringMap(translationKeys);
    return f.hexDigestAndReset();
  }

  @Override
  public void translate(Map<String, String> buildInfo, Properties properties) {
    for (Map.Entry<String, String> entry : translationKeys.entrySet()) {
      String translatedValue = translateValue(entry.getValue(), buildInfo);
      if (translatedValue != null) {
        properties.put(entry.getKey(), translatedValue);
      }
    }
  }

  private String translateValue(String valueDescription, Map<String, String> buildInfo) {
    String[] split = valueDescription.split("%");
    StringBuilder result = new StringBuilder();
    boolean isInsideKey = false;
    for (String key : split) {
      if (isInsideKey) {
        if (key.isEmpty()) {
          result.append("%"); // empty key means %%
        } else {
          String defaultValue = null;
          int i = key.lastIndexOf('|');
          if (i >= 0) {
            defaultValue = key.substring(i + 1);
            key = key.substring(0, i);
          }
          if (buildInfo.containsKey(key)) {
            result.append(buildInfo.get(key));
          } else if (defaultValue != null) {
            result.append(defaultValue);
          } else { // we haven't found the requested key so we ignore the whole value
            return null;
          }
        }
      } else {
        result.append(key);
      }
      isInsideKey = !isInsideKey;
    }
    return result.toString();
  }
}