Skip to content

Commit 06f17d4

Browse files
author
Krystian Panek
authored
Merge pull request #1 from Cognifide/base-impl
Base implementation
2 parents b94876b + 2660e3a commit 06f17d4

File tree

14 files changed

+226
-8
lines changed

14 files changed

+226
-8
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ When using argument `--config-path` then it is also needed to have at least file
6868
Use command, to run Lighthouse CI by using:
6969

7070
* default suite and its base URL: `sh gradlew lighthouseRun`,
71-
* only desired suite by name: `sh gradlew lighthouseRun -Plighthouse.suite=site.demo` (if suite by base URL not found, suite named `default` will be used),
71+
* only desired suite by name: `sh gradlew lighthouseRun -Plighthouse.suite=site.demo` (if suite by name not found, suite named `default` will be used),
7272
* only desired suite by base URL: `sh gradlew lighthouseRun -Plighthouse.baseUrl=http://example.com`,
7373
* any suite with any base URL: `sh gradlew lighthouseRun -Plighthouse.baseUrl=http://any-host.com -Plighthouse.suite=site.live` (base URL defined in suite will be overridden).
7474

75+
Note that after running one of above commands first time, new files might be generated:
76+
77+
* *package.json*
78+
* *yarn.lock*
79+
80+
This is indented behavior - __remember to save these files in VCS__.
81+
7582
## Building
7683

7784
1. Clone this project using command `git clone https://github.com/Cognifide/gradle-lighthouse-plugin.git`

build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ plugins {
44
id("org.jetbrains.kotlin.jvm")
55
}
66

7+
defaultTasks("build", "publishToMavenLocal")
8+
group = "com.cognifide.gradle"
9+
710
repositories {
811
jcenter()
12+
gradlePluginPortal()
913
}
1014

1115
dependencies {
1216
implementation(platform("org.jetbrains.kotlin:kotlin-bom:${properties["kotlin.version"]}"))
1317
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
18+
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.8")
19+
implementation("commons-io:commons-io:2.6")
20+
implementation("com.github.node-gradle:gradle-node-plugin:2.1.1")
21+
1422
testImplementation("org.jetbrains.kotlin:kotlin-test")
1523
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
1624
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
version=1.0.0
12
kotlin.version=1.3.50

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ pluginManagement {
44
}
55
}
66

7-
rootProject.name = "gradle-lighthouse-plugin"
7+
rootProject.name = "lighthouse-plugin"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.cognifide.gradle.lighthouse
2+
3+
class LighthouseException : Exception {
4+
5+
constructor(message: String): super(message)
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.cognifide.gradle.lighthouse
2+
3+
import com.cognifide.gradle.lighthouse.config.Config
4+
import com.cognifide.gradle.lighthouse.runner.Runner
5+
import com.cognifide.gradle.lighthouse.runner.RunUnit
6+
import org.gradle.api.Project
7+
import java.io.File
8+
import com.cognifide.gradle.lighthouse.Utils.prop
9+
10+
class LighthouseExtension(val project: Project) {
11+
12+
var configFile = project.file(project.prop("lighthouse.configFile") ?: "lighthouse/suites.json")
13+
14+
val config get() = Config.from(configFile)
15+
16+
var suiteName = project.prop("lighthouse.suite")
17+
18+
var baseUrl = project.prop("lighthouse.baseUrl")
19+
20+
var reportFileRule: (RunUnit) -> File = {
21+
project.file("build/lighthouse/${it.suite.name}/${it.url.substringAfter("://").replace("/", "_")}")
22+
}
23+
24+
var workingDir = project.file(project.prop("lighthouse.workingDir") ?: "build/lighthouse")
25+
26+
fun <T> runner(consumer: Runner.() -> T) = Runner(this).run(consumer)
27+
28+
companion object {
29+
fun get(project: Project) = project.extensions.getByType(LighthouseExtension::class.java)
30+
}
31+
}
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
package com.cognifide.gradle.lighthouse
22

3-
import org.gradle.api.Project
3+
import com.cognifide.gradle.lighthouse.tasks.LighthouseRun
4+
import com.moowork.gradle.node.NodeExtension
5+
import com.moowork.gradle.node.yarn.YarnInstallTask
46
import org.gradle.api.Plugin
7+
import org.gradle.api.Project
8+
import java.io.File
59

610
open class LighthousePlugin: Plugin<Project> {
711

812
override fun apply(project: Project) {
9-
project.tasks.register("lighthouseRun") { task ->
10-
task.doLast {
11-
println("Hello from plugin 'com.cognifide.lighthouse'")
13+
project.plugins.apply("com.github.node-gradle.node")
14+
15+
val node = NodeExtension.get(project)
16+
17+
node.apply {
18+
version = "10.16.3"
19+
yarnVersion = "1.19.0"
20+
download = true
21+
}
22+
23+
val yarnInstall = project.tasks.named(YarnInstallTask.NAME, YarnInstallTask::class.java) {
24+
File(node.nodeModulesDir, "package.json").apply {
25+
if (!exists()) {
26+
writeText("""
27+
{
28+
"license": "UNLICENSED",
29+
"devDependencies": {
30+
"lighthouse-ci": "^1.10.0"
31+
}
32+
}
33+
""".trimIndent())
34+
}
1235
}
1336
}
37+
38+
project.extensions.add("lighthouse", LighthouseExtension(project))
39+
40+
project.tasks.register("lighthouseRun", LighthouseRun::class.java) {
41+
it.dependsOn(yarnInstall)
42+
}
1443
}
1544
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cognifide.gradle.lighthouse
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import org.apache.commons.io.FilenameUtils
5+
import org.gradle.api.Project
6+
7+
object Utils {
8+
9+
fun Project.prop(name: String) = findProperty(name) as String?
10+
11+
fun <T> fromJson(json: String, clazz: Class<T>): T {
12+
return ObjectMapper().readValue(json, clazz)
13+
}
14+
15+
fun wildcardMatch(value: String?, matcher: String?): Boolean {
16+
if (matcher.isNullOrBlank()) {
17+
return false
18+
}
19+
20+
return matcher.split(",").any { FilenameUtils.wildcardMatch(value.orEmpty(), it) }
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cognifide.gradle.lighthouse.config
2+
3+
import com.cognifide.gradle.lighthouse.LighthouseException
4+
import com.cognifide.gradle.lighthouse.Utils
5+
import java.io.File
6+
7+
class Config {
8+
9+
var suites: List<Suite> = listOf()
10+
11+
companion object {
12+
13+
fun from(file: File) = file.run {
14+
if (!exists()) {
15+
throw LighthouseException("Lighthouse configuration file does not exists: $file!")
16+
}
17+
18+
Utils.fromJson(readText(), Config::class.java)
19+
}
20+
}
21+
}
22+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.cognifide.gradle.lighthouse.config
2+
3+
class Suite {
4+
5+
lateinit var name: String
6+
7+
var baseUrl: String? = null
8+
9+
var paths: List<String> = listOf()
10+
11+
var args: List<String> = listOf()
12+
}

0 commit comments

Comments
 (0)