diff --git a/Makefile.in b/Makefile.in index cf7480702..305c12826 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,6 +13,7 @@ SED = @SED@ ERL = @ERL@ EPMD = @EPMD@ IEX = @IEX@ +EMACS = @EMACS@ INSTALLUSER=@INSTALLUSER@ INSTALLGROUP=@INSTALLGROUP@ @@ -266,11 +267,15 @@ _build/edoc/logo.png: edoc_compile #' format / indent # +.SILENT: format indent + +FORMAT_LOG=/tmp/ejabberd-format.log + format: - tools/rebar3-format.sh $(REBAR3) + tools/rebar3-format.sh $(FORMAT_LOG) $(REBAR3) indent: - tools/emacs-indent.sh + tools/emacs-indent.sh $(FORMAT_LOG) $(EMACS) #. #' copy-files @@ -714,7 +719,7 @@ help: @echo " translations Extract translation files" @echo " TAGS Generate tags file for text editors" @echo "" - @echo " format Format source code using rebar3_format" + @echo " format Format source code using efmt [rebar3]" @echo " indent Indent source code using erlang-mode [emacs]" @echo "" @echo " dialyzer Run Dialyzer static analyzer" diff --git a/configure.ac b/configure.ac index b91595dc5..d3b549ea2 100644 --- a/configure.ac +++ b/configure.ac @@ -88,6 +88,8 @@ AC_PATH_PROG([ESCRIPT], [escript], [], [$ERLANG_ROOT_DIR/bin]) #locating make AC_CHECK_PROG([MAKE], [make], [make], []) +AC_PATH_TOOL(EMACS, emacs, , [${extra_erl_path}$PATH]) + if test "x$ESCRIPT" = "x"; then AC_MSG_ERROR(['escript' was not found]) fi diff --git a/rebar.config b/rebar.config index e3d42edce..a35b3bf0f 100644 --- a/rebar.config +++ b/rebar.config @@ -164,7 +164,7 @@ {branch, "consolidation_fix"}}} }]}}. {if_rebar3, {project_plugins, [configure_deps, - {if_var_true, tools, rebar3_format}, + {if_var_true, tools, rebar3_efmt}, {if_var_true, tools, {rebar3_lint, "4.1.1"}} ]}}. {if_not_rebar3, {plugins, [ diff --git a/tools/emacs-indent.sh b/tools/emacs-indent.sh index f3caecf4c..73d84470d 100755 --- a/tools/emacs-indent.sh +++ b/tools/emacs-indent.sh @@ -1,18 +1,29 @@ -#!/bin/bash +#!/bin/sh # To indent and remove tabs, surround the piece of code with: # %% @indent-begin # %% @indent-end # +# Install Emacs and erlang-mode. For example in Debian: +# apt-get install emacs erlang-mode +# # Then run: # make indent # -# Please note this script only indents the first occurrence. +# Please note this script only indents the first occurrence per file -FILES=$(git grep --name-only @indent-begin src/) +FILES=$(git grep --name-only @indent-begin include/ src/) +LOG=${1:-/tmp/ejabberd-format.log} +EMACS=${2:-emacs} + +if [ ! "$EMACS" ] || [ ! -x "$EMACS" ] +then + echo "==> Cannot indent source code because Emacs is not installed" + exit 1 +fi for FILENAME in $FILES; do - echo "==> Indenting $FILENAME..." + echo "==> Indenting $FILENAME..." >>$LOG emacs -batch $FILENAME \ -f "erlang-mode" \ --eval "(goto-char (point-min))" \ @@ -23,5 +34,13 @@ for FILENAME in $FILES; do --eval "(erlang-indent-region begin end)" \ --eval "(untabify begin end)" \ -f "delete-trailing-whitespace" \ - -f "save-buffer" + -f "save-buffer" >>$LOG 2>&1 done + +grep -q 'Error' $LOG \ + && cat $LOG +grep -q 'Error: void-function (erlang-mode)' $LOG \ + && echo \ + && echo "==> Maybe you need to install erlang-mode system package" \ + && exit 1 +rm $LOG diff --git a/tools/rebar3-format.sh b/tools/rebar3-format.sh index 2181b6870..2126f0a57 100755 --- a/tools/rebar3-format.sh +++ b/tools/rebar3-format.sh @@ -1,41 +1,16 @@ -#!/bin/bash +#!/bin/sh -# To start formatting a file, add a line that contains: -# @format-begin -# Formatting in that file can later be disabled adding another line with: -# @format-end -# -# It can be reenabled again later in the file. -# -# Finally, call: make format +# To format the source code, simply run: +# make format -REBAR=$1 +LOG=${1:-/tmp/ejabberd-format.log} +REBAR3=${2:-rebar3} -FORMAT() -{ -FPATH=$1 -ERLS=$(git grep --name-only @format-begin "$FPATH"/) +$REBAR3 efmt -w --parallel >$LOG 2>&1 -for ERL in $ERLS; do - perl -n -e 'sub o { open(OUT, ">", sprintf("%s-format-%02d", $f, $n++));}; BEGIN{($f)=@ARGV;o()}; o() if /\@format-/; print OUT $_;' $ERL -done - -EFMTS=$(find "$FPATH"/*-format-* -type f -exec grep --files-with-matches "@format-begin" '{}' ';') -EFMTS2="" -for EFMT in $EFMTS; do - EFMTS2="$EFMTS2 --files $EFMT" -done -$REBAR format $EFMTS2 - -for ERL in $ERLS; do - SPLITS=$(find $ERL-format-* -type f) - rm $ERL - for SPLIT in $SPLITS; do - cat $SPLIT >> $ERL - rm $SPLIT - done -done -} - -FORMAT src -FORMAT test +if ! grep -q 'All files were formatted correctly' $LOG +then + cat $LOG + exit 1 +fi +rm $LOG