1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// found in the LICENSE file.
4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/err.h"
7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/filesystem_utils.h"
8d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/functions.h"
9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/input_conversion.h"
10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/input_file.h"
11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/scheduler.h"
12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// TODO(brettw) consider removing this. I originally wrote it for making the
14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// WebKit bindings but misundersood what was required, and didn't need to
15d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// use this. This seems to have a high potential for misuse.
16d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
17a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)namespace functions {
18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
19a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)const char kReadFile[] = "read_file";
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochconst char kReadFile_HelpShort[] =
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    "read_file: Read a file into a variable.";
22a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)const char kReadFile_Help[] =
23a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "read_file: Read a file into a variable.\n"
24a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "\n"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "  read_file(filename, input_conversion)\n"
26a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "\n"
27a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "  Whitespace will be trimmed from the end of the file. Throws an error\n"
28a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "  if the file can not be opened.\n"
29a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "\n"
30c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    "Arguments:\n"
31a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "\n"
32a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "  filename\n"
33a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "      Filename to read, relative to the build file.\n"
34a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "\n"
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "  input_conversion\n"
36a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "      Controls how the file is read and parsed.\n"
37a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "      See \"gn help input_conversion\".\n"
38a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "\n"
3968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    "Example\n"
40a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    "  lines = read_file(\"foo.txt\", \"list lines\")\n";
41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
42a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)Value RunReadFile(Scope* scope,
43a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                  const FunctionCallNode* function,
44a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                  const std::vector<Value>& args,
45a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                  Err* err) {
46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  if (args.size() != 2) {
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *err = Err(function->function(), "Wrong number of arguments to read_file",
48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch               "I expected two arguments.");
49d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return Value();
50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  }
51d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  if (!args[0].VerifyTypeIs(Value::STRING, err))
52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return Value();
53d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
54d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Compute the file name.
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  const SourceDir& cur_dir = scope->GetSourceDir();
56d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  SourceFile source_file = cur_dir.ResolveRelativeFile(args[0].string_value());
57d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  base::FilePath file_path =
58d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch      scope->settings()->build_settings()->GetFullPath(source_file);
59d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
60d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Ensure that everything is recomputed if the read file changes.
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  g_scheduler->AddGenDependency(file_path);
62d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
63d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Read contents.
64d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  std::string file_contents;
6558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!base::ReadFileToString(file_path, &file_contents)) {
66d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    *err = Err(args[0], "Could not read file.",
67d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch               "I resolved this to \"" + FilePathToUTF8(file_path) + "\".");
68d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return Value();
69d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  }
70d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
71c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return ConvertInputToValue(scope->settings(), file_contents, function,
72c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                             args[1], err);
73d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch}
74a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
75a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}  // namespace functions
76