-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.odin
324 lines (293 loc) · 10.8 KB
/
main.odin
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**************************************************
* License At Bottom
* Copyright (c) 2023 blob1807. All rights reserved.
***************************************************/
package odinstart
import "core:fmt"
import "core:os"
import "core:slice"
import str "core:strings"
import fp "core:path/filepath"
MOD_PKG: string: `{
"version": "",
"description": "",
"url": "",
"readme": "",
"license": "",
"keywords": [""],
"dependencies": {"":""}
}
`
OLS_JSON: string: `{{
"$schema": "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/ols.schema.json",
"collections": [
{{
"name": "core",
"path": "{0}core"
}}
],
"enable_document_symbols": true,
"enable_semantic_tokens": false,
"enable_hover": true,
"enable_snippets": true
}}
`
ODINFMT_JSON: string: `{
"$schema": "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/odinfmt.schema.json",
"character_width": 100,
"spaces": 4,
"newline_limit": 2,
"tabs": true,
"tabs_width": 4,
"convert_do": false,
"exp_multiline_composite_literals": false,
"brace_style": "_1TBS",
"indent_cases": false,
"newline_style": "CRLF"
}
`
MAIN_ODIN: string:
`package {0}
import "core:fmt"
main :: proc() {{
fmt.println("Hellope")
}}
`
README_MD: string :`# {0}
A cool thing that's going to change to world.
`
HELP :: `=== Odin Start ===
Quickly start an Odin project.
Help:
"--" or "-" can be used if desired.
Required:
help, h: Prints this help.
init, i: Creates project in current directory.
new, n <path>: Creates project in given directory.
Optional:
file, f [main, ols, mod, readme, license, odinfmt, gitignore, all]:
Creates given files. Creates main, ols & readme if not used.
dir, d [bin, src, all]:
Creates given directories. Creates none if not used.
license, l <path> or <file name>
Looks for a license at the path or a file in {0}'s dir.
Looks for a 'LICENSE' file, if nothing is provided.
Then addes it to the 'license' file. Does nothing if not used.
Examples:
{0} init
{0} new bean_maker
{0} -init --file main ols
{0} --new "bean maker" -dir all file main
{0} i license bsd3.txt
{0} n "cakes" f license main l
`
Files :: bit_set[enum{Main, Ols, Odinfmt, Mod, Readme, License, Gitignore, All}]
Dirs :: bit_set[enum{Bin, Src, All}]
License: string
Params := [?]string{"help", "h", "init", "i", "new", "n", "file", "f", "dir", "d", "l"}
main :: proc() {
assert(len(os.args) > 0, "Executable path wasn't passed by OS.")
assert(len(os.args) <= 16, "To many arguments were given.")
help := fmt.aprintf(HELP, fp.short_stem(fp.base(os.args[0])))
if len(os.args) == 1 {
fmt.println(help)
return
}
ostart_dir := fp.dir(os.args[0])
pkg_dir, arg, lic_file: string
setup, file_set, dir_set, lic_set: bool
files: Files
dirs: Dirs
for i:=1; i < len(os.args); {
arg = str.trim_left(os.args[i], "-")
switch arg {
case "help", "h":
fmt.println(help)
return
case "init", "i":
if setup {
fmt.eprintln("\"new\" or \"init\" has already been provided\n")
os.exit(1)
}
pkg_dir = os.get_current_directory()
setup = true
i += 1
case "new", "n":
if setup {
fmt.eprintln("\"new\" or \"init\" has already been provided\n")
os.exit(1)
}
if len(os.args) == i+1 {
fmt.eprintln("No path was provided\n")
os.exit(1)
}
pkg_dir = fp.clean(os.args[i+1])
os.make_directory(pkg_dir)
os.set_current_directory(pkg_dir)
setup = true
i += 2
case "license", "licence", "l":
if lic_set {
fmt.eprintln("\"license\" has already been provided\n")
os.exit(1)
}
if len(os.args) == i+1 || slice.contains(Params[:], str.trim_left(os.args[i+1], "-")) {
// if no path or name prvided use 'LICENSE'
lic_file = str.join({ostart_dir, "LICENSE"}, "/")
if !os.exists(lic_file) {
lic_file = str.join({ostart_dir, "LICENCE"}, "/")
}
i += 1
} else {
lic_file = str.join({ostart_dir, fp.clean(os.args[i+1])}, "/")
i += 2
}
if !os.exists(lic_file) {
fmt.eprintln("Unable to find the file:\n", lic_file, "\n")
os.exit(1)
}
fmt.println("Found license file:", lic_file)
lic_set = true
case "file", "f":
if file_set {
fmt.eprintln("You've already selected your files to write.\n")
os.exit(1)
}
for a in os.args[i+1:] {
i += 1
s, _ := str.to_lower(a)
switch s {
case "mod", "mod.pkg" : files += {.Mod}
case "main", "main.odin" : files += {.Main}
case "osl", "osl.json" : files += {.Ols}
case "odinfmt", "odinfmt.json": files += {.Odinfmt}
case "readme", "readme.md" : files += {.Readme}
case "license" : files += {.License}
case "gitignore", ".gitignore": files += {.Gitignore}
case "all": files = {.Mod, .Main, .Ols, .Odinfmt, .Readme, .License, .Gitignore}; break
case: i-=1; break
}
}
if files == {} {
fmt.eprintln("No files were given.\n")
os.exit(1)
}
file_set = true
i += 1
case "dir", "d":
if dir_set {
fmt.eprintln("You've already selected your directories to make.\n")
os.exit(1)
}
for a in os.args[i+1:] {
i+=1
switch a {
case "bin": dirs += {.Bin}
case "src": dirs += {.Src}
case "all": dirs = {.Bin, .Src}; break
case: i-=1; break
}
}
if dirs == {} {
fmt.eprintln("No directories were given.\n")
os.exit(1)
}
dir_set = true
i += 1
case:
fmt.eprintf("Argument \"{0}\" isn't a valid argument.\n", arg)
os.exit(1)
}
}
if !setup {
fmt.eprintf("You need to provide \"init\" or \"new\".\n\n")
os.exit(1)
}
if !file_set do files = {.Main, .Ols, .Readme}
w_ok: bool
pkg_name, _ := str.replace_all(fp.base(pkg_dir), " ", "_")
pkg_name, _ = str.replace_all(pkg_name, "-", "_")
cur_dir := os.get_current_directory()
file_name: string
if .Bin in dirs do os.make_directory("bin")
if .Src in dirs do os.make_directory("src")
if .Main in files {
file := fmt.aprintf(MAIN_ODIN, str.to_lower(pkg_name))
if .Src in dirs {
file_name, _ = fp.from_slash("src/main.odin")
w_ok = os.write_entire_file(file_name, transmute([]byte)file)
} else {
w_ok = os.write_entire_file("main.odin", transmute([]byte)file)
}
if !w_ok do fmt.eprintln("Unable to write \"main.odin\" in", cur_dir)
}
if .Ols in files {
root, _ := fp.to_slash(ODIN_ROOT)
root, _ = str.replace_all(root, "/", "//")
root, _ = fp.from_slash(root)
file := fmt.aprintf(OLS_JSON, root)
fmt.println("Found Odin in:", root)
w_ok = os.write_entire_file("ols.json", transmute([]byte)file)
if !w_ok do fmt.eprintln("Unable to write \"ols.json\" in", cur_dir)
}
if .Odinfmt in files {
w_ok = os.write_entire_file("odinfmt.json", transmute([]byte)ODINFMT_JSON)
if !w_ok do fmt.eprintln("Unable to write \"odinfmt.json\" in", cur_dir)
}
if .Mod in files {
w_ok = os.write_entire_file("mod.pkg", transmute([]byte)MOD_PKG)
if !w_ok do fmt.eprintln("Unable to write \"mod.pkg\" in", cur_dir)
}
if .Readme in files {
file := fmt.aprintf(README_MD, str.to_lower(pkg_name))
w_ok = os.write_entire_file("README.MD", transmute([]byte)file)
if !w_ok do fmt.eprintln("Unable to write \"README.MD\" in", cur_dir)
}
if .License in files {
t: []byte
if lic_set {
tt, r_ok := os.read_entire_file_from_filename(lic_file)
if !r_ok do fmt.eprintf("Unable to read file \"{0}\"\n", lic_file)
t = tt
}
w_ok = os.write_entire_file("LICENSE", t)
if !w_ok do fmt.eprintln("Unable to write \"LICENSE\" in", cur_dir)
}
if .Gitignore in files {
t: []byte
w_ok = os.write_entire_file(".gitignore", t)
if !w_ok do fmt.eprintln("Unable to write \".gitignore\" in", cur_dir)
}
fmt.printf("Project \"{0}\" created in {1}", pkg_name, cur_dir)
return
}
/****************************************************************************
* LICENSE: BSD-3-Clause
*
* Copyright (c) 2023 blob1807. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/