Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Unified Diff: tools/gn/function_write_file.cc

Issue 21114002: Add initial prototype for the GN meta-buildsystem. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add owners and readme Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/function_toolchain.cc ('k') | tools/gn/functions.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/function_write_file.cc
diff --git a/tools/gn/function_write_file.cc b/tools/gn/function_write_file.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9356feb0495bac3bd76b04b7218e730a60431970
--- /dev/null
+++ b/tools/gn/function_write_file.cc
@@ -0,0 +1,84 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <iostream>
+#include <sstream>
+
+#include "base/file_util.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "tools/gn/err.h"
+#include "tools/gn/filesystem_utils.h"
+#include "tools/gn/functions.h"
+#include "tools/gn/input_file.h"
+#include "tools/gn/parse_tree.h"
+#include "tools/gn/scheduler.h"
+
+/*
+write_file: Read a file into a variable.
+
+ write_file(filename, data)
+
+ If data is a list, the list will be written one-item-per-line with no
+ quoting or brackets.
+
+ TODO(brettw) we probably need an optional third argument to control list
+ formatting.
+
+Arguments:
+
+ filename:
+ Filename to write. This must be within the output directory.
+
+ data:
+ The list or string to write.
+*/
+Value ExecuteWriteFile(Scope* scope,
+ const FunctionCallNode* function,
+ const std::vector<Value>& args,
+ Err* err) {
+ if (args.size() != 2) {
+ *err = Err(function->function(), "Wrong number of args to write_file",
+ "I expected two arguments.");
+ return Value();
+ }
+
+ // Compute the file name and make sure it's in the output dir.
+ if (!args[0].VerifyTypeIs(Value::STRING, err))
+ return Value();
+ const SourceDir& cur_dir = SourceDirForFunctionCall(function);
+ SourceFile source_file = cur_dir.ResolveRelativeFile(args[0].string_value());
+ if (!EnsureStringIsInOutputDir(
+ scope->settings()->build_settings()->build_dir(),
+ source_file.value(), args[0], err))
+ return Value();
+
+ // Compute output.
+ std::ostringstream contents;
+ if (args[1].type() == Value::LIST) {
+ const std::vector<Value>& list = args[1].list_value();
+ for (size_t i = 0; i < list.size(); i++)
+ contents << list[i].ToString() << std::endl;
+ } else {
+ contents << args[1].ToString();
+ }
+
+ // Write file, creating the directory if necessary.
+ base::FilePath file_path =
+ scope->settings()->build_settings()->GetFullPath(source_file);
+ const std::string& contents_string = contents.str();
+ if (!file_util::CreateDirectory(file_path.DirName())) {
+ *err = Err(function->function(), "Unable to create directory.",
+ "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\".");
+ return Value();
+ }
+ if (file_util::WriteFile(file_path,
+ contents_string.c_str(), contents_string.size())
+ != static_cast<int>(contents_string.size())) {
+ *err = Err(function->function(), "Unable to write file.",
+ "I was writing \"" + FilePathToUTF8(file_path) + "\".");
+ return Value();
+ }
+ return Value();
+}
« no previous file with comments | « tools/gn/function_toolchain.cc ('k') | tools/gn/functions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698