1
1
#!/usr/bin/env python3
2
- #
3
- # Automates the process of updating zsh and neovim configuration files in the 'includes'
4
- # directory. This is done by reading my 'dotfiles' in the 'submodules/dotfiles'
5
- # submodule and applying the necessary changes to the 'includes' directory.
6
- #
7
- # NOTE:
8
- # - This script does not require the initialization of a virtual environment. The
9
- # Pipfiles are only required when deploying the MkDocs site.
10
- # - This script has some hard-coded values that may make it fragile in some cases.
11
- # I've attempted to do my best an indicate where these values are, but it's always a
12
- # good idea to keep an eye on this script when making changes to the configuration.
13
- #
14
- ########################################################################################
15
- # [ Imports ]###########################################################################
2
+ """Configuration file update automation script.
3
+
4
+ This script automates the process of updating Neovim and zsh configuration files in the
5
+ `includes` directory by extracting relevant sections from dotfiles stored in the
6
+ `submodules/dotfiles` submodule.
7
+
8
+ The script handles two main types of configuration updates:
9
+ - Neovim configuration files with selective content extraction
10
+ - Zsh configuration files with snippet-based processing
11
+
12
+ Notes:
13
+ - This script is, unfortunately, fragile by nature. Changes to upstream marker text
14
+ or chezmoi template layout can silently break extractions. When this occurs,
15
+ adjust the markers in the constants module or update the `chezmoi_edge_case`
16
+ function as needed.
17
+ - No external dependencies are required; a virtual environment is not necessary.
18
+
19
+ Example:
20
+ Run the script directly to update all configuration files:
16
21
22
+ ```bash
23
+ $ python3 update_repo.py
24
+ ```
25
+ """
17
26
18
- from utils .file_utils import read_file , write_file
27
+ # [ Imports ]###########################################################################
28
+
29
+ from utils .file_utils import read_file , read_lines , write_file
19
30
from utils .constants import (
20
- CHEZMOI_STATEMENTS ,
31
+ CHEZMOI_DELIMITERS ,
21
32
NEOVIM_CONFIG_PATHS ,
22
33
ZSH_CONFIG_PATHS ,
23
34
NEOVIM_MARKERS ,
31
42
# [ Functions ]#########################################################################
32
43
33
44
34
- def neovim_config ():
35
- """Updates the neovim configuration files."""
36
- for operation , paths in NEOVIM_CONFIG_PATHS .items ():
37
- data : list [str ] | str = read_file (
38
- paths ["from" ], read_lines = (operation == "init_vim_no_plug" )
39
- )
45
+ def neovim_config () -> None :
46
+ """Process and write Neovim config file variants to the `includes` directory.
47
+
48
+ Handles two types of Neovim config processing:
49
+ 1. `init_vim_no_plug`: Extracts content between designated section markers,
50
+ excluding plugin-related configurations.
51
+ 2. Other variants: Copies the entire source file without modification.
52
+
53
+ The function iterates through all Neovim config paths defined in
54
+ `NEOVIM_CONFIG_PATHS` and processes each according to its operation type.
40
55
56
+ Note:
57
+ The `init_vim_no_plug` operation relies on `NEOVIM_MARKERS` to identify
58
+ content boundaries. The end marker line itself is excluded from output.
59
+ """
60
+ for operation , paths in NEOVIM_CONFIG_PATHS .items ():
41
61
if operation == "init_vim_no_plug" :
62
+ data : list [str ] = read_lines (paths ["from" ])
42
63
filtered_data : list [str ] = []
64
+
43
65
for current_line in data :
44
66
if NEOVIM_MARKERS .start_marker in current_line :
45
67
NEOVIM_MARKERS .is_within_section = True
@@ -53,22 +75,30 @@ def neovim_config():
53
75
filtered_data .append (current_line )
54
76
write_file (paths ["to" ], "" .join (filtered_data ))
55
77
else :
78
+ data : str = read_file (paths ["from" ])
56
79
write_file (paths ["to" ], data )
57
80
58
81
59
- def chezmoi_edge_case (current_line , data , line_number ):
60
- """Handles edge cases when chezmoi statements are encountered. These are hard coded
61
- strings that need to be processed in a very specific way. This means that changes to
62
- the configuration files can easily break or negate the functionality of this
63
- method. Keep a close eye when making changes to the configuration files.
82
+ def chezmoi_edge_case (current_line : str , data : list [str ], line_number : int ) -> int :
83
+ """Calculate the number of lines to skip for chezmoi template actions.
84
+
85
+ Handles special cases in chezmoi template processing by analyzing the current line
86
+ and subsequent lines to determine how many lines should be skipped during zsh
87
+ configuration processing.
88
+
89
+ Note:
90
+ This function's logic is tightly coupled to the current structure of the
91
+ dotfiles. Changes to the upstream chezmoi template layout may require updates
92
+ to the pattern matching logic.
64
93
65
94
Args:
66
- current_line (str): The line to process.
67
- data (list[str]): The data from the file.
68
- line_number (int): The current line number.
95
+ current_line: The current line being processed, which contains a chezmoi
96
+ template delimiter.
97
+ data: Complete list of lines from the source zsh configuration file.
98
+ line_number: Zero-based index of the current line within the data list.
69
99
70
100
Returns:
71
- int: The number of lines to skip.
101
+ Number of lines to skip (minimum 1) .
72
102
"""
73
103
if "data.isGUIEnvironment" in current_line :
74
104
if (
@@ -85,24 +115,37 @@ def chezmoi_edge_case(current_line, data, line_number):
85
115
return 1
86
116
87
117
88
- def zsh_config ():
89
- """Updates the zsh configuration files."""
118
+ def zsh_config () -> None :
119
+ """Process and write zsh config file variants and snippets to the `includes`
120
+ directory.
121
+
122
+ Handles two types of processing:
123
+ 1. Full file operations: Copies entire source file, filtering chezmoi template
124
+ actions.
125
+ 2. Snippet operations: Extracts specific sections and wraps with MkDocs section
126
+ markers.
127
+
128
+ For snippet operations, extracts alias and `LS_COLORS` sections based on predefined
129
+ markers and optionally appends hard-coded content.
130
+
131
+ Note:
132
+ Includes debug output for CI/CD troubleshooting.
133
+ """
90
134
for file_operation , file_paths in ZSH_CONFIG_PATHS .items ():
91
- data : list [str ] | str = read_file (file_paths ["from" ], read_lines = True )
135
+ data : list [str ] = read_lines (file_paths ["from" ])
92
136
output_data : list [str ] = []
93
137
line_number = 0
94
138
95
139
while line_number < len (data ):
96
140
current_line = data [line_number ]
97
141
98
- ## DEBUG: The below lines help with debugging, especially when running in a
99
- ## CI/CD environment.
142
+ ## DEBUG: The below lines help with debugging...
100
143
print (f"Processing line { line_number + 1 } of { file_paths ['from' ]} " )
101
144
print (f"Line: { current_line } " )
102
145
103
- if any (marker in current_line for marker in CHEZMOI_STATEMENTS ):
104
- skip_line = chezmoi_edge_case (current_line , data , line_number )
105
- line_number += skip_line
146
+ if any (marker in current_line for marker in CHEZMOI_DELIMITERS ):
147
+ skip_line_count = chezmoi_edge_case (current_line , data , line_number )
148
+ line_number += skip_line_count
106
149
continue
107
150
108
151
if not file_operation .endswith ("snippet" ):
@@ -143,8 +186,8 @@ def zsh_config():
143
186
write_file (file_paths ["to" ], "" .join (output_data ))
144
187
145
188
146
- def main ():
147
- """Main function ."""
189
+ def main () -> None :
190
+ """Execute all configuration file update routines ."""
148
191
neovim_config ()
149
192
zsh_config ()
150
193
0 commit comments