]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Major refactor: Rework all UI and related improvements
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 { classes: Cc, interfaces: Ci, utils: Cu } = Components
22
23 Cu.import('resource://gre/modules/Services.jsm')
24 Cu.import('resource://gre/modules/devtools/Console.jsm')
25
26 shutdownHandlers = []
27
28 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
29 baseURI = createURI(__SCRIPT_URI_SPEC__)
30
31 # Everything up to the first `!` is the absolute path to the .xpi.
32 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
33
34 require = (path, moduleRoot = '.', dir = '.') ->
35 unless path[0] == '.'
36 # Allow `require('module/lib/foo')` in additon to just `require('module')`.
37 [ match, name, subPath ] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
38 base = require.data[moduleRoot]?[name] ? moduleRoot
39 dir = "#{ base }/node_modules/#{ name }"
40 main = require.data[dir]?['']
41 path = subPath ? main ? 'index'
42 moduleRoot = dir
43
44 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
45
46 unless require.scopes[fullPath]?
47 module =
48 exports: {}
49 onShutdown: Function::call.bind(Array::push, shutdownHandlers)
50 require.scopes[fullPath] = scope =
51 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
52 module: module
53 exports: module.exports
54 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
55
56 return require.scopes[fullPath].module.exports
57
58 require.scopes = {}
59 require.data = require('./require-data')
60
61 # Set default prefs and apply migrations as early as possible.
62 { applyMigrations } = require('./lib/legacy')
63 migrations = require('./lib/migrations')
64 prefs = require('./lib/prefs')
65
66 prefs.default._init()
67 applyMigrations(migrations)
68
69 do (global = this) ->
70
71 global.startup = require('./lib/main')
72
73 global.shutdown = (data, reason) ->
74 for shutdownHandler in shutdownHandlers
75 try
76 shutdownHandler()
77 catch error
78 Cu.reportError(error)
79 shutdownHandlers = null
80
81 # Release everything in `require`d modules. This must be done _after_ all
82 # shutdownHandlers, since they use variables in these scopes.
83 for path, scope of require.scopes
84 for name of scope
85 scope[name] = null
86 require.scopes = null
87
88 global.install = (data, reason) ->
89
90 global.uninstall = (data, reason) ->
Imprint / Impressum