summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-11-13 08:41:07 +0800
committerGitHub <[email protected]>2022-11-13 08:41:07 +0800
commit02861cdf01d4d68786216a782b3773f456d0b913 (patch)
tree247319bb4f151e1a3259ed558960257cab195d0d
parenta26503ae05b34224c425547f409e4e679d960075 (diff)
parentb20601b1ff390328391dfd10dc2fb0f39f216c8e (diff)
Merge pull request #3049 from A2va/format-plugin
Clang-Format plugin
-rw-r--r--xmake/plugins/format/main.lua84
-rw-r--r--xmake/plugins/format/xmake.lua36
2 files changed, 120 insertions, 0 deletions
diff --git a/xmake/plugins/format/main.lua b/xmake/plugins/format/main.lua
new file mode 100644
index 000000000..17ce73066
--- /dev/null
+++ b/xmake/plugins/format/main.lua
@@ -0,0 +1,84 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file main.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.project.config")
+import("core.project.project")
+import("lib.detect.find_tool")
+import("private.action.require.impl.packagenv")
+import("private.action.require.impl.install_packages")
+
+-- main
+function main()
+
+ -- load configuration
+ config.load()
+
+ -- enter the environments of doxygen
+ local oldenvs = packagenv.enter("llvm")
+
+ -- find clang-format
+ local packages = {}
+ local clang_format = find_tool("clang-format")
+ if not clang_format then
+ table.join2(packages, install_packages("llvm"))
+ end
+
+ -- enter the environments of installed packages
+ for _, instance in ipairs(packages) do
+ instance:envs_enter()
+ end
+
+ -- we need force to detect and flush detect cache after loading all environments
+ if not clang_format then
+ clang_format = find_tool("clang-format", {force = true})
+ end
+ assert(clang_format, "clang-format not found!")
+
+ -- create style file
+ local argv = {}
+ local projectdir = project.directory()
+ if option.get("create") then
+ table.insert(argv, "--style=" .. (option.get("style") or "Google"))
+ table.insert(argv, "--dump-config")
+ os.execv(clang_format.program, argv, {stdout = path.join(projectdir, ".clang-format"), curdir = projectdir})
+ return
+ end
+
+ -- set style file
+ if option.get("style") then
+ table.insert(argv, "--style=" .. option.get("style"))
+ end
+
+ -- inplace flag
+ table.insert(argv, "-i")
+
+ -- set file to format
+ if option.get("file") then
+ table.insert(argv, option.get("file"))
+ end
+
+ -- format files
+ os.vrunv(clang_format.program, argv, {curdir = projectdir})
+ cprint("${color.success}formatting complete")
+
+ os.setenvs(oldenvs)
+end
diff --git a/xmake/plugins/format/xmake.lua b/xmake/plugins/format/xmake.lua
new file mode 100644
index 000000000..16710524a
--- /dev/null
+++ b/xmake/plugins/format/xmake.lua
@@ -0,0 +1,36 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file format.lua
+--
+
+task("format")
+ set_category("plugin")
+ on_run("main")
+ set_menu {
+ usage = "xmake format [options] [arguments]",
+ description = "Format the current project.",
+ options = {
+ {'s', "style", "kv", nil, "Set the path of .clang-format file, a coding style"..
+ "(LLVM, Google, Chromium, Mozilla, WebKit) or key value string" },
+ {nil, "create", "k", nil, "Create a .clang-format file from a coding style" },
+ {'f', "file", "v", "*.cpp *.h *.hpp", "Set file path or glob pattern"}
+ }
+ }
+
+
+