🐎 Load packages by path

Previously the package paths were converted to names and then
re-resolved to paths.

Now we just load the paths directly reducing the number of stat calls
and saving ~50ms in PackageManager::loadPackages

Also now internal packages are loaded by parsing the package.json
file for packageDependencies instead of loading the metadata for all
packages in node_modules and checking the engines field.
This commit is contained in:
Kevin Sawicki 2013-10-29 11:05:03 -07:00
parent 1f16cd3912
commit 059671c454
2 changed files with 17 additions and 8 deletions

View File

@ -15,7 +15,7 @@ describe "ThemeManager", ->
describe "theme getters and setters", ->
beforeEach ->
atom.packages.loadPackages()
atom.packages.loadPackages(sync: true)
it 'getLoadedThemes get all the loaded themes', ->
themes = themeManager.getLoadedThemes()

View File

@ -122,23 +122,27 @@ class PackageManager
@observingDisabledPackages = true
loadPackages: ->
loadPackages: (options) ->
# 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)
packagePaths = @getAvailablePackagePaths()
packagePaths = packagePaths.filter (packagePath) => not @isPackageDisabled(path.basename(packagePath))
packagePaths = _.uniq packagePaths, (packagePath) -> path.basename(packagePath)
@loadPackage(packagePath, options) for packagePath in packagePaths
@emit 'loaded'
loadPackage: (name, options) ->
if packagePath = @resolvePackagePath(name)
loadPackage: (nameOrPath, options) ->
if packagePath = @resolvePackagePath(nameOrPath)
name = path.basename(nameOrPath)
return pack if pack = @getLoadedPackage(name)
pack = Package.load(packagePath, options)
@loadedPackages[pack.name] = pack if pack?
pack
else
throw new Error("Could not resolve '#{name}' to a package path")
throw new Error("Could not resolve '#{nameOrPath}' to a package path")
unloadPackages: ->
@unloadPackage(name) for name in _.keys(@loadedPackages)
@ -191,8 +195,13 @@ class PackageManager
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)
try
metadataPath = path.join(@resourcePath, 'package.json')
{packageDependencies} = JSON.parse(fsUtils.read(metadataPath)) ? {}
packagesPath = path.join(@resourcePath, 'node_modules')
for packageName, packageVersion of packageDependencies ? {}
packagePath = path.join(packagesPath, packageName)
packagePaths.push(packagePath) if fsUtils.isDirectorySync(packagePath)
_.uniq(packagePaths)