aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/utils.js
blob: 4d1e08418550b9421f15d5b58b596364e030ac38 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// utility functions for modules
//

function appendAll_internal2(modules, name, property, seenValues)
{
    var result = []
    for (var m in modules) {
        if (m == name) {
            var value = modules[m][property]
            if (value) {
                if (!seenValues[value]) {
                    seenValues[value] = true
                    result = result.concat(value)
                }
            }
        } else {
            var values = appendAll_internal2(modules[m].modules, name, property, seenValues)
            if (values && values.length > 0)
                result = result.concat(values)
        }
    }
    return result
}

function appendAll_internal(modules, name, property)
{
    var seenValues = []
    return appendAll_internal2(modules, name, property, seenValues)
}

function appendAll(config, property)
{
    return appendAll_internal(config.modules, config.module.name, property)
}

function appendAllFromArtifacts(product, artifacts, moduleName, propertyName)
{
    var seenValues = []
    var result = appendAll_internal2(product.modules, moduleName, propertyName, seenValues)
    for (var i in artifacts)
        result = result.concat(appendAll_internal2(artifacts[i].modules, moduleName, propertyName, seenValues))
    return result
}

function findFirst(modules, name, property)
{
    for (var m in modules) {
        if (m == name) {
            var value = modules[m][property]
            if (value)
                return value
        } else {
            var value = findFirst(m.modules, name, property)
            if (value)
                return value
        }
    }
    return null
}

function dumpProperty(key, value, level)
{
    var indent = ''
    for (var k=0; k < level; ++k)
        indent += '  '
    print(indent + key + ': ' + value)
}

function traverseObject(obj, func, level)
{
    if (!level)
        level = 0
    var children = {}
    for (var i in obj) {
        if (typeof(obj[i]) == "object" && !(obj[i] instanceof Array))
            children[i] = obj[i]
        else
            func.apply(this, [i, obj[i], level])
    }
    level++
    for (var i in children) {
        func.apply(this, [i, children[i], level - 1])
        traverseObject(children[i], func, level)
    }
    level--
}

function dumpObject(obj, description)
{
    if (!description)
        description = 'object dump'
    print('+++++++++ ' + description + ' +++++++++')
    traverseObject(obj, dumpProperty)
}


//////////////////////////////////////////////////////////
// The EnvironmentVariable class
//
function EnvironmentVariable(name, separator, convertPathSeparators)
{
    if (!name)
        throw "EnvironmentVariable c'tor needs a name as first argument."
    this.name = name
    this.value = getenv(name).toString()
    this.separator = separator || ''
    this.convertPathSeparators = convertPathSeparators || false
}

EnvironmentVariable.prototype.prepend = function(v)
{
    if (this.value.length > 0 && this.value.charAt(0) != this.separator)
        this.value = this.separator + this.value
    if (this.convertPathSeparators)
        v = FileInfo.toWindowsSeparators(v)
    this.value = v + this.value
}

EnvironmentVariable.prototype.append = function(v)
{
    if (this.value.length > 0)
        this.value += this.separator
    if (this.convertPathSeparators)
        v = FileInfo.toWindowsSeparators(v)
    this.value += v
}

EnvironmentVariable.prototype.set = function()
{
    putenv(this.name, this.value)
}