sillydan1/graphedit

Adding Windows compatibility to build.gradle

Closed this issue ยท 7 comments

Because Windows uses different backslashes in folder names, the gitCommitSha and gitCommitShaLong methods need to be changed a little.
First the result is saved into a string, then all "" are replaced with "/". This is still a valid syntax, as using / in folder names is OS-independent.

This issue also suggests using multiline formating for the string from these methods, as they are added to BuildConfig in the buildConfig method.

gitCommitSha

def gitCommitSha = { ->
    def stdout = new ByteArrayOutputStream();
    exec {
        commandLine 'cmd', 'git', 'describe', '--long', '--always', '--dirty', '--exclude=*', '--abbrev=8';
        standardOutput = stdout;
    }
    def rawSha = stdout.toString();
    rawSha = rawSha.replace('\\', '/');
    return rawSha.trim();
}

gitCommitShaLong

def gitCommitShaLong = { ->
    def stdout = new ByteArrayOutputStream();
    exec {
        commandLine 'cmd', 'git', 'describe','--long','--always','--exclude=*','--abbrev=40';
        standardOutput = stdout;
    }
    def rawShaLong = stdout.toString();
    rawShaLong = rawShaLong.replace('\\', '/');
    return rawShaLong.trim();
}

buildConfig

buildConfig {
        buildConfigField("String", "APP_NAME", "\"${project.name}\"");
        buildConfigField("String", "APP_VERSION", provider { "\"${project.version}\"" } );
        buildConfigField("long", "BUILD_TIME", "${System.currentTimeMillis()}L");
        buildConfigField("String", "COMMIT_SHA", "\"\"\"\n${gitCommitSha()}\"\"\"");
        buildConfigField("String", "COMMIT_SHA_LONG", "\"\"\"\n${gitCommitShaLong()}\"\"\"");
        className("BuildConfig");
        packageName("dk.gtz.graphedit");
        useJavaOutput();
    }

Linking our mastodon discussion for reference

I believe that b1b5b47 solves this issue. I dont have a windows machine to test on, but it works in a debian-based docker container without git installed. Would you mind confirming if this works?

I tried downloading that specific commit as a zip, and when i open it and do a gradle run thru IntelliJ, it compiles and runs without any issue.
I do have git installed, not sure how much that affects the result.

Did you make sure to delete BuildConfig.java before doing gradle run? Also, how does the resulting BuildConfig.java look like?

I'm not sure what you mean by deleting BuildConfig.java, as that only shows up as compiled file in my IDE, and that means it's read only (shown by the error message). Can i delete is somehow? I tried just deleting the file from the file browser but the IDE still has it cached anyway.
But the result is same as before, just has empty COMMIT_SHA and COMMIT_SHA_LONG variables.
image

It should've made a source file in build/generated/sources/buildConfig/main/dk/gtz/graphedit/BuildConfig.java and a gradle clean should clear / delete it.

In any case, it seems that running git --version on your machine results in a non-zero return code (typically indicating an error) so the fix seems to (at least temporarily) fix the issue. I will close the issue for now, but if you run into any more issues don't hesitate to open another, or comment on this one ๐Ÿ˜„ Thanks for your patience and quick feedback!

I will also make an attempt to test on a real windows machine. I should have a laptop lying around somewhere that could serve as a test machine.

Okay, just to close the loop on this one, when i do a gradle clean and gradle loop, then the generated BuildConfig.java file looks the same as in that screenshot.
I'm happy to help anytime ๐Ÿ‘