Skip to content

Commit

Permalink
Update project specs to use samples from GH
Browse files Browse the repository at this point in the history
  • Loading branch information
tresat committed Aug 15, 2024
1 parent df6959e commit de5fc2e
Show file tree
Hide file tree
Showing 52 changed files with 903 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.gradle.integtests.fixtures

import org.gradle.internal.nativeintegration.console.TestOverrideConsoleDetector
import org.gradle.plugin.management.internal.autoapply.AutoAppliedPluginHandler
import org.gradle.test.fixtures.AbstractSpecification
import org.gradle.testkit.runner.GradleRunner

/**
* Base class for tests that generate, build, and validate (via run, or something else) included project init specifications.
*/
abstract class AbstractProjectInitSpecification extends AbstractSpecification {
private static final String DECLARATIVE_PROTOTYPE_VERSION = "0.1.11"

protected File projectDir = file("new-project").tap { mkdirs() }

abstract String getPluginId()

// TODO: add project type method here, specify type of project to be generated (need to update Gradle wrapper to new nightly containing getType() first)

def "can generate project from init project spec"() {
when:
runInitWithPluginAsInitProjectSpecSupplier()

then:
canBuildGeneratedProject()

and:
validateGeneratedProjectRuns()
}

protected void runInitWithPluginAsInitProjectSpecSupplier() {
def args = ["init",
"-D${AutoAppliedPluginHandler.INIT_PROJECT_SPEC_SUPPLIERS_PROP}=$pluginId:$DECLARATIVE_PROTOTYPE_VERSION",
"-D${TestOverrideConsoleDetector.INTERACTIVE_TOGGLE}=true"] as String[]

result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments(args)
.withPluginClasspath()
.withDebug(true)
.forwardOutput()
.build()
}

protected void canBuildGeneratedProject() {
result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments("build")
.withDebug(true)
.forwardOutput()
.build()
}

protected void validateGeneratedProjectRuns() {
result = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments(":app:run")
.forwardOutput()
.build()

assert result.output.contains("Hello World!")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ testing {

dependencies {
implementation(project(":internal-testing-utils"))
implementation(project())
}
}

Expand All @@ -49,6 +48,8 @@ testing {
}

gradlePlugin {
testSourceSets(project.sourceSets.getByName("integTest"))

plugins {
create("android-library") {
id = "org.gradle.experimental.android-library"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.gradle.api.experimental.android

import org.gradle.integtests.fixtures.AbstractProjectInitSpecification
import org.gradle.testkit.runner.GradleRunner

class AndroidApplicationInitProjectSpec extends AbstractProjectInitSpecification {
@Override
String getPluginId() {
return "org.gradle.experimental.android-ecosystem"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.gradle.api.experimental.buildinit;

import org.gradle.buildinit.projectspecs.InitProjectGenerator;
import org.gradle.buildinit.projectspecs.InitProjectSource;
import org.gradle.buildinit.projectspecs.InitProjectSpec;

import java.util.List;

/**
* A {@link InitProjectSource} of project specifications for Android projects.
*/
@SuppressWarnings("UnstableApiUsage")
public final class AndroidProjectSource implements InitProjectSource {
@Override
public List<InitProjectSpec> getProjectSpecs() {
return List.of(
new StaticProjectSpec("android-application", "Declarative Android Application Project")
);
}

@Override
public Class<? extends InitProjectGenerator> getProjectGenerator() {
return StaticProjectGenerator.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* This package uses the experimental API for dynamically providing project specifications for Gradle's {@code init} build task.
*/
@org.gradle.api.NonNullApi
package org.gradle.api.experimental.buildinit;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.api.experimental.buildinit.AndroidProjectSource
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# declarative-samples-android-app
A sample Android application written in the Declarative Gradle DSL, using the prototype Declarative Gradle `androidApplication` Software Type defined in the `org.gradle.experimental.android-ecosystem` ecosystem plugin.

## Building and Running

This sample shows the definition of a multiproject Android application implemented using Kotlin 1.9.23 source code.
The project is the result of reproducing the project produced by the `gradle init` command in Gradle 8.9 as an Android project.

To build the project without running, use:

```shell
./gradlew build
```

To run the application, first install it on a connected Android device using:

```shell
:app:installDebug
```

Then search for "Sample Declarative Gradle Android App" and launch app to see a hello world message.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
androidApplication {
namespace = "org.example.app"

dependencies {
implementation("org.apache.commons:commons-text:1.11.0")
implementation(project(":utilities"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:label="@string/app_name">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.example.app

import org.apache.commons.text.WordUtils

import org.example.list.LinkedList
import org.example.utilities.SplitUtils
import org.example.utilities.StringUtils

import android.widget.TextView
import android.os.Bundle
import android.app.Activity

class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val textView = findViewById(R.id.textView) as TextView
textView.text = buildMessage()
}

private fun buildMessage(): String {
val tokens: LinkedList
tokens = SplitUtils.split(MessageUtils.message())
val result: String = StringUtils.join(tokens)
return WordUtils.capitalize(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.example.app

internal object MessageUtils {
fun message() = "Hello World!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Sample Declarative Gradle Android App</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example.app

import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.assertEquals

class MessageUtilsTest {
@Test
fun testGetMessage() {
assertEquals("Hello World!", MessageUtils.message())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
androidLibrary {
namespace = "org.gradle.experimental.android.list"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.example.list

class LinkedList {
private var head: Node? = null

fun add(element: String?) {
val newNode = Node(element)

val it = tail(head)
if (it == null) {
head = newNode
} else {
it.next = newNode
}
}

fun remove(element: String): Boolean {
var result = false
var previousIt: Node? = null
var it: Node?
it = head
while (!result && it != null) {
if (0 == element.compareTo(it.data!!)) {
result = true
unlink(previousIt, it)
break
}
previousIt = it
it = it.next
}

return result
}

private fun unlink(previousIt: Node?, currentIt: Node) {
if (currentIt === head) {
head = currentIt.next
} else {
previousIt!!.next = currentIt.next
}
}

fun size(): Int {
var size = 0

var it = head
while (it != null) {
++size
it = it.next
}

return size
}

fun get(index: Int): String? {
var currIdx = index
var it = head
while (currIdx > 0 && it != null) {
it = it.next
currIdx--
}

if (it == null) {
throw java.lang.IndexOutOfBoundsException("Index is out of range")
}

return it.data
}

private class Node(val data: String?) {
var next: Node? = null
}

companion object {
private fun tail(head: Node?): Node? {
var it: Node?

it = head
while (it?.next != null) {
it = it.next
}

return it
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Jul 30 10:37:16 EDT 2024
sdk.dir=/Users/ttresansky/Library/Android/sdk
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pluginManagement {
repositories {
google() // Needed for the Android plugin, applied by the unified plugin
gradlePluginPortal()
}
}

plugins {
id("org.gradle.experimental.android-ecosystem") version "0.1.7"
}

rootProject.name = "example-android-app"

include("app")
include("list")
include("utilities")

defaults {
androidApplication {
jdkVersion = 11
compileSdk = 34
minSdk = 30

versionCode = 1
versionName = "0.1"
applicationId = "org.gradle.experimental.android.app"

testing {
dependencies {
implementation("org.junit.jupiter:junit-jupiter:5.10.2")
runtimeOnly("org.junit.platform:junit-platform-launcher")
}
}
}

androidLibrary {
jdkVersion = 11
compileSdk = 34
minSdk = 30

testing {
dependencies {
implementation("org.junit.jupiter:junit-jupiter:5.10.2")
runtimeOnly("org.junit.platform:junit-platform-launcher")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
androidLibrary {
namespace = "org.gradle.experimental.android.utilities"

dependencies {
api(project(":list"))
}
}
Loading

0 comments on commit de5fc2e

Please sign in to comment.