-
Notifications
You must be signed in to change notification settings - Fork 5
/
install
executable file
·106 lines (83 loc) · 2.89 KB
/
install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env python3
from getpass import getuser
from pathlib import Path
from shutil import which
from subprocess import run
import argparse
import os
import sys
def main(arguments):
if "GITPOD_HOST" in os.environ:
do_extra_gitpod_setup(arguments) # gitpod-io/gitpod#19255
make_symlinks(arguments)
if "GITPOD_HOST" in os.environ:
arguments.bin_dir.joinpath("fd").symlink_to(which("fdfind"))
sys.exit(0)
def make_symlinks(arguments):
"""
Symlink all the dotfiles into the destination directory.
"""
ignored = {".config", ".local", ".git", ".gitignore", ".gitmodules"}
for dotfile in arguments.dotfiles.glob(".*"):
if dotfile.name in ignored:
continue
dest = arguments.dest_dir / dotfile.name
symlink_unless_present(dotfile, dest)
for each in [".config", ".local", ".local/share"]:
dotfile_dir = arguments.dotfiles / each
dest_dir = arguments.dest_dir / each
try:
dest_dir.symlink_to(dotfile_dir)
except FileExistsError:
# Fall back on linking individual files if we failed.
if dest_dir.resolve() != dotfile_dir:
for dotfile in dotfile_dir.iterdir():
dest = dest_dir / dotfile.name
symlink_unless_present(dotfile, dest)
bin_dir = arguments.bin_dir.resolve()
bin_dir.mkdir(parents=True, exist_ok=True)
for bin in arguments.dotfiles.joinpath("bin").iterdir():
pass # let's not symlink by default for now
def do_extra_gitpod_setup(arguments):
# gitpod-io/gitpod#19254
run(["sudo", "chown", "-R", getuser(), str(Path.home())], check=True)
# gitpod-io/gitpod#19202
run(
["git", "submodule", "--quiet", "update", "--init"],
cwd=arguments.dotfiles,
check=True,
)
run(["sudo", "apt-get", "update", "--quiet"], check=True)
packages = {"bat", "fd-find", "fzy", "ripgrep", "universal-ctags", "zsh"}
run(["sudo", "apt-get", "install", "--quiet", "-y", *packages], check=True)
run(["sudo", "chsh", "-s", "zsh"], check=True)
def symlink_unless_present(path: Path, dest: Path):
"""
Symlink a file into the destination unless it exists already.
"""
path = path.absolute()
try:
dest.symlink_to(path)
except FileExistsError:
print(f"{dest} already exists! Skipping...")
else:
print(f"Linked {path} into {dest}")
parser = argparse.ArgumentParser(description="Dotfiles installer")
parser.add_argument(
"-b", "--bin-dir",
default=Path.home() / ".local/bin",
help="the directory to use for local binaries",
)
parser.add_argument(
"-d", "--dest-dir",
type=Path,
default=Path.home(),
help="the directory to link into",
)
parser.add_argument(
"--dotfiles",
type=Path,
default=Path(__file__).parent.resolve(),
help="the dotfiles to link",
)
main(parser.parse_args())