logo

class

compiler::FindSourceFiles

sys::Obj
  compiler::CompilerSupport
    compiler::CompilerStep
      compiler::FindSourceFiles
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   2 Dec 05  Brian Frank  Creation
   7  //   3 Jun 06  Brian Frank  Port from Java to Fan
   8  //
   9  
  10  **
  11  ** Search the module's source tree for all the fan source files and
  12  ** map them to SourceFile instances and store to Compiler.srcFiles.
  13  ** Also find all the resource files and map them to Compiler.resFiles.
  14  ** For right now, resource files are anything found in {srcDir}/res.
  15  ** Summary:
  16  **   Compiler.srcDir/*.fan -> Compiler.srcFiles
  17  **   Compiler.srcDir/res/* -> Compiler.resFiles
  18  **
  19  class FindSourceFiles : CompilerStep
  20  {
  21  
  22  //////////////////////////////////////////////////////////////////////////
  23  // Constructor
  24  //////////////////////////////////////////////////////////////////////////
  25  
  26    new make(Compiler compiler)
  27      : super(compiler)
  28    {
  29    }
  30  
  31  //////////////////////////////////////////////////////////////////////////
  32  // Run
  33  //////////////////////////////////////////////////////////////////////////
  34  
  35    override Void run()
  36    {
  37      findSrcFiles
  38      findResFiles
  39  
  40      if (compiler.srcFiles.isEmpty && compiler.resFiles.isEmpty)
  41        throw err("No fan source files found", null)
  42    }
  43  
  44  //////////////////////////////////////////////////////////////////////////
  45  // Source Files
  46  //////////////////////////////////////////////////////////////////////////
  47  
  48    private Void findSrcFiles()
  49    {
  50      srcFiles := File[,]
  51      compiler.input.srcDirs.each |File dir| { find(dir, srcFiles, "fan") }
  52      compiler.srcFiles = srcFiles
  53  
  54      if (log.isDebug)
  55      {
  56        log.debug("FindSourceFiles [${srcFiles.size} files]")
  57        log.indent
  58        srcFiles.each |File f| { log.debug("[$f]") }
  59        log.unindent
  60      }
  61      else
  62      {
  63        log.info("FindSourceFiles [${srcFiles.size} files]")
  64      }
  65    }
  66  
  67  //////////////////////////////////////////////////////////////////////////
  68  // Resource Files
  69  //////////////////////////////////////////////////////////////////////////
  70  
  71    private Void findResFiles()
  72    {
  73      resFiles := File[,]
  74      compiler.input.resDirs.each |File dir| { find(dir, resFiles, null) }
  75      compiler.resFiles = resFiles
  76  
  77      if (log.isDebug)
  78      {
  79        log.debug("ResourceFiles [${resFiles.size} files]")
  80        log.indent
  81        resFiles.each |File f| { log.debug("[$f]") }
  82        log.unindent
  83      }
  84    }
  85  
  86  //////////////////////////////////////////////////////////////////////////
  87  // Utils
  88  //////////////////////////////////////////////////////////////////////////
  89  
  90    private Void find(File dir, File[] acc, Str ext)
  91    {
  92      if (!dir.isDir) throw err("Invalid directory", Location.makeFile(dir))
  93      dir.list.each |File f|
  94      {
  95        if (f.isDir) return
  96        if (ext == null || f.ext == ext) acc.add(f)
  97      }
  98    }
  99  
 100  }