annotate luabuild/luarocks-2.0.12/src/luarocks/dir.lua @ 1130:54653afec7cc

<GreyKnight> (cd luabuild/luarocks-2.0.12; make)
author HackBot
date Fri, 14 Dec 2012 22:22:17 +0000
parents 87f6d05d4b4a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1125
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
1
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
2 --- Generic utilities for handling pathnames.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
3 module("luarocks.dir", package.seeall)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
4
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
5 separator = "/"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
6
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
7 --- Strip the path off a path+filename.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
8 -- @param pathname string: A path+name, such as "/a/b/c"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
9 -- or "\a\b\c".
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
10 -- @return string: The filename without its path, such as "c".
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
11 function base_name(pathname)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
12 assert(type(pathname) == "string")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
13
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
14 local base = pathname:gsub("[/\\]*$", ""):match(".*[/\\]([^/\\]*)")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
15 return base or pathname
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
16 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
17
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
18 --- Strip the name off a path+filename.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
19 -- @param pathname string: A path+name, such as "/a/b/c".
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
20 -- @return string: The filename without its path, such as "/a/b/".
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
21 -- For entries such as "/a/b/", "/a/" is returned. If there are
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
22 -- no directory separators in input, "" is returned.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
23 function dir_name(pathname)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
24 assert(type(pathname) == "string")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
25
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
26 return (pathname:gsub("/*$", ""):match("(.*/)[^/]*")) or ""
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
27 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
28
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
29 --- Describe a path in a cross-platform way.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
30 -- Use this function to avoid platform-specific directory
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
31 -- separators in other modules. Removes trailing slashes from
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
32 -- each component given, to avoid repeated separators.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
33 -- Separators inside strings are kept, to handle URLs containing
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
34 -- protocols.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
35 -- @param ... strings representing directories
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
36 -- @return string: a string with a platform-specific representation
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
37 -- of the path.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
38 function path(...)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
39 local items = {...}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
40 local i = 1
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
41 while items[i] do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
42 items[i] = items[i]:gsub("(.+)/+$", "%1")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
43 if items[i] == "" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
44 table.remove(items, i)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
45 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
46 i = i + 1
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
47 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
48 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
49 return (table.concat(items, "/"):gsub("(.+)/+$", "%1"))
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
50 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
51
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
52 --- Split protocol and path from an URL or local pathname.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
53 -- URLs should be in the "protocol://path" format.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
54 -- For local pathnames, "file" is returned as the protocol.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
55 -- @param url string: an URL or a local pathname.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
56 -- @return string, string: the protocol, and the pathname without the protocol.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
57 function split_url(url)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
58 assert(type(url) == "string")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
59
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
60 local protocol, pathname = url:match("^([^:]*)://(.*)")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
61 if not protocol then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
62 protocol = "file"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
63 pathname = url
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
64 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
65 return protocol, pathname
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
66 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
67
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
68 function normalize(name)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
69 return name:gsub("\\", "/"):gsub("(.)/*$", "%1")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
70 end