Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting variables that are defined in the .template but not set in the .env? #42

Open
kohenkatz opened this issue Nov 11, 2022 · 2 comments

Comments

@kohenkatz
Copy link

I have been using this plugin for several years for providing various Android Gradle settings, but now I want to extend to also using it to hold settings that are used in the app itself.

Here's the code I came up with to do that:

androidComponents {
    onVariants(selector().all(), { variant ->
        env.allVariables.each {
            if (it.key.startsWith("ANDROID_")) {
                def key = it.key.substring(8)
                    .split("_")
                    .collect {it.substring(0,1).toUpperCase(Locale.ROOT)+it.substring(1).toLowerCase(Locale.ROOT)}
                    .join("")
                key = key.substring(0,1).toLowerCase(Locale.ROOT)+key.substring(1)

                def type = "String"
                def wrapper = '"%s"'
                def value = it.value
                if (value.isEmpty()) {
                    wrapper = '%s'
                    value = null
                } else if (value.matches("-?\\d+")) {
                    type = "int"
                    wrapper = '%s'
                } else if (value.matches("-?\\d+(.\\d+)")) {
                    type = "double"
                    wrapper = '%s'
                } else if (value.matches("httpS?://.*")) {
                    type = "android.net.Uri"
                    wrapper = 'android.net.Uri.parse("%s")'
                }

                variant.buildConfigFields.put(key, new BuildConfigField(type, String.format(wrapper, value), "Field from .env"))
            }
        }
    })
}

This adds any environment variable (including from .env) that starts with ANDROID_ to the BuildConfig file to allow the app to use it.

The problem is that I want to have a sane null value for any variable that is defined in .env.template but is not set with an actual value in .env. For example, in production I can set ANDROID_SENTRY_DSN to the actual Sentry reporting URL for this app, but in development I need to make sure to leave the key in the .env file, set to an empty string.

Another problem is that I want a way to warn other developers that they are missing values from .env (such as when a developer adds a new value). While I could manually keep a list of variables that are required, that introduces an extra maintenance burden of having to add new variables to three places instead of just two: the actual .env, the .env.template, and the list in Gradle.

I see that env.allVariables filters out null values. It would be really nice if there was a way to keep the null values for anything that is present in .env.template as a way to positively show that those are values that we want to see but they are missing? Perhaps a getter for allVariablesWithUndefined (or similar) that is the same as allVariables but skips the null check?

@uzzu
Copy link
Owner

uzzu commented Nov 18, 2022

@kohenkatz Thanks for reporting issue 😄

The problem is that I want to have a sane null value for any variable that is defined in .env.template but is not set with an actual value in .env. For example, in production I can set ANDROID_SENTRY_DSN to the actual Sentry reporting URL for this app, but in development I need to make sure to leave the key in the .env file, set to an empty string.

Whether it should be provided as a feature of this Gradle plugin is a matter for consideration, but I understand the desire to validate .env files as you have raised. There are other ways to do it, such as providing GitHub Actions or other CI-oriented scripts, in addition to providing it as a feature of this Gradle Plugin.

I would like to provide minimum functionality for this Gradle plugin, but what kind of functionality could be nice to have for integrating with validation CI? Any suggestions would be appreciated. (For example I think, outputting checkstyle reports, etc.)

Perhaps a getter for allVariablesWithUndefined (or similar) that is the same as allVariables but skips the null check?

This is a clear functionality that this plugin should provide, so perhaps it could be added to this Gradle plugin.
I create a separate issue #43 , so please watch it.

@uzzu
Copy link
Owner

uzzu commented Nov 26, 2023

@kohenkatz

Sorry for keeping you waiting for a long time.

(Map<String, String?>) env.allVariablesOrNull() is released in 2.1.0. You can try to use it, and give me a feedback.

https://github.com/uzzu/dotenv-gradle/blob/main/CHANGELOG.md#210---2023-11-26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants