Sublime Forum

Environment variables in project's paths

#1

Hi,

In a .sublime-project file it seems impossible to give a path that contains environment variables in it.

At work we use Perforce as source control tool, and time to time we create different branches of a project. Perforce is not like Git or Mercurial, its branches are in fact copies to a different file system location.
So we end up having something like:

C:\OurNiceProject\Main\...
C:\OurNiceProject\Branch1\...
C:\OurNiceProject\Branch2\...

Now to cope with this, we have a system-wide environment variable that tell us the “active” branch, the one that I’m supposed to work on, at a specific moment.
So in my .sublime-project I could use it and have the content of it being dynamically updated. (for dynamic I mean when the variable change and I re-open Sublime)
example:

{
  "folders":
  [
    {
      "name": "Active Branch"
      "path": "C:/OurNiceProject/%ACTIVE_P4_BRANCH%"
    }
  ]
}

Unfortunately environment variables in .sublime-project’s paths, are not picked up and I have to undergo the tedious process of updating them every and each time I switch Perforce branch.

This is just an example, but it could also help to use the same .sublime-project on different computers, maybe also on different OS if the variable syntax in the path can be unix style also on windows (obviously my example is for windows, but again, it is just an example)

0 Likes

#2

The normal way of handling that in my mind would be linking directories. Can you not map a folder or drive in this case?

0 Likes

#3

What do you mean exactly? Symbolic Links?
Sure that could work, but it is not exactly solving the problem as every-time I switch branch I have to re-create the link.
Better than change 10 to 20 paths, but still not optimal.
Handling environment variables is not that complicated.

0 Likes

#4

Obviously it exactly solves your problem but in general it doesn’t do anything you can’t do right now by writing a script, which is the general solution for these sorts of user specific needs.

In your case it’s a matter of writing a script that runs at startup, reads the environment variable in question and modifies the project files. There may even be a plugin that does something like that that can be adapted.

0 Likes

#5

I don’t see how I can hook into the way Sublime reads a *.sublime-project file. I can’t find anything in the api that seems to serve the porpoise.
About changing the *.sublime-project somehow, at startup, is really a hack, an in my opinion is better to don’t endeavour in writing these kind of intrusive behaviours.

Again, I insist, my feature proposal of simply resolve the variables in the paths seems clean and easy, done in many other parts in Sublime. But this is something that must be implemented in the source code, not in a plugin.

0 Likes

#6

Actually all you need to do is have relative paths in your project file and open the project file from within the active branch directory. You could even check in the project file so you wouldn’t have to anything manually.

Just to be clear, I’m just addressing how you’d solve your problem with the existing tools as they are now. If you’re mainly interested in pushing your favoured enhancement then I’ll leave you in peace, but don’t hold your breath, if history is any guide you’re more likely to have a newly discovered subatomic particle named after you than the developers implement this. But you never know, you might get lucky (I’m still talking about the subatomic particle).

0 Likes

#7

If you use the Project menu, you’ll see an entry for Edit Project, which just opens a JSON file that defines your project and settings. When you save that file, the changes are read, and Sublime Text responds accordingly.

In your case @daniele.niero, it seems you could write a small bit of Python to read the JSON in, update the path to the folder you want Sublime Text to show, and then save the JSON back to the .sublime-project file. That should accomplish everything you need and shouldn’t really take more than around 25 lines of Python.

However, I suspect the real issue you may run into is getting the current environmental variable value. See, when an application starts, the environment variables are set, and the process reads those values. To get an updated value, you will likely need to start a new subprocess (in a new group) that reads the current values of the environment it is started in. Alternatively perhaps you can query p4 to get the current branch.

2 Likes