简单的实践操作

前言

这两天一直在写算法,但是一个一个测试太麻烦了,并且也没有文档看,简直累人,所以就写了个action让它自动生成并部署到GithubPage上面去,爽了。

测试

引入依赖

1
2
3
4
5
6
7
8
dependencies {
......
testImplementation(
'org.assertj:assertj-core:3.12.2',
'org.junit.jupiter:junit-jupiter-api:5.4.2'
)
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
}

编写测试task

1
2
3
test {
useJUnitPlatform()
}

完成!看看效果吧~

test
test

测试成功~

文档生成

本次使用的是kotlin官方文档生成器dokka,内含有多种生成方案,并且可以集成到Gradle中,爽的。

引入依赖

这里以插件方式引入:

1
2
3
4
plugins {
......
id 'org.jetbrains.dokka' version '0.10.1'
}

编写task

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
dokka {
// 样式,具体看官方文档
outputFormat = 'javadoc'
outputDirectory = "$buildDir/dokka"
disableAutoconfiguration = false

cacheRoot = 'default'

configuration {
moduleName = 'doc'

// Use to include or exclude non public members.
includeNonPublic = false

// Do not output deprecated members. Applies globally, can be overridden by packageOptions
skipDeprecated = false

// Emit warnings about not documented members. Applies globally, also can be overridden by packageOptions
reportUndocumented = true

// Do not create index pages for empty packages
skipEmptyPackages = true

// This is a list of platform names that will be shown in the final result. See the "Platforms" section of this readme
targets = ["JVM"]

// Platform used for code analysis. See the "Platforms" section of this readme
platform = "JVM"

sourceRoot {
// 源文件目录
path = "src"
}

sourceLink {
// Unix based directory relative path to the root of the project (where you execute gradle respectively).
path = "src/main/kotlin" // or simply "./"

// URL showing where the source code can be accessed through the web browser
url = "https://github.com/xmmmmmovo/Algorithms4thEditionKotlinSolutions/blob/master/src/main/kotlin" //remove src/main/kotlin if you use "./" above

// 指定行数前缀,github是#L
lineSuffix = "#L"
}

// 用于链接java官方文档
jdkVersion = 8

// Disable linking to online kotlin-stdlib documentation
noStdlibLink = false

// Disable linking to online JDK documentation
noJdkLink = false
}
}

完成!看看效果~

成功~

自动化

每次都运行脚本太累了,干脆直接用CI自动跑了,这里用的是GithubAction,具体如何使用可以看:如何利用GitHubAction和GithubPage部署React应用 这篇,这里只写出yml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# workflow name
name: Test Workflow

on:
push:
branches:
- master

jobs:
test:
name: Kotlin Tests
runs-on: ubuntu-latest
env:
TZ: Asia/Shanghai

steps:
# check it to your workflow can access it
# from: https://github.com/actions/checkout
- name: Checkout Repository master branch
uses: actions/checkout@master

# 验证wrapper
- name: validate wrapper
uses: gradle/wrapper-validation-action@v1

# 安装jdk
- name: Set up JDK 8
uses: actions/setup-java@v1
with:
java-version: 1.8

# 缓存
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle

# 给权限
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# 测试程序
- name: Test with gradle
run: ./gradlew test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# workflow name
name: Doc Workflow

on:
release:
types: [published]
branches:
- master

jobs:
test:
name: Kotlin Documentation Generation
runs-on: ubuntu-latest
env:
TZ: Asia/Shanghai

steps:
# check it to your workflow can access it
# from: https://github.com/actions/checkout
- name: Checkout Repository master branch
uses: actions/checkout@master

# 验证wrapper
- name: validate wrapper
uses: gradle/wrapper-validation-action@v1

# 安装jdk
- name: Set up JDK 8
uses: actions/setup-java@v1
with:
java-version: 1.8

# 缓存
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle

# 给权限
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# 缓存
# 暂时还未找到hash值的解决方案,找到了请pr
# - name: Cache Dokka
# uses: actions/cache@v2
# with:
# path: ~/.cache/dokka
# key: ${{ runner.os }}-dokka-${{ hashFiles('**/*') }}
# restore-keys: ${{ runner.os }}-dokka

# 生成文档
- name: Generate documentation
run: ./gradlew dokka

# 部署到ghpage
- name: Deploy to gh-pages
uses: peaceiris/actions-gh-pages@v3
with:
deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
publish_dir: ./build/dokka

完成!测试一下~

成功~

结语

爽到了爽到了~

GitHub地址