[CI] Adds formatter to CI (#6272)
authormustiikhalil <mustii@mmk.one>
Fri, 11 Dec 2020 18:46:10 +0000 (21:46 +0300)
committerGitHub <noreply@github.com>
Fri, 11 Dec 2020 18:46:10 +0000 (10:46 -0800)
* Adds formatters CI

Adds Error message & setup formatting like cpp

Adds Swift

Adds typescript

Adds python

tests yarn

Adds format.md

* Removes unneeded scripts + moves install script to install phase

* Adds format.md content

* Adds cpp to the formatter.md and fixes ci

* Adds cpp to formatter ci

.travis.yml
.travis/format_check.sh [new file with mode: 0644]
.travis/format_install.sh [new file with mode: 0644]
Formatters.md [new file with mode: 0644]

index 17d3cc0..fb174fc 100644 (file)
@@ -200,3 +200,11 @@ matrix:
       script:
         - cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .; make; export PATH="$PATH:${PWD}"
         - cd android; ./gradlew clean build
+
+    - language: generic
+      os: linux
+      install:
+        - bash .travis/format_install.sh
+        
+      script:
+        - bash .travis/format_check.sh
diff --git a/.travis/format_check.sh b/.travis/format_check.sh
new file mode 100644 (file)
index 0000000..cae8a29
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/bash
+#
+# Copyright 2020 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -e
+
+# HACKY solution to make nodejs work.
+source ~/.nvm/nvm.sh
+nvm alias default node
+nvm use default
+
+sh src/clang-format-git.sh
+
+node_modules/.bin/eslint ts/** --ext .ts --quiet --fix
+
+#PYTHON IS DISABLED UNTIL WE CREATE A .pylintrc FILE FOR IT
+pylint python/** --disable=all
+
+swiftformat --config swift.swiftformat .
+
+if ! git diff --quiet; then
+  echo >&2
+  echo "ERROR: ********************************************************" >&2
+  echo "ERROR: The following differences were found after running" >&2
+  echo "ERROR: .travis/format_check.sh script. Maybe you forgot to format" >&2
+  echo "ERROR: the code after making changes? please check Formatters.md" >&2
+  echo "ERROR: ********************************************************" >&2
+  echo >&2
+  git diff --binary --exit-code
+fi
\ No newline at end of file
diff --git a/.travis/format_install.sh b/.travis/format_install.sh
new file mode 100644 (file)
index 0000000..41a3fed
--- /dev/null
@@ -0,0 +1,82 @@
+#!/bin/bash
+#
+# Copyright 2020 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -e
+set -x
+
+# install devtools
+install_languages() {
+  sudo apt update
+
+  # Install nodeJS and yarn
+  wget https://raw.githubusercontent.com/creationix/nvm/v0.31.0/nvm.sh -O ~/.nvm/nvm.sh
+  source ~/.nvm/nvm.sh
+  nvm install node
+  node --version
+  curl -o- -L https://yarnpkg.com/install.sh | bash
+  export PATH="$HOME/.yarn/bin:$PATH"
+  yarn config set prefix ~/.yarn -g
+  export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
+
+  # Install swift
+  sudo apt-get install \
+          binutils \
+          git \
+          libc6-dev \
+          libcurl3 \
+          libedit2 \
+          libgcc-5-dev \
+          libpython2.7 \
+          libsqlite3-0 \
+          libstdc++-5-dev \
+          libxml2 \
+          pkg-config \
+          tzdata \
+          zlib1g-dev
+
+  SWIFT_URL=https://swift.org/builds/swift-5.3.1-release/ubuntu1604/swift-5.3.1-RELEASE/swift-5.3.1-RELEASE-ubuntu16.04.tar.gz
+  curl -fSsL "$SWIFT_URL" -o swift.tar.gz
+
+  mkdir ~/swiftbuild
+  tar -xvzf swift.tar.gz -C ~/swiftbuild
+
+  export PATH="~/swiftbuild/swift-5.3.1-RELEASE-ubuntu16.04/usr/bin:$PATH"
+
+  swift --version
+  yarn -v
+  node -v
+}
+
+install_formatters() {
+  # installing swift formatter
+  git clone --depth 1 --branch 0.47.4 https://github.com/nicklockwood/SwiftFormat.git
+  cd SwiftFormat
+  swift build -c release
+  sudo cp .build/release/swiftformat /usr/local/bin/swiftformat
+  cd ..
+
+  which yarn
+  which node
+  yarn -v
+  node -v
+
+  yarn install
+  pip install pylint
+}
+
+install_languages
+export PATH="~/swift/swift/usr/bin:$PATH"
+install_formatters
\ No newline at end of file
diff --git a/Formatters.md b/Formatters.md
new file mode 100644 (file)
index 0000000..18a51c4
--- /dev/null
@@ -0,0 +1,22 @@
+# Format Guidelines
+
+If you are interesting in contributing to the flatbuffers project, please take a second to read this document. Each language has it's own set of rules, that are defined in their respective formatter/linter documents.
+
+# Notes
+
+- Run the linter on the language you are working on before making a Pull Request.
+- DONT format/lint the generated code.
+
+# Languages
+
+## C++
+
+C++ uses `clang-format` as it's formatter. Run the following script `sh src/clang-format-git.sh`, and it should style the C++ code according to [google style guide](https://google.github.io/styleguide/cppguide.html).
+
+## Swift
+
+Swift uses swiftformat as it's formatter. Take a look at [how to install here](https://github.com/nicklockwood/SwiftFormat/blob/master/README.md#how-do-i-install-it). Run the following command `swiftformat --config swift.swiftformat .` in the root directory of the project
+
+## Typescript
+
+Typescript uses eslint as it's linter. Take a look at [how to install here](https://eslint.org/docs/user-guide/getting-started). Run the following command `eslint ts/** --ext .ts` in the root directory of the project
\ No newline at end of file