Skip to content

Instantly share code, notes, and snippets.

@realyze
Created December 8, 2017 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realyze/5c118df24ee418eae9523fd4b7009792 to your computer and use it in GitHub Desktop.
Save realyze/5c118df24ee418eae9523fd4b7009792 to your computer and use it in GitHub Desktop.
FILES=$(git diff --name-only `git merge-base HEAD green`)
FORMAT_BIN=${HOME}/.ideaformatter/Contents/bin/format.sh
if [[ ! -f ${FORMAT_BIN} ]]; then
echo 'Please symlink idea into $HOME/.ideaformatter';
exit 1;
fi
FILES_ARR=($FILES)
ABS_FILES=( "${FILES_ARR[@]/#/`cd .. && pwd`/}" )
echo "Formatting ${ABS_FILES[@]}"
SETTINGS_FILE=`pwd`/.idea/codeStyleSettings.xml
cd ~/.ideaformatter/Contents/bin && ./format.sh -s ${SETTINGS_FILE} ${ABS_FILES[@]}
@realyze
Copy link
Author

realyze commented Dec 8, 2017

You'll need to:

  1. Symlink IntelliJ to ~/.ideaformatter (e.g. ln -s ${HOME}/Library/Application\ Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/173.3622.25/IntelliJ\ IDEA\ 2017.3\ EAP.app ${HOME}/.ideaformatter (necessary because spaces in path seem to trip up intellij)
  2. Run this script with /bin/bash from Canva web/ dir.

@benjaminjt
Copy link

benjaminjt commented Dec 8, 2017

Tweaked it a little, WDYT?

#!/usr/bin/env bash
CANVA_WEB_DIR=${HOME}/github/Canva/canva/web
SETTINGS_FILE=${CANVA_WEB_DIR}/.idea/codeStyleSettings.xml
INTELLIJ_DIR=${HOME}/Library/Application\ Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/
INTELLIJ_APP=$(find "$INTELLIJ_DIR" -name '*.app' | sort | tail -n 1)
FORMAT_BIN=${INTELLIJ_APP}/Contents/bin/format.sh

if [[ ! -f ${FORMAT_BIN} ]]; then
  echo 'Could not find IntelliJ formatter';
  exit 1;
fi

cd "$CANVA_WEB_DIR"

FILES=$(git diff --name-only `git merge-base HEAD green` -- '*.ts' '*.tsx')

if [[ ! ${FILES} ]]; then
  echo 'No files to format';
  exit 0;
fi

FILES_ARR=($FILES)
ABS_FILES=( "${FILES_ARR[@]/#/`cd .. && pwd`/}" )

echo "Formatting the following files:"
echo

for FILE in ${FILES_ARR[@]}
do
  echo $FILE
done

echo
echo "Starting..."
echo

/bin/bash "$FORMAT_BIN" -s ${SETTINGS_FILE} ${ABS_FILES[@]}

@alextreppass
Copy link

alextreppass commented Dec 8, 2017

The following works better for me -- it only formats changed files (checked out + staged). I was finding the branch-based approach touch to use.

I have IJ installed standalone and not as part of the toolbox.

Assumes $CANVA points to your canva dir (which can be set up in your ~/.bashrc, sourced from ~/.bash_profile):

SETTINGS_FILE=$CANVA/.idea/codeStyleSettings.xml
FILES=$(git diff --name-only HEAD')
FILES_ARR=($FILES)
ABS_FILES=( "${FILES_ARR[@]/#/`cd .. && pwd`/}" )
(cd /Applications/IntelliJ\ IDEA.app/Contents/bin && ./format.sh -s $SETTINGS_FILE ${ABS_FILES[@]})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment