[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / gn / function_get_path_info.cc
bloba3f2c109301c120ddf01f4a7c4f7da45b2660db2
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/gn/err.h"
6 #include "tools/gn/filesystem_utils.h"
7 #include "tools/gn/functions.h"
8 #include "tools/gn/parse_tree.h"
9 #include "tools/gn/scope.h"
10 #include "tools/gn/value.h"
12 namespace functions {
14 namespace {
16 // Corresponds to the various values of "what" in the function call.
17 enum What {
18 WHAT_FILE,
19 WHAT_NAME,
20 WHAT_EXTENSION,
21 WHAT_DIR,
22 WHAT_ABSPATH,
23 WHAT_GEN_DIR,
24 WHAT_OUT_DIR,
27 // Returns the directory containing the input (resolving it against the
28 // |current_dir|), regardless of whether the input is a directory or a file.
29 SourceDir DirForInput(const Settings* settings,
30 const SourceDir& current_dir,
31 const std::string& input_string) {
32 if (!input_string.empty() && input_string[input_string.size() - 1] == '/') {
33 // Input is a directory.
34 return current_dir.ResolveRelativeDir(input_string,
35 settings->build_settings()->root_path_utf8());
38 // Input is a directory.
39 return current_dir.ResolveRelativeFile(input_string,
40 settings->build_settings()->root_path_utf8()).GetDir();
43 std::string GetOnePathInfo(const Settings* settings,
44 const SourceDir& current_dir,
45 What what,
46 const Value& input,
47 Err* err) {
48 if (!input.VerifyTypeIs(Value::STRING, err))
49 return std::string();
50 const std::string& input_string = input.string_value();
51 if (input_string.empty()) {
52 *err = Err(input, "Calling get_path_info on an empty string.");
53 return std::string();
56 switch (what) {
57 case WHAT_FILE: {
58 return FindFilename(&input_string).as_string();
60 case WHAT_NAME: {
61 std::string file = FindFilename(&input_string).as_string();
62 size_t extension_offset = FindExtensionOffset(file);
63 if (extension_offset == std::string::npos)
64 return file;
65 // Trim extension and dot.
66 return file.substr(0, extension_offset - 1);
68 case WHAT_EXTENSION: {
69 return FindExtension(&input_string).as_string();
71 case WHAT_DIR: {
72 base::StringPiece dir_incl_slash = FindDir(&input_string);
73 if (dir_incl_slash.empty())
74 return std::string(".");
75 // Trim slash since this function doesn't return trailing slashes. The
76 // times we don't do this are if the result is "/" and "//" since those
77 // slashes can't be trimmed.
78 if (dir_incl_slash == "/")
79 return std::string("/.");
80 if (dir_incl_slash == "//")
81 return std::string("//.");
82 return dir_incl_slash.substr(0, dir_incl_slash.size() - 1).as_string();
84 case WHAT_GEN_DIR: {
85 return DirectoryWithNoLastSlash(
86 GetGenDirForSourceDir(settings,
87 DirForInput(settings, current_dir,
88 input_string)));
90 case WHAT_OUT_DIR: {
91 return DirectoryWithNoLastSlash(
92 GetOutputDirForSourceDir(settings,
93 DirForInput(settings, current_dir,
94 input_string)));
96 case WHAT_ABSPATH: {
97 if (!input_string.empty() &&
98 input_string[input_string.size() - 1] == '/') {
99 return current_dir.ResolveRelativeDir(input_string,
100 settings->build_settings()->root_path_utf8()).value();
101 } else {
102 return current_dir.ResolveRelativeFile(input_string,
103 settings->build_settings()->root_path_utf8()).value();
106 default:
107 NOTREACHED();
108 return std::string();
112 } // namespace
114 const char kGetPathInfo[] = "get_path_info";
115 const char kGetPathInfo_HelpShort[] =
116 "get_path_info: Extract parts of a file or directory name.";
117 const char kGetPathInfo_Help[] =
118 "get_path_info: Extract parts of a file or directory name.\n"
119 "\n"
120 " get_path_info(input, what)\n"
121 "\n"
122 " The first argument is either a string representing a file or\n"
123 " directory name, or a list of such strings. If the input is a list\n"
124 " the return value will be a list containing the result of applying the\n"
125 " rule to each item in the input.\n"
126 "\n"
127 "Possible values for the \"what\" parameter\n"
128 "\n"
129 " \"file\"\n"
130 " The substring after the last slash in the path, including the name\n"
131 " and extension. If the input ends in a slash, the empty string will\n"
132 " be returned.\n"
133 " \"foo/bar.txt\" => \"bar.txt\"\n"
134 " \"bar.txt\" => \"bar.txt\"\n"
135 " \"foo/\" => \"\"\n"
136 " \"\" => \"\"\n"
137 "\n"
138 " \"name\"\n"
139 " The substring of the file name not including the extension.\n"
140 " \"foo/bar.txt\" => \"bar\"\n"
141 " \"foo/bar\" => \"bar\"\n"
142 " \"foo/\" => \"\"\n"
143 "\n"
144 " \"extension\"\n"
145 " The substring following the last period following the last slash,\n"
146 " or the empty string if not found. The period is not included.\n"
147 " \"foo/bar.txt\" => \"txt\"\n"
148 " \"foo/bar\" => \"\"\n"
149 "\n"
150 " \"dir\"\n"
151 " The directory portion of the name, not including the slash.\n"
152 " \"foo/bar.txt\" => \"foo\"\n"
153 " \"//foo/bar\" => \"//foo\"\n"
154 " \"foo\" => \".\"\n"
155 "\n"
156 " The result will never end in a slash, so if the resulting\n"
157 " is empty, the system (\"/\") or source (\"//\") roots, a \".\"\n"
158 " will be appended such that it is always legal to append a slash\n"
159 " and a filename and get a valid path.\n"
160 "\n"
161 " \"out_dir\"\n"
162 " The output file directory corresponding to the path of the\n"
163 " given file, not including a trailing slash.\n"
164 " \"//foo/bar/baz.txt\" => \"//out/Default/obj/foo/bar\"\n"
166 " \"gen_dir\"\n"
167 " The generated file directory corresponding to the path of the\n"
168 " given file, not including a trailing slash.\n"
169 " \"//foo/bar/baz.txt\" => \"//out/Default/gen/foo/bar\"\n"
170 "\n"
171 " \"abspath\"\n"
172 " The full absolute path name to the file or directory. It will be\n"
173 " resolved relative to the currebt directory, and then the source-\n"
174 " absolute version will be returned. If the input is system-\n"
175 " absolute, the same input will be returned.\n"
176 " \"foo/bar.txt\" => \"//mydir/foo/bar.txt\"\n"
177 " \"foo/\" => \"//mydir/foo/\"\n"
178 " \"//foo/bar\" => \"//foo/bar\" (already absolute)\n"
179 " \"/usr/include\" => \"/usr/include\" (already absolute)\n"
180 "\n"
181 " If you want to make the path relative to another directory, or to\n"
182 " be system-absolute, see rebase_path().\n"
183 "\n"
184 "Examples\n"
185 " sources = [ \"foo.cc\", \"foo.h\" ]\n"
186 " result = get_path_info(source, \"abspath\")\n"
187 " # result will be [ \"//mydir/foo.cc\", \"//mydir/foo.h\" ]\n"
188 "\n"
189 " result = get_path_info(\"//foo/bar/baz.cc\", \"dir\")\n"
190 " # result will be \"//foo/bar\"\n"
191 "\n"
192 " # Extract the source-absolute directory name,\n"
193 " result = get_path_info(get_path_info(path, \"dir\"), \"abspath\")\n";
195 Value RunGetPathInfo(Scope* scope,
196 const FunctionCallNode* function,
197 const std::vector<Value>& args,
198 Err* err) {
199 if (args.size() != 2) {
200 *err = Err(function, "Expecting two arguments to get_path_info.");
201 return Value();
204 // Extract the "what".
205 if (!args[1].VerifyTypeIs(Value::STRING, err))
206 return Value();
207 What what;
208 if (args[1].string_value() == "file") {
209 what = WHAT_FILE;
210 } else if (args[1].string_value() == "name") {
211 what = WHAT_NAME;
212 } else if (args[1].string_value() == "extension") {
213 what = WHAT_EXTENSION;
214 } else if (args[1].string_value() == "dir") {
215 what = WHAT_DIR;
216 } else if (args[1].string_value() == "out_dir") {
217 what = WHAT_OUT_DIR;
218 } else if (args[1].string_value() == "gen_dir") {
219 what = WHAT_GEN_DIR;
220 } else if (args[1].string_value() == "abspath") {
221 what = WHAT_ABSPATH;
222 } else {
223 *err = Err(args[1], "Unknown value for 'what'.");
224 return Value();
227 const SourceDir& current_dir = scope->GetSourceDir();
228 if (args[0].type() == Value::STRING) {
229 return Value(function, GetOnePathInfo(scope->settings(), current_dir, what,
230 args[0], err));
231 } else if (args[0].type() == Value::LIST) {
232 const std::vector<Value>& input_list = args[0].list_value();
233 Value result(function, Value::LIST);
234 for (const auto& cur : input_list) {
235 result.list_value().push_back(Value(function,
236 GetOnePathInfo(scope->settings(), current_dir, what, cur, err)));
237 if (err->has_error())
238 return Value();
240 return result;
243 *err = Err(args[0], "Path must be a string or a list of strings.");
244 return Value();
247 } // namespace functions