From 489d4296444bcd0d55ce75dc0ae630b590ce2728 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sat, 19 Nov 2016 08:55:46 +0100 Subject: [PATCH] refactor(ci): Split into scripts --- .travis.yml | 94 +++++-------------------------- common/travis/build.sh | 2 + common/travis/configure.sh | 9 +++ common/travis/environment.sh | 16 ++++++ common/travis/install_cmake.sh | 6 ++ common/travis/install_deps.sh | 6 ++ common/travis/install_libcpp.sh | 22 ++++++++ common/travis/summary.sh | 9 +++ common/travis/tests.sh | 12 ++++ src/modules/battery.cpp | 22 ++++++++ tests/unit_tests/utils/string.cpp | 3 +- 11 files changed, 121 insertions(+), 80 deletions(-) create mode 100755 common/travis/build.sh create mode 100755 common/travis/configure.sh create mode 100755 common/travis/environment.sh create mode 100755 common/travis/install_cmake.sh create mode 100755 common/travis/install_deps.sh create mode 100755 common/travis/install_libcpp.sh create mode 100755 common/travis/summary.sh create mode 100755 common/travis/tests.sh diff --git a/.travis.yml b/.travis.yml index 52bab5d7..a691c763 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,100 +18,33 @@ addons: env: global: + - BUILD_TESTS="ON" - BUILD_TYPE="Release" - LLVM_VERSION="3.8.1" - LLVM_URL="http://llvm.org/releases/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz" - LIBCXX_URL="http://llvm.org/releases/${LLVM_VERSION}/libcxx-${LLVM_VERSION}.src.tar.xz" - LIBCXXABI_URL="http://llvm.org/releases/${LLVM_VERSION}/libcxxabi-${LLVM_VERSION}.src.tar.xz" - CMAKE_URL="https://cmake.org/files/v3.6/cmake-3.6.2-Linux-x86_64.tar.gz" + - PYTHONPATH="/usr/lib/python2.7/dist-packages:${PYTHONPATH}" cache: directories: - ${TRAVIS_BUILD_DIR}/deps/cmake - ${TRAVIS_BUILD_DIR}/deps/llvm-3.8.1/install -before_install: - - sudo apt-add-repository -y "ppa:george-edison55/george-edison" - - sudo apt-get -qq update - - # Install packages : core - #--------------------------------------------------------------------------x - - sudo apt-get install -y cmake cmake-data libboost-dev - - # Install packages : xcb - #--------------------------------------------------------------------------x - - sudo apt-get install -y libxcb1-dev libxcb-util0-dev libxcb-image0-dev libxcb-randr0-dev libxcb-ewmh-dev libxcb-icccm4-dev xcb-proto python-xcbgen libfreetype6-dev - - # Install packages : optional - #--------------------------------------------------------------------------x - - sudo apt-get install -y i3-wm libiw-dev libasound2-dev libmpdclient-dev - install: - # Install dependencies in ${TRAVIS_BUILD_DIR}/deps - #--------------------------------------------------------------------------x - - DEPS_DIR="${TRAVIS_BUILD_DIR}/deps" - - LLVM_ROOT="${DEPS_DIR}/llvm-${LLVM_VERSION}" - - mkdir -p "${DEPS_DIR}" - - # Update python path to make sure we find the xcbgen module - #--------------------------------------------------------------------------x - - export PYTHONPATH="/usr/lib/python2.7/dist-packages:${PYTHONPATH}" - - # Install a later version of cmake - #--------------------------------------------------------------------------x - - | - cd "${DEPS_DIR}" - if [[ -z "$(ls -A ${DEPS_DIR}/cmake/bin 2>/dev/null)" ]]; then - mkdir -p cmake && travis_retry wget --no-check-certificate --quiet -O - "${CMAKE_URL}" | tar --strip-components=1 -xz -C cmake - fi - - export PATH="${DEPS_DIR}/cmake/bin:${PATH}" - - # Install LLVM libc++ - #--------------------------------------------------------------------------x - - | - if [[ "${CXX}" == "clang++" ]] && [[ -z "$(ls -A ${LLVM_ROOT}/install/include 2>/dev/null)" ]]; then - mkdir -p "${LLVM_ROOT}" "${LLVM_ROOT}/build" "${LLVM_ROOT}/projects/libcxx" "${LLVM_ROOT}/projects/libcxxabi" - travis_retry wget --quiet -O - "${LLVM_URL}" | tar --strip-components=1 -xJ -C "${LLVM_ROOT}" - travis_retry wget --quiet -O - "${LIBCXX_URL}" | tar --strip-components=1 -xJ -C "${LLVM_ROOT}/projects/libcxx" - travis_retry wget --quiet -O - "${LIBCXXABI_URL}" | tar --strip-components=1 -xJ -C "${LLVM_ROOT}/projects/libcxxabi" - (cd "${LLVM_ROOT}/build" && cmake .. -DCMAKE_CXX_COMPILER=clang++ && make cxxabi cxx -j2) - (cd "${LLVM_ROOT}/build/projects/libcxx" && make install) - (cd "${LLVM_ROOT}/build/projects/libcxxabi" && make install) - export CXXFLAGS="${CXXFLAGS} -I${LLVM_ROOT}/install/include" - export CXXFLAGS="${CXXFLAGS} -I${LLVM_ROOT}/install/include/c++/v1" - export CXXFLAGS="${CXXFLAGS} -stdlib=libc++" - export LDFLAGS="${LDFLAGS} -L${LLVM_ROOT}/install/lib" - export LDFLAGS="${LDFLAGS} -lc++" - export LDFLAGS="${LDFLAGS} -lc++abi" - export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${LLVM_ROOT}/install/lib" - fi - - # Set compiler's - #--------------------------------------------------------------------------x - - if [[ "${CXX}" == "clang++" ]]; then export CXX="clang++-3.8" CC="clang-3.8" CXXFLAGS="${CXXFLAGS} -Qunused-arguments"; fi - - if [[ "${CXX}" == "g++" ]]; then export CXX="g++-5" CC="gcc-5"; fi + - source ${TRAVIS_BUILD_DIR}/common/travis/environment.sh + - source ${TRAVIS_BUILD_DIR}/common/travis/install_deps.sh + - source ${TRAVIS_BUILD_DIR}/common/travis/install_cmake.sh + - source ${TRAVIS_BUILD_DIR}/common/travis/install_libcpp.sh before_script: - - eval "${CXX} --version" - - eval "${CC} --version" - - cmake --version - - mkdir -p "${TRAVIS_BUILD_DIR}/build" - - cd "${TRAVIS_BUILD_DIR}/build" - - cmake -DCMAKE_C_COMPILER="${CC}" -DCMAKE_CXX_COMPILER="${CXX}" -DCMAKE_CXX_FLAGS="${CXXFLAGS}" -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" -DBUILD_TESTS:BOOL=ON .. + - source ${TRAVIS_BUILD_DIR}/common/travis/summary.sh + - source ${TRAVIS_BUILD_DIR}/common/travis/configure.sh script: - - make - - | - for test in tests/unit_test.*; do - if [ -x $test ]; then - echo -n "${test##*/} "; - if $test; then - echo -e "\033[1;32mpassed\033[0m"; - else - echo -e "\033[1;31mfailed\033[0m"; - fi - fi - done + - source ${TRAVIS_BUILD_DIR}/common/travis/build.sh + - source ${TRAVIS_BUILD_DIR}/common/travis/tests.sh notifications: email: false @@ -119,5 +52,8 @@ notifications: channels: - "irc.freenode.org#polybar" template: - - "%{author} pushed #%{commit} \"%{commit_subject}\" -> %{repository_name}/%{branch}" - - "Build #%{build_number}: %{result} // Compare at %{compare_url}" + - "%{author} pushed #%{commit} \"%{commit_subject}\" to %{repository_name}/%{branch}" + - "Build #%{build_number} %{result} / %{build_url} (diff: %{compare_url})" + use_notice: true + on_success: change + on_failure: change diff --git a/common/travis/build.sh b/common/travis/build.sh new file mode 100755 index 00000000..8f27d3e5 --- /dev/null +++ b/common/travis/build.sh @@ -0,0 +1,2 @@ +#!/bin/sh +cd "${TRAVIS_BUILD_DIR}/build" && make diff --git a/common/travis/configure.sh b/common/travis/configure.sh new file mode 100755 index 00000000..d850e8f2 --- /dev/null +++ b/common/travis/configure.sh @@ -0,0 +1,9 @@ +#!/bin/sh +mkdir -p "${TRAVIS_BUILD_DIR}/build" +cd "${TRAVIS_BUILD_DIR}/build" || false +cmake \ + -DCMAKE_C_COMPILER="${CC}" \ + -DCMAKE_CXX_COMPILER="${CXX}" \ + -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \ + -DCMAKE_BUILD_TYPE="${BUILD_TYPE:-Release}" \ + -DBUILD_TESTS:BOOL="${BUILD_TESTS:-OFF}" .. diff --git a/common/travis/environment.sh b/common/travis/environment.sh new file mode 100755 index 00000000..eed2f1d8 --- /dev/null +++ b/common/travis/environment.sh @@ -0,0 +1,16 @@ +#!/bin/sh +export DEPS_DIR="${TRAVIS_BUILD_DIR}/deps" +export LLVM_ROOT="${DEPS_DIR}/llvm-${LLVM_VERSION}" + +mkdir -p "${DEPS_DIR}" +mkdir -p "${LLVM_ROOT}" + +# Update compiler flags +if [ "${CXX}" = "clang++" ]; then + export CXX="clang++-3.8" + export CC="clang-3.8" + export CXXFLAGS="${CXXFLAGS} -Qunused-arguments" +elif [ "${CXX}" = "g++" ]; then + export CXX="g++-5" + export CC="gcc-5" +fi diff --git a/common/travis/install_cmake.sh b/common/travis/install_cmake.sh new file mode 100755 index 00000000..a2842fa3 --- /dev/null +++ b/common/travis/install_cmake.sh @@ -0,0 +1,6 @@ +#!/bin/sh +if [ -z "$(ls -A "${DEPS_DIR}/cmake/bin" 2>/dev/null)" ]; then + mkdir -p cmake && travis_retry wget --no-check-certificate --quiet -O - "${CMAKE_URL}" | tar --strip-components=1 -xz -C cmake +fi + +export PATH="${DEPS_DIR}/cmake/bin:${PATH}" diff --git a/common/travis/install_deps.sh b/common/travis/install_deps.sh new file mode 100755 index 00000000..ddf3d92f --- /dev/null +++ b/common/travis/install_deps.sh @@ -0,0 +1,6 @@ +#!/bin/sh +sudo apt-add-repository -y "ppa:george-edison55/george-edison" +sudo apt-get -qq update +sudo apt-get install -y cmake cmake-data libboost-dev +sudo apt-get install -y libxcb1-dev libxcb-util0-dev libxcb-image0-dev libxcb-randr0-dev libxcb-ewmh-dev libxcb-icccm4-dev xcb-proto python-xcbgen libfreetype6-dev +sudo apt-get install -y i3-wm libiw-dev libasound2-dev libmpdclient-dev diff --git a/common/travis/install_libcpp.sh b/common/travis/install_libcpp.sh new file mode 100755 index 00000000..31ae3886 --- /dev/null +++ b/common/travis/install_libcpp.sh @@ -0,0 +1,22 @@ +#!/bin/sh +if [ "${CXX}" = "clang++" ]; then + if [ -z "$(ls -A "${LLVM_ROOT}/install/include" 2>/dev/null)" ]; then + mkdir -p "${LLVM_ROOT}" "${LLVM_ROOT}/build" "${LLVM_ROOT}/projects/libcxx" "${LLVM_ROOT}/projects/libcxxabi" + + travis_retry wget --quiet -O - "${LLVM_URL}" | tar --strip-components=1 -xJ -C "${LLVM_ROOT}" + travis_retry wget --quiet -O - "${LIBCXX_URL}" | tar --strip-components=1 -xJ -C "${LLVM_ROOT}/projects/libcxx" + travis_retry wget --quiet -O - "${LIBCXXABI_URL}" | tar --strip-components=1 -xJ -C "${LLVM_ROOT}/projects/libcxxabi" + + (cd "${LLVM_ROOT}/build" && cmake .. -DCMAKE_CXX_COMPILER=clang++ && make cxxabi cxx -j2) + (cd "${LLVM_ROOT}/build/projects/libcxx" && make install) + (cd "${LLVM_ROOT}/build/projects/libcxxabi" && make install) + fi + + export CXXFLAGS="${CXXFLAGS} -I${LLVM_ROOT}/install/include" + export CXXFLAGS="${CXXFLAGS} -I${LLVM_ROOT}/install/include/c++/v1" + export CXXFLAGS="${CXXFLAGS} -stdlib=libc++" + export LDFLAGS="${LDFLAGS} -L${LLVM_ROOT}/install/lib" + export LDFLAGS="${LDFLAGS} -lc++" + export LDFLAGS="${LDFLAGS} -lc++abi" + export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${LLVM_ROOT}/install/lib" +fi diff --git a/common/travis/summary.sh b/common/travis/summary.sh new file mode 100755 index 00000000..0c95ea3b --- /dev/null +++ b/common/travis/summary.sh @@ -0,0 +1,9 @@ +#!/bin/sh +echo "${CXX} --version" +eval "${CXX} --version" + +echo "${CC} --version" +eval "${CC} --version" + +echo "cmake --version" +cmake --version diff --git a/common/travis/tests.sh b/common/travis/tests.sh new file mode 100755 index 00000000..89fedc6d --- /dev/null +++ b/common/travis/tests.sh @@ -0,0 +1,12 @@ +#!/bin/sh +if [ "${BUILD_TESTS:-OFF}" = "ON" ]; then + for test in tests/unit_test.*; do + [ -x "$test" ] || continue + + if $test; then + printf "\033[1;32m%s\033[0m\n" "${test##*/} passed" + else + printf "\033[1;31m%s\033[0m\n" "${test##*/} failed" + fi + done +fi diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 68fda671..ab782c93 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -114,13 +114,35 @@ namespace modules { m_percentage = percentage; m_state = state; + string time_remaining; + auto rate = strtoul(file_util::get_contents("/sys/class/power_supply/BAT0/current_now").c_str(), nullptr, 10) / 1000; + // int rate{atoi(file_util::get_contents("/sys/class/power_supply/BAT0/current_now").c_str()) / 1000}; + // time_remaining = to_string(rate); + auto capacity = strtoul(file_util::get_contents("/sys/class/power_supply/BAT0/charge_now").c_str(), nullptr, 10) / 1000; + auto voltage = strtoul(file_util::get_contents("/sys/class/power_supply/BAT0/voltage_now").c_str(), nullptr, 10) / 1000; + capacity = capacity * 1000 / voltage; + rate = rate * 1000 / voltage; + printf("rate=%lu cap=%lu volt=%lu\n", rate, capacity, voltage); + int seconds = 3600 * capacity / rate; + + int hours = seconds / 3600; + seconds -= 3600 * hours; + int minutes = seconds / 60; + seconds -= 60 * minutes; + char buffer[9]{0}; + snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", hours, minutes, seconds); + time_remaining = buffer; + + if (m_label_charging) { m_label_charging->reset_tokens(); m_label_charging->replace_token("%percentage%", to_string(m_percentage) + "%"); + m_label_charging->replace_token("%time%", time_remaining); } if (m_label_discharging) { m_label_discharging->reset_tokens(); m_label_discharging->replace_token("%percentage%", to_string(m_percentage) + "%"); + m_label_discharging->replace_token("%time%", time_remaining); } if (m_label_full) { m_label_full->reset_tokens(); diff --git a/tests/unit_tests/utils/string.cpp b/tests/unit_tests/utils/string.cpp index 2a153134..8bae85c1 100644 --- a/tests/unit_tests/utils/string.cpp +++ b/tests/unit_tests/utils/string.cpp @@ -75,7 +75,8 @@ int main() { unsigned long hashB1{string_util::hash("Foo")}; unsigned long hashB2{string_util::hash("Bar")}; expect(hashA1 == hashA2); - expect(hashA1 != hashB1 != hashB2); + expect(hashA1 != hashB1); + expect(hashA1 != hashB2); expect(hashB1 != hashB2); }; }