diff options
| author | ruki <[email protected]> | 2017-12-08 00:43:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-12-07 22:19:48 +0800 |
| commit | 185daf2de0ac372ed78d61b69bf65d5a69687295 (patch) | |
| tree | cea368dac14a2e064ed6dcb43d566e2d1a01781f | |
| parent | 06ff3989aaf30f63c7e0cd0a8e9b2d6133799b43 (diff) | |
improve project directory
| -rw-r--r-- | xmake/actions/create/main.lua | 3 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 5 | ||||
| -rw-r--r-- | xmake/core/main.lua | 68 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 1 |
4 files changed, 51 insertions, 26 deletions
diff --git a/xmake/actions/create/main.lua b/xmake/actions/create/main.lua index 6a031c82c..1b261d016 100644 --- a/xmake/actions/create/main.lua +++ b/xmake/actions/create/main.lua @@ -30,6 +30,9 @@ import("core.project.template") -- main function main() + -- enter the original working directory, because the default directory is in the project directory + os.cd(os.workingdir()) + -- the target name local targetname = option.get("target") or option.get("name") or path.basename(project.directory()) or "demo" diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 296b3ec01..e311af3a3 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -827,6 +827,11 @@ function os.programfile() return xmake._PROGRAM_FILE end +-- get the working directory +function os.workingdir() + return xmake._WORKING_DIR +end + -- get the project directory function os.projectdir() return xmake._PROJECT_DIR 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 diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 933f08e5b..5c7efac91 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -52,6 +52,7 @@ sandbox_os.getenv = os.getenv sandbox_os.setenv = os.setenv sandbox_os.addenv = os.addenv sandbox_os.emptydir = os.emptydir +sandbox_os.workingdir = os.workingdir sandbox_os.programdir = os.programdir sandbox_os.programfile = os.programfile sandbox_os.projectdir = os.projectdir |
