-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_repo.py
executable file
·154 lines (127 loc) · 5.5 KB
/
update_repo.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
#
# NOTE:
# - This script does not require the initialization of a virtual environment. The
# Pipfiles are only required when deploying the MkDocs site.
# - This script has some hard-coded values that may make it fragile in some cases.
# I've attempted to do my best an indicate where these values are, but it's always a
# good idea to keep an eye on this script when making changes to the configuration.
#
########################################################################################
"""
Automates the process of updating zsh and neovim configuration files in the 'includes'
directory. This is done by reading my 'dotfiles' in the 'submodules/dotfiles' submodule
and applying the necessary changes to the 'includes' directory.
"""
# [ Imports ]###########################################################################
from utils.file_utils import read_file, write_file
from utils.constants import (
CHEZMOI_STATEMENTS,
NEOVIM_CONFIG_PATHS,
ZSH_CONFIG_PATHS,
NEOVIM_MARKERS,
ZSH_ALIAS_MARKERS,
ZSH_LS_COLORS_MARKERS,
MKDOCS_USER_CONFIG_MARKERS,
MKDOCS_LS_COLORS_MARKERS,
)
# [ Functions ]#########################################################################
def neovim_config():
"""Updates the neovim configuration files."""
for operation, paths in NEOVIM_CONFIG_PATHS.items():
data: list[str] | str = read_file(
paths["from"], read_lines=(operation == "init_vim_no_plug")
)
if operation == "init_vim_no_plug":
filtered_data: list[str] = []
for line in data:
if NEOVIM_MARKERS.start_marker in line:
NEOVIM_MARKERS.is_within_section = True
if (
NEOVIM_MARKERS.end_marker in line
and NEOVIM_MARKERS.is_within_section
):
NEOVIM_MARKERS.is_within_section = False
break
if NEOVIM_MARKERS.is_within_section:
filtered_data.append(line)
write_file(paths["to"], "".join(filtered_data))
else:
write_file(paths["to"], data)
def chezmoi_edge_case(line, data, line_number):
"""Handles edge cases when chezmoi statements are encountered. These are hard coded
strings that need to be processed in a very specific way. This means that changes to
the configuration files can easily break or negate the functionality of this
method. Keep a close eye when making changes to the configuration files.
Args:
line (str): The line to process.
data (list[str]): The data from the file.
line_number (int): The current line number.
Returns:
int: The number of lines to skip.
"""
if "{{ if $data.isGUIEnvironment -}}" in line:
if (
"plugins=(" in data[line_number + 1]
and "plugins=(" in data[line_number + 2]
):
return 3
elif (
"hash xdg-open" in data[line_number + 1]
and "{{- end }}" in data[line_number + 2]
):
return 1
return 1
def zsh_config():
"""Updates the zsh configuration files."""
for file_operation, file_paths in ZSH_CONFIG_PATHS.items():
data: list[str] = read_file(file_paths["from"], read_lines=True)
output_data: list[str] = []
line_number = 0
while line_number < len(data):
line = data[line_number]
# Uncomment the code below to help debug edge cases.
# print(f"Processing line {line_number + 1} of {file_paths['from']}")
# print(f"Line: {line}")
if any(marker in line for marker in CHEZMOI_STATEMENTS):
skip_line = chezmoi_edge_case(line, data, line_number)
line_number += skip_line
continue
if not file_operation.endswith("snippet"):
output_data.append(line)
line_number += 1
continue
if ZSH_ALIAS_MARKERS.start_marker in line:
ZSH_ALIAS_MARKERS.is_within_section = True
output_data.append(MKDOCS_USER_CONFIG_MARKERS.start_marker)
elif ZSH_LS_COLORS_MARKERS.start_marker in line:
ZSH_LS_COLORS_MARKERS.is_within_section = True
output_data.append(MKDOCS_LS_COLORS_MARKERS.start_marker)
if (
ZSH_ALIAS_MARKERS.end_marker in line
and ZSH_ALIAS_MARKERS.is_within_section
):
ZSH_ALIAS_MARKERS.is_within_section = False
output_data.append(MKDOCS_USER_CONFIG_MARKERS.end_marker)
elif (
ZSH_LS_COLORS_MARKERS.end_marker in line
and ZSH_LS_COLORS_MARKERS.is_within_section
):
ZSH_LS_COLORS_MARKERS.is_within_section = False
if ZSH_LS_COLORS_MARKERS.hard_coded_inclusion:
output_data.extend(ZSH_LS_COLORS_MARKERS.hard_coded_inclusion)
output_data.append(MKDOCS_LS_COLORS_MARKERS.end_marker)
if (
ZSH_ALIAS_MARKERS.is_within_section
or ZSH_LS_COLORS_MARKERS.is_within_section
):
output_data.append(line)
line_number += 1
write_file(file_paths["to"], "".join(output_data))
def main():
"""Main function."""
neovim_config()
zsh_config()
# [ Dunder Main ]#######################################################################
if __name__ == "__main__":
main()