Skip to content

Commit adaa2a7

Browse files
authored
Preserve customized Xcode asset catalogs across regeneration (#713)
Folder-reference assets (Assets.xcassets on macOS, mediaAssets on iOS/ tvOS/visionOS/catOS) were being copied with a bare !exists guard. That blocked a template bugfix from reaching projects that had never been touched, and if the guard were loosened to always copy we'd wipe out every user's own icons and media.
1 parent eb5b179 commit adaa2a7

4 files changed

Lines changed: 101 additions & 14 deletions

File tree

‎commandLine/src/defines.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define OFPROJECTGENERATOR_MAJOR_VERSION "0"
2-
#define OFPROJECTGENERATOR_MINOR_VERSION "129"
2+
#define OFPROJECTGENERATOR_MINOR_VERSION "130"
33
#define OFPROJECTGENERATOR_PATCH_VERSION "0"
44

55
#define PG_VERSION (OFPROJECTGENERATOR_MAJOR_VERSION "." OFPROJECTGENERATOR_MINOR_VERSION "." OFPROJECTGENERATOR_PATCH_VERSION)

‎commandLine/src/projects/xcodeProject.cpp‎

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,89 @@ fs::path resolveRefPath(const json & objects, const std::map<string, string> & p
145145
return accumulated;
146146
}
147147

148+
// Folder-reference assets (Assets.xcassets, mediaAssets) ship in templates and get copied into new
149+
// projects. Once copied, users customize them — add their own icons, imagesets, media files — and
150+
// a naive copy on every regen would wipe that. We decide per-folder using a three-state rule:
151+
// CopyFresh destination is missing, copy it in.
152+
// CopyRefresh destination exists and is byte-identical to the template, refresh in place so a
153+
// template bugfix (e.g. new default icon) reaches projects that never got touched.
154+
// KeepCustom destination differs from the template in any way (added/removed/edited file),
155+
// leave it alone — all-or-nothing, never merge.
156+
// .DS_Store and other dot-files are ignored on both sides.
157+
enum class folderUpdateDecision { CopyFresh, CopyRefresh, KeepCustom };
158+
159+
bool isIgnoredAssetEntry(const fs::path & p) {
160+
const string name = p.filename().string();
161+
return !name.empty() && name.front() == '.';
162+
}
163+
164+
std::set<string> collectRelativeFiles(const fs::path & root) {
165+
std::set<string> files;
166+
if (!fs::exists(root) || !fs::is_directory(root)) return files;
167+
for (auto it = fs::recursive_directory_iterator(root); it != fs::recursive_directory_iterator(); ++it) {
168+
const auto & entry = *it;
169+
if (isIgnoredAssetEntry(entry.path())) {
170+
if (entry.is_directory()) it.disable_recursion_pending();
171+
continue;
172+
}
173+
if (entry.is_regular_file()) {
174+
files.insert(fs::relative(entry.path(), root).generic_string());
175+
}
176+
}
177+
return files;
178+
}
179+
180+
bool filesByteIdentical(const fs::path & a, const fs::path & b) {
181+
std::error_code ec;
182+
auto sa = fs::file_size(a, ec); if (ec) return false;
183+
auto sb = fs::file_size(b, ec); if (ec) return false;
184+
if (sa != sb) return false;
185+
std::ifstream fa(a, std::ios::binary);
186+
std::ifstream fb(b, std::ios::binary);
187+
if (!fa || !fb) return false;
188+
constexpr size_t BUF = 64 * 1024;
189+
std::vector<char> ba(BUF), bb(BUF);
190+
while (fa && fb) {
191+
fa.read(ba.data(), BUF);
192+
fb.read(bb.data(), BUF);
193+
auto na = fa.gcount(); auto nb = fb.gcount();
194+
if (na != nb) return false;
195+
if (na == 0) break;
196+
if (!std::equal(ba.begin(), ba.begin() + na, bb.begin())) return false;
197+
}
198+
return !fa.bad() && !fb.bad();
199+
}
200+
201+
folderUpdateDecision decideFolderUpdate(const fs::path & from, const fs::path & to) {
202+
if (!fs::exists(to)) return folderUpdateDecision::CopyFresh;
203+
auto fromFiles = collectRelativeFiles(from);
204+
auto toFiles = collectRelativeFiles(to);
205+
if (fromFiles != toFiles) return folderUpdateDecision::KeepCustom;
206+
for (const auto & rel : fromFiles) {
207+
if (!filesByteIdentical(from / rel, to / rel)) return folderUpdateDecision::KeepCustom;
208+
}
209+
return folderUpdateDecision::CopyRefresh;
210+
}
211+
212+
// Returns true iff anything was copied; false means destination was either kept as a user
213+
// customization or the source doesn't exist in this template.
214+
bool copyTemplateFolderIfUnchanged(const fs::path & from, const fs::path & to, const string & label) {
215+
if (!fs::exists(from)) return false;
216+
switch (decideFolderUpdate(from, to)) {
217+
case folderUpdateDecision::CopyFresh:
218+
fs::copy(from, to, fs::copy_options::recursive);
219+
return true;
220+
case folderUpdateDecision::CopyRefresh:
221+
fs::remove_all(to);
222+
fs::copy(from, to, fs::copy_options::recursive);
223+
return true;
224+
case folderUpdateDecision::KeepCustom:
225+
std::cout << label << ": customized, keeping project version" << std::endl;
226+
return false;
227+
}
228+
return false;
229+
}
230+
148231
json withoutSystemLinks(const json & value, bool frameworks, bool libs, const std::set<string> & known) {
149232
json kept = json::array();
150233
for (auto & unit : xcodeSettings::flagUnits("OTHER_LDFLAGS", value)) {
@@ -519,19 +602,23 @@ bool xcodeProject::createProjectFile() {
519602
for (auto & f : { "openFrameworks-Info.plist", "of.entitlements" }) {
520603
copyTemplateFiles.push_back({ normalizePath(templatePath / f), normalizePath(projectDir / f) });
521604
}
605+
try {
606+
copyTemplateFolderIfUnchanged(normalizePath(templatePath / "Assets.xcassets"),
607+
normalizePath(projectDir / "Assets.xcassets"), "Assets.xcassets");
608+
} catch (const std::exception & e) {
609+
std::cerr << "Error copying Assets.xcassets: " << e.what() << std::endl;
610+
return false;
611+
}
522612
} else if (target == "ios" || target == "tvos" || target == "visionos" || target == "catos" || target == "macos") {
523613
for (auto & f : { "ofxiOS-Info.plist", "ofxiOS_Prefix.pch" }) {
524614
copyTemplateFiles.push_back({ normalizePath(templatePath / f), normalizePath(projectDir / f) });
525-
try {
526-
fs::path from = normalizePath(templatePath / "mediaAssets");
527-
fs::path to = normalizePath(projectDir / "mediaAssets");
528-
if (!fs::exists(to)) {
529-
fs::copy(from, to, fs::copy_options::recursive | fs::copy_options::update_existing);
530-
}
531-
} catch (const std::exception & e) {
532-
std::cerr << "Error copying template files: " << e.what() << std::endl;
533-
return false;
534-
}
615+
}
616+
try {
617+
copyTemplateFolderIfUnchanged(normalizePath(templatePath / "mediaAssets"),
618+
normalizePath(projectDir / "mediaAssets"), "mediaAssets");
619+
} catch (const std::exception & e) {
620+
std::cerr << "Error copying mediaAssets: " << e.what() << std::endl;
621+
return false;
535622
}
536623
}
537624

‎frontend/npm-shrinkwrap.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎frontend/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "projectGenerator",
33
"productName": "projectGenerator",
4-
"version": "0.124.0",
4+
"version": "0.125.0",
55
"description": "Build electron GUI openFrameworks projectGenerator",
66
"license": "MIT",
77
"main": "index.js",

0 commit comments

Comments
 (0)