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 <iostream> | 5 #include <iostream> |
6 #include <sstream> | 6 #include <sstream> |
7 | 7 |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 const char kWriteFile_HelpShort[] = | 21 const char kWriteFile_HelpShort[] = |
22 "write_file: Write a file to disk."; | 22 "write_file: Write a file to disk."; |
23 const char kWriteFile_Help[] = | 23 const char kWriteFile_Help[] = |
24 "write_file: Write a file to disk.\n" | 24 "write_file: Write a file to disk.\n" |
25 "\n" | 25 "\n" |
26 " write_file(filename, data)\n" | 26 " write_file(filename, data)\n" |
27 "\n" | 27 "\n" |
28 " If data is a list, the list will be written one-item-per-line with no\n" | 28 " If data is a list, the list will be written one-item-per-line with no\n" |
29 " quoting or brackets.\n" | 29 " quoting or brackets.\n" |
30 "\n" | 30 "\n" |
| 31 " If the file exists and the contents are identical to that being\n" |
| 32 " written, the file will not be updated. This will prevent unnecessary\n" |
| 33 " rebuilds of targets that depend on this file.\n" |
| 34 "\n" |
31 " TODO(brettw) we probably need an optional third argument to control\n" | 35 " TODO(brettw) we probably need an optional third argument to control\n" |
32 " list formatting.\n" | 36 " list formatting.\n" |
33 "\n" | 37 "\n" |
34 "Arguments:\n" | 38 "Arguments:\n" |
35 "\n" | 39 "\n" |
36 " filename\n" | 40 " filename\n" |
37 " Filename to write. This must be within the output directory.\n" | 41 " Filename to write. This must be within the output directory.\n" |
38 "\n" | 42 "\n" |
39 " data:\n" | 43 " data:\n" |
40 " The list or string to write.\n"; | 44 " The list or string to write.\n"; |
(...skipping 20 matching lines...) Expand all Loading... |
61 | 65 |
62 // Compute output. | 66 // Compute output. |
63 std::ostringstream contents; | 67 std::ostringstream contents; |
64 if (args[1].type() == Value::LIST) { | 68 if (args[1].type() == Value::LIST) { |
65 const std::vector<Value>& list = args[1].list_value(); | 69 const std::vector<Value>& list = args[1].list_value(); |
66 for (size_t i = 0; i < list.size(); i++) | 70 for (size_t i = 0; i < list.size(); i++) |
67 contents << list[i].ToString(false) << std::endl; | 71 contents << list[i].ToString(false) << std::endl; |
68 } else { | 72 } else { |
69 contents << args[1].ToString(false); | 73 contents << args[1].ToString(false); |
70 } | 74 } |
| 75 const std::string& new_contents = contents.str(); |
| 76 base::FilePath file_path = |
| 77 scope->settings()->build_settings()->GetFullPath(source_file); |
| 78 |
| 79 // Make sure we're not replacing the same contents. |
| 80 std::string existing_contents; |
| 81 if (base::ReadFileToString(file_path, &existing_contents) && |
| 82 existing_contents == new_contents) |
| 83 return Value(); // Nothing to do. |
71 | 84 |
72 // Write file, creating the directory if necessary. | 85 // Write file, creating the directory if necessary. |
73 base::FilePath file_path = | |
74 scope->settings()->build_settings()->GetFullPath(source_file); | |
75 const std::string& contents_string = contents.str(); | |
76 if (!base::CreateDirectory(file_path.DirName())) { | 86 if (!base::CreateDirectory(file_path.DirName())) { |
77 *err = Err(function->function(), "Unable to create directory.", | 87 *err = Err(function->function(), "Unable to create directory.", |
78 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\"."); | 88 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\"."); |
79 return Value(); | 89 return Value(); |
80 } | 90 } |
81 int int_size = static_cast<int>(contents_string.size()); | 91 |
82 if (base::WriteFile(file_path, contents_string.c_str(), int_size) | 92 int int_size = static_cast<int>(new_contents.size()); |
| 93 if (base::WriteFile(file_path, new_contents.c_str(), int_size) |
83 != int_size) { | 94 != int_size) { |
84 *err = Err(function->function(), "Unable to write file.", | 95 *err = Err(function->function(), "Unable to write file.", |
85 "I was writing \"" + FilePathToUTF8(file_path) + "\"."); | 96 "I was writing \"" + FilePathToUTF8(file_path) + "\"."); |
86 return Value(); | 97 return Value(); |
87 } | 98 } |
88 return Value(); | 99 return Value(); |
89 } | 100 } |
90 | 101 |
91 } // namespace functions | 102 } // namespace functions |
OLD | NEW |