OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "tools/gn/err.h" | 6 #include "tools/gn/err.h" |
7 #include "tools/gn/filesystem_utils.h" | 7 #include "tools/gn/filesystem_utils.h" |
8 #include "tools/gn/functions.h" | 8 #include "tools/gn/functions.h" |
9 #include "tools/gn/input_conversion.h" | 9 #include "tools/gn/input_conversion.h" |
10 #include "tools/gn/input_file.h" | 10 #include "tools/gn/input_file.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 " See \"gn help input_conversion\".\n" | 35 " See \"gn help input_conversion\".\n" |
36 "\n" | 36 "\n" |
37 "Example\n" | 37 "Example\n" |
38 " lines = read_file(\"foo.txt\", \"list lines\")\n"; | 38 " lines = read_file(\"foo.txt\", \"list lines\")\n"; |
39 | 39 |
40 Value RunReadFile(Scope* scope, | 40 Value RunReadFile(Scope* scope, |
41 const FunctionCallNode* function, | 41 const FunctionCallNode* function, |
42 const std::vector<Value>& args, | 42 const std::vector<Value>& args, |
43 Err* err) { | 43 Err* err) { |
44 if (args.size() != 2) { | 44 if (args.size() != 2) { |
45 *err = Err(function->function(), "Wrong number of args to read_file", | 45 *err = Err(function->function(), "Wrong number of arguments to read_file", |
46 "I expected two arguments."); | 46 "I expected two arguments."); |
47 return Value(); | 47 return Value(); |
48 } | 48 } |
49 if (!args[0].VerifyTypeIs(Value::STRING, err)) | 49 if (!args[0].VerifyTypeIs(Value::STRING, err)) |
50 return Value(); | 50 return Value(); |
51 | 51 |
52 // Compute the file name. | 52 // Compute the file name. |
53 const SourceDir& cur_dir = scope->GetSourceDir(); | 53 const SourceDir& cur_dir = scope->GetSourceDir(); |
54 SourceFile source_file = cur_dir.ResolveRelativeFile(args[0].string_value()); | 54 SourceFile source_file = cur_dir.ResolveRelativeFile(args[0].string_value()); |
55 base::FilePath file_path = | 55 base::FilePath file_path = |
56 scope->settings()->build_settings()->GetFullPath(source_file); | 56 scope->settings()->build_settings()->GetFullPath(source_file); |
57 | 57 |
58 // Ensure that everything is recomputed if the read file changes. | 58 // Ensure that everything is recomputed if the read file changes. |
59 g_scheduler->AddGenDependency(file_path); | 59 g_scheduler->AddGenDependency(file_path); |
60 | 60 |
61 // Read contents. | 61 // Read contents. |
62 std::string file_contents; | 62 std::string file_contents; |
63 if (!base::ReadFileToString(file_path, &file_contents)) { | 63 if (!base::ReadFileToString(file_path, &file_contents)) { |
64 *err = Err(args[0], "Could not read file.", | 64 *err = Err(args[0], "Could not read file.", |
65 "I resolved this to \"" + FilePathToUTF8(file_path) + "\"."); | 65 "I resolved this to \"" + FilePathToUTF8(file_path) + "\"."); |
66 return Value(); | 66 return Value(); |
67 } | 67 } |
68 | 68 |
69 return ConvertInputToValue(file_contents, function, args[1], err); | 69 return ConvertInputToValue(file_contents, function, args[1], err); |
70 } | 70 } |
71 | 71 |
72 } // namespace functions | 72 } // namespace functions |
OLD | NEW |