how to use barrel

How to correctly use a barrel in an app ?

* add it ?

* use ?

* follow version under git ?

thanks

  • The first thing is to decide what you want in the barrel.  I use them to share common code between apps, and not to share with other devs.  I have barrels for parts of my common watch faces, things like the background process to get weather data, etc.

    The first thing then is to create a barrel project.  In eclipse file>new>project>monkey barrel.  And put your code/resource in that project. For simple ones, I tend to write the .mc file such that I can use a link in eclipse for the "calling project" so that apps for devices that don't support barrels uses the same code.

    With auto-build in eclipse, when you save the files in a barrel project, the barrel should be built in that projects bin directory (<mybarrel>.barrel)

    To add that to another project, in eclipse, right click on that project and select "configure barrels".

    Jungles and eclipse links don't work together, so using the code in a barrel project with Jungles, eclipse links aren't an option, but to simply things in development you can copy the code/etc from the barrel project and make it local to your app and put it back in the barrel project when you're ready.

    (don't configure the barrel, but copy <mycode>.mc to the project you are testing with, and when you're ready, copy the mc back to the barrel project and configure it)

    I've never used git, but I'm thinking that a barrel project should be the same as any other CIQ project.

  • I don’t think there’s a good way to express version dependencies between a CIQ project and a barrel in git unless you use git submodules, which of course defeats one of the purposes of having a barrel in the first place. (A git submodule is a separate copy of an external project under your current project’s source tree, whereas a barrel exists as a library outside of your source tree.)

    IOW if you use git submodules, you almost don’t need barrels. 

    You could still do something like this:

    MyProject-root (container for project and barrel(s))

    |
    |
    ——— MyProject-source (actual project files go here)
    |
    |
    ——— MyBarrel (as git submodule)

    It would be awkward because you would still have a separate copy of the same barrel for each of the projects that uses it, which is clearly not the intent. But I guess any kind of shared library in git has the same issue if you use it as a submodule.

    The alternatives are:

    1) don’t make breaking changes in your barrels, so any future barrel versions always work with older projects

    2) use barrel version numbers (in ciq) and carefully document which version of a barrel is required for each project (but then you may as well go back to submodules, if different projects require different versions, since you’ll need different copies for different versions) 

  • from what i understand, a barrel is nothing more than a new project that we can use as a library. and from what you say, for debugging purpose, it's better to have it included in source in the project.

    from an interface stand point :

    * how  to call/Reference function in it ?

    * how about resources and/or variables ?  are they global ?

  •  I think the official doc should explain everything. They say that your barrel code should be in the form of a CIQ Module, and then you’d use it just like any other module. I suppose you could try adding global symbols to a barrel, although that wouldn’t be good practice of course. 

    https://developer.garmin.com/connect-iq/programmers-guide/shareable-libraries/

    One thing to note is there might be a slight memory overhead to using barrels, similar to the overhead of using plain modules. 

    The real advantage is just like any other library — the ability to easily reuse code and share it with others. For my personal projects I just share common sub folders with git submodules. In my case I don’t want or need the overhead of barrels as nobody else sees the code. 

  • When you include a .barrel file, if you look at it, it's actually a .zip of the barrel project.  It get's exampnd and compiled.

    A barrel is a module, so

    module MyBarrel {

      class BarrelClass {

      }

    }

    would use MyBarrel.BarrelClass as the reference (new MyBarrel.BarrelClass(); )

    Resources are a bit different in a Barrel, in that you don't just use "Rez" but in the above, for a font, you'd use

    MyBarrel.Rez.Fonts.id_icon vs Rez.Fonts.id_icon

  • 1) don’t make breaking changes in your barrels, so any future barrel versions always work with older projects

    That's what I do.  I may add something new, but I try hard not to break things that are there.  If for some reason, I do break something, it will be something that's easy to catch in the sim (maybe adding a new parameter to a function)