Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(create_package)
export(use_appveyor)
export(use_build_ignore)
export(use_clang_format)
export(use_code_of_conduct)
export(use_coverage)
export(use_cran_badge)
Expand Down
42 changes: 42 additions & 0 deletions R/clang-format.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Use clang-format
#'
#' Creates \code{src/.clang-format} and adds a before_install step to
#' `.travis.yml` to check the format is followed.
#'
#' @inheritParams use_template
#' @export
use_clang_format <- function(base_path = ".") {
if (!uses_travis(base_path)) {
stop("You must use_travis() first", call. = FALSE)
}

use_directory(
"travis",
ignore = TRUE,
base_path = base_path)

use_template(
"check_format.sh",
file.path("travis", "check_format.sh"),
ignore = TRUE,
base_path = base_path)
Sys.chmod(file.path("travis", "check_format.sh"), "0744")

use_template(
"clang-format",
file.path("src", ".clang-format"),
ignore = TRUE,
base_path = base_path)

message("Next run: `travis/check_format.sh` and commit the result")
message("Then:")
message("* Add to `.travis.yml`:\n",
"addons:\n",
" apt:\n",
" sources:\n",
" - ubuntu-toolchain-r-test\n",
" - llvm-toolchain-precise-3.9\n",
" packages:\n",
" - clang-format-3.9\n",
"before_install: travis/check_format.sh\n")
}
13 changes: 13 additions & 0 deletions inst/templates/check_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

shopt -s extglob

clang_format=$(command -v clang-format)
if $clang_format --version | grep -q '3.4'; then
clang_format=$(command -v clang-format-3.9)
fi
if [ -z "$clang_format" ]; then
echo "Could not find recent version of clang-format"
exit 1
fi
"$clang_format" -i src/!(RcppExports).@(c|h|cpp) && git diff-files -U --exit-code
11 changes: 11 additions & 0 deletions inst/templates/clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
Language: Cpp
BasedOnStyle: LLVM
Standard: Cpp03
AlignAfterOpenBracket: AlwaysBreak
AllowShortBlocksOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
PointerAlignment: Left
...
15 changes: 15 additions & 0 deletions man/use_clang_format.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.