WIP: Start on Package class w/ TextMate- & Atom-specific subclasses

The `load` method on the superclass will provide a general template for
loading the package's resource. Each subclass will be responsible for
loading resources in a manner appropriate for the package type. There's
some initial progress on loading TextMate settings as scoped properties,
translating the TextMate scope selectors to CSS-style atom selectors.
This commit is contained in:
Nathan Sobo 2012-12-23 15:36:06 -06:00
parent 515feca7a6
commit a1d28a9f74
4 changed files with 82 additions and 18 deletions

View File

@ -0,0 +1,17 @@
Package = require 'package'
fs = require 'fs'
module.exports =
class AtomPackage extends Package
constructor: ->
super
@module = require(@path)
@module.name = @name
load: ->
try
rootView.activatePackage(@module)
extensionKeymapPath = require.resolve(fs.join(@name, "src/keymap"), verifyExistence: false)
require extensionKeymapPath if fs.exists(extensionKeymapPath)
catch e
console.error "Failed to load package named '#{name}'", e.stack

View File

@ -1,6 +1,8 @@
TextMateBundle = require("text-mate-bundle")
fs = require 'fs'
_ = require 'underscore'
Package = require 'package'
TextMatePackage = require 'text-mate-package'
messageIdCounter = 1
originalSendMessageToBrowserProcess = atom.sendMessageToBrowserProcess
@ -18,7 +20,7 @@ _.extend atom,
_.unique(allPackageNames)
getAvailableTextMateBundles: ->
@getAvailablePackages().filter (packageName) => @isTextMateBundle(packageName)
@getAvailablePackages().filter (packageName) => TextMatePackage.testName(packageName)
loadPackages: (packageNames=@getAvailablePackages()) ->
disabledPackages = config.get("core.disabledPackages") ? []
@ -26,20 +28,7 @@ _.extend atom,
@loadPackage(packageName) unless _.contains(disabledPackages, packageName)
loadPackage: (name) ->
try
if @isTextMateBundle(name)
TextMateBundle.load(name)
else
packagePath = require.resolve(name, verifyExistence: false)
throw new Error("No package found named '#{name}'") unless packagePath
packagePath = fs.directory(packagePath)
packageModule = require(packagePath)
packageModule.name = name
rootView.activatePackage(packageModule)
extensionKeymapPath = require.resolve(fs.join(name, "src/keymap"), verifyExistence: false)
require extensionKeymapPath if fs.exists(extensionKeymapPath)
catch e
console.error "Failed to load package named '#{name}'", e.stack
Package.forName(name).load()
open: (args...) ->
@sendMessageToBrowserProcess('open', args)
@ -104,6 +93,3 @@ _.extend atom,
if name is 'reply'
[messageId, callbackIndex] = data.shift()
@pendingBrowserProcessCallbacks[messageId]?[callbackIndex]?(data...)
isTextMateBundle: (packageName) ->
/(\.|_|-)tmbundle$/.test(packageName)

21
src/app/package.coffee Normal file
View File

@ -0,0 +1,21 @@
fs = require 'fs'
module.exports =
class Package
@forName: (name) ->
AtomPackage = require 'atom-package'
TextMatePackage = require 'text-mate-package'
if TextMatePackage.testName(name)
new TextMatePackage(name)
else
new AtomPackage(name)
constructor: (@name) ->
@path = require.resolve(@name, verifyExistence: false)
throw new Error("No package found named '#{@name}'") unless @path
@path = fs.directory(@path) unless fs.isDirectory(@path)
load: ->
# WIP: Going to load scoped properties into `syntax` global here
@getScopedProperties()

View File

@ -0,0 +1,40 @@
Package = require 'package'
TextMateBundle = require 'text-mate-bundle'
fs = require 'fs'
plist = require 'plist'
module.exports =
class TextMatePackage extends Package
@testName: (packageName) ->
/(\.|_|-)tmbundle$/.test(packageName)
@translateScopeSelector: (scopeSelector) ->
scopeSelector.split(', ').map((commaFragment) ->
commaFragment.split(' ').map((spaceFragment) ->
spaceFragment.split('.').map((dotFragment) ->
'.' + dotFragment
).join('')
).join(' ')
).join(', ')
load: ->
TextMateBundle.load(@name)
super
constructor: ->
super
@preferencesPath = fs.join(@path, "Preferences")
@syntaxesPath = fs.join(@path, "Syntaxes")
getScopedProperties: ->
scopedProperties = []
if fs.exists(@preferencesPath)
for preferencePath in fs.list(@preferencesPath)
plist.parseString fs.read(preferencePath), (e, data) ->
if e
console.warn "Failed to parse preference at path '#{preferencePath}'", e.stack
else
{ scope, settings } = data[0]
selector = TextMatePackage.translateScopeSelector(scope)
scopedProperties.push({selector: selector, properties: settings})
scopedProperties