<commit_before>package asset.pipeline

import org.codehaus.groovy.grails.web.context.ServletContextHolder

class DirectiveProcessor {

  static DIRECTIVES = [require_self: "requireSelfDirective" ,require_tree: "requireTreeDirective", require: "requireFileDirective"]

  def contentType
  def files = []
  def servletContext = ServletContextHolder.getServletContext()

  DirectiveProcessor(contentType) {
    this.contentType = contentType
  }

  def compile(file) {
    if(file.class.name == 'java.io.File') {
      return file.getBytes()
    }
    this.files = []
    def tree = getDependencyTree(file)
    def buffer = ""

    buffer = loadContentsForTree(tree,buffer)
    return buffer
  }

  def getFlattenedRequireList(file) {
    if(file.class.name == 'java.io.File') {
      return relativePath(file)
    }
    def flattenedList = []
    def tree = getDependencyTree(file)

    flattenedList = loadRequiresForTree(tree, flattenedList)
    return flattenedList
  }

  def loadRequiresForTree(treeSet, flattenedList) {
    def selfLoaded = false
    for(childTree in treeSet.tree) {
      if(childTree == "self") {
        def extension = treeSet.file.compiledExtension
        def fileName = AssetHelper.fileNameWithoutExtensionFromArtefact(relativePath(treeSet.file.file, true),treeSet.file)
        flattenedList << "${fileName}.${extension}"
        selfLoaded = true
      } else {
        flattenedList = loadRequiresForTree(childTree, flattenedList)
      }
    }

    if(!selfLoaded) {
      def extension = treeSet.file.compiledExtension
      def fileName = AssetHelper.fileNameWithoutExtensionFromArtefact(relativePath(treeSet.file.file, true),treeSet.file)
      flattenedList << "${fileName}.${extension}"
    }
    return flattenedList
  }

  def loadContentsForTree(treeSet,buffer) {
    def selfLoaded = false
    for(childTree in treeSet.tree) {
      if(childTree == "self") {
        buffer += fileContents(treeSet.file) + "\n"
        selfLoaded = true
      } else {
        buffer = loadContentsForTree(childTree,buffer)
      }
    }

    if(!selfLoaded) {
      buffer += fileContents(treeSet.file) + "\n"
    }
    return buffer
  }

	def getDependencyTree(file) {
    this.files << file
		def tree = [file:file,tree:[]]
    if(file.class.name != 'java.io.File') {
      this.findDirectives(file,tree)
    }

    return tree
	}

  def fileContents(file) {

    if(file.class.name == 'java.io.File') {

      return file.text
    }
    return file.processedStream()
  }

	def findDirectives(fileSpec, tree) {
		// try {
			fileSpec.file.eachLine { line ->
        if(!line) {
          return false
          throw "End of Directive Set"
        }
        def directive = fileSpec.directiveForLine(line)
				if(directive) {
					directive = directive.trim()
          def unprocessedArgs = directive.split(" ")

          def processor = DIRECTIVES[unprocessedArgs[0].toLowerCase()]

          if(processor) {
            def directiveArguments = new groovy.text.GStringTemplateEngine().createTemplate(directive).make().toString().split(" ")
            directiveArguments[0] = directiveArguments[0].toLowerCase()
            this."${processor}"(directiveArguments, fileSpec,tree)
          }
				}
        return true
			}
		// } catch(except) {
  //     //TODO: Narrow exception scope here please!
  //     log.info "Done Processing Directive for File"
		// }
	}

  def requireSelfDirective(command, file, tree) {
    tree.tree << "self"
  }

  def requireTreeDirective(command, fileSpec, tree) {
    def parentFile
    if(!command[1] || command[1] == '.') {
      parentFile = new File(fileSpec.file.getParent())
      // println "Requiring Tree for File: ${parentFile}"
    } else {
      parentFile = new File([fileSpec.file.getParent(),command[1]].join(File.separator))
    }

    recursiveTreeAppend(parentFile,tree)
  }

  def recursiveTreeAppend(directory,tree) {
    def files = directory.listFiles()
    files = files.sort { a, b -> a.name.compareTo b.name }
    for(file in files) {
      // println("Finding FIle with Type: ${AssetHelper.assetMimeTypeForURI(file.getAbsolutePath())} against ${contentType}")
      if(file.isDirectory()) {
        recursiveTreeAppend(file,tree)
      }
      else if(AssetHelper.assetMimeTypeForURI(file.getAbsolutePath()) == contentType) {
        if(!isFileInTree(file,tree)) {
          // println "Appending to Tree, ${file}"
          tree.tree << getDependencyTree(AssetHelper.artefactForFile(file,contentType))
        }
      }
    }
  }

  def isFileInTree(file,currentTree) {
    def realFile = file
    if(file.class.name != 'java.io.File') {
      realFile = file.file
    }
    def result = files.find { it -><commit_msg>correcting bug in recursive tree append<commit_after>package asset.pipeline

import org.codehaus.groovy.grails.web.context.ServletContextHolder

class DirectiveProcessor {

  static DIRECTIVES = [require_self: "requireSelfDirective" ,require_tree: "requireTreeDirective", require: "requireFileDirective"]

  def contentType
  def files = []
  def servletContext = ServletContextHolder.getServletContext()

  DirectiveProcessor(contentType) {
    this.contentType = contentType
  }

  def compile(file) {
    if(file.class.name == 'java.io.File') {
      return file.getBytes()
    }
    this.files = []
    def tree = getDependencyTree(file)
    def buffer = ""

    buffer = loadContentsForTree(tree,buffer)
    return buffer
  }

  def getFlattenedRequireList(file) {
    if(file.class.name == 'java.io.File') {
      return relativePath(file)
    }
    def flattenedList = []
    def tree = getDependencyTree(file)

    flattenedList = loadRequiresForTree(tree, flattenedList)
    return flattenedList
  }

  def loadRequiresForTree(treeSet, flattenedList) {
    def selfLoaded = false
    for(childTree in treeSet.tree) {
      if(childTree == "self") {
        def extension = treeSet.file.compiledExtension
        def fileName = AssetHelper.fileNameWithoutExtensionFromArtefact(relativePath(treeSet.file.file, true),treeSet.file)
        flattenedList << "${fileName}.${extension}"
        selfLoaded = true
      } else {
        flattenedList = loadRequiresForTree(childTree, flattenedList)
      }
    }

    if(!selfLoaded) {
      def extension = treeSet.file.compiledExtension
      def fileName = AssetHelper.fileNameWithoutExtensionFromArtefact(relativePath(treeSet.file.file, true),treeSet.file)
      flattenedList << "${fileName}.${extension}"
    }
    return flattenedList
  }

  def loadContentsForTree(treeSet,buffer) {
    def selfLoaded = false
    for(childTree in treeSet.tree) {
      if(childTree == "self") {
        buffer += fileContents(treeSet.file) + "\n"
        selfLoaded = true
      } else {
        buffer = loadContentsForTree(childTree,buffer)
      }
    }

    if(!selfLoaded) {
      buffer += fileContents(treeSet.file) + "\n"
    }
    return buffer
  }

	def getDependencyTree(file) {
    this.files << file
		def tree = [file:file,tree:[]]
    if(file.class.name != 'java.io.File') {
      this.findDirectives(file,tree)
    }

    return tree
	}

  def fileContents(file) {

    if(file.class.name == 'java.io.File') {

      return file.text
    }
    return file.processedStream()
  }

	def findDirectives(fileSpec, tree) {
		// try {
			fileSpec.file.eachLine { line ->
        if(!line) {
          return false
          throw "End of Directive Set"
        }
        def directive = fileSpec.directiveForLine(line)
				if(directive) {
					directive = directive.trim()
          def unprocessedArgs = directive.split(" ")

          def processor = DIRECTIVES[unprocessedArgs[0].toLowerCase()]

          if(processor) {
            def directiveArguments = new groovy.text.GStringTemplateEngine().createTemplate(directive).make().toString().split(" ")
            directiveArguments[0] = directiveArguments[0].toLowerCase()
            this."${processor}"(directiveArguments, fileSpec,tree)
          }
				}
        return true
			}
		// } catch(except) {
  //     //TODO: Narrow exception scope here please!
  //     log.info "Done Processing Directive for File"
		// }
	}

  def requireSelfDirective(command, file, tree) {
    tree.tree << "self"
  }

  def requireTreeDirective(command, fileSpec, tree) {
    def parentFile
    if(!command[1] || command[1] == '.') {
      parentFile = new File(fileSpec.file.getParent())
      // println "Requiring Tree for File: ${parentFile}"
    } else {
      parentFile = new File([fileSpec.file.getParent(),command[1]].join(File.separator))
    }

    recursiveTreeAppend(parentFile,tree)
  }

  def recursiveTreeAppend(directory,tree) {
    def files = directory.listFiles()
    files = files?.sort { a, b -> a.name.compareTo b.name }
    for(file in files) {
      // println("Finding FIle with Type: ${AssetHelper.assetMimeTypeForURI(file.getAbsolutePath())} against ${contentType}")
      if(file.isDirectory()) {
        recursiveTreeAppend(file,tree)
      }
      else if(AssetHelper.assetMimeTypeForURI(file.getAbsolutePath()) == contentType) {
        if(!isFileInTree(file,tree)) {
          // println "Appending to Tree, ${file}"
          tree.tree << getDependencyTree(AssetHelper.artefactForFile(file,contentType))
        }
      }
    }
  }

  def isFileInTree(file,currentTree) {
    def realFile = file
    if(file.class.name != 'java.io.File') {
      realFile = file.file
    }
    def result = files.find { it ->
{'fraction_non_alphanumeric': 0.12080465854949708, 'fraction_numerical': 0.001270513499205929, 'mean_word_length': 2.9227574750830563, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1686689', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2854, 'n_words': 531}