ReactiveCircus / app-versioning

A Gradle Plugin for lazily generating Android app's versionCode & versionName from Git tags.
Apache License 2.0
205 stars 3 forks source link

Not a valid gitRootDirectory on composite build project #21

Closed kennir closed 2 years ago

kennir commented 2 years ago

Hi

I have the structure of composite build project look likes

- workspace
    - app
        - .git 
        - build.gradle.kts 
    - lib
       - .git
       - build.gradle.kts

I added the appVersioning plugin to app/build.gradle.kts

appVersioning {
    fetchTagsWhenNoneExistsLocally.set(true)

    println("My root folder is: ${rootProject.file("./")}")

    overrideVersionName { gitTag, _, _ ->
        // TODO generate a String from the given gitTag, providers, build variant
        "${gitTag.rawTagName}-${gitTag.commitHash}"
    }
}

but I encountered the error:

> Configure project :app
My root folder is: /Users/ken/Developer/projects/2021/workspace/app

> Task :app:generateAppVersionInfoForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:generateAppVersionInfoForDebug'.
> A failure occurred while executing io.github.reactivecircus.appversioning.tasks.GenerateAppVersionInfoWorkAction
   > Android App Versioning Gradle Plugin works with git tags but root project 'browser-app' is not a git root directory, and a valid gitRootDirectory is not provided.

I confirmed that the app folder contains the.git directory

Thank you

ychescale9 commented 2 years ago

You can specify the git root directory:


appVersioning {
  gitRootDirectory.set(rootProject.file("app/"))
}
kennir commented 2 years ago

Hi @ychescale9

Thanks for your reply,

I added the line to the build.gradle.kts

appVersioning {
    fetchTagsWhenNoneExistsLocally.set(true)

    println("My root folder is: ${rootProject.file("app/")}")

    gitRootDirectory.set(rootProject.file("app/"))

    ......
}

and the error still exists, I think the root directory is wrong after the line is added.

BUILD FAILED in 3s
1 actionable task: 1 executed
❯ ./gradlew generateAppVersionInfoForDebug

> Configure project :app
My root folder is: /Users/ken/Developer/projects/2021/workspace/app/app

> Task :app:generateAppVersionInfoForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:generateAppVersionInfoForDebug'.
> A failure occurred while executing io.github.reactivecircus.appversioning.tasks.GenerateAppVersionInfoWorkAction
   > Android App Versioning Gradle Plugin works with git tags but root project 'browser-app' is not a git root directory, and a valid gitRootDirectory is not provided.
ychescale9 commented 2 years ago

so :app is already your root Gradle project? what's :browser-app?

kennir commented 2 years ago

Yes, the app and lib are two different git repository, :browser-app is name of project, app is the directory

folder structure:

- workspace (/Users/ken/Developer/projects/2021/workspace)
    - app (/Users/ken/Developer/projects/2021/workspace/app)      :browser-app 
    - lib (/Users/ken/Developer/projects/2021/workspace/lib)         :scaffold-lib

there are two top-level Gradle project, linked by composite build

app/settings.gradle

includeBuild("../lib")

app/app/build.gradle.kts

dependencies {

    implementation("scaffold-lib:baseapp")
}

I checked the tag of commit seemed ok

cd app

git log

commit b98b5d4a8e60997b065e7c9a0a5801765c86930e (HEAD -> develop, tag: 3.0.0, origin/develop)
ychescale9 commented 2 years ago

Ok then I would expect it to work without setting gitRootDirectory.

Does this folder exist? /Users/ken/Developer/projects/2021/workspace/app/.git/refs/

kennir commented 2 years ago

the app/.git is not a folder, It's seems a symbol-link

cat app/.git

gitdir: ../.repo/projects/app.git

I think we found the cause of the error, I use git-repo-go for manage multi repositories, I think this tool modify the default git behavior.

I tried adding this line to build.grade.kts, It didn't work as expected, because ../.repo/projects/app.git is .git instead of not contains it

gitRootDirectory.set(rootProject.file("../.repo/projects/app.git"))
ychescale9 commented 2 years ago

Yeah this is not going to work, as the plugin expect GIT_REFS_DIRECTORY = ".git/refs".

If you go into app.git do you see a refs folder?

kennir commented 2 years ago

Yeah, I thinks It's the ".git" directory itself

❯ ls
COMMIT_EDITMSG   HEAD             co.gitup.mac     description      index            logs             packed-refs      sourcetreeconfig
FETCH_HEAD       ORIG_HEAD        config           hooks            info             objects          refs
ychescale9 commented 2 years ago

Ok then we could add a gitRefsDirectory to point to the /refs directly (and ignore gitRootDirectory), but I'm not sure how useful this is to others as it seems like an uncommon setup 😄

kennir commented 2 years ago

Yeah, this is not common scene

I think If the .git directory of android repo is same as go-repo, there should more project use composite build and the .git will be a link, not a folder .

give me some time, let me confirm it :)

Thank you very much

kennir commented 2 years ago

https://git-scm.com/docs/gitrepository-layout

Hi, It's seemed the gitdir is an option of git

ychescale9 commented 2 years ago

Thanks I didn't know that 😅

I'll come up with a fix soon hopefully without introducing new plugin configuration.

ychescale9 commented 2 years ago

I've just added support for specifying the location of bare git repository:

appVersioning {
    /**
     * Bare Git repository directory.
     * Use this to explicitly set the directory of a bare git repository (e.g. `app.git`) instead of the standard `.git`.
     * Setting this will override the value of [gitRootDirectory] property.
     */
    bareGitRepoDirectory.set(rootProject.file("../.repo/projects/app.git")) // if the .git directory in the Gradle project root is a symlink to app.git.
}

Can you please help me test the 1.1.0-SNAPSHOT version of the plugin?

You'll need to add the sonatype snapshot repository to the buildscript:

maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots/'
  }

I had to add a new plugin config because the plugin doesn't know the name of a bare git repo.

kennir commented 2 years ago

I did modify these files

app/app/build.gradle.kts

plugins {
    id("com.android.application")
    id("io.github.reactivecircus.app-versioning") version "1.1.0-SNAPSHOT"
    kotlin("android")
    kotlin("kapt")
    id("dagger.hilt.android.plugin")
    id("dependencies")
}

app/build.gradle.kts

buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            setUrl("https://maven.aliyun.com/nexus/content/groups/public/")
        }

        maven {
            setUrl("https://jitpack.io")
        }

        maven {
            setUrl("https://oss.sonatype.org/content/repositories/snapshots/")
        }
    }

I got error message

org.gradle.internal.exceptions.LocationAwareException: Build file '/Users/ken/Developer/projects/2021/workspace/app/app/build.gradle.kts' line: 4
Plugin [id: 'io.github.reactivecircus.app-versioning', version: '1.1.0-SNAPSHOT'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Included Builds (None of the included builds contain this plugin)
- Plugin Repositories (could not resolve plugin artifact 'io.github.reactivecircus.app-versioning:io.github.reactivecircus.app-versioning.gradle.plugin:1.1.0-SNAPSHOT')
  Searched in the following repositories:
    Gradle Central Plugin Repository
ychescale9 commented 2 years ago

Ah the snapshots are only published to maven central so you can't use id("io.github.reactivecircus.app-versioning") version "x.y.z" which needs Gradle Plugin Portal or defining classpath("io.github.reactivecircus.appversioning:app-versioning-gradle-plugin:${Versions.appVersioning}").

I'll just publish a RC build now so you don't need to use snapshot 😄

ychescale9 commented 2 years ago

I just published 1.1.0. Can you try again?

appVersioning {
    ...
    bareGitRepoDirectory.set(rootProject.file("../.repo/projects/app.git"))
}
kennir commented 2 years ago
> Task :app:generateAppVersionInfoForDebug
Generated app version code: 30000.
Generated app version name: "3.0.0-4fef94e".

Nice, It works as well as normal .git

Thank you very much!