2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
5 # This file is part of VimFx.
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.
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.
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/>.
21 # This file boots the main VimFx process, as well as each frame script. It tries
22 # to do the minimum amount of things to run main.coffee, or main-frame.coffee
23 # for frame scripts. It defines a few global variables, and sets up a
24 # Node.js-style `require` module loader.
26 # Expose `Components` shortcuts a well as `Services`, `console` and
27 # `IS_FRAME_SCRIPT` in all modules. The `@`s are needed for frame scripts.
28 { classes: @Cc, interfaces: @Ci, utils: @Cu } = Components
30 Cu.import('resource://gre/modules/Services.jsm')
31 Cu.import('resource://gre/modules/devtools/Console.jsm')
33 @IS_FRAME_SCRIPT = (typeof content != 'undefined')
38 [ global.__SCRIPT_URI_SPEC__ ] = sendSyncMessage('VimFx:tabCreated')
39 return if __SCRIPT_URI_SPEC__ == false
40 global.FRAME_SCRIPT_ENVIRONMENT = global
44 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
45 baseURI = createURI(__SCRIPT_URI_SPEC__)
47 # Everything up to the first `!` is the absolute path to the .xpi.
48 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
50 require = (path, moduleRoot = '.', dir = '.') ->
52 # Allow `require('module/lib/foo')` in additon to `require('module')`.
53 [ match, name, subPath ] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
54 base = require.data[moduleRoot]?[name] ? moduleRoot
55 dir = "#{ base }/node_modules/#{ name }"
56 main = require.data[dir]?['']
57 path = subPath ? main ? 'index'
60 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
62 unless require.scopes[fullPath]?
65 onShutdown: Function::call.bind(Array::push, shutdownHandlers)
66 require.scopes[fullPath] = scope =
67 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
69 exports: module.exports
70 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
72 return require.scopes[fullPath].module.exports
75 require.data = require('./require-data')
77 unless IS_FRAME_SCRIPT
78 # Set default prefs and apply migrations as early as possible.
79 { applyMigrations } = require('./lib/legacy')
80 migrations = require('./lib/migrations')
81 prefs = require('./lib/prefs')
84 applyMigrations(migrations)
86 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
87 global.startup = require(main)
90 require('./lib/message-manager').send('shutdown') unless IS_FRAME_SCRIPT
92 for shutdownHandler in shutdownHandlers
97 shutdownHandlers = null
99 # Release everything in `require`d modules. This must be done _after_ all
100 # shutdownHandlers, since they use variables in these scopes.
101 for path, scope of require.scopes
104 require.scopes = null
108 global.uninstall = ->
111 require('./lib/message-manager').listenOnce('shutdown', shutdown)