     packageName = path.basename(packageName)
     /(^language-.+)|((\.|_|-)tmbundle$)/.test(packageName)
 
-  @getActivationQueue: ->
-    return @activationQueue if @activationQueue?
-    @activationQueue = async.queue (pack, done) ->
-      pack.loadGrammars ->
-        pack.loadScopedProperties(done)
+  @addPackageToActivationQueue: (pack)->
+    @activationQueue ?= []
+    @activationQueue.push(pack)
+    @activateNextPacakageInQueue() if @activationQueue.length == 1
 
-    @activationQueue
+  @activateNextPacakageInQueue: ->
+    if pack = @activationQueue[0]
+      pack.loadGrammars()
+        .then ->
+          pack.loadScopedProperties()
+        .then ->
+          @activationQueue.shift()
+          @activateNextPacakageInQueue()
 
   constructor: ->
     super


  legalGrammarExtensions: ['plist', 'tmLanguage', 'tmlanguage', 'json', 'cson']

  loadGrammars: (done) ->
    fs.isDirectory @getSyntaxesPath(), (isDirectory) =>
      if isDirectory
        fs.list @getSyntaxesPath(), @legalGrammarExtensions, (error, paths) =>
          if error?
            console.log("Error loading grammars of TextMate package '#{@path}':", error.stack, error)
            done()
          else
            async.eachSeries(paths, @loadGrammarAtPath, done)
      else
        done()

  loadGrammarAtPath: (grammarPath, done) =>
    atom.syntax.readGrammar grammarPath, (error, grammar) =>
      if error?
        console.log("Error loading grammar at path '#{grammarPath}':", error.stack ? error)
      else
        @addGrammar(grammar)
        done?()

  loadGrammarsSync: ->
    for grammarPath in fs.listSync(@getSyntaxesPath(), @legalGrammarExtensions)
      @addGrammar(atom.syntax.readGrammarSync(grammarPath))

  addGrammar: (grammar) ->
    @grammars.push(grammar)

_ = require 'underscore-plus'
fs = require 'fs-plus'
async = require 'async'
Q = require 'q'

### Internal ###

