summaryrefslogtreecommitdiff
path: root/node_modules/jake/lib/namespace.js
blob: e614665253a9ff998cc79c7304a4f6bf50b09f3b (plain) (blame)
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
const ROOT_NAMESPACE_NAME = '__rootNamespace__';

class Namespace {
  constructor(name, parentNamespace) {
    this.name = name;
    this.parentNamespace = parentNamespace;
    this.childNamespaces = {};
    this.tasks = {};
    this.rules = {};
    this.path = this.getPath();
    this.fullName = this.getFullName();

    //console.log('namespace name', this.name);
    //console.log('namespace path', this.path);
    //console.log('namespace fullName', this.fullName);
  }

  resolveTask(name) {
    // Namespaced, exact matches only
    let task;
    if (name.indexOf(':') > -1) {
      task = jake.Task[name];
    }
    // Bare task name, look on the current namespace
    // Or in top-level namespace
    else {
      task = this.tasks[name] || jake.Task[name];
    }
    return task || null;
  }


  resolveNamespace(relativeName) {
    //console.log(relativeName || 'none passed');

    if (!relativeName) {
      return this;
    }

    let parts = relativeName.split(':');
    let ns = this;

    for (let i = 0, ii = parts.length; ns && i < ii; i++) {
      ns = ns.childNamespaces[parts[i]];
    }

    return (ns || (this.parentNamespace &&
        this.parentNamespace.resolveNamespace(relativeName)));
  }

  matchRule(relativeName) {
    let parts = relativeName.split(':');
    parts.pop();
    let ns = this.resolveNamespace(parts.join(':'));
    let rules = ns ? ns.rules : [];
    let r;
    let match;

    for (let p in rules) {
      r = rules[p];
      if (r.match(relativeName)) {
        match = r;
      }
    }

    return (ns && match) ||
        (this.parentNamespace &&
        this.parentNamespace.matchRule(relativeName));
  }

  getPath() {
    let parts = [];
    let next = this.parentNamespace;
    while (next) {
      parts.push(next.name);
      next = next.parentNamespace;
    }
    parts.pop(); // Remove '__rootNamespace__'
    return parts.reverse().join(':');
  }

  getFullName() {
    let path = this.path;
    path = (path && path.split(':')) || [];
    path.push(this.name);
    return path.join(':');
  }

  isRootNamespace() {
    return !this.parentNamespace;
  }
}

Namespace.ROOT_NAMESPACE_NAME = ROOT_NAMESPACE_NAME;

module.exports.Namespace = Namespace;