diff options
| author | A2va <[email protected]> | 2022-11-10 22:52:23 +0100 |
|---|---|---|
| committer | A2va <[email protected]> | 2022-11-10 22:52:23 +0100 |
| commit | 808290756e24ce7ee70d5897477c5b20bd751981 (patch) | |
| tree | 48a08ce7d45bff4afc62327f6a1aadc3f859d221 /xmake/plugins/format/main.lua | |
| parent | b3bec1b2ff0c27986c56f4b92f4d5e06b53c9ee1 (diff) | |
Add format plugin
Diffstat (limited to 'xmake/plugins/format/main.lua')
| -rw-r--r-- | xmake/plugins/format/main.lua | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/xmake/plugins/format/main.lua b/xmake/plugins/format/main.lua new file mode 100644 index 000000000..a3973fee9 --- /dev/null +++ b/xmake/plugins/format/main.lua @@ -0,0 +1,73 @@ +--!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!") + + local argv = {} + + -- create style file + if option.get("create-style") then + table.insert(argv, "--style=" .. option.get("create-style")) + table.insert(argv, "--dump-config") + local outdata, errdata = os.iorunv(clang_format.program, argv) + io.writefile(".clang-format", outdata, {curdir = project.directory()}) + return + end + + -- set style file + if option.get("style") then + table.insert(argv, "--style=" .. option.get("style")) + end + + os.setenvs(oldenvs) +end |
