Forum Discussion

FCF-Ben's avatar
FCF-Ben
Reel Rookie
3 years ago
Solved

Setting environment variables in brigthscript (best practice)?

There is one thing that I've been wondering, where do people set their environment variables, and is there anything like best practices. 

For example, we run two data environments, a dev and a production one, and it would make our lifeeasier if we could just have an environment file where we set DATA_SOURCE, and we can set it to something like

 

DATA_SOURCE="https://www.ourdomain.com/api/dev/v1.01"

 

Then in the code being able to do something like:

 

xfer.SetURL="$$DATA_SOURCE$$/get_content"

 

 

I looked at the manifest file, and there is a function (or option) called 

uri_resolution_autosub

https://developer.roku.com/en-gb/docs/developer-program/getting-started/architecture/channel-manifest.md 

Which works this way, but it seems like a reserved word (or something like that). 
 
And I guess I can put it in an screengraph XML, but truth to be told, every time I've thought I've understood first thing about data scopes in Roku it throws a curveball and I feel rather lost (I'm also relatively new to Roku development). So was hoping there is a simple way of setting constant variables available anywhere just for environment settings. 

Thank so very much, and sorry if this isn't the right place to ask. 
 
 
  • There's at least a few different ways to run dev. vs. production environments.


    One is you could write dynamic code to check roAppInfo IsDev(), which will return true for side-loaded channels, and then vary the runtime behavior based on that.

    E.g.

    appInfo = CreateObject("roAppInfo")
    if appInfo.isDev()
        my_url = "http://something.dev"
    else
        my_url = "http://something.prod"
    end if


    If you prefer to control behavior by changing a source file, you can use the manifest file for that.


    One option is to use conditional compilation:

    https://developer.roku.com/en-gb/docs/references/brightscript/language/conditional-compilation.md 

    E.g. in the manifest file put:

    bs_const=USE_DEV_SERVER=false

    Then in .brs do:

    #if USE_DEV_SERVER
        my_url = "http://whatever.dev"
    #else
        my_url = "http://whatever.prod"
    #end if


    You can also put custom key entries in the manifest file, e.g.

    my_url=http://something.dev

    vs.

    my_url=http://something.prod

    where you change the manifest file between runs.


    Then in code, you can retrieve the configured value using

    appInfo = CreateObject("roAppInfo")
    my_url = appInfo.GetValue("my_url") 

    Note that although I mention it, I don't recommend this approach myself, and it can often cause confusion in development if you mix Roku OS-defined manifest attributes with your own custom entries.

    A better approach is to just make your own configuration file, e.g. my_config.json, and read that from your Main function or wherever you need it.

    Hope that helps.

3 Replies

  • RokuKC's avatar
    RokuKC
    Roku Employee

    There's at least a few different ways to run dev. vs. production environments.


    One is you could write dynamic code to check roAppInfo IsDev(), which will return true for side-loaded channels, and then vary the runtime behavior based on that.

    E.g.

    appInfo = CreateObject("roAppInfo")
    if appInfo.isDev()
        my_url = "http://something.dev"
    else
        my_url = "http://something.prod"
    end if


    If you prefer to control behavior by changing a source file, you can use the manifest file for that.


    One option is to use conditional compilation:

    https://developer.roku.com/en-gb/docs/references/brightscript/language/conditional-compilation.md 

    E.g. in the manifest file put:

    bs_const=USE_DEV_SERVER=false

    Then in .brs do:

    #if USE_DEV_SERVER
        my_url = "http://whatever.dev"
    #else
        my_url = "http://whatever.prod"
    #end if


    You can also put custom key entries in the manifest file, e.g.

    my_url=http://something.dev

    vs.

    my_url=http://something.prod

    where you change the manifest file between runs.


    Then in code, you can retrieve the configured value using

    appInfo = CreateObject("roAppInfo")
    my_url = appInfo.GetValue("my_url") 

    Note that although I mention it, I don't recommend this approach myself, and it can often cause confusion in development if you mix Roku OS-defined manifest attributes with your own custom entries.

    A better approach is to just make your own configuration file, e.g. my_config.json, and read that from your Main function or wherever you need it.

    Hope that helps.

  • renojim's avatar
    renojim
    Community Streaming Expert

    This is the place to ask, but I don't have a good answer for you.  One method that comes to mind is to save "constants" in the registry right at the start of Main/RunUserInterface and then you can access them anywhere.  It's not very elegant and hardly a simple substitution line the $$$ thing, but it works.

  • Deleted, because I did not read properly before responding :)