summaryrefslogtreecommitdiff
path: root/project/build/AdditionalResources.scala
blob: bf1baf3e6b5f15bca2e5eef7a3c6985a50157edd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sbt._
import java.util.jar.{Manifest}
import java.io.{FileInputStream}
import AdditionalResources._
/**
 * Additional tasks that are required to obtain a complete compiler and library pair, but that are not part of the
 * compilation task. It copies additional files and generates the properties files
 * @author Grégory Moix
 */
trait AdditionalResources {
  self : BasicLayer  =>

 /* lazy val copyAdditionalFiles = task {
      def copy0(steps:List[Step]):Option[String]= steps match{
        case x::xs => x match{
          case c:ResourcesToCopy => {
            c.copy orElse copy0(xs)
          }
          case _ => copy0(xs)
        }
        case Nil => None
      }
     copy0(allSteps.topologicalSort)
  }.dependsOn(externalCompilation)*/

   def writeProperties: Option[String] = {
      def write0(steps:List[Step]):Option[String]= steps match{
        case x::xs => x match{
        case c:PropertiesToWrite => {
            c.writeProperties orElse write0(xs)
          }
          case _ => write0(xs)
        }
        case Nil => None
      }
     write0(allSteps.topologicalSort)
  }



}

object AdditionalResources {
  /**
   * A FileFilter that defines what are the files that will be copied
   */
  lazy val basicFilter =  "*.tmpl" | "*.xml"| "*.js"| "*.css" | "*.properties" | "*.swf" | "*.png"
  implicit def stringToGlob(s:String):NameFilter=GlobFilter(s)
}

trait ResourcesToCopy {
  self : CompilationStep =>
  def getResources(from:Path,filter:FileFilter):PathFinder = (from ##)** filter
  def getResources(from:Path):PathFinder = getResources(from,AdditionalResources.basicFilter)

  def copyDestination:Path
  def filesToCopy:PathFinder
  def copy = {
    log.info("Copying files for "+name)
    try{
      FileUtilities.copy(filesToCopy.get,copyDestination,log)
    }catch{
      case e=>Some(e.toString)
    }
    None
  }
}

trait PropertiesToWrite {
  self : CompilationStep =>

  def propertyList:List[Tuple2[String,String]]
  def propertyDestination:Path

  def writeProperties:Option[String]={
    import java.io._
    import java.util.Properties

    val properties = new Properties

    def insert(list:List[Tuple2[String,String]]):Unit=list match{
      case Nil =>
      case x::xs => {
        properties setProperty(x._1, x._2)
        insert(xs)
      }
    }

    try{
      insert(propertyList)
      val destFile = propertyDestination.asFile
      val stream = new FileOutputStream(destFile)
      properties.store(stream, null)
    }catch{
      case e => Some(e.toString)
    }
    None
  }

}