Skip to content

Commit 67189ba

Browse files
committed
✅ Add / use deploy script
1 parent c851596 commit 67189ba

File tree

2 files changed

+239
-97
lines changed

2 files changed

+239
-97
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -19,101 +19,6 @@ jobs:
1919
- name: Check out
2020
uses: actions/checkout@v4
2121

22-
# For each directory containing a changed config file, copy the .h files and build the code:
22+
# Run the mfconfig script with CI action
2323
- name: Deploy bugfix-2.1.x
24-
run: |
25-
IMPORT=import-2.1.x
26-
EXPORT=bugfix-2.1.x
27-
CEXA=config/examples
28-
CDEF=config/default
29-
BC=Configuration.h
30-
AC=Configuration_adv.h
31-
32-
git config user.email "thinkyhead@users.noreply.github.com"
33-
git config user.name "Scott Lahteine"
34-
35-
echo "- Initializing BASE branch..."
36-
37-
# Copy to a temporary location
38-
TEMP=$( mktemp -d ) ; cp -R config $TEMP
39-
40-
# Strip all #error lines
41-
IFS=$'\n'; set -f
42-
for fn in $( find $TEMP/config -type f -name "Configuration.h" ); do
43-
sed -i~ -e "20,30{/#error/d}" "$fn"
44-
rm "$fn~"
45-
done
46-
unset IFS; set +f
47-
48-
# Create 'BASE' as a copy of 'init-repo' (README, LICENSE, etc.)
49-
git fetch origin init-repo >/dev/null
50-
git checkout origin/init-repo -b BASE >/dev/null || exit
51-
52-
# Copy all config files into place
53-
echo "- Copying configs from fresh $IMPORT..."
54-
cp -R "$TEMP/config" . >/dev/null
55-
56-
echo "- Deleting extras"
57-
58-
# Delete anything that's not a Configuration file
59-
find config -type f \! -name "Conf*.h" -exec rm "{}" \;
60-
61-
# Init Cartesian/SCARA/TPARA configurations to default
62-
echo "- Initializing configs to default state..."
63-
64-
find "$CEXA" -name $BC -print0 \
65-
| while read -d $'\0' F ; do cp "$CDEF/$BC" "$F" >/dev/null ; done
66-
find "$CEXA" -name $AC -print0 \
67-
| while read -d $'\0' F ; do cp "$CDEF/$AC" "$F" >/dev/null ; done
68-
69-
# Update the %VERSION% in the README.md file
70-
VERS=$( echo $EXPORT | sed 's/release-//' )
71-
eval "sed -E -i~ -e 's/%VERSION%/$VERS/g' README.md"
72-
rm -f README.md~
73-
74-
# Commit the 'BASE', ready for customizations
75-
git add . >/dev/null && git commit --amend --no-edit >/dev/null
76-
77-
# Create a new branch from 'BASE' for the final result
78-
echo "- Creating 'built-temp' branch..."
79-
git checkout -b built-temp >/dev/null || exit
80-
81-
# Delete temporary branch
82-
git branch -D BASE 2>/dev/null
83-
84-
echo "- Applying customizations..."
85-
cp -R "$TEMP/config" .
86-
find config -type f \! -name "Configuration*" -exec rm "{}" \;
87-
88-
addpathlabels() {
89-
cd $CEXA
90-
find . -name "Conf*.h" -print0 | while read -d $'\0' fn ; do
91-
fldr=$(dirname "$fn" | sed "s/^\.\///")
92-
blank_line=$(awk '/^\s*$/ {print NR; exit}' "$fn")
93-
sed -i~ "${blank_line}i\\\n#define CONFIG_EXAMPLES_DIR \"$fldr\"" "$fn"
94-
rm -f "$fn~"
95-
done
96-
cd -
97-
}
98-
99-
echo "- Applying path labels..."
100-
addpathlabels >/dev/null 2>&1
101-
102-
git add . >/dev/null && git commit -m "Examples Customizations" >/dev/null
103-
104-
echo "- Adding all the extras..."
105-
cp -R "$TEMP/config" .
106-
107-
echo "- Applying path labels..."
108-
addpathlabels >/dev/null 2>&1
109-
110-
git add . >/dev/null && git commit -m "Examples Extras" >/dev/null
111-
112-
echo "- Replace $EXPORT branch"
113-
git fetch origin $EXPORT >/dev/null
114-
git checkout >/dev/null $EXPORT
115-
git reset --hard built-temp
116-
git push -f
117-
git branch -D built-temp
118-
119-
rm -rf $TEMP
24+
run: bin/mfconfig CI

bin/mfconfig

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
#!/usr/bin/env python3
2+
#
3+
# mfconfig [manual|init|repath] [source] [dest] [repo-path]
4+
#
5+
# Operate on the MarlinFirmware/Configurations repository.
6+
#
7+
# The MarlinFirmware/Configurations layout could be broken up into branches,
8+
# but this makes management more complicated and requires more commits to
9+
# perform the same operation, so this uses a single branch with subfolders.
10+
#
11+
# init - Initialize the repo with a base commit and changes:
12+
# - Source will be an 'import' branch containing all current configs.
13+
# - Create an empty 'WORK' branch from 'init-repo'.
14+
# - Add Marlin config files, but reset all to defaults.
15+
# - Commit this so changes will be clear in following commits.
16+
# - Add changed Marlin config files and commit.
17+
#
18+
# manual - Import changes from a local Marlin folder, then init.
19+
# - Replace 'default' configs with your local Marlin configs.
20+
# - Wait for manual propagation to the rest of the configs.
21+
# - Run init with the given 'source' and 'dest'
22+
#
23+
# repath - Add path labels to all config files, if needed
24+
# - Add a #define CONFIG_EXAMPLES_DIR to each Configuration*.h file.
25+
#
26+
# CI - Run in CI mode, using the current folder as the repo.
27+
# - For GitHub Actions to update the Configurations repo.
28+
#
29+
import os, sys, subprocess, shutil, datetime, tempfile
30+
from pathlib import Path
31+
32+
# Set to 1 for extra debug commits
33+
COMMIT_STEPS = 0
34+
35+
# Get the shell arguments into ACTION, IMPORT, and EXPORT
36+
ACTION = sys.argv[1] if len(sys.argv) > 1 else 'manual'
37+
IMPORT = sys.argv[2] if len(sys.argv) > 2 else 'import-2.1.x'
38+
EXPORT = sys.argv[3] if len(sys.argv) > 3 else 'bugfix-2.1.x'
39+
40+
# Get repo paths
41+
CI = False
42+
if ACTION == 'CI':
43+
_REPOS = "."
44+
REPOS = Path(_REPOS)
45+
CONFIGREPO = REPOS
46+
ACTION = 'init'
47+
CI = True
48+
else:
49+
_REPOS = sys.argv[4] if len(sys.argv) > 4 else '~/Projects/Maker/Firmware'
50+
REPOS = Path(_REPOS).expanduser()
51+
CONFIGREPO = REPOS / "Configurations"
52+
53+
def usage():
54+
print(f"Usage: {os.path.basename(sys.argv[0])} [manual|init|repath] [source] [dest] [repo-path]")
55+
56+
if ACTION not in ('manual','init','repath'):
57+
print(f"Unknown action '{ACTION}'")
58+
usage()
59+
sys.exit(1)
60+
61+
CONFIGCON = CONFIGREPO / "config"
62+
CONFIGDEF = CONFIGCON / "default"
63+
CONFIGEXA = CONFIGCON / "examples"
64+
65+
# Configurations repo folder must exist
66+
if not CONFIGREPO.exists():
67+
print(f"Can't find Configurations repo at {_REPOS}")
68+
sys.exit(1)
69+
70+
# Run git within CONFIGREPO
71+
def git(etc):
72+
if COMMIT_STEPS: print(f"> git {' '.join(etc)}")
73+
return subprocess.run(["git"] + etc, cwd=CONFIGREPO, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
74+
75+
# Get the current branch name
76+
def branch(): return git(["rev-parse", "--abbrev-ref", "HEAD"])
77+
78+
# git add . ; git commit -m ...
79+
def commit(msg, who="."): git(["add", who]) ; return git(["commit", "-m", msg])
80+
81+
# git checkout ...
82+
def checkout(etc): return git(["checkout"] + ([etc] if isinstance(etc, str) else etc))
83+
84+
# git branch -D ...
85+
def gitbd(name): return git(["branch", "-D", name]).stdout
86+
87+
def changes(): return git(["status", "--porcelain"]).stdout.decode().strip()
88+
89+
# Stash uncommitted changes at the destination?
90+
if changes():
91+
print(f"There are uncommitted Configurations repo changes.")
92+
STASH_YES = input("Stash changes? [Y/n] ") ; print()
93+
if STASH_YES not in ('Y','y',''): print("Can't continue") ; sys.exit()
94+
git(["stash", "-m", f"!!GitHub_Desktop<{branch()}>"])
95+
if changes(): print(f"Can't stash changes!") ; sys.exit(1)
96+
97+
def add_path_labels():
98+
print("- Adding path labels to all configs...")
99+
for fn in CONFIGEXA.glob("**/Configuration*.h"):
100+
fldr = str(fn.parent.relative_to(CONFIGCON)).replace("examples/", "")
101+
with open(fn, 'r') as f:
102+
lines = f.readlines()
103+
emptyline = -1
104+
for i, line in enumerate(lines):
105+
issp = line.isspace()
106+
if emptyline < 0:
107+
if issp: emptyline = i
108+
elif not issp:
109+
if not "CONFIG_EXAMPLES_DIR" in line:
110+
lines.insert(emptyline, f"\n#define CONFIG_EXAMPLES_DIR \"{fldr}\"\n")
111+
with open(fn, 'w') as f: f.writelines(lines)
112+
break
113+
114+
if ACTION == "repath":
115+
add_path_labels()
116+
117+
elif ACTION == "manual":
118+
119+
MARLINREPO = Path(REPOS / "MarlinFirmware")
120+
if not MARLINREPO.exists():
121+
print("Can't find MarlinFirmware at {_REPOS}!")
122+
sys.exit(1)
123+
124+
print(f"Updating '{IMPORT}' from Marlin...")
125+
126+
checkout(IMPORT)
127+
128+
# Replace examples/default with our local copies
129+
shutil.copy(MARLINREPO / "Marlin" / "Configuration*.h", CONFIGDEF)
130+
131+
#git add . && git commit -m "Changes from Marlin ($(date '+%Y-%m-%d %H:%M'))."
132+
#commit(f"Changes from Marlin ({datetime.datetime.now()}).")
133+
134+
print(f"Prepare the import branch and continue when ready.")
135+
INIT_YES = input("Ready to init? [y/N] ") ; print()
136+
if INIT_YES not in ('Y','y'): print("Done.") ; sys.exit()
137+
138+
ACTION = 'init'
139+
140+
if ACTION == "init":
141+
print(f"Building branch '{EXPORT}'...")
142+
print("- Init WORK branch...")
143+
144+
# Use the import branch as the source
145+
result = checkout(IMPORT)
146+
if result.returncode != 0:
147+
print(f"Can't find branch '{IMPORT}'!") ; sys.exit()
148+
149+
# Copy to a temporary location
150+
TEMP = Path(tempfile.mkdtemp())
151+
TEMPCON = TEMP / "config"
152+
shutil.copytree(CONFIGCON, TEMPCON)
153+
154+
# Strip #error lines from Configuration.h
155+
for fn in TEMPCON.glob("**/Configuration.h"):
156+
with open(fn, 'r') as f:
157+
lines = f.readlines()
158+
outlines = []
159+
for line in lines:
160+
if not line.startswith("#error"):
161+
outlines.append(line)
162+
with open(fn, 'w') as f:
163+
f.writelines(outlines)
164+
165+
# Make sure we're not on the 'WORK' branch...
166+
checkout("init-repo")
167+
168+
# Create a fresh 'WORK' as a copy of 'init-repo' (README, LICENSE, etc.)
169+
gitbd("WORK")
170+
checkout(["-b", "WORK"])
171+
172+
# Copy default configurations into the repo
173+
print("- Create configs in default state...")
174+
for fn in TEMPCON.glob("**/*"):
175+
if fn.is_dir(): continue
176+
relpath = fn.relative_to(TEMPCON)
177+
os.makedirs(CONFIGCON / os.path.dirname(relpath), exist_ok=True)
178+
if fn.name.startswith("Configuration"):
179+
shutil.copy(TEMPCON / "default" / os.path.basename(fn), CONFIGCON / relpath)
180+
181+
# DEBUG: Commit the reset for review
182+
if COMMIT_STEPS: commit("[DEBUG] Create defaults")
183+
184+
def replace_in_file(fn, search, replace):
185+
with open(fn, 'r') as f: lines = f.read()
186+
with open(fn, 'w') as f: f.write(lines.replace(search, replace))
187+
188+
# Update the %VERSION% in the README.md file
189+
replace_in_file(CONFIGREPO / "README.md", "%VERSION%", EXPORT.replace("release-", ""))
190+
191+
# Commit all changes up to now; amend if not debugging
192+
if COMMIT_STEPS:
193+
commit("[DEBUG] Update README.md version", "README.md")
194+
else:
195+
git(["add", "."])
196+
git(["commit", "--amend", "--no-edit"])
197+
198+
# Copy configured Configuration*.h to the working copy
199+
print("- Copy examples into place...")
200+
for fn in TEMPCON.glob("examples/**/Configuration*.h"):
201+
shutil.copy(fn, CONFIGCON / fn.relative_to(TEMPCON))
202+
203+
# Put #define CONFIG_EXAMPLES_DIR .. before the first blank line
204+
add_path_labels()
205+
206+
print("- Commit config changes...")
207+
commit("Examples Customizations")
208+
209+
# Copy over all files not matching Configuration*.h to the working copy
210+
print("- Copy extras into place...")
211+
for fn in TEMPCON.glob("examples/**/*"):
212+
if fn.is_dir(): continue
213+
if fn.name.startswith("Configuration"): continue
214+
# Copy the default config file to the repo folder
215+
shutil.copy(fn, CONFIGCON / fn.relative_to(TEMPCON))
216+
217+
print("- Commit extras...")
218+
commit("Examples Extras")
219+
220+
# Delete the temporary folder
221+
shutil.rmtree(TEMP)
222+
223+
# Push to the remote (if desired)
224+
if CI:
225+
PUSH_YES = 'Y'
226+
else:
227+
print()
228+
PUSH_YES = input(f"Push to upstream/{EXPORT}? [y/N] ")
229+
print()
230+
231+
REMOTE = "origin" if CI else "upstream"
232+
233+
if PUSH_YES in ('Y','y'):
234+
print("- Push to remote...")
235+
git(["push", "-f", REMOTE, f"WORK:{EXPORT}"])
236+
237+
print("Done.")

0 commit comments

Comments
 (0)