summaryrefslogtreecommitdiff
path: root/modules/dynamic_link/macros/utils/addPathToEnv.sci
blob: afba19231e114adb2d633610dab5ff943306f533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// =============================================================================
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
//
// This file is distributed under the same license as the Scilab package.
// =============================================================================

function outStrings = removeTrailingChar(inStrings, trailingChar)
    outStrings = [];
    for i = 1:size(inStrings, "*")
        str = inStrings(i);
        if str <> "" then
            l = length(str);
            if part(str, l:l) == trailingChar then
                outStrings(i) = part(str, 1:l-1);
            else
                outStrings(i) = inStrings(i);
            end
        end
    end
endfunction

function res = addPathToEnv(envName, paths)
    if ~isempty(paths) then
        // Retrieve path list to add
        pathsConverted = pathconvert(paths, %t, %f);
        pathToAddList = tokens(pathsConverted, pathsep());
        pathToAddList = removeTrailingChar(pathToAddList, filesep());

        // Retrieve env path list
        envPaths = getenv(envName, "");
        tmpEnvPath = pathconvert(envPaths, %t, %f);
        envPathList = tokens(tmpEnvPath, pathsep());
        envPathList = removeTrailingChar(envPathList, filesep());
        envPathList(find(envPathList == "")) = [];

        // For each path to add to env
        for i = 1:size(pathToAddList, "r")
            pathToAdd = pathToAddList(i);
            lcPathToAdd = convstr(pathToAdd, "l");
            lcEnvPathList = convstr(envPathList, "l");

            // Add path if do not exist in env path list
            found = find(lcPathToAdd == lcEnvPathList);
            if found == [] then
                envPathList = [envPathList; pathToAdd];
            end

            // Get env new value by concatening env path list
            envPathList = removeTrailingChar(envPathList, pathsep());
            envPaths = strcat(envPathList, pathsep());

            // Set the new env value
            if ~setenv(envName, envPaths) then
                errMsg = msprintf("error while setting" ..
                + " environment variable ''%s''", envName);
                error(errMsg);
            end
        end
    end
    res = %t;
endfunction