summaryrefslogtreecommitdiff
path: root/xmake/core/main.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2017-12-08 00:43:18 +0800
committerruki <[email protected]>2017-12-07 22:19:48 +0800
commit185daf2de0ac372ed78d61b69bf65d5a69687295 (patch)
treecea368dac14a2e064ed6dcb43d566e2d1a01781f /xmake/core/main.lua
parent06ff3989aaf30f63c7e0cd0a8e9b2d6133799b43 (diff)
improve project directory
Diffstat (limited to 'xmake/core/main.lua')
-rw-r--r--xmake/core/main.lua68
1 files changed, 42 insertions, 26 deletions
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index cfa66b1b8..5e11acd28 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -131,11 +131,43 @@ function main._show_help()
end
end
+-- find the root project file
+function main._find_root(projectfile)
+
+ -- make all parent directories
+ local dirs = {}
+ local dir = path.directory(projectfile)
+ while os.isdir(dir) do
+ table.insert(dirs, 1, dir)
+ local parentdir = path.directory(dir)
+ if parentdir and parentdir ~= dir and parentdir ~= '.' then
+ dir = parentdir
+ else
+ break
+ end
+ end
+
+ -- find the first `xmake.lua` from it's parent directory
+ for _, dir in ipairs(dirs) do
+ local file = path.join(dir, "xmake.lua")
+ if os.isfile(file) then
+ return file
+ end
+ end
+ return projectfile
+end
+
-- the init function for main
function main._init()
+ -- get project directory from the argument option
+ local opt_projectdir = option.find(xmake._ARGV, "project", "P")
+
+ -- get project file from the argument option
+ local opt_projectfile = option.find(xmake._ARGV, "file", "F")
+
-- init the project directory
- local projectdir = option.find(xmake._ARGV, "project", "P") or xmake._PROJECT_DIR
+ local projectdir = opt_projectdir or xmake._PROJECT_DIR
if projectdir and not path.is_absolute(projectdir) then
projectdir = path.absolute(projectdir)
elseif projectdir then
@@ -145,38 +177,22 @@ function main._init()
assert(projectdir)
-- init the xmake.lua file path
- local projectfile = option.find(xmake._ARGV, "file", "F") or xmake._PROJECT_FILE
+ local projectfile = opt_projectfile or xmake._PROJECT_FILE
if projectfile and not path.is_absolute(projectfile) then
projectfile = path.absolute(projectfile, projectdir)
end
xmake._PROJECT_FILE = projectfile
assert(projectfile)
- -- make all parent directories
- local dirs = {}
- local dir = path.directory(projectfile)
- while os.isdir(dir) do
- table.insert(dirs, 1, dir)
- local parentdir = path.directory(dir)
- if parentdir and parentdir ~= dir and parentdir ~= '.' then
- dir = parentdir
- else
- break
- end
+ -- find the root project file
+ if not os.isfile(projectfile) or (not opt_projectdir and not opt_projectfile) then
+ projectfile = main._find_root(projectfile)
end
- -- find the first `xmake.lua` from it's parent directory
- for _, dir in ipairs(dirs) do
- local file = path.join(dir, "xmake.lua")
- if os.isfile(file) then
-
- -- switch to the project directory
- xmake._PROJECT_DIR = dir
- xmake._PROJECT_FILE = file
- os.cd(dir)
- break
- end
- end
+ -- update and enter project
+ xmake._PROJECT_DIR = path.directory(projectfile)
+ xmake._PROJECT_FILE = projectfile
+ xmake._WORKING_DIR = os.cd(xmake._PROJECT_DIR)
-- add the directory of the program file (xmake) to $PATH environment
local programfile = os.programfile()
@@ -229,7 +245,7 @@ Or you can add `--root` option to allow run as root temporarily.
end
-- save command lines to history
- if os.isfile(xmake._PROJECT_FILE) then
+ if os.isfile(os.projectfile()) then
history("local.history"):save("cmdlines", option.cmdline())
end