1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/files/file_util.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "tools/gn/err.h"
12 #include "tools/gn/filesystem_utils.h"
13 #include "tools/gn/functions.h"
14 #include "tools/gn/input_file.h"
15 #include "tools/gn/parse_tree.h"
16 #include "tools/gn/scheduler.h"
20 const char kWriteFile
[] = "write_file";
21 const char kWriteFile_HelpShort
[] =
22 "write_file: Write a file to disk.";
23 const char kWriteFile_Help
[] =
24 "write_file: Write a file to disk.\n"
26 " write_file(filename, data)\n"
28 " If data is a list, the list will be written one-item-per-line with no\n"
29 " quoting or brackets.\n"
31 " If the file exists and the contents are identical to that being\n"
32 " written, the file will not be updated. This will prevent unnecessary\n"
33 " rebuilds of targets that depend on this file.\n"
35 " TODO(brettw) we probably need an optional third argument to control\n"
41 " Filename to write. This must be within the output directory.\n"
44 " The list or string to write.\n";
46 Value
RunWriteFile(Scope
* scope
,
47 const FunctionCallNode
* function
,
48 const std::vector
<Value
>& args
,
50 if (args
.size() != 2) {
51 *err
= Err(function
->function(), "Wrong number of arguments to write_file",
52 "I expected two arguments.");
56 // Compute the file name and make sure it's in the output dir.
57 if (!args
[0].VerifyTypeIs(Value::STRING
, err
))
59 const SourceDir
& cur_dir
= scope
->GetSourceDir();
60 SourceFile source_file
= cur_dir
.ResolveRelativeFile(args
[0].string_value());
61 if (!EnsureStringIsInOutputDir(
62 scope
->settings()->build_settings()->build_dir(),
63 source_file
.value(), args
[0].origin(), err
))
67 std::ostringstream contents
;
68 if (args
[1].type() == Value::LIST
) {
69 const std::vector
<Value
>& list
= args
[1].list_value();
70 for (size_t i
= 0; i
< list
.size(); i
++)
71 contents
<< list
[i
].ToString(false) << std::endl
;
73 contents
<< args
[1].ToString(false);
75 const std::string
& new_contents
= contents
.str();
76 base::FilePath file_path
=
77 scope
->settings()->build_settings()->GetFullPath(source_file
);
79 // Make sure we're not replacing the same contents.
80 std::string existing_contents
;
81 if (base::ReadFileToString(file_path
, &existing_contents
) &&
82 existing_contents
== new_contents
)
83 return Value(); // Nothing to do.
85 // Write file, creating the directory if necessary.
86 if (!base::CreateDirectory(file_path
.DirName())) {
87 *err
= Err(function
->function(), "Unable to create directory.",
88 "I was using \"" + FilePathToUTF8(file_path
.DirName()) + "\".");
92 int int_size
= static_cast<int>(new_contents
.size());
93 if (base::WriteFile(file_path
, new_contents
.c_str(), int_size
)
95 *err
= Err(function
->function(), "Unable to write file.",
96 "I was writing \"" + FilePathToUTF8(file_path
) + "\".");
102 } // namespace functions