OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <iostream> | |
6 #include <sstream> | |
7 | |
8 #include "base/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" | |
17 | |
18 /* | |
19 write_file: Read a file into a variable. | |
20 | |
21 write_file(filename, data) | |
22 | |
23 If data is a list, the list will be written one-item-per-line with no | |
24 quoting or brackets. | |
25 | |
26 TODO(brettw) we probably need an optional third argument to control list | |
27 formatting. | |
28 | |
29 Arguments: | |
30 | |
31 filename: | |
32 Filename to write. This must be within the output directory. | |
33 | |
34 data: | |
35 The list or string to write. | |
36 */ | |
37 Value ExecuteWriteFile(Scope* scope, | |
38 const FunctionCallNode* function, | |
39 const std::vector<Value>& args, | |
40 Err* err) { | |
41 if (args.size() != 2) { | |
42 *err = Err(function->function(), "Wrong number of args to write_file", | |
43 "I expected two arguments."); | |
44 return Value(); | |
45 } | |
46 | |
47 // Compute the file name and make sure it's in the output dir. | |
48 if (!args[0].VerifyTypeIs(Value::STRING, err)) | |
49 return Value(); | |
50 const SourceDir& cur_dir = SourceDirForFunctionCall(function); | |
51 SourceFile source_file = cur_dir.ResolveRelativeFile(args[0].string_value()); | |
52 if (!EnsureStringIsInOutputDir( | |
53 scope->settings()->build_settings()->build_dir(), | |
54 source_file.value(), args[0], err)) | |
55 return Value(); | |
56 | |
57 // Compute output. | |
58 std::ostringstream contents; | |
59 if (args[1].type() == Value::LIST) { | |
60 const std::vector<Value>& list = args[1].list_value(); | |
61 for (size_t i = 0; i < list.size(); i++) | |
62 contents << list[i].ToString() << std::endl; | |
63 } else { | |
64 contents << args[1].ToString(); | |
65 } | |
66 | |
67 // Write file, creating the directory if necessary. | |
68 base::FilePath file_path = | |
69 scope->settings()->build_settings()->GetFullPath(source_file); | |
70 const std::string& contents_string = contents.str(); | |
71 if (!file_util::CreateDirectory(file_path.DirName())) { | |
72 *err = Err(function->function(), "Unable to create directory.", | |
73 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\"."); | |
74 return Value(); | |
75 } | |
76 if (file_util::WriteFile(file_path, | |
77 contents_string.c_str(), contents_string.size()) | |
78 != static_cast<int>(contents_string.size())) { | |
79 *err = Err(function->function(), "Unable to write file.", | |
80 "I was writing \"" + FilePathToUTF8(file_path) + "\"."); | |
81 return Value(); | |
82 } | |
83 return Value(); | |
84 } | |
OLD | NEW |