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"
16 // Corresponds to the various values of "what" in the function call.
25 std::string
GetOnePathInfo(const SourceDir
& current_dir
,
29 if (!input
.VerifyTypeIs(Value::STRING
, err
))
31 const std::string
& input_string
= input
.string_value();
32 if (input_string
.empty()) {
33 *err
= Err(input
, "Calling get_path_info on an empty string.");
39 return FindFilename(&input_string
).as_string();
42 std::string file
= FindFilename(&input_string
).as_string();
43 size_t extension_offset
= FindExtensionOffset(file
);
44 if (extension_offset
== std::string::npos
)
46 // Trim extension and dot.
47 return file
.substr(0, extension_offset
- 1);
49 case WHAT_EXTENSION
: {
50 return FindExtension(&input_string
).as_string();
53 base::StringPiece dir_incl_slash
= FindDir(&input_string
);
54 if (dir_incl_slash
.empty())
55 return std::string(".");
56 // Trim slash since this function doesn't return trailing slashes. The
57 // times we don't do this are if the result is "/" and "//" since those
58 // slashes can't be trimmed.
59 if (dir_incl_slash
== "/")
60 return std::string("/.");
61 if (dir_incl_slash
== "//")
62 return std::string("//.");
63 return dir_incl_slash
.substr(0, dir_incl_slash
.size() - 1).as_string();
66 if (!input_string
.empty() && input_string
[input_string
.size() - 1] == '/')
67 return current_dir
.ResolveRelativeDir(input_string
).value();
69 return current_dir
.ResolveRelativeFile(input_string
).value();
79 const char kGetPathInfo
[] = "get_path_info";
80 const char kGetPathInfo_HelpShort
[] =
81 "get_path_info: Extract parts of a file or directory name.";
82 const char kGetPathInfo_Help
[] =
83 "get_path_info: Extract parts of a file or directory name.\n"
85 " get_path_info(input, what)\n"
87 " The first argument is either a string representing a file or\n"
88 " directory name, or a list of such strings. If the input is a list\n"
89 " the return value will be a list containing the result of applying the\n"
90 " rule to each item in the input.\n"
92 "Possible values for the \"what\" parameter\n"
95 " The substring after the last slash in the path, including the name\n"
96 " and extension. If the input ends in a slash, the empty string will\n"
98 " \"foo/bar.txt\" => \"bar.txt\"\n"
99 " \"bar.txt\" => \"bar.txt\"\n"
100 " \"foo/\" => \"\"\n"
104 " The substring of the file name not including the extension.\n"
105 " \"foo/bar.txt\" => \"bar\"\n"
106 " \"foo/bar\" => \"bar\"\n"
107 " \"foo/\" => \"\"\n"
110 " The substring following the last period following the last slash,\n"
111 " or the empty string if not found. The period is not included.\n"
112 " \"foo/bar.txt\" => \"txt\"\n"
113 " \"foo/bar\" => \"\"\n"
116 " The directory portion of the name, not including the slash.\n"
117 " \"foo/bar.txt\" => \"foo\"\n"
118 " \"//foo/bar\" => \"//foo\"\n"
119 " \"foo\" => \".\"\n"
121 " The result will never end in a slash, so if the resulting\n"
122 " is empty, the system (\"/\") or source (\"//\") roots, a \".\"\n"
123 " will be appended such that it is always legal to append a slash\n"
124 " and a filename and get a valid path.\n"
127 " The full absolute path name to the file or directory. It will be\n"
128 " resolved relative to the currebt directory, and then the source-\n"
129 " absolute version will be returned. If the input is system-\n"
130 " absolute, the same input will be returned.\n"
131 " \"foo/bar.txt\" => \"//mydir/foo/bar.txt\"\n"
132 " \"foo/\" => \"//mydir/foo/\"\n"
133 " \"//foo/bar\" => \"//foo/bar\" (already absolute)\n"
134 " \"/usr/include\" => \"/usr/include\" (already absolute)\n"
136 " If you want to make the path relative to another directory, or to\n"
137 " be system-absolute, see rebase_path().\n"
140 " sources = [ \"foo.cc\", \"foo.h\" ]\n"
141 " result = get_path_info(source, \"abspath\")\n"
142 " # result will be [ \"//mydir/foo.cc\", \"//mydir/foo.h\" ]\n"
144 " result = get_path_info(\"//foo/bar/baz.cc\", \"dir\")\n"
145 " # result will be \"//foo/bar\"\n"
147 " # Extract the source-absolute directory name,\n"
148 " result = get_path_info(get_path_info(path, \"dir\"), \"abspath\")\n";
150 Value
RunGetPathInfo(Scope
* scope
,
151 const FunctionCallNode
* function
,
152 const std::vector
<Value
>& args
,
154 if (args
.size() != 2) {
155 *err
= Err(function
, "Expecting two arguments to get_path_info.");
159 // Extract the "what".
160 if (!args
[1].VerifyTypeIs(Value::STRING
, err
))
163 if (args
[1].string_value() == "file") {
165 } else if (args
[1].string_value() == "name") {
167 } else if (args
[1].string_value() == "extension") {
168 what
= WHAT_EXTENSION
;
169 } else if (args
[1].string_value() == "dir") {
171 } else if (args
[1].string_value() == "abspath") {
174 *err
= Err(args
[1], "Unknown value for 'what'.");
178 const SourceDir
& current_dir
= scope
->GetSourceDir();
179 if (args
[0].type() == Value::STRING
) {
180 return Value(function
, GetOnePathInfo(current_dir
, what
, args
[0], err
));
181 } else if (args
[0].type() == Value::LIST
) {
182 const std::vector
<Value
>& input_list
= args
[0].list_value();
183 Value
result(function
, Value::LIST
);
184 for (size_t i
= 0; i
< input_list
.size(); i
++) {
185 result
.list_value().push_back(Value(function
,
186 GetOnePathInfo(current_dir
, what
, input_list
[i
], err
)));
187 if (err
->has_error())
193 *err
= Err(args
[0], "Path must be a string or a list of strings.");
197 } // namespace functions