Author Topic: Setting workspace environment variables  (Read 214 times)

bengle

  • Senior Community Member
  • Posts: 127
  • Hero Points: 4
Setting workspace environment variables
« on: December 06, 2018, 12:40:36 pm »
Is there a way to programmatically set workspace environment variables from a macro?  I would like to have macros that can set the environment variables for a workspace and have them saved with the workspace.

I periodically have to create new directories and load projects from source control.  It is a pain to have to manually reenter all the workspace environment variables manually.  I would like to be able to automate that.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2220
  • Hero Points: 118
Re: Setting workspace environment variables
« Reply #1 on: December 06, 2018, 01:09:55 pm »
You can do this on the Open tab of the Project Properties dialog.

Rodney

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 732
  • Hero Points: 47
Re: Setting workspace environment variables
« Reply #2 on: December 06, 2018, 02:05:25 pm »
@bengle Please correct the email address associated with your forum account. Notifications of replies sent by this forum to your email account are bouncing.

++rodney

bengle

  • Senior Community Member
  • Posts: 127
  • Hero Points: 4
Re: Setting workspace environment variables
« Reply #3 on: December 10, 2018, 01:29:36 pm »
I am looking for a way to do this programmatically with a macro, rather than the Open tab of the Project Properties dialog.  For various reasons I periodically have to delete my project directories and reload them from source control.  I have quite a few projects and each one has quite a few environment variables.  It is tedious and time consuming to type them all in again on the Open tab Project Properties dialogs on each of the projects.  I would like to have a macro for each project that I could just run and it would add the entire set of environment variables at once without having to do all of the typing each time.

By the way, I did fix the email address in my profile.  I didn't even realize it had that obsolete email address.

Dan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2220
  • Hero Points: 118
Re: Setting workspace environment variables
« Reply #4 on: December 10, 2018, 01:38:26 pm »
Sorry, this should get you going:

Code: [Select]
_command void test_set_environment_variables() name_info(',')
{
   set_env('TEST1','value');
}

bengle

  • Senior Community Member
  • Posts: 127
  • Hero Points: 4
Re: Setting workspace environment variables
« Reply #5 on: December 17, 2018, 03:14:36 am »
I tried this, but it doesn't seem to save the environment variables into the workspace properties like the workspace properties dialog does.  It does set the environment variables temporarily, but the variables are not persistent.  I don't want to have to run the macro every tine I open the project.  I would like to run it only when I delete the project directory and recreate it from source control.

Attached is my macro, which is supposed to set the workspace environment variables from a file called "Environment.ini".  It seems to work, except that the variables are nit persistent.  In other words, if I open the workspace properties dialog after running the macro, the new variables are not listed.

Graeme

  • Senior Community Member
  • Posts: 2237
  • Hero Points: 280
Re: Setting workspace environment variables
« Reply #6 on: December 17, 2018, 08:16:04 am »
There's a couple of ways to do it.  The open tab of project properties allows you to enter commands that get run when the *project* is made active.  These are stored in the project file *.vpj.  You can see them at the bottom of the vpj file - they look like this
    <Macro>
        <ExecMacro CmdLine="testrunm"/>
    </Macro>

A workspace can separately have a set of environment variables associated with it.  These are assigned via project -> workspace properties -> Environment.  Unlike the open tab in project properties, the only things that can be here are set abc=def things.
   <Environment>
      <Set Name="gp1" Value="kkkkh"/>
   </Environment>


You can't programmatically change either project commands or workspace environment variables but there are alternatives.  On the open tab of project properties you can enter the name of a Slick C macro command that will be run when the *project* is made active - so you could run the macro you wrote from there.

Another way to do it is to use the Slick C "callback / call_list" mechanism.  If you search Slick C sources for call_list, you'll find this in wkspace.e in the workspace_open function
   call_list('_workspace_opened_');
Any Slick C macro whose name starts with _workspace_opened_ gets called automatically when the workspace is opened.
So you can do this
void _workspace_opened_do_something()
{
   say("abc " :+ _workspace_filename);
}

and this macro will run when the workspace is opened.
If you scan a few lines up in the workspace_open function you'll see this
   setEnvironmentFromXMLFile();

It opens the "Environment" node in the the workspace xml file like this
   int status = _xmlcfg_find_simple_array(gWorkspaceHandle, VPWX_SET, setNodeArray);

After a bit more searching you'll find there is a function called _WorkspaceSet_EnvironmentVariable that you might be able to call (it's non static) if you really wanted but I don't recommend it.



bengle

  • Senior Community Member
  • Posts: 127
  • Hero Points: 4
Re: Setting workspace environment variables
« Reply #7 on: December 17, 2018, 01:37:20 pm »
Thank you so much for your help.  I made a few changes and renamed the macro I wrote to "_workspace_opened_set_env" and that does exactly what I need.   :)