|
1 | 1 | import os |
| 2 | +import sys |
2 | 3 | import json |
3 | | -from argparse import ArgumentParser |
4 | 4 |
|
5 | | -# 1. Read plugin_dir |
6 | | -parser = ArgumentParser() |
7 | | -parser.add_argument('plugin_dir') |
8 | | -args = parser.parse_args() |
9 | | -plugin_dir = args.plugin_dir |
| 5 | +def run(): |
| 6 | + # check path, must be in root folder |
| 7 | + if not os.path.exists('plugin.json'): |
| 8 | + os.chdir('..') |
| 9 | + if not os.path.exists('plugin.json'): |
| 10 | + print('plugin.json not found, exit') |
| 11 | + return |
10 | 12 |
|
11 | | -# 2. Read name in plugin.json |
12 | | -with open('plugin.json', 'r', encoding='utf-8') as f: |
13 | | - content = json.load(f) |
14 | | -name = content.get('name') |
| 13 | + # 1. Read plugin_dir |
| 14 | + plugin_dir = '' |
| 15 | + if len(sys.argv) > 1: |
| 16 | + plugin_dir = sys.argv[1] |
| 17 | + while not os.path.exists(plugin_dir): |
| 18 | + plugin_dir = input('Please input the directory of siyuan/data/plugins: ') |
| 19 | + if plugin_dir == 'exit' or plugin_dir == 'quit' or plugin_dir == 'q': |
| 20 | + return |
| 21 | + if not os.path.exists(plugin_dir): |
| 22 | + print('plugin_dir not found!') |
| 23 | + continue |
15 | 24 |
|
16 | | -# ...error if name not found |
17 | | -if not name or name == '': |
18 | | - print('"name" in plugin.json not found, exit') |
19 | | - exit() |
20 | | - |
21 | | -dev_dir = os.path.abspath('dev') |
22 | | -if not os.path.exists(dev_dir): |
23 | | - os.mkdir(dev_dir) |
| 25 | + # 2. Read name in plugin.json |
| 26 | + with open('plugin.json', 'r', encoding='utf-8') as f: |
| 27 | + content = json.load(f) |
| 28 | + name = content.get('name') |
24 | 29 |
|
25 | | -# 3. Create symlink |
26 | | -if not os.path.exists(os.path.join(plugin_dir, name)): |
27 | | - link = os.path.join(plugin_dir, name) |
28 | | - os.symlink(dev_dir, link) |
29 | | - print('Symlink created:', link) |
30 | | -else: |
31 | | - print('Folder already exists, exit') |
32 | | - exit() |
| 30 | + # ...error if name not found |
| 31 | + if not name or name == '': |
| 32 | + print('"name" in plugin.json not found, exit') |
| 33 | + return |
| 34 | + |
| 35 | + dev_dir = os.path.abspath('dev') |
| 36 | + if not os.path.exists(dev_dir): |
| 37 | + os.mkdir(dev_dir) |
| 38 | + |
| 39 | + # 3. Create symlink |
| 40 | + if not os.path.exists(os.path.join(plugin_dir, name)): |
| 41 | + link = os.path.join(plugin_dir, name) |
| 42 | + os.symlink(dev_dir, link) |
| 43 | + print('Symlink created:', link) |
| 44 | + else: |
| 45 | + print('Folder already exists, exit') |
| 46 | + return |
| 47 | + |
| 48 | +run() |
| 49 | +os.system('pause') |
0 commit comments