| 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 "base/file_util.h" | |
| 6 #include "tools/gn/err.h" | |
| 7 #include "tools/gn/filesystem_utils.h" | |
| 8 #include "tools/gn/functions.h" | |
| 9 #include "tools/gn/input_conversion.h" | |
| 10 #include "tools/gn/input_file.h" | |
| 11 #include "tools/gn/scheduler.h" | |
| 12 | |
| 13 // TODO(brettw) consider removing this. I originally wrote it for making the | |
| 14 // WebKit bindings but misundersood what was required, and didn't need to | |
| 15 // use this. This seems to have a high potential for misuse. | |
| 16 | |
| 17 /* | |
| 18 read_file: Read a file into a variable. | |
| 19 | |
| 20 read_file(filename, how_to_read) | |
| 21 | |
| 22 Whitespace will be trimmed from the end of the file. Throws an error if the | |
| 23 file can not be opened. | |
| 24 | |
| 25 Arguments: | |
| 26 | |
| 27 filename: | |
| 28 Filename to read, relative to the build file. | |
| 29 | |
| 30 input_conversion: | |
| 31 Controls how the file is read and parsed. See "help input_conversion". | |
| 32 | |
| 33 Example: | |
| 34 | |
| 35 lines = read_file("foo.txt", "list lines") | |
| 36 */ | |
| 37 Value ExecuteReadFile(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 read_file", | |
| 43 "I expected two arguments."); | |
| 44 return Value(); | |
| 45 } | |
| 46 if (!args[0].VerifyTypeIs(Value::STRING, err)) | |
| 47 return Value(); | |
| 48 | |
| 49 // Compute the file name. | |
| 50 const SourceDir& cur_dir = SourceDirForFunctionCall(function); | |
| 51 SourceFile source_file = cur_dir.ResolveRelativeFile(args[0].string_value()); | |
| 52 base::FilePath file_path = | |
| 53 scope->settings()->build_settings()->GetFullPath(source_file); | |
| 54 | |
| 55 // Ensure that everything is recomputed if the read file changes. | |
| 56 g_scheduler->AddGenDependency(source_file); | |
| 57 | |
| 58 // Read contents. | |
| 59 std::string file_contents; | |
| 60 if (!file_util::ReadFileToString(file_path, &file_contents)) { | |
| 61 *err = Err(args[0], "Could not read file.", | |
| 62 "I resolved this to \"" + FilePathToUTF8(file_path) + "\"."); | |
| 63 return Value(); | |
| 64 } | |
| 65 | |
| 66 return ConvertInputToValue(file_contents, function, args[1], err); | |
| 67 } | |
| OLD | NEW |