This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
/
Resources.scala
88 lines (83 loc) · 2.65 KB
/
Resources.scala
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
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/* sbt -- Simple Build Tool
* Copyright 2008, 2009 Mark Harrah
*/
package sbt
import java.io.File
import IO._
import Resources.error
object Resources
{
def apply(basePath: String) =
{
require(basePath.startsWith("/"))
val resource = getClass.getResource(basePath)
if(resource == null)
error("Resource base directory '" + basePath + "' not on classpath.")
else
{
val file = toFile(resource)
if(file.exists)
new Resources(file)
else
error("Resource base directory '" + basePath + "' does not exist.")
}
}
def error(msg: String) = throw new ResourcesException(msg)
private val LoadErrorPrefix = "Error loading initial project: "
}
class ResourcesException(msg: String) extends Exception(msg)
class Resources(val baseDirectory: File)
{
import Resources._
// The returned directory is not actually read-only, but it should be treated that way
def readOnlyResourceDirectory(group: String, name: String): File =
{
val groupDirectory = new File(baseDirectory, group)
if(groupDirectory.isDirectory)
{
val resourceDirectory = new File(groupDirectory, name)
if(resourceDirectory.isDirectory)
resourceDirectory
else
error("Resource directory '" + name + "' in group '" + group + "' not found.")
}
else
error("Group '" + group + "' not found.")
}
def readWriteResourceDirectory[T](group: String, name: String)(withDirectory: File => T): T =
{
val file = readOnlyResourceDirectory(group, name)
readWriteResourceDirectory(file)(withDirectory)
}
def readWriteResourceDirectory[T](readOnly: File)(withDirectory: File => T): T =
{
require(readOnly.isDirectory)
def readWrite(readOnly: File)(temporary: File): T =
{
val readWriteDirectory = new File(temporary, readOnly.getName)
copyDirectory(readOnly, readWriteDirectory)
withDirectory(readWriteDirectory)
}
withTemporaryDirectory(readWrite(readOnly))
}
}