aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/actions/Template.java
blob: 4cd870b7208f66dc3e485ef4e96d0e828a9a3b1f (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
156
157
158
159
160
161
162
163
164
// 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.analysis.actions;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.util.ResourceFileLoader;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;

/** A template that contains text content, or alternatively throws an {@link IOException}. */
@Immutable // all subclasses are immutable
public abstract class Template {

  static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

  /** We only allow subclasses in this file. */
  private Template() {}

  /** Returns the text content of the template. */
  protected abstract String getContent() throws IOException;

  @Nullable
  public Artifact getTemplateArtifact() {
    return null;
  }

  /**
   * Returns a string that is used for the action key. This must change if the getContent method
   * returns something different, but is not allowed to throw an exception.
   */
  protected abstract String getKey();

  @AutoCodec.VisibleForSerialization
  @AutoCodec
  static final class ErrorTemplate extends Template {
    private final IOException e;
    private final String templateName;

    @AutoCodec.VisibleForSerialization
    @AutoCodec.Instantiator
    ErrorTemplate(IOException e, String templateName) {
      this.e = e;
      this.templateName = templateName;
    }

    @Override
    protected String getContent() throws IOException {
      throw new IOException(
          "failed to load resource file '" + templateName + "' due to I/O error: " + e.getMessage(),
          e);
    }

    @Override
    protected String getKey() {
      return "ERROR: " + e.getMessage();
    }
  }

  @AutoCodec.VisibleForSerialization
  @AutoCodec
  static final class StringTemplate extends Template {
    private final String templateText;

    @AutoCodec.VisibleForSerialization
    @AutoCodec.Instantiator
    StringTemplate(String templateText) {
      this.templateText = templateText;
    }

    @Override
    protected String getContent() {
      return templateText;
    }

    @Override
    protected String getKey() {
      return templateText;
    }
  }

  @AutoCodec.VisibleForSerialization
  @AutoCodec
  static final class ArtifactTemplate extends Template {
    private final Artifact templateArtifact;

    @AutoCodec.VisibleForSerialization
    @AutoCodec.Instantiator
    ArtifactTemplate(Artifact templateArtifact) {
      this.templateArtifact = templateArtifact;
    }

    @Override
    protected String getContent() throws IOException {
      Path templatePath = templateArtifact.getPath();
      try {
        return FileSystemUtils.readContent(templatePath, DEFAULT_CHARSET);
      } catch (IOException e) {
        throw new IOException(
            "failed to load template file '"
                + templatePath.getPathString()
                + "' due to I/O error: "
                + e.getMessage(),
            e);
      }
    }

    @Override
    protected String getKey() {
      // This isn't strictly necessary, because the action inputs are automatically considered.
      return "ARTIFACT: " + templateArtifact.getExecPathString();
    }

    @Override
    public Artifact getTemplateArtifact() {
      return templateArtifact;
    }
  }
  /**
   * Loads a template from the given resource. The resource is looked up relative to the given
   * class. If the resource cannot be loaded, the returned template throws an {@link IOException}
   * when {@link #getContent} is called. This makes it safe to use this method in a constant
   * initializer.
   */
  public static Template forResource(final Class<?> relativeToClass, final String templateName) {
    try {
      String content = ResourceFileLoader.loadResource(relativeToClass, templateName);
      return forString(content);
    } catch (final IOException e) {
      return new ErrorTemplate(e, templateName);
    }
  }

  /** Returns a template for the given text string. */
  public static Template forString(final String templateText) {
    return new StringTemplate(templateText);
  }

  /**
   * Returns a template that loads the given artifact. It is important that the artifact is also an
   * input for the action, or this won't work. Therefore this method is private, and you should use
   * the corresponding {@link TemplateExpansionAction} constructor.
   */
  static Template forArtifact(final Artifact templateArtifact) {
    return new ArtifactTemplate(templateArtifact);
  }
}