Skip to content

Commit

Permalink
internal: use wasm-fmt for clang-format and add git hooks installer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Feb 10, 2025
1 parent 729ef34 commit 3f06cc0
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"build": {
"dockerfile": "Dockerfile"
},
"postCreateCommand": "mkdir -p .git/hooks && ln -sf $(pwd)/misc/git-hooks/pre-commit .git/hooks/pre-commit && chmod +x misc/git-hooks/pre-commit && cd misc/git-hooks && npm install simple-git",
"postCreateCommand": "cmake -P misc/install_git_hooks.cmake",
"remoteUser": "node"
}
22 changes: 17 additions & 5 deletions misc/git-hooks/clang-format-hook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { execSync } = require("child_process");
const simpleGit = require("simple-git");
const fs = require("fs");
const path = require("path");
import init, { format } from "@wasm-fmt/clang-format";
import fs from "fs";
import path from "path";
import simpleGit from "simple-git";
import { execSync } from "child_process";

const extensions = [".cpp", ".h", ".hpp", ".cxx", ".cc"];

Expand All @@ -28,14 +29,25 @@ const formatFiles = (files) => {
return;
}

const config = fs.readFileSync(".clang-format", "utf8");

console.log("Formatting files:");
filesToFormat.forEach((file) => {
console.log(` - ${file}`);
execSync(`clang-format -i ${file}`, { stdio: "inherit" });

const formatted = format(
fs.readFileSync(file, "utf8"),
file,
config,
);

fs.writeFileSync(file, formatted);
});
};

(async () => {
await init();

const args = process.argv.slice(2);

try {
Expand Down
65 changes: 0 additions & 65 deletions misc/git-hooks/package-lock.json

This file was deleted.

2 changes: 2 additions & 0 deletions misc/git-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"type": "module",
"dependencies": {
"@wasm-fmt/clang-format": "^19.1.7",
"simple-git": "^3.27.0"
}
}
41 changes: 41 additions & 0 deletions misc/git-hooks/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@wasm-fmt/clang-format@^19.1.7":
version "19.1.7"
resolved "https://registry.yarnpkg.com/@wasm-fmt/clang-format/-/clang-format-19.1.7.tgz#2d2074934c2f4eb4381b4165b13a9b2a341647b9"
integrity sha512-MF5MDlffHoB3YYRVabCuy1rhOx8ReKey3BoqFxqVYgHZUeXO+KCC1tfhXtAL+4iYD+EmvUU5ZP3R+QmG57tVbg==

debug@^4.1.1, debug@^4.3.5:
version "4.4.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.0.tgz#2b3f2aea2ffeb776477460267377dc8710faba8a"
integrity sha512-6WTZ/IxCY/T6BALoZHaE4ctp9xm+Z5kY/pzYaCHRFeyVhojxlrm+46y68HA6hr0TcwEssoxNiDEUJQjfPZ/RYA==
dependencies:
ms "^2.1.3"

ms@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

simple-git@^3.27.0:
version "3.27.0"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.27.0.tgz#f4b09e807bda56a4a3968f635c0e4888d3decbd5"
integrity sha512-ivHoFS9Yi9GY49ogc6/YAi3Fl9ROnF4VyubNylgCkA+RVqLaKWnDSzXOVzya8csELIaWaYNutsEuAhZrtOjozA==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.5"
28 changes: 28 additions & 0 deletions misc/install_git_hooks.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# A utility script for Git Hooks installation
# Usage:
# cmake -P misc/install_git_hooks.cmake

set(REPO_ROOT "${CMAKE_CURRENT_LIST_DIR}/..")
get_filename_component(REPO_ROOT ${REPO_ROOT} REALPATH)

file(MAKE_DIRECTORY ${REPO_ROOT}/.git/hooks)

# Copy pre-commit hook
file(COPY ${REPO_ROOT}/misc/git-hooks/pre-commit DESTINATION ${REPO_ROOT}/.git/hooks)


# Make pre-commit hook executable if running on Unix
if(UNIX)
execute_process(COMMAND chmod +x ${REPO_ROOT}/.git/hooks/pre-commit)
endif()

# Install dependencies

include(${REPO_ROOT}/cmake/yarn.cmake)

yarn_execute_command(
WORKING_DIRECTORY ${REPO_ROOT}/misc/git-hooks
COMMAND install
)

message(STATUS "Git hooks installed successfully")

0 comments on commit 3f06cc0

Please sign in to comment.