Skip to content

Commit 4a33c4b

Browse files
committed
added a 'verbose' option for debugging .mog files
added ability to run a mog file from another directory added more documentation to cli help output
1 parent 4fafde7 commit 4a33c4b

File tree

6 files changed

+178
-109
lines changed

6 files changed

+178
-109
lines changed

.mog

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44

55
v_path = [which v]
66

7-
@dep(test2)
7+
@dep(fmt)
88
default:
99

1010
@desc(run mog, this is kinda circular)
@@ -15,6 +15,9 @@ run:
1515
run_trace:
1616
v -d trace_logs run .
1717

18+
b:
19+
{build}
20+
1821
@desc(build mog into an executable)
1922
build:
2023
mkdir -p release && v -prod -o release/mog . && echo built
@@ -27,6 +30,30 @@ check:
2730
fmt:
2831
v fmt . -w
2932

33+
commit:
34+
git add -A
35+
git commit for arg in {$"@"}; do -m "$arg"; done
36+
git push
37+
38+
c:
39+
{commit}
40+
41+
amend:
42+
git add -A
43+
if [ {$#} -eq 0 ]; then
44+
git commit --amend --no-edit
45+
else
46+
output=()
47+
for arg in {$"@"}; do
48+
output+=( "-m $arg" )
49+
done
50+
git commit --amend "${output[@]}"
51+
fi
52+
git push -f
53+
54+
a:
55+
{amend}
56+
3057
@desc(for testing)
3158
test:
3259
echo {$bold}{v_path}{$normal_intensity}
@@ -37,11 +64,11 @@ test:
3764
read name
3865
echo {$normal_intensity}hello ${name}
3966
echo {$green}${EDITOR}
40-
hogwash || echo no hogwash
67+
hogwash {$silence_out_err} || echo no hogwash
4168

4269
test2:
43-
echo testing
44-
echo {v_path}
70+
pwd
71+
echo test2
4572

4673
@dep(up.test)
4774
test3:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ See this projects various .mog files
7474

7575
## Help
7676

77-
Just run `mog -h`
77+
Just run `mog -h` or `mog --help` or `mog help`
7878

7979

8080
## TODO
8181

82-
Nothing ATM!
82+
- test on Windows?

src/another/.mog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
test:
2-
echo in another
2+
echo in another
3+
pwd

src/main.v

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ import mog { Mog, debug, parse }
77
const defualt_task = 'default'
88

99
fn main() {
10-
mut args := arguments()[1..]
10+
cur_dir := os.getwd()
11+
mut args := arguments()
12+
mog_file_name := args.pop_left().split('/').last()
1113
mut dash_args := []string{}
1214

1315
if args.len > 0 && args.first() == 'symlink' {
14-
result := os.execute('ln -s ${os.getwd()}/mog ${os.home_dir()}/.local/bin/mog')
15-
println(result.output)
16+
result := os.execute('ln -s ${cur_dir}/${mog_file_name} ${os.home_dir()}/.local/bin/mog')
17+
if result.exit_code == 0 {
18+
println('Linked ${cur_dir}/${mog_file_name} to ${os.home_dir()}/.local/bin/mog')
19+
} else {
20+
println(result.output)
21+
}
1622
exit(result.exit_code)
1723
}
1824

25+
mut positional_args := []string{}
1926
for arg in args {
2027
if arg.starts_with('-') {
2128
dash_args << arg
2229
} else {
23-
break
30+
positional_args << arg
2431
}
2532
}
2633
for _ in dash_args {
@@ -32,8 +39,23 @@ fn main() {
3239
exit(0)
3340
}
3441

35-
mog_file := os.read_file('.mog') or {
36-
if args.len == 0 || 'help' !in args {
42+
mut mog_file_path := '.'
43+
if '-p' in dash_args {
44+
args.pop_left()
45+
mog_file_path = positional_args.first()
46+
mog_file_path = os.abs_path(cur_dir + '/' + mog_file_path)
47+
if !os.exists(mog_file_path) {
48+
println('Invalid path: ${mog_file_path}')
49+
exit(1)
50+
}
51+
os.chdir(mog_file_path) or {
52+
println('Invalid path: ${mog_file_path}')
53+
exit(1)
54+
}
55+
}
56+
57+
mog_file := os.read_file('${mog_file_path}/.mog') or {
58+
if args.len != 0 && 'help' !in args && '-h' !in dash_args && '--help' !in dash_args {
3759
println('Failed to read file.')
3860
exit(1)
3961
}
@@ -60,10 +82,21 @@ fn main() {
6082
exit(0)
6183
}
6284

85+
mut verbose := false
86+
if '-v' in dash_args {
87+
verbose = true
88+
}
89+
6390
if '-h' in dash_args || '--help' in dash_args || (args.len > 0 && 'help' == args.first()) {
64-
if args.len > 1 && args[1] == 'arguments' {
65-
print_arguments_help()
66-
exit(0)
91+
if args.len > 1 {
92+
if args[1] == 'arguments' {
93+
print_arguments_help()
94+
exit(0)
95+
}
96+
if args[1] == 'variables' {
97+
print_builtin_vars_help()
98+
exit(0)
99+
}
67100
}
68101
print_help(m)
69102
exit(0)
@@ -74,7 +107,9 @@ fn main() {
74107
}
75108

76109
if args.len == 0 {
77-
println("Add a task named '${defualt_task}' to the .mog file to have it run when no task is provided to the mog command\n")
110+
if mog_file != '' {
111+
println("Add a task named '${defualt_task}' to the .mog file to have it run when no task is provided to the mog command\n")
112+
}
78113
print_version()
79114
println('')
80115
print_help(m)
@@ -86,7 +121,10 @@ fn main() {
86121
eprint("No task named '${task_name}' found")
87122
exit(1)
88123
}
89-
task.execute()
124+
if mog_file_path != '.' && '--no-cd' !in dash_args {
125+
task.body.prepend('cd ${mog_file_path}')
126+
}
127+
task.execute(verbose)
90128
}
91129

92130
fn print_version() {
@@ -127,23 +165,39 @@ fn print_help(m ?Mog) {
127165
println('Mog is a tool for running tasks from a .mog file in the current directory\n')
128166
println('Usage:')
129167
println(' mog [options] [task] [arguments]\n')
130-
println('Any arguments passed after the task name will be forwarded to that task if you use the bash like {$#} syntax. For more info run "mog help arguments"\n')
131168
print_options()
132169
println('')
133170
print_builtins_help()
134171
println('')
135-
print_commands(m)
172+
print_list_help_topics()
173+
println('')
174+
if definite_mog := m {
175+
if definite_mog.tasks.keys().len > 0 || definite_mog.imports.keys().len > 0 {
176+
print_commands(m)
177+
}
178+
}
179+
}
180+
181+
fn print_list_help_topics() {
182+
println('Help topics (run "mog help [topic]"):')
183+
println(' arguments:\tShow information on using forwarded arguments from the cli')
184+
println(' variables:\tShow built in variables')
136185
}
137186

138187
fn print_options() {
139188
println('Options:')
189+
println(' -v:\t\t\tShow the commands that will be executed')
190+
println(' -p [path]:\t\tRun a .mog file from another location')
191+
println('')
192+
println(" --no-cd:\t\tDon't change cwd when running a mog file from another directory with '-p'")
193+
println('')
140194
println(' -l | --list:\t\tList available tasks')
141195
println(' -h | --help:\t\tShow the help output')
142196
println(' -V | --version:\tShow the version of mog')
143197
}
144198

145199
fn print_builtins_help() {
146-
println("Built in task names that shouldn't be used in a .mog file:")
200+
println("Built in tasks (these shouldn't be used in a .mog file):")
147201
println(' help:\t\tShow the help output')
148202
println(' symlink:\tCreate a symlink for the mog command to ~/.local/bin')
149203
}
@@ -156,6 +210,24 @@ fn print_arguments_help() {
156210
println('- {$"*"} becomes a single string, e.g., "arg1 arg2 arg3"')
157211
println('- {$@} expands positional parameters as separate quoted strings')
158212
println('- {$"@"} expands to "{$1}" "{$2}" "{$3}", treating each argument as a distinct entity')
213+
println('\nExample:\n')
214+
println('```')
215+
println('run:')
216+
println('\tpython my_script.py {$@} # this passes all cli arguments to the python script\n')
217+
println('```\n')
218+
println('In the cli:\n')
219+
println('$ mog run arg1 arg2')
220+
}
221+
222+
fn print_builtin_vars_help() {
223+
println('Built in variables:\n')
224+
for key, value in mog.built_in_vars {
225+
mut val := value
226+
if value.starts_with('\e') {
227+
val = '\\e${value[1..]}'
228+
}
229+
println('- ${key} = "${val}"')
230+
}
159231
}
160232

161233
fn ljust(str string, len int, fill string) string {

0 commit comments

Comments
 (0)