Skip to content

Commit

Permalink
feat: implement robust project-wide formatting
Browse files Browse the repository at this point in the history
Now we can format cmake files with cmake-format and any of the file
types supported by clang-format (including C++, JavaScript and Json)
with clang-format.

The following additional targets are defined:
* format Shows which files are affected by clang-format
* check-format errors if files are affected by clang-format (for CI)
* fix-format Applies clang-format to all affected files

Dedicated targets for each of `cmake-format` and `clang-format`
are also added (e.g. cmake-format, clang-format, check-clang-format,...)
  • Loading branch information
abdes committed Sep 19, 2022
1 parent 1823a22 commit afcaebe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 45 deletions.
10 changes: 8 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
---
Language: Cpp
# BasedOnStyle: LLVM
BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: DontAlign
AlignConsecutiveMacros: false
Expand Down Expand Up @@ -134,4 +135,9 @@ TabWidth: 8
UseCRLF: false
UseTab: Never
---

Language: JavaScript
BasedOnStyle: Google
ColumnLimit: 80
---
Language: Json
ColumnLimit: 80
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,11 @@ add_subdirectory(tools/version-info)
# ------------------------------------------------------------------------------

if(${META_PROJECT_ID}_IS_MASTER_PROJECT)
include(ClangFormat)
include(ClangTidy)
asap_create_clang_tidy_targets()

# Enable this target when the project has header or source files (.h or .cpp)
# asap_setup_clang_format()

# Enable this target when the project has source files (.cpp)
# asap_create_clang_tidy_targets()
include(Format)
asap_create_format_targets()
endif()

asap_pop_project(${META_PROJECT_NAME})
27 changes: 0 additions & 27 deletions cmake/ClangFormat.cmake

This file was deleted.

10 changes: 10 additions & 0 deletions cmake/Format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ===-----------------------------------------------------------------------===#
# Distributed under the 3-Clause BSD License. See accompanying file LICENSE or
# copy at https://opensource.org/licenses/BSD-3-Clause).
# SPDX-License-Identifier: BSD-3-Clause
# ===-----------------------------------------------------------------------===#

macro(asap_create_format_targets)
cpmaddpackage(NAME Format.cmake VERSION 1.7.3 GITHUB_REPOSITORY
TheLartians/Format.cmake)
endmacro()
26 changes: 16 additions & 10 deletions cmake/scripts/standard-version-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const major_rex = /set\(META_VERSION_MAJOR\s+\"(\d+)\"\)/;
const minor_rex = /set\(META_VERSION_MINOR\s+\"(\d+)\"\)/;
const patch_rex = /set\(META_VERSION_PATCH\s+\"(\d+)\"\)/;

module.exports.readVersion = function (contents) {
module.exports.readVersion =
function(contents) {
var major = null, minor = null, patch = null;

const lines = contents.split(/\r?\n/);
Expand Down Expand Up @@ -70,24 +71,29 @@ module.exports.readVersion = function (contents) {
};

if (major == null)
console.error("Your CmakeLists.txt is missing META_VERSION_MAJOR variable!");
console.error(
'Your CmakeLists.txt is missing META_VERSION_MAJOR variable!');
if (minor == null)
console.error("Your CmakeLists.txt is missing META_VERSION_MINOR variable!");
console.error(
'Your CmakeLists.txt is missing META_VERSION_MINOR variable!');
if (patch == null)
console.error("Your CmakeLists.txt is missing META_VERSION_PATCH variable!");
console.error(
'Your CmakeLists.txt is missing META_VERSION_PATCH variable!');

return major + "." + minor + "." + patch;
return major + '.' + minor + '.' + patch;
}

module.exports.writeVersion = function (contents, version) {
var [major, minor, patch] = version.split(".");
module.exports.writeVersion = function(contents, version) {
var [major, minor, patch] = version.split('.');
var newContents = [];

const lines = contents.split(/\r?\n/);
lines.forEach(line => {
var newLine = line.replace(major_rex, "set(META_VERSION_MAJOR \"" + major + "\")")
.replace(minor_rex, "set(META_VERSION_MINOR \"" + minor + "\")")
.replace(patch_rex, "set(META_VERSION_PATCH \"" + patch + "\")");
var newLine =
line.replace(major_rex, 'set(META_VERSION_MAJOR "' + major + '")')
.replace(minor_rex, 'set(META_VERSION_MINOR "' + minor + '")')
.replace(
patch_rex, 'set(META_VERSION_PATCH "' + patch + '")');
newContents.push(newLine);
});

Expand Down

0 comments on commit afcaebe

Please sign in to comment.