Classifier = require '../lib/classifier'

module.exports = (grunt) ->
  grunt.registerMultiTask 'classify', 'Convert CoffeeScript classes to AngularJS modules', ->
    opts = @options()
    file = ''
    @files.forEach((filePair) ->
      filePair.src.filter((filePath) ->
        file = grunt.file.read(filePath)
        if !grunt.file.exists filePath
          grunt.log.warn("Source file [#{filePath}] not found.")
          return false
        else if !/^.*@classify\s*\((.|\n)*\)/g.test(file)
          grunt.log.warn("Skipping unannotated file [#{filePath}]")
          return false
        else
          return true
      ).map((filePath) ->
        content = new Classifier(grunt, opts).compile(file);
        grunt.file.write(filePair.dest, content if content)
        grunt.log.ok("Classified [#{filePath}] to [#{filePair.dest}]")
      )
    )