-
Notifications
You must be signed in to change notification settings - Fork 0
/
MkDir.scala
79 lines (64 loc) · 2.71 KB
/
MkDir.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
package com.mayankrastogi.cs474.hw3.commands
import com.mayankrastogi.cs474.hw3.commands.MkDir.MkDirParameters._
import com.mayankrastogi.cs474.hw3.framework.Command
import com.mayankrastogi.cs474.hw3.framework.CommandResultParser.DefaultParsers._
/**
* Provides a functional, type-safe interface for building the Unix `mkdir` command.
*
* This command can be used to create directories at a specified path.
*/
object MkDir {
/**
* Creates an empty mkdir command builder.
*
* @return A type-safe mkdir command builder.
*/
def apply(): MkDirBuilder[Empty] = MkDirBuilder(".", createParents = false)
/**
* Builds a mkdir command for execution.
*
* The builder is private and can only be instantiated by the user by invoking the `apply` method of
* [[com.mayankrastogi.cs474.hw3.commands.MkDir]] object.
*
* @param dirName The path at which to create the directory.
* @param createParents Should missing directories in the path be created.
* @tparam I Phantom types for building a type-safe builder.
*/
private[MkDir] case class MkDirBuilder[I <: MkDirParameters](private val dirName: String, private val createParents: Boolean) {
/**
* The name or path of the directory to be created.
*
* @return A re-configured type-safe builder.
*/
def name(dirName: String): MkDirBuilder[I with FilePath] = copy(dirName = dirName)
/**
* Create parent directories as needed and raise no error if directory already exists, when set to `true`. An error
* is raised if a parent directory is missing or if the directory being created already exists, when reset to
* `false` (default).
*
* Equivalent to the `-p` option of the mkdir command if the flag is set to `true`.
*
* @return A re-configured type-safe builder.
*/
def createMissingDirectoriesInPath(flag: Boolean): MkDirBuilder[I with ParentFlag] = copy(createParents = flag)
/**
* Builds the `mkdir` command for execution.
*
* @param ev Implicit evidence that proves that the builder has all the necessary parameters supplied to it for
* building the command.
* @return A command that creates directory(ies) at the specified path and generates no output.
*/
def build(implicit ev: I =:= MandatoryParameters): Command[Unit] =
Command(s"mkdir '$dirName'${if (createParents) " -p" else ""}")
}
/**
* Phantom types for the type-safe builder.
*/
object MkDirParameters {
type MandatoryParameters = Empty with FilePath with ParentFlag
sealed trait MkDirParameters
sealed trait Empty extends MkDirParameters
sealed trait FilePath extends MkDirParameters
sealed trait ParentFlag extends MkDirParameters
}
}