Skip to content

Commit 3adf82a

Browse files
workaround(omnix): add shell application to fixup failed string replacements
Signed-off-by: Cameron Smith <cameron.ray.smith@gmail.com>
1 parent cf00434 commit 3adf82a

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

nix/modules/template-rename.nix

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
inputs,
3+
lib,
4+
config,
5+
pkgs,
6+
...
7+
}:
8+
9+
{
10+
perSystem =
11+
{ system, pkgs, ... }:
12+
{
13+
packages.fix-template-names = pkgs.writeShellApplication {
14+
name = "fix-template-names";
15+
runtimeInputs = with pkgs; [
16+
coreutils
17+
gnused
18+
gnugrep
19+
findutils
20+
file
21+
];
22+
text = ''
23+
#!/usr/bin/env bash
24+
25+
set -euo pipefail
26+
27+
# Colors for output
28+
GREEN='\033[0;32m'
29+
YELLOW='\033[1;33m'
30+
RED='\033[0;31m'
31+
NC='\033[0m' # No Color
32+
33+
# Default old names
34+
OLD_NAME="python-nix-template"
35+
OLD_UNDERSCORE="python_nix_template"
36+
37+
# Get the current directory name as default new name
38+
DEFAULT_NEW_NAME=$(basename "$(pwd)")
39+
# Convert to underscore version
40+
DEFAULT_NEW_UNDERSCORE=$(echo "$DEFAULT_NEW_NAME" | tr '-' '_')
41+
42+
# Use command line argument if provided, otherwise use defaults
43+
if [ $# -eq 1 ]; then
44+
NEW_NAME="$1"
45+
# Convert to underscore version
46+
NEW_UNDERSCORE=$(echo "$NEW_NAME" | tr '-' '_')
47+
elif [ $# -eq 0 ]; then
48+
NEW_NAME="$DEFAULT_NEW_NAME"
49+
NEW_UNDERSCORE="$DEFAULT_NEW_UNDERSCORE"
50+
else
51+
echo -e "''${RED}Error: Too many arguments.''${NC}"
52+
echo "Usage: fix-template-names [new-name]"
53+
echo "If no argument is provided, the current directory name ($DEFAULT_NEW_NAME) will be used."
54+
exit 1
55+
fi
56+
57+
echo -e "''${YELLOW}Starting replacement of template names...''${NC}"
58+
echo -e "''${YELLOW}Replacing $OLD_NAME with $NEW_NAME''${NC}"
59+
echo -e "''${YELLOW}Replacing $OLD_UNDERSCORE with $NEW_UNDERSCORE''${NC}"
60+
61+
# Find all text files in the project (excluding .git, node_modules, and other common directories to ignore)
62+
# and replace the strings in-place
63+
find . -type f \
64+
-not -path "*/\.*" \
65+
-not -path "*/node_modules/*" \
66+
-not -path "*/venv/*" \
67+
-not -path "*/build/*" \
68+
-not -path "*/dist/*" \
69+
-not -path "*/\__pycache__/*" \
70+
-exec grep -l "$OLD_NAME\|$OLD_UNDERSCORE" {} \; | while read -r file; do
71+
echo "Processing: $file"
72+
73+
# Check if file is binary
74+
if file "$file" | grep -q "binary"; then
75+
echo " Skipping binary file"
76+
continue
77+
fi
78+
79+
# Make replacements
80+
sed -i.bak "s/$OLD_NAME/$NEW_NAME/g" "$file"
81+
sed -i.bak "s/$OLD_UNDERSCORE/$NEW_UNDERSCORE/g" "$file"
82+
83+
# Remove backup files
84+
rm -f "''${file}.bak"
85+
done
86+
87+
echo -e "''${GREEN}Replacement complete!''${NC}"
88+
echo -e "''${YELLOW}Note: You may need to rebuild or reinstall your package after these changes.''${NC}"
89+
'';
90+
};
91+
};
92+
}

0 commit comments

Comments
 (0)