Skip to content

Commit b21bcde

Browse files
committed
register every script as own command
1 parent 5006e57 commit b21bcde

File tree

9 files changed

+46
-31
lines changed

9 files changed

+46
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Debug
33
obj
44
*.exe
55
*.dll
6+
*.lcpkg

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ apkg get https://github.com/alexcoder04/LeoConsole-external-scripts.git
2626

2727
## Usage
2828

29-
It adds following commands to your console:
29+
This plugin adds following commands to your console:
3030

31-
### `script <name>`
31+
### `<script-name> [args...]`
3232

3333
Runs a script. It can be a Shell, Python, ... script or even a binary. Every
3434
executable in `$SAVEPATH/share/scripts` is considered a script. Following arguments
35-
are passed to the script (args are arguments you passed inside LeoConsole):
35+
are passed to the script (`args` are arguments you passed inside LeoConsole):
3636

3737
```text
38-
username savepath downloadpath args...
38+
username savepath downloadpath [args...]
3939
```
4040

41+
**Warning**: if one of the variables username, savepath or downloadpath has
42+
spaces, these will be replaced with dashes (`-`). This is also the case if the
43+
variable is empty or not defined.
44+
4145
### `scripts-list`
4246

4347
Lists all scripts (executables) from `$SAVEPATH/share/scripts`.
4448

45-
### `exec`
49+
### `exec <program> [args...]`
4650

4751
Executes an arbitrary binary (from your `$PATH`).
4852

exec.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ private bool runProcess(string name, string args, string pwd) {
4747
}
4848
return true;
4949
}
50-
5150
}
5251
}
5352

external-scripts.lcpkg

-10.5 KB
Binary file not shown.

list.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
using ILeoConsole.Plugin;
55
using ILeoConsole.Core;
66

7-
namespace LeoConsole_externalScripts
8-
{
9-
public class ListScripts : ICommand
10-
{
7+
namespace LeoConsole_externalScripts {
8+
public class ListScripts : ICommand {
119
public string Name { get { return "scripts-list"; } }
1210
public string Description { get { return "list installed scripts"; } }
1311
public Action CommandFunktion { get { return () => Command(); } }
1412
private string[] _InputProperties;
1513
public string[] InputProperties { get { return _InputProperties; } set { _InputProperties = value; } }
1614
public IData data = new ConsoleData();
1715

18-
public void Command()
19-
{
16+
public void Command() {
2017
try {
2118
foreach (string filename in Directory.GetFiles(Path.Join(data.SavePath, "share", "scripts"))) {
2219
Console.WriteLine(Path.GetFileName(filename));

manifest.apkg.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"manifestVersion": 2.0,
2+
"manifestVersion": 2.1,
33
"packageName": "external-scripts",
4+
"packageVersion": "1.0.0",
45
"build": {
56
"command": "dotnet",
67
"args": ["build", "--nologo"],
@@ -11,7 +12,7 @@
1112
"project": {
1213
"maintainer": "alexcoder04",
1314
"email": "alexcoder04@protonmail.com",
14-
"homepage": "https://github.com/alexcoder04/LeoConsole-apkg",
15-
"bugTracker": "https://github.com/alexcoder04/LeoConsole-apkg/issues"
15+
"homepage": "https://github.com/alexcoder04/LeoConsole-external-scripts",
16+
"bugTracker": "https://github.com/alexcoder04/LeoConsole-external-scripts/issues"
1617
}
1718
}

plugin.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ public void PluginInit() {
3030
_data = new ConsoleData();
3131

3232
_Commands = new List<ICommand>();
33-
_Commands.Add(new Script());
3433
_Commands.Add(new ListScripts());
3534
_Commands.Add(new Exec());
3635
}
3736

3837
public void PluginMain() {
3938
if (!Directory.Exists(Path.Join(_data.SavePath, "share", "scripts"))) {
40-
Console.WriteLine("warning: scripts directory does not exist!");
39+
Console.WriteLine("error: scripts folder does not exist!");
40+
return;
41+
}
42+
foreach (string s in Directory.GetFiles(Path.Join(_data.SavePath, "share", "scripts"))) {
43+
_Commands.Add(new Script(Path.GetFileName(s)));
4144
}
4245
}
4346

script.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,41 @@
66

77
namespace LeoConsole_externalScripts {
88
public class Script : ICommand {
9-
public string Name { get { return "script"; } }
10-
public string Description { get { return "run script"; } }
9+
public string Name { get; set; }
10+
public string Description { get { return "external script"; } }
1111
public Action CommandFunktion { get { return () => Command(); } }
1212
private string[] _InputProperties;
1313
public string[] InputProperties { get { return _InputProperties; } set { _InputProperties = value; } }
1414
public IData data = new ConsoleData();
1515

16+
public Script(string name) {
17+
Name = name;
18+
}
19+
1620
public void Command() {
17-
if (_InputProperties.Length < 2){
18-
Console.WriteLine("you need to provide the script name to run");
19-
return;
21+
string username = data.User.name.Replace(" ", "-");
22+
if (username == "") {
23+
username = "-";
24+
}
25+
string savepath = data.SavePath.Replace(" ", "-");
26+
if (savepath == "") {
27+
savepath = "-";
28+
}
29+
string dlpath = data.DownloadPath.Replace(" ", "-");
30+
if (dlpath == "") {
31+
dlpath = "-";
2032
}
21-
Console.WriteLine("running " + _InputProperties[1] + "...");
2233
string args = "";
2334
for (int i = 1; i < _InputProperties.Length; i++) {
2435
args = args + " " + _InputProperties[i];
2536
}
2637

2738
if (!runProcess(
28-
Path.Join(data.SavePath, "share", "scripts", _InputProperties[1]),
29-
$"{data.User.name} {data.SavePath} {data.DownloadPath} {args}",
30-
data.SavePath)
31-
) {
32-
Console.WriteLine("error running " + _InputProperties[1]);
39+
Path.Join(data.SavePath, "share", "scripts", Name),
40+
$"{username} {savepath} {dlpath} {args}",
41+
data.SavePath
42+
)) {
43+
Console.WriteLine("error running " + Name);
3344
}
3445
}
3546

@@ -52,7 +63,6 @@ private bool runProcess(string name, string args, string pwd) {
5263
}
5364
return true;
5465
}
55-
5666
}
5767
}
5868

share/scripts/example.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
echo "This is an example shell script"
3+
echo "This is an example shell script for LeoConsole"
44

55
username="$1"; shift
66
savepath="$1"; shift
@@ -9,12 +9,12 @@ dlpath="$1"; shift
99
cat <<EOF
1010
1111
---
12-
1312
Data I got from LeoConsole:
1413
- data.User.name: $username
1514
- data.SavePath: $savepath
1615
- data.DownloadPath: $dlpath
1716
- args: $@
17+
---
1818
1919
EOF
2020

0 commit comments

Comments
 (0)