Sublime Forum

Build System : Auto increment build number

#1

Hi All,
I was wondering if I can add a auto increment build number to my build. I have a custom build system that looks like this.

"cmd": ["build.cmd", "target_name", "arg_1", "arg_2", "*custom_build_number*"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${project_path}/build/", "variants": [ { "name": "Clean", "cmd": ["build.cmd", "target_name", "-c"], }, ]

I want to pass a {custom_build_number}, is this possible using the built in build system? Even if there is some way to ask the user for a build number, rather than automatic increment, I can work with that. Or can I create a custom variable that is ‘visible’ to the build system? Do I have to resort to a plugin?

Thanks,
Rajah

0 Likes

#2

I don’t think there is any direct way to do something like this without plugin code or enhancing something elsewhere, but I may be wrong.

One way to go about that would be to have whatever tracks the build number be done externally, within build.cmd itself. That way no matter how you execute the build (from within sublime, from the command line, etc) your build number would always auto-increment, which from a completeness/correctness standpoint may be the “best” way (best being used very loosely).

It is possible to modify a build system so that instead of invoking the exec command directly to execute the build, it executes a custom sublime command instead (from a plugin). That would necessitate building some plugin logic that either tracks the current build number and increments it every time, or prompting the user for what to use.

Prompting the user may quickly get tedious if you do frequent builds, although you could mitigate that somewhat by perhaps remembering what was used last time and then providing that as the default for next time, or only asking on the first build in a session and then auto-incrementing after that.

Below is an example of enhancing the build system to allow you to specify your own custom build variables outside of those inherently supported by Sublime. The code assumes that the extra settings are coming from a setting specific to the project file, but you could modify that so that instead of doing that it looked e.g. at some file on disk and used that as the value of the variable instead.

1 Like

Inserting Build Numbers
#3

Hi @OdatNurd, thanks for your prompt reply. Unfortunately, the build.cmd is not maintained by me, I cannot modify that. However, I could create a duplicate file and modify that and call that each time for compile. The build.cmd file does not change often. Its a good idea, I had not explored that option.

I will also explore the plugin option from the github link.

Thanks,
Rajah

0 Likes

#4

I guess that’s going to be what you need, or instead of running build.cmd, you run an other script that runs build.cmd which increments the counter for you. (I think the second solution is the best)

0 Likes

#5

Hi @OdatNurd, I was interested in adding my own custom variables and followed your code on github. I have modified my sublime-build file to look like this

    {
     "target": "my_custom_build",
     "command": ["build.cmd", "target_name, "arg1", "arg2", "${auto_build_number}"],
     "working_dir": "${project_path}/build/",
     "shell": false
    },

I have copied your custom_build_variables.py to my plugins folder and sublime loads it. Now when I launch build drop down (Ctrl + Shift + B), I don’t see the newly added item there. Did I miss something or my understanding of your build enhancement plugin is wrong?

0 Likes

#6

Hi @math2001, that’s a good idea, Let me try that. I dont see why that would not work. I am hoping I can get @OdatNurd’s method to work, it will help me add more variable in the future. :slightly_smiling:
Thanks for your time all.

Rajah

0 Likes

#7

The popup that you get with that key sequence shows you the list of variants in the currently selected build system.

Build systems show up in the menu based on the name of the file that you saved them into, e.g. MyBuildSystem.sublime-build shows up in the menu as MyBuildSystem. Since there isn’t anything in the build system as defined that tells Sublime to use it automatically, you need to manually select the build from the menu (Tools > Build System) to select it, and then just press Ctrl+B to build with it.

The modification doesn’t change how the build is presented to you, only in what happens when the build executes.

Note also that there is a trailing comma in the text you posted above after the closing }; if that is in your build system it won’t work (unless that excerpt is e.g. from a project specific build in your project file; in that case, the name of the build in the menu is based on a name field in the build system, which you would need to add to see it).

0 Likes

#8

I have a custom build, my file is called Scons.sublime-build. In this file, I have many variants. They all pop up when I hit Ctrl+Shift+B. I can then select from this list and it builds without issue. I added a new item to this list as indicated above, however the newly added item does not show in the list, so I am unable to invoke that build.

The trailing ‘,’ is for other variants in my Scons.sublime-build file. I was missing the ‘name’ part, its showing up now.

Thanks,
Rajah

1 Like

#9

Hi @OdatNurd, now that I am able to call the command, I have another question. I have added the following to the custom_build_var.py

custom_var_list = [“audo_build_num”]

How/Where do you initialize a value for this? I see that in the plugin, you are reading a project settings file.

project_data = self.window.project_data ()
project_settings = (project_data or {}).get (“settings”, {})

Which settings file is this? Is this the sublime-project file? Right now, when I invoke the command from build, the build is called with an empty stings for my build number.

Thanks,
Rajah

0 Likes

#10

Yes, it’s possible to have a "settings" key in your project file to apply settings that are specific to your project. That might look something like this:

{
    "folders":
    [
        {
            "path": "."
        }
    ],
    "settings":
    {
        "font_size": 14
    }
}

As written, the code tries to pull the setting first from the view settings, with the project settings being used if the view doesn’t have that setting. That allows for you to have an overall project specific setting for something but to override it for a particular file or view if you want to.

Views get settings from other places (e.g. global preferences and syntax specific) so if you put your setting directly in your user preferences instead of your project, it would get pulled from there instead for example.

For your purposes you may want to pull that setting from somewhere else instead of the project settings (or get the default from another location) so that you can increment the value after you apply it and store the value back for next time you build.

0 Likes

#11

Wonderful! Works great. I was missing the settings keyword, I was doing something like this.

{
    "folders":
    [
        {
            "path": "."
        }
    ],
    "auto_build_number": "0"
}

Thank you very much. I agree, I should probably move it out of the project settings, some global location would work best.

1 Like