This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.fs
167 lines (139 loc) · 5.58 KB
/
Commands.fs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
namespace Clam
open System
open System.IO
open LiteDB
open FsToolkit.ErrorHandling
open Clam.Types
module Commands =
let private getRepoName (fullRepoName: string) =
match
fullRepoName.Split("/")
|> Array.filter (String.IsNullOrWhiteSpace >> not)
with
| [| _; repoName |] -> Ok repoName
| [| _ |] -> Error MissingRepoName
| _ -> Error WrongGithubFormat
let private getTemplateAndChild (templateName: string) =
match
templateName.Split("/")
|> Array.filter (String.IsNullOrWhiteSpace >> not)
with
| [| user; template; child |] -> Some user, template, Some child
| [| template; child |] -> None, template, Some child
| [| template |] -> None, template, None
| _ -> None, templateName, None
let private matchParseResult result =
match result with
| Ok _ -> ()
| Error MissingRepoName
| Error WrongGithubFormat ->
printfn "Wrong repository format"
printfn "The repository name should look like this:"
printfn "Username/templateRepo"
let private updateRepo repoFullName branch =
option {
let! repo = Database.findByFullName repoFullName
let repo = { repo with branch = branch }
return!
repo
|> Scaffolding.downloadRepo
|> Scaffolding.unzipAndClean
|> Database.updateEntry
}
let runList () =
let results = Database.listEntries ()
if Seq.isEmpty results then
printfn "No Repositories downloaded"
else
for entry in results do
let date =
match entry.updatedAt |> Option.ofNullable with
| Some date -> date.ToShortDateString()
| None -> entry.createdAt.ToShortDateString()
printfn $"[{date}] {entry.fullName} [{entry.branch}] -> {entry.path}"
let runAdd (opts: RepositoryOptions) =
result {
let! repoName = getRepoName opts.repositoryName
if Database.existsByFullName opts.repositoryName then
printfn "The Repository already exists, Do you want to update it? [y/N]"
match Console.ReadKey().Key with
| ConsoleKey.Y ->
updateRepo opts.repositoryName opts.branch
|> ignore
| _ -> ()
else
let path =
$"./templates/{repoName}-{opts.branch}"
|> Path.GetFullPath
let newRepo =
{ _id = ObjectId.NewObjectId()
name = repoName
fullName = opts.repositoryName
branch = opts.branch
path = path
createdAt = DateTime.Now
updatedAt = Nullable() }
newRepo
|> Scaffolding.downloadRepo
|> Scaffolding.unzipAndClean
|> Database.createEntry
|> ignore
}
|> matchParseResult
let runUpdate (opts: RepositoryOptions) =
result {
let! repoName = getRepoName opts.repositoryName
match Database.findByFullName opts.repositoryName with
| Some repo -> updateRepo repo.fullName opts.branch |> ignore
| None -> ()
}
|> matchParseResult
let runRemove (fullName: string) =
option {
let! repo = Database.findByFullName fullName
Directory.Delete(repo.path, true)
return! Database.deleteByFullName repo.fullName
}
|> fun result ->
match result with
| Some true -> printfn $"{fullName} deleted from repositories."
| Some false -> printfn $"{fullName} could not be deleted from repositories."
| None -> printfn "Repository Not Fund"
let runNewProject (opts: ProjectOptions) =
option {
let (user, template, child) = getTemplateAndChild opts.templateName
let! repo =
match user, child with
| Some user, Some _ -> Database.findByFullName $"{user}/{template}"
| Some _, None -> Database.findByFullName opts.templateName
| None, _ -> Database.findByName template
let templatePath =
match child with
| Some child -> Path.Combine(repo.path, child)
| None -> repo.path
|> Path.GetFullPath
let targetPath =
Path.Combine("./", opts.projectName)
|> Path.GetFullPath
let content =
let readTemplateScript =
try
File.ReadAllText(Path.Combine(templatePath, "templating.fsx"))
|> Some
with
| _ -> None
let readRepoScript () =
try
File.ReadAllText(Path.Combine(repo.path, "templating.fsx"))
|> Some
with
| _ -> None
readTemplateScript
|> Option.orElseWith (fun () -> readRepoScript ())
match content with
| Some content ->
Extensibility.getConfigurationFromScript content
|> Scaffolding.compileAndCopy templatePath targetPath
| None -> Scaffolding.compileAndCopy templatePath targetPath None
}
|> ignore