Skip to content

Commit 7568474

Browse files
committed
added common escape sequences
added ability to run imported commands from current directory directly added ability to call other tasks in the middle of a task
1 parent dae71ab commit 7568474

File tree

9 files changed

+219
-51
lines changed

9 files changed

+219
-51
lines changed

.mog

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,19 @@ fmt:
2929

3030
@desc(for testing)
3131
test:
32-
echo {v_path}
32+
echo {$bold}{v_path}{$normal_intensity}
33+
{up.test}
3334
sleep 2
34-
echo {$1}
35+
echo {$dim}{$1}
3536
echo Enter your name
3637
read name
37-
echo hello ${name}
38-
echo ${EDITOR}
38+
echo {$normal_intensity}hello ${name}
39+
echo {$green}${EDITOR}
3940
hogwash || echo no hogwash
4041

4142
test2:
4243
echo testing
43-
pwd
44-
cd ..
45-
pwd
44+
echo {v_path}
4645

4746
@dep(up.test)
4847
test3:

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ Define your tasks in a `.mog` file and run them with the `mog` command. Indentat
1212
- interpolation of variables using `{}` (unless the `{` has a `$` in front, in which case it is left alone for the shell to interpret)
1313
- escape the `[` and `{` characters with `\`
1414
- additional cli arguments are passed to the task being ran
15-
- task dependencies
16-
- task descriptions
17-
- single line comments with #
15+
- task dependencies with `@dep()` decorator
16+
- task descriptions with `@desc()` decorator
17+
- single line comments with `#`
1818
- importing other `.mog` files and using the tasks or variables with dot syntax
19+
- call another task in the middle of a task
20+
- execute an imported task directly from the cli `$ mog imported_mog.task_name`
1921

2022

2123
## What does it look like?
@@ -37,9 +39,9 @@ v_path = [which v] # storing a shell eval into a variable
3739
# the default task which is executed when calling a bare 'mog' with no arguments
3840
@dep(run)
3941
default:
40-
# with a declared dependacy and no body this is basically an alias
42+
# with a declared dependency and no body this is basically an alias
4143
42-
@dep(
44+
@dep( # defines dependencies that will be run before this task (in the same shell instance)
4345
build
4446
start
4547
my_alias.some_task_name

src/.mog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import (
2+
another
3+
)
4+
15
val = yes
26
uname = [uname -s]
37

48
@dep(test2)
59
default:
610

11+
@desc(hey)
712
test:
813
echo in src test
914
echo {uname}

src/another/.mog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
echo in another

src/lexer.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ fn (mut l Lexer) eat_command_body() !Token {
183183
return l.eat_newline()
184184
}
185185
if l.current_char != indent {
186-
debug("not at indent")
187186
l.context = .root
188-
return error("no indent")
187+
return error('no indent')
189188
}
190189
l.skip_whitespace()
191190
l.add_to_word(['\n', ''])

src/main.v

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ fn main() {
1010
mut args := arguments()[1..]
1111
mut dash_args := []string{}
1212

13+
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+
exit(result.exit_code)
17+
}
18+
1319
for arg in args {
1420
if arg.starts_with('-') {
1521
dash_args << arg
@@ -27,27 +33,43 @@ fn main() {
2733
}
2834

2935
mog_file := os.read_file('.mog') or {
30-
println('Failed to read file.')
31-
exit(1)
36+
if args.len == 0 || 'help' !in args {
37+
println('Failed to read file.')
38+
exit(1)
39+
}
40+
''
3241
}
42+
mut parse_args := []string{}
3343

34-
mut m := parse(mog_file) or {
35-
println('Failed to parse .mog file. ${err}')
36-
exit(1)
44+
if args.len > 0 {
45+
parse_args = args[1..].clone()
46+
}
47+
48+
mut m := Mog{}
49+
50+
if mog_file.len > 0 {
51+
m = parse(mog_file, parse_args) or {
52+
println('Failed to parse .mog file. ${err}')
53+
exit(1)
54+
}
55+
debug('${m}')
3756
}
38-
debug('${m}')
3957

4058
if '-l' in dash_args || '--list' in dash_args {
4159
print_commands(m)
4260
exit(0)
4361
}
4462

45-
if '-h' in dash_args || '--help' in dash_args {
63+
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)
67+
}
4668
print_help(m)
4769
exit(0)
4870
}
4971

50-
if args.len == 0 && defualt_task in m.tasks.keys() {
72+
if args.len == 0 && defualt_task in m.tasks {
5173
args << defualt_task
5274
}
5375

@@ -60,12 +82,11 @@ fn main() {
6082
}
6183

6284
task_name := args.pop_left()
63-
if task_name !in m.tasks.keys() {
85+
mut task := m.get_task(task_name) or {
6486
eprint("No task named '${task_name}' found")
6587
exit(1)
6688
}
67-
mut task := m.tasks[task_name]
68-
m.execute(mut task, args)
89+
task.execute()
6990
}
7091

7192
fn print_version() {
@@ -75,35 +96,68 @@ fn print_version() {
7596

7697
fn print_commands(m ?Mog) {
7798
definite_m := m or { return }
78-
println('Tasks available from the current .mog file:')
79-
mut labels := definite_m.tasks.keys()
99+
println('Available tasks:')
100+
sub_print_commands(definite_m, '')
101+
}
102+
103+
fn sub_print_commands(m Mog, mog_name string) {
104+
mut mut_mog_name := mog_name.clone()
105+
if mog_name.len > 0 {
106+
mut_mog_name += '.'
107+
}
108+
mut labels := m.tasks.keys()
80109
for mut label in labels {
81-
label = ' ${label}:'
110+
label = ' ${mut_mog_name}${label}:'
82111
}
83112
len := longest(labels)
84-
for name, task in definite_m.tasks {
85-
just_name := ljust(' ${name}:', len, ' ')
113+
for name, task in m.tasks {
114+
just_name := ljust(' ${mut_mog_name}${name}:', len, ' ')
86115
if task.desc.len > 0 {
87116
println('${just_name}\t${task.desc}')
88117
} else {
89118
println('${just_name}')
90119
}
91120
}
121+
for import_mog_name, imported_mog in m.imports {
122+
sub_print_commands(imported_mog, '${mut_mog_name}${import_mog_name}')
123+
}
92124
}
93125

94126
fn print_help(m ?Mog) {
95127
println('Mog is a tool for running tasks from a .mog file in the current directory\n')
96128
println('Usage:')
97129
println(' mog [options] [task] [arguments]\n')
98-
println('Any arguments passed after the task name will be forwarded to that task\n')
99-
println('Options:')
100-
println(' -l | --list:\tList available tasks')
101-
println(' -h | --help:\tShow this output')
102-
println(' -V | --version:\tShow the version of mog')
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')
131+
print_options()
132+
println('')
133+
print_builtins_help()
103134
println('')
104135
print_commands(m)
105136
}
106137

138+
fn print_options() {
139+
println('Options:')
140+
println(' -l | --list:\t\tList available tasks')
141+
println(' -h | --help:\t\tShow the help output')
142+
println(' -V | --version:\tShow the version of mog')
143+
}
144+
145+
fn print_builtins_help() {
146+
println("Built in task names that shouldn't be used in a .mog file:")
147+
println(' help:\t\tShow the help output')
148+
println(' symlink:\tCreate a symlink for the mog command to ~/.local/bin')
149+
}
150+
151+
fn print_arguments_help() {
152+
println('Mog argument access:\n')
153+
println('- Individual arguments are accessed using {$1} for the first argument, {$2} for the second, and so on')
154+
println('- {$#} holds the total count of positional arguments')
155+
println('- {$*} expands all positional parameters into a single string, separated by the a space')
156+
println('- {$"*"} becomes a single string, e.g., "arg1 arg2 arg3"')
157+
println('- {$@} expands positional parameters as separate quoted strings')
158+
println('- {$"@"} expands to "{$1}" "{$2}" "{$3}", treating each argument as a distinct entity')
159+
}
160+
107161
fn ljust(str string, len int, fill string) string {
108162
if str.len >= len {
109163
return str

0 commit comments

Comments
 (0)