Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / gn / function_process_file_template.cc
index 061c901..540d1ed 100644 (file)
@@ -2,11 +2,12 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "tools/gn/file_template.h"
 #include "tools/gn/functions.h"
 #include "tools/gn/parse_tree.h"
 #include "tools/gn/scope.h"
 #include "tools/gn/settings.h"
+#include "tools/gn/substitution_list.h"
+#include "tools/gn/substitution_writer.h"
 #include "tools/gn/target.h"
 #include "tools/gn/value_extractors.h"
 
@@ -36,14 +37,8 @@ const char kProcessFileTemplate_Help[] =
     "  The template can be a string or a list. If it is a list, multiple\n"
     "  output strings are generated for each input.\n"
     "\n"
-    "  The following template substrings are used in the template arguments\n"
-    "  and are replaced with the corresponding part of the input file name:\n"
-    "\n"
-    "    {{source}}\n"
-    "        The entire source name.\n"
-    "\n"
-    "    {{source_name_part}}\n"
-    "        The source name with no path or extension.\n"
+    "  The template should contain source expansions to which each name in\n"
+    "  the source list is applied. See \"gn help source_expansion\".\n"
     "\n"
     "Example:\n"
     "\n"
@@ -71,26 +66,41 @@ Value RunProcessFileTemplate(Scope* scope,
     return Value();
   }
 
-  FileTemplate file_template(scope->settings(), args[1], err);
-  if (err->has_error())
-    return Value();
-
+  // Source list.
   Target::FileList input_files;
   if (!ExtractListOfRelativeFiles(scope->settings()->build_settings(), args[0],
                                   scope->GetSourceDir(), &input_files, err))
     return Value();
 
-  Value ret(function, Value::LIST);
-
-  // Temporary holding place, allocate outside to re-use buffer.
-  std::vector<std::string> string_output;
+  std::vector<std::string> result_files;
+  SubstitutionList subst;
 
-  for (size_t i = 0; i < input_files.size(); i++) {
-    string_output.clear();
-    file_template.Apply(input_files[i], &string_output);
-    for (size_t out_i = 0; out_i < string_output.size(); out_i++)
-      ret.list_value().push_back(Value(function, string_output[out_i]));
+  // Template.
+  const Value& template_arg = args[1];
+  if (template_arg.type() == Value::STRING) {
+    // Convert the string to a SubstitutionList with one pattern in it to
+    // simplify the code below.
+    std::vector<std::string> list;
+    list.push_back(template_arg.string_value());
+    if (!subst.Parse(list, template_arg.origin(), err))
+      return Value();
+  } else if (template_arg.type() == Value::LIST) {
+    if (!subst.Parse(template_arg, err))
+      return Value();
+  } else {
+    *err = Err(template_arg, "Not a string or a list.");
+    return Value();
   }
+
+  SubstitutionWriter::ApplyListToSourcesAsString(
+      scope->settings(), subst, input_files, &result_files);
+
+  // Convert the list of strings to the return Value.
+  Value ret(function, Value::LIST);
+  ret.list_value().reserve(result_files.size());
+  for (size_t i = 0; i < result_files.size(); i++)
+    ret.list_value().push_back(Value(function, result_files[i]));
+
   return ret;
 }