Author Topic: [RESOLVED]Squirrel set $(var_name)  (Read 762 times)

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
[RESOLVED]Squirrel set $(var_name)
« on: September 13, 2019, 05:11:08 pm »
Hi,

Before every builds and for every projects, I have create this script to set my envars without to use that: VAR1 = [[parse cfg, get keyvalue return value]]

Code: [Select]
local deps_config_filename = _T("../../dependencies/dependencies.cfg");

local config_exist = IO.FileExists(deps_config_filename);
assert(config_exist);

if(config_exist)
{
    local file_content = IO.ReadFileContents(deps_config_filename);
    local groups = GetArrayFromString(file_content, _T("["), true);

    for(local i = 0; i < groups.GetCount(); i++)
    {
        local group = GetArrayFromString(groups.Item(i), _T("]"), true);

        local groupName = group.Item(0);
        local values = GetArrayFromString(group.Item(1), _T("\n"), true);

        print("groupName " + i + ": " + groupName);

        for(local j = 0; j < values.GetCount(); j++)
        {
            local keyvalue = GetArrayFromString(values.Item(j), _T("="), true);

            if(keyvalue.GetCount() != 2)
            {
                ShowError(_T("Pour le groupe: " + groupName + " et valeur: " + j + " il y a plus d'un charactère '='."));
            }

            for(local k = 0; k < keyvalue.GetCount(); k++)
            {
                local key = keyvalue.Item(0).MakeUpper();
                local value = keyvalue.Item(1);
                key = ::EscapeSpaces(key);
                value = ::EscapeSpaces(value);
               
                // $(key) = value;

                print("key: " + key + " value: " + value));
            }
        }
    }
}

But I don't know how to set the value.

$(key) = value;

Can you help me ?
« Last Edit: September 16, 2019, 11:17:02 pm by Suryavarman »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12062
    • Travis build status
Re: Squirrel set $(var_name)
« Reply #1 on: September 13, 2019, 07:29:34 pm »
Have you tried printing the value instead of returning it from the [[ ... ]] construct?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: Squirrel set $(var_name)
« Reply #2 on: September 13, 2019, 08:50:02 pm »
 :o

If i copy past the script into:

Project build options > Pre/post build steps:

I have this error:
Running project pre-build steps
Code: [Select]
local deps_config_filename = _T("../../dependencies/dependencies.cfg");
local config_exist = IO.FileExists(deps_config_filename);
/bin/sh: 1: Syntax error: "(" unexpected
Process terminated with status 2 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))


If attach the script from the Project/target > Build Scripts

I have this error:

Code: [Select]
AN ERROR HAS OCCURED [assertion failed]

CALLSTACK
*FUNCTION [main()] /home/gandi/Working/Ma/code/API//src/LoadConfig.script line [4]

LOCALS
[config_exist] false
[deps_config_filename] INSTANCE
[this] TABLE

The relative path doesn't work (but it's work from the scriping console)

I have to fix it and will say you if it's work or not.

Thank you for you answer.


Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: Squirrel set $(var_name)
« Reply #3 on: September 13, 2019, 09:26:12 pm »
With this attached script it doesn't work (Project/target > Build Scripts):
Code: [Select]
// http://wiki.codeblocks.org/index.php/Build_scripts

function SetBuildOptions(base)
{
    local deps_config_filename = ::ReplaceMacros(_T("$(PROJECT_DIR)"));
    deps_config_filename += _T("../../dependencies/dependencies.cfg");


    local config_exist = IO.FileExists(deps_config_filename);
    assert(config_exist);

    if(config_exist)
    {
        local file_content = IO.ReadFileContents(deps_config_filename);
        local groups = GetArrayFromString(file_content, _T("["), true);

        for(local i = 0; i < groups.GetCount(); i++)
        {
            local group = GetArrayFromString(groups.Item(i), _T("]"), true);

            local groupName = group.Item(0);
            local values = GetArrayFromString(group.Item(1), _T("\n"), true);

            //print("groupName " + i + ": " + groupName);

            for(local j = 0; j < values.GetCount(); j++)
            {
                local keyvalue = GetArrayFromString(values.Item(j), _T("="), true);

                if(keyvalue.GetCount() != 2)
                {
                    ShowError(_T("Pour le groupe: " + groupName + " et valeur: " + j + " il y a plus d'un charactère '='."));
                }

                local key = keyvalue.Item(0).MakeUpper();
                local value = keyvalue.Item(1);
                key = ::EscapeSpaces(key);
                value = ::EscapeSpaces(value);

                // $(key) = value;
                print(key + "=" + value +"\n");
               
            }
        }
    }
}
« Last Edit: September 13, 2019, 11:53:13 pm by Suryavarman »

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: Squirrel set $(var_name)
« Reply #4 on: September 13, 2019, 11:10:34 pm »
I have try to use SetCommandsBeforeBuild. But nothing happen. The echo command send to the CommandsBeforeBuild doesn't appear/works.

Code: [Select]

function SetBuildOptions(base)
{
    local deps_config_filename = ::ReplaceMacros(_T("$(PROJECT_DIR)"));
    deps_config_filename += _T("../../dependencies/dependencies.cfg");


    local config_exist = IO.FileExists(deps_config_filename);
    assert(config_exist);

    if(config_exist)
    {
        local file_content = IO.ReadFileContents(deps_config_filename);
        local groups = GetArrayFromString(file_content, _T("["), true);

        local commands = wxArrayString();

        for(local i = 0; i < groups.GetCount(); i++)
        {
            local group = GetArrayFromString(groups.Item(i), _T("]"), true);

            local groupName = group.Item(0);
            local values = GetArrayFromString(group.Item(1), _T("\n"), true);

            //print("groupName " + i + ": " + groupName);

            for(local j = 0; j < values.GetCount(); j++)
            {
                local keyvalue = GetArrayFromString(values.Item(j), _T("="), true);

                if(keyvalue.GetCount() != 2)
                {
                    ShowError(_T("Pour le groupe: " + groupName + " et valeur: " + j + " il y a plus d'un charactère '='."));
                }


                local key = keyvalue.Item(0).MakeUpper();
                local value = keyvalue.Item(1);
                key = ::EscapeSpaces(key);
                value = ::EscapeSpaces(value);

                // $(key) = value;
                commands.Add(key + _T("=\"") + value + _T("\""), 1);
                commands.Add(_T("echo $(") + key + _T(")"), 1);
                //print(key + "=" + value +"\n");
               

            }
        }

        for(local i = 0; i < commands.GetCount(); i++)
        {
            print(commands.Item(i));
        }

        base.SetCommandsBeforeBuild(commands);

    }
}
« Last Edit: September 13, 2019, 11:52:41 pm by Suryavarman »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2539
Re: Squirrel set $(var_name)
« Reply #5 on: September 14, 2019, 12:07:20 am »
I have not read or understand your problem ( i will try to) but to quickly try to help: you can use
Code: [Select]
Log () to log to the console for debugging purposes...

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2539
Re: Squirrel set $(var_name)
« Reply #6 on: September 14, 2019, 12:08:39 am »
Also can you add an example project file?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2539
Re: Squirrel set $(var_name)
« Reply #7 on: September 14, 2019, 12:47:10 am »
I am afraid, i do not think you can set environment variables with squirrel at the moment.

If you post a example project i can try to implement something.

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: Squirrel set $(var_name)
« Reply #8 on: September 14, 2019, 10:14:03 am »
Oo thank you BuelHazzard…Really.

Here you have the git project:
https://framagit.org/Suryavarman/ma/

Here the codeblocks workspace ( i don't like how cmake generate CB project. i prefer to use CB without makefile and with a Workspace)
https://framagit.org/Suryavarman/ma/

Here one of the three projects:
https://framagit.org/Suryavarman/ma/blob/master/code/API/API.cbp

Here the bash script to create the dependencies.cfg and to build some dependencies.
https://framagit.org/Suryavarman/ma/blob/master/dependencies/install_dependencies.sh

Here some config files exemples:
https://framagit.org/Suryavarman/ma/blob/master/dependencies/dependencies_kubuntu18.04.cfg
https://framagit.org/Suryavarman/ma/blob/master/dependencies/dependencies_mageia6.cfg

Here the squirrel scripts to be place into the:
- «Project/target > Build Scripts»:
https://framagit.org/Suryavarman/ma/blob/master/code/API/src/LoadConfig.script

- «project pre-build steps»:
https://framagit.org/Suryavarman/ma/blob/master/code/API/src/LoadConfigPreBuild.script
 Here an output example from LoadConfigPostBuild.script :
Code: [Select]
MA_PYTHON_PATH="/usr/bin/python3.7m"
MA_PYTHONENV_PATH="/home/blabla/Ma/dependencies/venv-test"
MA_PYTHONENV_SP_PATH="/home/blabla/Ma/dependencies/venv-test/lib64/python3.7/site-packages"
MA_PYTHONENV_INCLUDE_PATH="/home/blabla/Ma/dependencies/venv-test/include/python3.7m"
MA_WXPYTHON_PATH="/home/blabla/Ma/dependencies/wxPython"
MA_WX_MINOR_VERSION="0"
MA_WX_SO_SUFFIX=".so.0"
MA_CPPCLAY_PATH="/home/blabla/Ma/dependencies/CppClay/CppClay"
MA_CPPCLAYDLL_PATH="/home/blabla/Ma/dependencies/CppClay/CppClay/src/clay"
MA_PYBIND11_PATH="/home/blabla/Ma/dependencies/pybind11"
MA_OGRE_INCLUDE_PATH="/usr/include/OGRE"
MA_LINUX_GTK3_INCLUDE_PATH="/usr/include/gtk-3.0"
MA_LINUX_GLIB_INCLUDE_PATH="/usr/include/glib-2.0"
MA_LINUX_GLIBCONFIG_INCLUDE_PATH="/usr/lib64/glib-2.0/include"
MA_LINUX_PANGO_INCLUDE_PATH="/usr/include/pango-1.0"
MA_LINUX_CAIRO_INCLUDE_PATH="/usr/include/cairo"
MA_LINUX_GDKPIXBUF_INCLUDE_PATH="/usr/include/gdk-pixbuf-2.0"
MA_LINUX_ATK_INCLUDE_PATH="/usr/include/atk-1.0"

« Last Edit: September 14, 2019, 10:25:00 am by Suryavarman »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2539
Re: Squirrel set $(var_name)
« Reply #9 on: September 16, 2019, 08:45:28 pm »
Ok, i found the function to set environment variables and a quick test worked. I did not test your project, but please report if it works for you!

Code: [Select]
EnvvarApplythe syntax is
Code: [Select]
EnvvarApply(wxString vraname, wxString value)this worked for me on windows in the prebuild steps:
Code: [Select]
[[EnvvarApply(_("test"), _("theValueOfTest"));]]
echo %test%

There are other functions, provided by envar plugin:
Code: [Select]
wxArrayString EnvvarGetEnvvarSetNames()
wxArrayString EnvvarGetActiveSetName()
wxArrayString EnvVarGetEnvvarsBySetPath(wxString path)
bool EnvvarSetExists(wxString path)
EnvvarSetApply(wxString setName, bool evenIfActive)
EnvvarSetDiscard(wxString setName) 
EnvvarApply(wxString key, wxString value)
EnvvarDiscard(wxString key)

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2539
Re: Squirrel set $(var_name)
« Reply #10 on: September 16, 2019, 10:13:21 pm »
I have also updated the wiki article: http://wiki.codeblocks.org/index.php/Scripting_commands#Plugins_with_script_bindings and http://wiki.codeblocks.org/index.php/Environment_Variables_plugin if someone found an error please report, or help by fixing it :)
thank you!

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: Squirrel set $(var_name)
« Reply #11 on: September 16, 2019, 11:06:18 pm »
Really thank you.

But i haven't the Envar Plugin, may be the reason is that : I have build codeblocks without this plugin :p

I try to find how to activate this plugin and I will tell you.

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: Squirrel set $(var_name)
« Reply #12 on: September 16, 2019, 11:13:18 pm »
Thats so great, it's Works :)

 8)

Here my code to load a config file:
Code: [Select]
class Dependencies
{
    constructor()
    {
        local deps_config_filename = ::ReplaceMacros(_T("$(PROJECT_DIR)"));
        deps_config_filename += _T("./../../dependencies/dependencies.cfg");

        local config_exist = IO.FileExists(deps_config_filename);
        assert(config_exist);

        if(config_exist)
        {
            local file_content = IO.ReadFileContents(deps_config_filename);
            local groups = GetArrayFromString(file_content, _T("["), true);

            local commands = wxArrayString();

            for(local i = 0; i < groups.GetCount(); i++)
            {
                local group = GetArrayFromString(groups.Item(i), _T("]"), true);

                local groupName = group.Item(0);
                local values = GetArrayFromString(group.Item(1), _T("\n"), true);

                for(local j = 0; j < values.GetCount(); j++)
                {
                    local keyvalue = GetArrayFromString(values.Item(j), _T("="), true);

                    if(keyvalue.GetCount() != 2)
                    {
                        ShowError(_T("Pour le groupe: " + groupName + " et valeur: " + j + " il y a plus d'un charactère '='."));
                    }

                    local key = keyvalue.Item(0).MakeUpper();
                    local value = keyvalue.Item(1);
                    key = ::EscapeSpaces(key);
                    value = ::EscapeSpaces(value);

                    key_paths.rawset(key, value);

                    EnvvarApply(key, value);
                }
            }
        }

    }

    key_paths = {};
}

function SetBuildOptions(base)
{
    local deps = Dependencies();
}
« Last Edit: September 16, 2019, 11:29:45 pm by Suryavarman »

Offline Suryavarman

  • Multiple posting newcomer
  • *
  • Posts: 31
    • Suryavarman
Re: [RESOLVED]Squirrel set $(var_name)
« Reply #13 on: September 16, 2019, 11:40:31 pm »
I take advantage of the opportunity to ask if codeblocks have plane to:

1 - make a bijection between wxString <-> Squirrel String.

2 - make possible to use wxString to define Data.key like that : 
Code: [Select]
local data = {};
local value = 2;
data.rawset{_T("KEY"), value);

print(data.rawget(_T("KEY"))); // error there are no key.

Actually this code works:
Code: [Select]
local data = {};
local key = _T("KEY");
local value = 2;
data.rawset(key, value);
print(data.rawget(key)); // print 2 because key pointer adress is using by Squirrel to find the key.
data.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2539
Re: [RESOLVED]Squirrel set $(var_name)
« Reply #14 on: September 17, 2019, 10:42:52 am »
Quote
1 - make a bijection between wxString <-> Squirrel String.
we have an experimental build where this was possible. Maybe in the future we will work on this more.
The first step in the near future is to update squirrel to the latest release, because codeblocks uses the old version 2 and not 3.1.

In general you can normally use squirrel strings everywhere, and put them trough
Code: [Select]
_T() in the last step before the codeblocks function...
Code: [Select]
local iAmAsquirrelString = "hello";
iAmAsquirrelString  += " world";
// Now you put it into a codeblocks function, use _T() to convert it
Log(_T(iAmAsquirrelString  ));
Basically the same is the other way around by using the
Code: [Select]
tostring() function:
Code: [Select]
Log(_T(_T("test").tostring()));
I really would like to improve the squirrel support, but there are tons of other things on the todo list, and time is limited...