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

Side by Side Diff: tools/gn/function_write_file.cc

Issue 1656253003: [GN] Don't rewrite files with the same contents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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 unified diff | Download patch
OLDNEW
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/files/file_util.h" 8 #include "base/files/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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 123
124 // Track how to recreate this file, since we write it a gen time. 124 // Track how to recreate this file, since we write it a gen time.
125 // Note this is a hack since the correct output is not a dependency proper, 125 // Note this is a hack since the correct output is not a dependency proper,
126 // but an addition of this file to the output of the gn rule that writes it. 126 // but an addition of this file to the output of the gn rule that writes it.
127 // This dependency will, however, cause the gen step to be re-run and the 127 // This dependency will, however, cause the gen step to be re-run and the
128 // build restarted if the file is missing. 128 // build restarted if the file is missing.
129 g_scheduler->AddGenDependency( 129 g_scheduler->AddGenDependency(
130 scope->settings()->build_settings()->GetFullPath(source_file)); 130 scope->settings()->build_settings()->GetFullPath(source_file));
131 131
132 // Compute output. 132 // Compute output.
133 std::ostringstream contents; 133 std::stringstream contents;
134 if (args[1].type() == Value::LIST) { 134 if (args[1].type() == Value::LIST) {
135 const std::vector<Value>& list = args[1].list_value(); 135 const std::vector<Value>& list = args[1].list_value();
136 for (const auto& cur : list) 136 for (const auto& cur : list)
137 contents << cur.ToString(false) << std::endl; 137 contents << cur.ToString(false) << std::endl;
138 } else { 138 } else {
139 contents << args[1].ToString(false); 139 contents << args[1].ToString(false);
140 } 140 }
141 const std::string& new_contents = contents.str(); 141
142 base::FilePath file_path = 142 base::FilePath file_path =
143 scope->settings()->build_settings()->GetFullPath(source_file); 143 scope->settings()->build_settings()->GetFullPath(source_file);
144 144
145 // Make sure we're not replacing the same contents. 145 // Make sure we're not replacing the same contents.
146 std::string existing_contents; 146 if (ContentsEqual(file_path, &contents))
147 if (base::ReadFileToString(file_path, &existing_contents) &&
148 existing_contents == new_contents)
149 return Value(); // Nothing to do. 147 return Value(); // Nothing to do.
150 148
151 // Write file, creating the directory if necessary. 149 // Write file, creating the directory if necessary.
brettw 2016/02/02 20:50:23 Can the rest of this function and DoWriteFile all
Tomasz Moniuszko 2016/02/03 10:20:30 Done.
152 if (!base::CreateDirectory(file_path.DirName())) { 150 if (!base::CreateDirectory(file_path.DirName())) {
153 *err = Err(function->function(), "Unable to create directory.", 151 *err = Err(function->function(), "Unable to create directory.",
154 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\"."); 152 "I was using \"" + FilePathToUTF8(file_path.DirName()) + "\".");
155 return Value(); 153 return Value();
156 } 154 }
157 155
156 const std::string& new_contents = contents.str();
158 int int_size = static_cast<int>(new_contents.size()); 157 int int_size = static_cast<int>(new_contents.size());
159 if (DoWriteFile(file_path, new_contents.c_str(), int_size) 158 if (DoWriteFile(file_path, new_contents.c_str(), int_size)
160 != int_size) { 159 != int_size) {
161 *err = Err(function->function(), "Unable to write file.", 160 *err = Err(function->function(), "Unable to write file.",
162 "I was writing \"" + FilePathToUTF8(file_path) + "\"."); 161 "I was writing \"" + FilePathToUTF8(file_path) + "\".");
163 return Value(); 162 return Value();
164 } 163 }
165 return Value(); 164 return Value();
166 } 165 }
167 166
168 } // namespace functions 167 } // namespace functions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698