Skip to content

Commit 93f2f8f

Browse files
committed
Add userspace-setup subcommand
1 parent 1e98a2e commit 93f2f8f

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

qmk_cli/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
import os
44
import json
5+
from platformdirs import user_data_dir
56
from functools import lru_cache
67
from pathlib import Path
78

@@ -44,6 +45,10 @@ def find_qmk_firmware():
4445
return path.resolve()
4546
return path
4647

48+
hidden_home = Path(user_data_dir('qmk_cli', 'QMK')) / 'qmk_firmware'
49+
if hidden_home.exists():
50+
return hidden_home
51+
4752
return Path.home() / 'qmk_firmware'
4853

4954

qmk_cli/subcommands/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
from . import console # noqa
77
from . import env # noqa
88
from . import setup # noqa
9+
from . import userspace # noqa

qmk_cli/subcommands/userspace.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Setup qmk_userspace on your computer.
2+
"""
3+
import os
4+
import sys
5+
from pathlib import Path
6+
7+
from milc import cli
8+
9+
from milc.questions import yesno
10+
from qmk_cli.git import git_clone
11+
from qmk_cli.helpers import find_qmk_firmware, is_qmk_firmware, is_qmk_userspace
12+
13+
default_base = 'https://github.com'
14+
default_fork = 'qmk/qmk_userspace'
15+
default_branch = 'main'
16+
17+
18+
@cli.argument('-n', '--no', arg_only=True, action='store_true', help='Answer no to all questions')
19+
@cli.argument('-y', '--yes', arg_only=True, action='store_true', help='Answer yes to all questions')
20+
@cli.argument('--baseurl', arg_only=True, default=default_base, help='The URL all git operations start from. Default: %s' % default_base)
21+
@cli.argument('-b', '--branch', arg_only=True, default=default_branch, help='The branch to clone. Default: %s' % default_branch)
22+
@cli.argument('-H', '--home', arg_only=True, default=Path(os.environ['QMK_USERSPACE']), type=Path, help='The location for qmk_userspace. Default: %s' % os.environ['QMK_HOME'])
23+
@cli.argument('fork', arg_only=True, default=default_fork, nargs='?', help='The qmk_userspace fork to clone. Default: %s' % default_fork)
24+
@cli.subcommand('Setup your computer for qmk_userspace.')
25+
def userspace_setup(cli):
26+
"""Guide the user through setting up their QMK environment.
27+
"""
28+
# Sanity checks
29+
if cli.args.yes and cli.args.no:
30+
cli.log.error("Can't use both --yes and --no at the same time.")
31+
exit(1)
32+
33+
if is_qmk_userspace(cli.args.home):
34+
cli.log.info(f'Found qmk_userspace at {cli.args.home}.')
35+
else:
36+
cli.log.error('Could not find qmk_userspace!')
37+
if yesno('Would you like to clone qmk_userspace?'):
38+
git_url = f'{cli.args.baseurl}/{cli.args.fork}'
39+
if not git_clone(git_url, cli.args.home, cli.args.branch):
40+
exit(1)
41+
else:
42+
cli.log.warning('Not cloning qmk_userspace due to user input or --no flag.')
43+
44+
qmk_firmware = find_qmk_firmware()
45+
if is_qmk_firmware(qmk_firmware):
46+
cli.log.info(f'Found qmk_firmware at {qmk_firmware}.')
47+
else:
48+
cli.log.error('Could not find qmk_firmware!')
49+
if yesno('Would you like to clone qmk_firmware?'):
50+
git_url = f'{cli.args.baseurl}/qmk/qmk_firmware'
51+
hidden_home = Path.home() / '.qmk_firmware'
52+
if not git_clone(git_url, hidden_home, 'master'):
53+
exit(1)
54+
else:
55+
cli.log.warning('Not cloning qmk_firmware due to user input or --no flag.')
56+
57+
# Run `qmk doctor` to check the rest of the environment out
58+
if cli.args.home.exists():
59+
color = '--color' if cli.config.general.color else '--no-color'
60+
unicode = '--unicode' if cli.config.general.unicode else '--no-unicode'
61+
doctor_command = [Path(sys.argv[0]).as_posix(), color, unicode, 'doctor']
62+
63+
if cli.args.no:
64+
doctor_command.append('-n')
65+
66+
if cli.args.yes:
67+
doctor_command.append('-y')
68+
69+
cli.run(doctor_command, stdin=None, capture_output=False, cwd=cli.args.home)

0 commit comments

Comments
 (0)