From c413f024f2d37869b55b9fbf0a10404b087a8fdd Mon Sep 17 00:00:00 2001 From: Matt Colyer Date: Fri, 13 Sep 2013 16:21:26 -0700 Subject: [PATCH] Make the build green --- src/atom-package.coffee | 9 +++--- src/package.coffee | 3 ++ src/theme-manager.coffee | 65 ++++++++++++++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/atom-package.coffee b/src/atom-package.coffee index 5f4b7bea4..26846a2e6 100644 --- a/src/atom-package.coffee +++ b/src/atom-package.coffee @@ -13,6 +13,8 @@ module.exports = class AtomPackage extends Package _.extend @prototype, EventEmitter + @stylesheetsDir: 'stylesheets' + metadata: null keymaps: null stylesheets: null @@ -84,17 +86,16 @@ class AtomPackage extends Package @stylesheets = @getStylesheetPaths().map (stylesheetPath) -> [stylesheetPath, loadStylesheet(stylesheetPath)] getStylesheetsPath: -> - path.join(@path, 'stylesheets') + path.join(@path, @constructor.stylesheetsDir) getStylesheetPaths: -> stylesheetDirPath = @getStylesheetsPath() - indexStylesheet = fsUtils.resolve(@path, 'index', ['css', 'less']) if @metadata.stylesheetMain - [fsUtils.resolve(stylesheetDirPath, @metadata.stylesheetMain)] + [fsUtils.resolve(@path, @metadata.stylesheetMain)] else if @metadata.stylesheets @metadata.stylesheets.map (name) -> fsUtils.resolve(stylesheetDirPath, name, ['css', 'less', '']) - else if indexStylesheet + else if indexStylesheet = fsUtils.resolve(@path, 'index', ['css', 'less']) [indexStylesheet] else fsUtils.listSync(stylesheetDirPath, ['css', 'less']) diff --git a/src/package.coffee b/src/package.coffee index ecdd9a5b0..1ada8fbbc 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -36,3 +36,6 @@ class Package isActive: -> atom.isPackageActive(@name) + + isTheme: -> + !!@metadata?.theme diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index 6e38a19c3..070b063cb 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -1,5 +1,7 @@ path = require 'path' EventEmitter = require 'event-emitter' +Package = require 'package' +AtomPackage = require 'atom-package' _ = require 'underscore' @@ -11,22 +13,28 @@ class ThemeManager _.extend @prototype, EventEmitter constructor: -> - @registeredThemes = [] + @loadedThemes = [] @activeThemes = [] + # Internal-only: register: (theme) -> - @registeredThemes.push(theme) + @loadedThemes.push(theme) + theme + # Internal-only: getAvailableNames: -> - _.map(@registeredThemes, (t) -> t?.metadata?.name) + _.map(@loadedThemes, (t) -> t.metadata.name) + # Internal-only: getActiveThemes: -> _.clone(@activeThemes) + # Internal-only: unload: -> removeStylesheet(@userStylesheetPath) if @userStylesheetPath? theme.deactivate() while theme = @activeThemes.pop() + # Internal-only: load: -> config.observe 'core.themes', (themeNames) => @unload() @@ -36,17 +44,48 @@ class ThemeManager @trigger('reloaded') - activateTheme: (name) -> - theme = _.find(@registeredThemes, (t) -> t.metadata.name == name) - return console.warn("Theme '#{name}' not found.") unless theme + # Private: + loadTheme: (name, options) -> + if themePath = @resolveThemePath(name) + return theme if theme = @getLoadedTheme(name) + pack = Package.load(themePath, options) + if pack.isTheme() + @register(pack) + else + throw new Error("Attempted to load a non-theme package '#{name}' as a theme") + else + throw new Error("Could not resolve '#{name}' to a theme path") + # Private: + getLoadedTheme: (name) -> + _.find(@loadedThemes, (t) -> t.metadata.name == name) + + # Private: + resolveThemePath: (name) -> + return name if fsUtils.isDirectorySync(name) + + packagePath = fsUtils.resolve(config.packageDirPaths..., name) + return packagePath if fsUtils.isDirectorySync(packagePath) + + packagePath = path.join(window.resourcePath, 'node_modules', name) + return packagePath if @isThemePath(packagePath) + + # Private: + isThemePath: (packagePath) -> + {engines, theme} = Package.loadMetadata(packagePath, true) + engines?.atom? and theme + + # Private: + activateTheme: (name) -> try + theme = @loadTheme(name) theme.activate() @activeThemes.push(theme) @trigger('theme-activated', theme) catch error console.warn("Failed to load theme #{name}", error.stack ? error) + # Public: getUserStylesheetPath: -> stylesheetPath = fsUtils.resolve(path.join(config.configDirPath, 'user'), ['css', 'less']) if fsUtils.isFileSync(stylesheetPath) @@ -54,17 +93,17 @@ class ThemeManager else null + # Public: getImportPaths: -> - if @activeThemes.length - theme.getStylesheetPath() for theme in @activeThemes when theme.getStylesheetPath + if @activeThemes.length > 0 + themePaths = (theme.getStylesheetsPath() for theme in @activeThemes when theme) else themeNames = config.get('core.themes') - themes = [] - for themeName in themeNames - theme = _.find(@registeredTheme, (t) -> t.metadata.name == themeName) - themes.push(theme.getStylesheetPath()) if theme?.getStylesheetPath - themes + themePaths = (path.join(@resolveThemePath(themeName), AtomPackage.stylesheetsDir) for themeName in themeNames) + themePath for themePath in themePaths when fsUtils.isDirectorySync(themePath) + + # Private: loadUserStylesheet: -> if userStylesheetPath = @getUserStylesheetPath() @userStylesheetPath = userStylesheetPath