mirror of
https://github.com/atom/atom.git
synced 2026-02-15 09:05:58 -05:00
135 lines
3.9 KiB
CoffeeScript
135 lines
3.9 KiB
CoffeeScript
{Emitter} = require 'emissary'
|
|
fsUtils = require './fs-utils'
|
|
_ = require './underscore-extensions'
|
|
Package = require './package'
|
|
path = require 'path'
|
|
|
|
module.exports =
|
|
class PackageManager
|
|
_.extend @prototype, Emitter
|
|
|
|
constructor: ({configDirPath, devMode, @resourcePath}) ->
|
|
@packageDirPaths = [path.join(configDirPath, "packages")]
|
|
if devMode
|
|
@packageDirPaths.unshift(path.join(configDirPath, "dev", "packages"))
|
|
|
|
@loadedPackages = {}
|
|
@activePackages = {}
|
|
@packageStates = {}
|
|
|
|
getPackageState: (name) ->
|
|
@packageStates[name]
|
|
|
|
setPackageState: (name, state) ->
|
|
@packageStates[name] = state
|
|
|
|
activatePackages: ->
|
|
@activatePackage(pack.name) for pack in @getLoadedPackages()
|
|
|
|
activatePackage: (name, options) ->
|
|
if pack = @loadPackage(name, options)
|
|
@activePackages[pack.name] = pack
|
|
pack.activate(options)
|
|
pack
|
|
|
|
deactivatePackages: ->
|
|
@deactivatePackage(pack.name) for pack in @getActivePackages()
|
|
|
|
deactivatePackage: (name) ->
|
|
if pack = @getActivePackage(name)
|
|
@setPackageState(pack.name, state) if state = pack.serialize?()
|
|
pack.deactivate()
|
|
delete @activePackages[pack.name]
|
|
else
|
|
throw new Error("No active package for name '#{name}'")
|
|
|
|
getActivePackage: (name) ->
|
|
@activePackages[name]
|
|
|
|
isPackageActive: (name) ->
|
|
@getActivePackage(name)?
|
|
|
|
getActivePackages: ->
|
|
_.values(@activePackages)
|
|
|
|
loadPackages: ->
|
|
# Ensure atom exports is already in the require cache so the load time
|
|
# of the first package isn't skewed by being the first to require atom
|
|
require '../exports/atom'
|
|
|
|
@loadPackage(name) for name in @getAvailablePackageNames() when not @isPackageDisabled(name)
|
|
@emit 'loaded'
|
|
|
|
loadPackage: (name, options) ->
|
|
if @isPackageDisabled(name)
|
|
return console.warn("Tried to load disabled package '#{name}'")
|
|
|
|
if packagePath = @resolvePackagePath(name)
|
|
return pack if pack = @getLoadedPackage(name)
|
|
|
|
pack = Package.load(packagePath, options)
|
|
if pack.metadata?.theme
|
|
atom.themes.register(pack)
|
|
else
|
|
@loadedPackages[pack.name] = pack
|
|
pack
|
|
else
|
|
throw new Error("Could not resolve '#{name}' to a package path")
|
|
|
|
unloadPackage: (name) ->
|
|
if @isPackageActive(name)
|
|
throw new Error("Tried to unload active package '#{name}'")
|
|
|
|
if pack = @getLoadedPackage(name)
|
|
delete @loadedPackages[pack.name]
|
|
else
|
|
throw new Error("No loaded package for name '#{name}'")
|
|
|
|
resolvePackagePath: (name) ->
|
|
return name if fsUtils.isDirectorySync(name)
|
|
|
|
packagePath = fsUtils.resolve(@packageDirPaths..., name)
|
|
return packagePath if fsUtils.isDirectorySync(packagePath)
|
|
|
|
packagePath = path.join(@resourcePath, 'node_modules', name)
|
|
return packagePath if @isInternalPackage(packagePath)
|
|
|
|
isInternalPackage: (packagePath) ->
|
|
{engines} = Package.loadMetadata(packagePath, true)
|
|
engines?.atom?
|
|
|
|
getLoadedPackage: (name) ->
|
|
@loadedPackages[name]
|
|
|
|
isPackageLoaded: (name) ->
|
|
@getLoadedPackage(name)?
|
|
|
|
getLoadedPackages: ->
|
|
_.values(@loadedPackages)
|
|
|
|
isPackageDisabled: (name) ->
|
|
_.include(config.get('core.disabledPackages') ? [], name)
|
|
|
|
getAvailablePackagePaths: ->
|
|
packagePaths = []
|
|
|
|
for packageDirPath in @packageDirPaths
|
|
for packagePath in fsUtils.listSync(packageDirPath)
|
|
packagePaths.push(packagePath) if fsUtils.isDirectorySync(packagePath)
|
|
|
|
for packagePath in fsUtils.listSync(path.join(@resourcePath, 'node_modules'))
|
|
packagePaths.push(packagePath) if @isInternalPackage(packagePath)
|
|
|
|
_.uniq(packagePaths)
|
|
|
|
getAvailablePackageNames: ->
|
|
_.uniq _.map @getAvailablePackagePaths(), (packagePath) -> path.basename(packagePath)
|
|
|
|
getAvailablePackageMetadata: ->
|
|
packages = []
|
|
for packagePath in @getAvailablePackagePaths()
|
|
name = path.basename(packagePath)
|
|
metadata = @getLoadedPackage(name)?.metadata ? Package.loadMetadata(packagePath, true)
|
|
packages.push(metadata)
|
|
packages
|