From 50e27854cce7919583f1ff7a30fe550cdab69db9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 30 Jul 2014 10:49:25 -0700 Subject: [PATCH] Cache package metadata in main package.json file --- build/tasks/build-task.coffee | 2 +- build/tasks/compile-packages-slug-task.coffee | 35 +++++++++++++++++++ src/package.coffee | 30 +++++++++++----- 3 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 build/tasks/compile-packages-slug-task.coffee diff --git a/build/tasks/build-task.coffee b/build/tasks/build-task.coffee index 1aefcd6f5..585bb17b6 100644 --- a/build/tasks/build-task.coffee +++ b/build/tasks/build-task.coffee @@ -153,7 +153,7 @@ module.exports = (grunt) -> fs.writeFileSync path.join(appDir, 'node_modules', 'symbols-view', 'vendor', 'ctags-win32.exe.ignore'), '' fs.writeFileSync path.join(shellAppDir, 'atom.exe.gui'), '' - dependencies = ['compile', 'generate-license:save', 'generate-module-cache'] + dependencies = ['compile', 'generate-license:save', 'generate-module-cache', 'compile-packages-slug'] dependencies.push('copy-info-plist') if process.platform is 'darwin' dependencies.push('set-exe-icon') if process.platform is 'win32' grunt.task.run(dependencies...) diff --git a/build/tasks/compile-packages-slug-task.coffee b/build/tasks/compile-packages-slug-task.coffee new file mode 100644 index 000000000..17f94c740 --- /dev/null +++ b/build/tasks/compile-packages-slug-task.coffee @@ -0,0 +1,35 @@ +path = require 'path' +CSON = require 'season' +fs = require 'fs-plus' + +module.exports = (grunt) -> + {spawn} = require('./task-helpers')(grunt) + + grunt.registerTask 'compile-packages-slug', 'Add package metadata information to to the main package.json file', -> + appDir = grunt.config.get('atom.appDir') + + modulesDirectory = path.join(appDir, 'node_modules') + packages = {} + + for moduleDirectory in fs.listSync(modulesDirectory) + continue if path.basename(moduleDirectory) is '.bin' + + metadata = grunt.file.readJSON(path.join(moduleDirectory, 'package.json')) + continue unless metadata?.engines?.atom? + + pack = {metadata, keymaps: {}, menus: {}} + + for keymapPath in fs.listSync(path.join(moduleDirectory, 'keymaps'), ['.cson', '.json']) + keymapPath = path.relative(appDir, keymapPath) + pack.keymaps[keymapPath] = CSON.readFileSync(keymapPath) + + for menuPath in fs.listSync(path.join(moduleDirectory, 'menus'), ['.cson', '.json']) + menuPath = path.relative(appDir, menuPath) + pack.menus[menuPath] = CSON.readFileSync(menuPath) + + packages[metadata.name] = pack + + metadata = grunt.file.readJSON(path.join(appDir, 'package.json')) + metadata._atomPackages = packages + + grunt.file.write(path.join(appDir, 'package.json'), JSON.stringify(metadata, null, 2)) diff --git a/src/package.coffee b/src/package.coffee index 42d05d433..5ced991d2 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -13,6 +13,11 @@ $ = null # Defer require in case this is in the window-less browser process ModuleCache = require './module-cache' ScopedProperties = require './scoped-properties' +try + packagesCache = require('../package.json') +catch + packagesCache = {} + # Loads and activates a package's main module and resources such as # stylesheets, keymaps, grammar, editor properties, and menus. module.exports = @@ -22,13 +27,16 @@ class Package @stylesheetsDir: 'stylesheets' @loadMetadata: (packagePath, ignoreErrors=false) -> - if metadataPath = CSON.resolve(path.join(packagePath, 'package')) - try - metadata = CSON.readFileSync(metadataPath) - catch error - throw error unless ignoreErrors + packageName = path.basename(packagePath) + metadata = packagesCache[packageName]?.metadata + unless metadata? + if metadataPath = CSON.resolve(path.join(packagePath, 'package')) + try + metadata = CSON.readFileSync(metadataPath) + catch error + throw error unless ignoreErrors metadata ?= {} - metadata.name = path.basename(packagePath) + metadata.name = packageName metadata keymaps: null @@ -177,10 +185,16 @@ class Package @scopedPropertiesActivated = true loadKeymaps: -> - @keymaps = @getKeymapPaths().map (keymapPath) -> [keymapPath, CSON.readFileSync(keymapPath)] + if packagesCache[@name]? + @keymaps = ([keymapPath, keymapObject] for keymapPath, keymapObject of packagesCache[@name].keymaps) + else + @keymaps = @getKeymapPaths().map (keymapPath) -> [keymapPath, CSON.readFileSync(keymapPath)] loadMenus: -> - @menus = @getMenuPaths().map (menuPath) -> [menuPath, CSON.readFileSync(menuPath)] + if packagesCache[@name]? + @menus = ([menuPath, menuObject] for menuPath, menuObject of packagesCache[@name].menus) + else + @menus = @getMenuPaths().map (menuPath) -> [menuPath, CSON.readFileSync(menuPath)] getKeymapPaths: -> keymapsDirPath = path.join(@path, 'keymaps')