Skip to content

Commit

Permalink
Setup GitHub actions (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
mars885 authored Mar 8, 2021
1 parent 246bcbb commit 94c7fdf
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 115 deletions.
23 changes: 23 additions & 0 deletions .github/ci-gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Copyright 2021 Paul Rybitskyi, [email protected]
#
# 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.
#

org.gradle.daemon=false
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx5120m
org.gradle.workers.max=2

# https://stackoverflow.com/questions/50138863/why-does-my-kotlin-gradle-build-die-with-exit-code-137
kotlin.compiler.execution.strategy=in-process
111 changes: 111 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
unit-test:
runs-on: [ubuntu-latest]
if: ${{ !contains(github.event.head_commit.message, '[ci skip]') }}

steps:
- name: Checkout Repo
uses: actions/checkout@v2

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

- name: Generate a Cache Key
run: ./checksum.sh checksum.txt

- name: Copy CI Gradle Properties
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties

- name: Cache Gradle Folders
uses: actions/cache@v2
with:
path: |
~/.gradle/caches/
~/.gradle/wrapper/
key: cache-gradle-${{ runner.os }}-${{ hashFiles('checksum.txt') }}
restore-keys: |
cache-gradle-${{ runner.os }}-
cache-gradle-
- name: Run all unit tests
run: ./gradlew test

instrumentation-test:
runs-on: [macos-latest]
if: ${{ !contains(github.event.head_commit.message, '[ci skip]') }}

steps:
- name: Checkout Repo
uses: actions/checkout@v2

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

- name: Generate a Cache Key
run: ./checksum.sh checksum.txt

- name: Copy CI Gradle Properties
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties

- name: Cache Gradle Folders
uses: actions/cache@v2
with:
path: |
~/.gradle/caches/
~/.gradle/wrapper/
key: cache-gradle-${{ runner.os }}-${{ hashFiles('checksum.txt') }}
restore-keys: |
cache-gradle-${{ runner.os }}-
cache-gradle-
- name: Run all instrumentation tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 30
target: google_apis # https://github.com/ReactiveCircus/android-emulator-runner/issues/106
script: ./gradlew connectedAndroidTest

assemble:
runs-on: [ubuntu-latest]
if: ${{ !contains(github.event.head_commit.message, '[ci skip]') }}

steps:
- name: Checkout Repo
uses: actions/checkout@v2

- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8

- name: Generate a Cache Key
run: ./checksum.sh checksum.txt

- name: Copy CI Gradle Properties
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties

- name: Cache Gradle Folders
uses: actions/cache@v2
with:
path: |
~/.gradle/caches/
~/.gradle/wrapper/
key: cache-gradle-${{ runner.os }}-${{ hashFiles('checksum.txt') }}
restore-keys: |
cache-gradle-${{ runner.os }}-
cache-gradle-
- name: Build Debug APK
run: ./gradlew assembleDebug
19 changes: 19 additions & 0 deletions .github/workflows/gradle-wrapper-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Validate Gradle Wrapper

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
validation:
runs-on: ubuntu-latest
if: ${{ !contains(github.event.head_commit.message, '[ci skip]') }}

steps:
- name: Checkout Repo
uses: actions/checkout@v2

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
23 changes: 23 additions & 0 deletions checksum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
RESULT_FILE=$1

if [ -f $RESULT_FILE ]; then
rm $RESULT_FILE
fi
touch $RESULT_FILE

checksum_file() {
echo $(openssl md5 $1 | awk '{print $2}')
}

FILES=()
while read -r -d ''; do
FILES+=("$REPLY")
done < <(find . -type f \( -name "build.gradle*" -o -name "Dependencies.kt" -o -name "gradle-wrapper.properties" \) -print0)

# Loop through files and append MD5 to result file
for FILE in ${FILES[@]}; do
echo $(checksum_file $FILE) >> $RESULT_FILE
done
# Now sort the file so that it is
sort $RESULT_FILE -o $RESULT_FILE
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.paulrybitskyi.gamedge.core.formatters

import com.paulrybitskyi.gamedge.core.providers.TimeFormat
import com.paulrybitskyi.gamedge.core.providers.TimeFormatProvider
import com.paulrybitskyi.gamedge.core.providers.TimestampProvider
import com.paulrybitskyi.gamedge.core.providers.TimeProvider
import com.paulrybitskyi.hiltbinder.BindType
import java.time.Instant
import java.time.LocalDateTime
Expand All @@ -38,7 +38,7 @@ interface ArticlePublicationDateFormatter {
@BindType
internal class ArticlePublicationDateFormatterImpl @Inject constructor(
private val relativeDateFormatter: RelativeDateFormatter,
private val timestampProvider: TimestampProvider,
private val timeProvider: TimeProvider,
private val timeFormatProvider: TimeFormatProvider
) : ArticlePublicationDateFormatter {

Expand All @@ -55,7 +55,10 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor(


override fun formatPublicationDate(timestamp: Long): String {
val dateTime = toLocalDateTime(timestamp)
val dateTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp),
ZoneId.systemDefault()
)

return if(shouldFormatAsRelativeDate(dateTime)) {
relativeDateFormatter.formatRelativeDate(dateTime)
Expand All @@ -65,16 +68,8 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor(
}


private fun toLocalDateTime(timestamp: Long): LocalDateTime {
return LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp),
ZoneId.systemDefault()
)
}


private fun shouldFormatAsRelativeDate(dateTime: LocalDateTime): Boolean {
val currentDateTime = toLocalDateTime(timestampProvider.getUnixTimestamp())
val currentDateTime = timeProvider.getCurrentDateTime()
val dayDiffCount = ChronoUnit.DAYS.between(dateTime, currentDateTime)

return (dayDiffCount == 0L)
Expand All @@ -90,7 +85,7 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor(


private fun getAbsoluteDatePattern(dateTime: LocalDateTime): String {
val currentDateTime = LocalDateTime.now()
val currentDateTime = timeProvider.getCurrentDateTime()
val yearDiffCount = ChronoUnit.YEARS.between(dateTime, currentDateTime).toInt()
val hasYearDiff = (yearDiffCount > 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package com.paulrybitskyi.gamedge.core.formatters

import com.paulrybitskyi.gamedge.core.R
import com.paulrybitskyi.gamedge.core.providers.StringProvider
import com.paulrybitskyi.gamedge.core.providers.TimestampProvider
import com.paulrybitskyi.gamedge.core.providers.TimeProvider
import com.paulrybitskyi.hiltbinder.BindType
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import javax.inject.Inject

Expand All @@ -36,16 +34,13 @@ interface RelativeDateFormatter {

@BindType
internal class RelativeDateFormatterImpl @Inject constructor(
private val timestampProvider: TimestampProvider,
private val timeProvider: TimeProvider,
private val stringProvider: StringProvider
) : RelativeDateFormatter {


override fun formatRelativeDate(dateTime: LocalDateTime): String {
val currentDateTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestampProvider.getUnixTimestamp()),
ZoneId.systemDefault()
)
val currentDateTime = timeProvider.getCurrentDateTime()
val isDateTimeInFuture = currentDateTime.isBefore(dateTime)

return if(isDateTimeInFuture) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2021 Paul Rybitskyi, [email protected]
*
* 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.
*/

package com.paulrybitskyi.gamedge.core.providers

import com.paulrybitskyi.hiltbinder.BindType
import java.time.LocalDateTime
import javax.inject.Inject


interface TimeProvider {

fun getCurrentDateTime(): LocalDateTime

}


@BindType
internal class TimeProviderImpl @Inject constructor() : TimeProvider {


override fun getCurrentDateTime(): LocalDateTime {
return LocalDateTime.now()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 Paul Rybitskyi, [email protected]
*
* 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.
*/

package com.paulrybitskyi.gamedge.core.utils

import java.time.LocalDateTime
import java.time.ZoneId


fun LocalDateTime.toMillis(zoneId: ZoneId = ZoneId.systemDefault()): Long {
return atZone(zoneId).toInstant().toEpochMilli()
}
Loading

0 comments on commit 94c7fdf

Please sign in to comment.