aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/analysis/actions/FileWriteAction.java
blob: 01ae8eb8c18ac613e965df9bbd214a469fe170d0 (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
// 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 static java.nio.charset.StandardCharsets.UTF_8;

import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Executor;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.util.Fingerprint;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

/**
 * Action to write to a file.
 * <p>TODO(bazel-team): Choose a better name to distinguish this class from
 * {@link BinaryFileWriteAction}.
 */
public class FileWriteAction extends AbstractFileWriteAction {

  private static final String GUID = "332877c7-ca9f-4731-b387-54f620408522";

  /**
   * We keep it as a CharSequence for memory-efficiency reasons. The toString()
   * method of the object represents the content of the file.
   *
   * <p>For example, this allows us to keep a {@code List<Artifact>} wrapped
   * in a {@code LazyString} instead of the string representation of the concatenation.
   * This saves memory because the Artifacts are shared objects but the
   * resulting String is not.
   */
  private final CharSequence fileContents;

  /**
   * Creates a new FileWriteAction instance without inputs using UTF8 encoding.
   *
   * @param owner the action owner.
   * @param output the Artifact that will be created by executing this Action.
   * @param fileContents the contents to be written to the file.
   * @param makeExecutable iff true will change the output file to be
   *   executable.
   */
  public FileWriteAction(ActionOwner owner, Artifact output, CharSequence fileContents,
      boolean makeExecutable) {
    this(owner, Artifact.NO_ARTIFACTS, output, fileContents, makeExecutable);
  }

  /**
   * Creates a new FileWriteAction instance using UTF8 encoding.
   *
   * @param owner the action owner.
   * @param inputs the Artifacts that this Action depends on
   * @param output the Artifact that will be created by executing this Action.
   * @param fileContents the contents to be written to the file.
   * @param makeExecutable iff true will change the output file to be
   *   executable.
   */
  public FileWriteAction(ActionOwner owner, Collection<Artifact> inputs,
      Artifact output, CharSequence fileContents, boolean makeExecutable) {
    super(owner, inputs, output, makeExecutable);
    this.fileContents = fileContents;
  }

  /**
   * Creates a new FileWriteAction instance using UTF8 encoding.
   *
   * @param owner the action owner.
   * @param inputs the Artifacts that this Action depends on
   * @param output the Artifact that will be created by executing this Action.
   * @param makeExecutable iff true will change the output file to be
   *   executable.
   */
  protected FileWriteAction(ActionOwner owner, Collection<Artifact> inputs,
      Artifact output, boolean makeExecutable) {
    this(owner, inputs, output, "", makeExecutable);
  }

  public String getFileContents() {
    return fileContents.toString();
  }

  /**
   * Create a DeterministicWriter for the content of the output file as provided by
   * {@link #getFileContents()}.
   */
  @Override
  public DeterministicWriter newDeterministicWriter(EventHandler eventHandler,
      Executor executor) {
    return new DeterministicWriter() {
      @Override
      public void writeOutputFile(OutputStream out) throws IOException {
        out.write(getFileContents().getBytes(UTF_8));
      }
    };
  }

  /**
   * Computes the Action key for this action by computing the fingerprint for
   * the file contents.
   */
  @Override
  protected String computeKey() {
    Fingerprint f = new Fingerprint();
    f.addString(GUID);
    f.addString(String.valueOf(makeExecutable));
    f.addString(getFileContents());
    return f.hexDigestAndReset();
  }

  /**
   * Creates a FileWriteAction to write contents to the resulting artifact
   * fileName in the genfiles root underneath the package path.
   *
   * @param ruleContext the ruleContext that will own the action of creating this file.
   * @param fileName name of the file to create.
   * @param contents data to write to file.
   * @param executable flags that file should be marked executable.
   * @return Artifact describing the file to create.
   */
  public static Artifact createFile(RuleContext ruleContext,
      String fileName, CharSequence contents, boolean executable) {
    Artifact scriptFileArtifact = ruleContext.getPackageRelativeArtifact(
        fileName, ruleContext.getConfiguration().getGenfilesDirectory());
    ruleContext.registerAction(new FileWriteAction(
        ruleContext.getActionOwner(), scriptFileArtifact, contents, executable));
    return scriptFileArtifact;
  }
}