diff --git a/commandLine/src/defines.h b/commandLine/src/defines.h index ac63ee45..3ed1ef5d 100644 --- a/commandLine/src/defines.h +++ b/commandLine/src/defines.h @@ -1,5 +1,5 @@ #define OFPROJECTGENERATOR_MAJOR_VERSION "0" -#define OFPROJECTGENERATOR_MINOR_VERSION "128" +#define OFPROJECTGENERATOR_MINOR_VERSION "129" #define OFPROJECTGENERATOR_PATCH_VERSION "0" #define PG_VERSION (OFPROJECTGENERATOR_MAJOR_VERSION "." OFPROJECTGENERATOR_MINOR_VERSION "." OFPROJECTGENERATOR_PATCH_VERSION) diff --git a/commandLine/src/projects/xcodeProject.cpp b/commandLine/src/projects/xcodeProject.cpp index fd8678de..87adb713 100644 --- a/commandLine/src/projects/xcodeProject.cpp +++ b/commandLine/src/projects/xcodeProject.cpp @@ -104,6 +104,47 @@ bool installedSDKItem(const string & key) { return false; } +// child-UUID -> parent-group-UUID over every PBXGroup in `objects`. Used by resolveRefPath to +// turn a -relative PBXFileReference back into a SOURCE_ROOT path. +std::map parentGroupMap(const json & objects) { + std::map parentOf; + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (!it.value().is_object()) continue; + if (it.value().value("isa", "") != "PBXGroup") continue; + if (!it.value().contains("children") || !it.value()["children"].is_array()) continue; + for (auto & c : it.value()["children"]) { + if (c.is_string()) parentOf[c.get()] = it.key(); + } + } + return parentOf; +} + +// Resolve a PBXFileReference's path by walking parents up to SOURCE_ROOT. +// Returns an empty path for refs rooted at SDKROOT / DEVELOPER_DIR / BUILT_PRODUCTS_DIR / etc. +fs::path resolveRefPath(const json & objects, const std::map & parentOf, const string & refUUID) { + if (!objects.contains(refUUID)) return {}; + const auto & ref = objects.at(refUUID); + fs::path accumulated = ref.value("path", ""); + string sourceTree = ref.value("sourceTree", ""); + if (sourceTree == "SOURCE_ROOT") return accumulated; + if (sourceTree != "") return {}; + string cur = refUUID; + for (int depth = 0; depth < 32; ++depth) { // safety cap in case of a cycle + auto p = parentOf.find(cur); + if (p == parentOf.end()) break; + const string & parentUUID = p->second; + if (!objects.contains(parentUUID)) break; + const auto & g = objects.at(parentUUID); + string gPath = g.value("path", ""); + string gTree = g.value("sourceTree", ""); + if (!gPath.empty()) accumulated = fs::path(gPath) / accumulated; + if (gTree == "SOURCE_ROOT") return accumulated; + if (gTree != "") return {}; + cur = parentUUID; + } + return accumulated; +} + json withoutSystemLinks(const json & value, bool frameworks, bool libs, const std::set & known) { json kept = json::array(); for (auto & unit : xcodeSettings::flagUnits("OTHER_LDFLAGS", value)) { @@ -167,6 +208,8 @@ bool xcodeProject::readAppleSettings() { xcodeSettings::configurations(appleProject); targetAttributes(appleProject); xcodeResources::snapshot(appleProject); + snapshotFileSettings(); + snapshotShellScriptPhases(); } for (auto name : { "Project.xcconfig", "openFrameworks-Info.plist", "of.entitlements", "ofxiOS-Info.plist" }) { if (!fs::exists(templatePath / name)) continue; @@ -1403,6 +1446,265 @@ void xcodeProject::addCommand(const string & command) { commands.emplace_back(command); } +void xcodeProject::removeDeadSrcReferences(json & project) { + if (!project.is_object() || !project.contains("objects")) return; + auto & objects = project["objects"]; + if (!objects.is_object()) return; + + auto parentOf = parentGroupMap(objects); + + // Collect basenames of every file that actually exists under projectDir/src/ (any depth). + // We only remove a top-level template placeholder (src/) when the user's project + // has a replacement with the same basename in a subfolder — the ofxMultiplatform pattern + // (src/iOS/main.mm, src/Apps/ofApp.cpp). If nothing matches, the placeholder is kept so a + // project with no main / no ofApp still has a visible entry point in the sidebar. + std::set srcBasenames; + fs::path srcRoot = projectDir / "src"; + if (fs::exists(srcRoot) && fs::is_directory(srcRoot)) { + std::error_code ec; + for (auto it = fs::recursive_directory_iterator(srcRoot, fs::directory_options::skip_permission_denied, ec); + it != fs::recursive_directory_iterator(); it.increment(ec)) { + if (ec) { ec.clear(); continue; } + if (!it->is_regular_file(ec)) continue; + srcBasenames.insert(it->path().filename().string()); + } + } + + std::set deadRefUUIDs; + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (!it.value().is_object()) continue; + if (it.value().value("isa", "") != "PBXFileReference") continue; + string sourceTree = it.value().value("sourceTree", ""); + if (sourceTree != "SOURCE_ROOT" && sourceTree != "") continue; + fs::path resolved = resolveRefPath(objects, parentOf, it.key()); + string resolvedStr = resolved.generic_string(); + // only src/* files (not bin/*, not root-level plists) — baseProject enumerates src/ itself + if (resolvedStr.rfind("src/", 0) != 0) continue; + if (fs::exists(projectDir / resolved)) continue; + + // Depth 1 (src/) is where the template places its main / ofApp placeholders. + // Keep that dead ref unless a same-named file was found elsewhere under src/ (the + // "sources were moved into a subfolder" case). Deeper dead refs (src/sub/.../file) + // are prior-regen leftovers from user additions that have since been deleted — drop + // those unconditionally, they can't be anything but orphans. + auto depth = std::distance(resolved.begin(), resolved.end()); + if (depth == 2) { // "src" / "" + string basename = resolved.filename().string(); + if (!srcBasenames.count(basename)) { + ofLogVerbose(LOG_NAME) << "keeping dead template placeholder " << resolvedStr + << " — no replacement with that basename found under src/"; + continue; + } + } + deadRefUUIDs.insert(it.key()); + } + + if (deadRefUUIDs.empty()) return; + + std::set deadBuildUUIDs; + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (!it.value().is_object()) continue; + if (it.value().value("isa", "") != "PBXBuildFile") continue; + string refUUID = it.value().value("fileRef", ""); + if (deadRefUUIDs.count(refUUID)) deadBuildUUIDs.insert(it.key()); + } + + auto scrubArray = [](json & arr, const std::set & toRemove) { + if (!arr.is_array()) return; + json kept = json::array(); + for (auto & e : arr) { + if (e.is_string() && toRemove.count(e.get())) continue; + kept.push_back(e); + } + arr = std::move(kept); + }; + + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (!it.value().is_object()) continue; + string isa = it.value().value("isa", ""); + if (isa == "PBXGroup" && it.value().contains("children")) { + scrubArray(it.value()["children"], deadRefUUIDs); + } else if (isa.find("BuildPhase") != string::npos && it.value().contains("files")) { + scrubArray(it.value()["files"], deadBuildUUIDs); + } + } + + for (auto & u : deadRefUUIDs) objects.erase(u); + for (auto & u : deadBuildUUIDs) objects.erase(u); + + ofLogNotice(LOG_NAME) << "removed " << deadRefUUIDs.size() + << " dead src/* template file reference(s) (sources live in a src/ subfolder)"; +} + +// Walk the pre-regen pbxproj (appleProject, populated by readAppleSettings) and remember any +// PBXBuildFile.settings keyed by the resolved file path. These carry per-file compiler flags +// like -fno-objc-arc (COMPILER_FLAGS), header visibility (ATTRIBUTES), etc. - anything the +// user set in Xcode's File Inspector that would otherwise be wiped when the template pbxproj +// is copied over. +void xcodeProject::snapshotFileSettings() { + appleFileSettings.clear(); + if (appleProject.empty() || !appleProject.is_object() || !appleProject.contains("objects")) return; + const auto & objects = appleProject.at("objects"); + if (!objects.is_object()) return; + auto parentOf = parentGroupMap(objects); + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (!it.value().is_object()) continue; + if (it.value().value("isa", "") != "PBXBuildFile") continue; + if (!it.value().contains("settings")) continue; + const auto & settings = it.value().at("settings"); + if (!settings.is_object() || settings.empty()) continue; + string refUUID = it.value().value("fileRef", ""); + if (refUUID.empty()) continue; + fs::path resolved = resolveRefPath(objects, parentOf, refUUID); + string key = resolved.generic_string(); + if (key.empty()) continue; + // prefer the first non-empty snapshot for a given path (multi-target templates have + // one PBXBuildFile per target; the user usually sets flags once and it propagates). + if (!appleFileSettings.count(key)) appleFileSettings[key] = settings; + } + if (!appleFileSettings.empty()) { + ofLogVerbose(LOG_NAME) << "snapshot per-file settings for " << appleFileSettings.size() << " file(s)"; + } +} + +// Re-apply the snapshotted per-file settings on the freshly generated PBXBuildFile entries. +// The new pbxproj has new UUIDs (generateUUID is random), so we match by resolved file path. +void xcodeProject::restoreFileSettings(json & project) { + if (appleFileSettings.empty()) return; + if (!project.is_object() || !project.contains("objects")) return; + auto & objects = project["objects"]; + if (!objects.is_object()) return; + auto parentOf = parentGroupMap(objects); + int restored = 0; + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (!it.value().is_object()) continue; + if (it.value().value("isa", "") != "PBXBuildFile") continue; + string refUUID = it.value().value("fileRef", ""); + if (refUUID.empty()) continue; + fs::path resolved = resolveRefPath(objects, parentOf, refUUID); + string key = resolved.generic_string(); + if (key.empty()) continue; + auto found = appleFileSettings.find(key); + if (found == appleFileSettings.end()) continue; + // Merge: the generator may have set its own settings (e.g. ATTRIBUTES CodeSignOnCopy on + // framework copies). Preserve those and overlay the user's snapshot on top so a user's + // COMPILER_FLAGS wins against the fresh defaults. + json merged = it.value().value("settings", json::object()); + if (!merged.is_object()) merged = json::object(); + for (auto s = found->second.begin(); s != found->second.end(); ++s) { + merged[s.key()] = s.value(); + } + it.value()["settings"] = merged; + ++restored; + } + if (restored) { + ofLogNotice(LOG_NAME) << "restored per-file compiler settings on " << restored + << " build file(s) (e.g. -fno-objc-arc via COMPILER_FLAGS)"; + } +} + +// Walk the pre-regen pbxproj and remember any PBXShellScriptBuildPhase that isn't shipped by the +// template — this is the user's own Run Script (Editor → Add Build Phase → New Run Script Phase). +// They'd otherwise disappear when createProjectFile overwrites the pbxproj with the template. +// Addon-added scripts (addAfterRule) are filtered out at restore time by signature match. +void xcodeProject::snapshotShellScriptPhases() { + appleShellScriptPhases.clear(); + if (appleProject.empty() || !appleProject.is_object() || !appleProject.contains("objects")) return; + + // (name, shellScript) pairs shipped by the current template — anything matching stays owned by + // the template, not the user. + std::set templateSigs; + try { + auto tmpl = xcodeSettings::readProject(readSettingsFile(templatePath / "emptyExample.xcodeproj/project.pbxproj")); + if (tmpl.is_object() && tmpl.contains("objects") && tmpl.at("objects").is_object()) { + for (auto it = tmpl.at("objects").begin(); it != tmpl.at("objects").end(); ++it) { + if (!it.value().is_object()) continue; + if (it.value().value("isa", "") != "PBXShellScriptBuildPhase") continue; + templateSigs.insert(it.value().value("name", "") + '\x1f' + it.value().value("shellScript", "")); + } + } + } catch (const std::exception & e) { + ofLogVerbose(LOG_NAME) << "snapshotShellScriptPhases: cannot read template for filtering: " << e.what(); + } + + const auto & objects = appleProject.at("objects"); + if (!appleProject.contains("rootObject")) return; + string rootId = appleProject.at("rootObject").get(); + if (!objects.contains(rootId)) return; + const auto & root = objects.at(rootId); + + for (auto & targetId : root.value("targets", json::array())) { + if (!targetId.is_string() || !objects.contains(targetId.get())) continue; + const auto & target = objects.at(targetId.get()); + if (!target.is_object() || target.value("isa", "") != "PBXNativeTarget") continue; + string targetName = target.value("name", ""); + for (auto & phaseId : target.value("buildPhases", json::array())) { + if (!phaseId.is_string() || !objects.contains(phaseId.get())) continue; + const auto & phase = objects.at(phaseId.get()); + if (!phase.is_object() || phase.value("isa", "") != "PBXShellScriptBuildPhase") continue; + string sig = phase.value("name", "") + '\x1f' + phase.value("shellScript", ""); + if (templateSigs.count(sig)) continue; // shipped by template, not user-owned + appleShellScriptPhases.push_back({ { "targetName", targetName }, { "phase", phase } }); + } + } + if (!appleShellScriptPhases.empty()) { + ofLogVerbose(LOG_NAME) << "snapshot " << appleShellScriptPhases.size() << " custom Run Script phase(s)"; + } +} + +// Re-attach snapshotted user Run Script phases to the matching target in the freshly generated +// project. Phases already re-added by addon processing (same name + shellScript) are skipped. +void xcodeProject::restoreShellScriptPhases(json & project) { + if (appleShellScriptPhases.empty()) return; + if (!project.is_object() || !project.contains("objects") || !project.contains("rootObject")) return; + auto & objects = project["objects"]; + if (!objects.is_object()) return; + string rootId = project.at("rootObject").get(); + if (!objects.contains(rootId)) return; + const auto & root = objects.at(rootId); + + // index current targets by name + build existing shell-script signatures to avoid dupes + std::map targetUUIDByName; + std::map> existingByTarget; + for (auto & targetId : root.value("targets", json::array())) { + if (!targetId.is_string() || !objects.contains(targetId.get())) continue; + string tid = targetId.get(); + auto & target = objects[tid]; + if (!target.is_object() || target.value("isa", "") != "PBXNativeTarget") continue; + string tname = target.value("name", ""); + targetUUIDByName[tname] = tid; + for (auto & phaseId : target.value("buildPhases", json::array())) { + if (!phaseId.is_string() || !objects.contains(phaseId.get())) continue; + const auto & phase = objects.at(phaseId.get()); + if (!phase.is_object() || phase.value("isa", "") != "PBXShellScriptBuildPhase") continue; + existingByTarget[tname].insert(phase.value("name", "") + '\x1f' + phase.value("shellScript", "")); + } + } + + int restored = 0; + for (size_t i = 0; i < appleShellScriptPhases.size(); ++i) { + const auto & entry = appleShellScriptPhases[i]; + string tname = entry.value("targetName", ""); + const auto & phase = entry.at("phase"); + string sig = phase.value("name", "") + '\x1f' + phase.value("shellScript", ""); + if (existingByTarget[tname].count(sig)) continue; // addon script re-added, or template dup + auto tit = targetUUIDByName.find(tname); + if (tit == targetUUIDByName.end()) { + ofLogWarning(LOG_NAME) << "restoreShellScriptPhases: target '" << tname + << "' no longer exists, dropping Run Script '" << phase.value("name", "") << "'"; + continue; + } + string phaseUUID = generateUUID(string("preserved-shell-") + tname + "-" + std::to_string(i)); + objects[phaseUUID] = phase; + objects[tit->second]["buildPhases"].push_back(phaseUUID); + existingByTarget[tname].insert(sig); + ++restored; + } + if (restored) { + ofLogNotice(LOG_NAME) << "restored " << restored << " custom Run Script build phase(s)"; + } +} + bool xcodeProject::saveProjectFile() { // debugCommands = true; @@ -1457,6 +1759,12 @@ bool xcodeProject::saveProjectFile() { contents.close(); + try { + removeDeadSrcReferences(j); + } catch (const std::exception & e) { + ofLogWarning(LOG_NAME) << "removeDeadSrcReferences: " << e.what(); + } + for (auto & c : commands) { // alert (c, 31); // readable comments enabled now. @@ -1579,6 +1887,16 @@ bool xcodeProject::saveProjectFile() { ofLogError(LOG_NAME) << "Cannot merge Apple settings: " << e.what(); return false; } + try { + restoreFileSettings(j); + } catch (const std::exception & e) { + ofLogWarning(LOG_NAME) << "restoreFileSettings: " << e.what(); + } + try { + restoreShellScriptPhases(j); + } catch (const std::exception & e) { + ofLogWarning(LOG_NAME) << "restoreShellScriptPhases: " << e.what(); + } std::ofstream jsonFile(fileName); // This is not pretty but address some differences in nlohmann json 3.11.2 to 3.11.3 diff --git a/commandLine/src/projects/xcodeProject.h b/commandLine/src/projects/xcodeProject.h index 156b479c..02d64537 100644 --- a/commandLine/src/projects/xcodeProject.h +++ b/commandLine/src/projects/xcodeProject.h @@ -20,11 +20,29 @@ class xcodeProject : public baseProject { bool loadProjectFile() override; bool saveProjectFile() override; void saveMakefile(); + // drop template PBXFileReference entries under src/ whose file is missing on disk + // (ofxMultiplatform / addon examples that put main/ofApp in a subfolder of src/) + void removeDeadSrcReferences(nlohmann::json & project); + // snapshot per-file PBXBuildFile.settings (COMPILER_FLAGS like -fno-objc-arc, ATTRIBUTES, ...) from + // the previous pbxproj so that regenerating the project does not wipe them. + void snapshotFileSettings(); + // re-apply those per-file settings on the freshly generated PBXBuildFile entries. + void restoreFileSettings(nlohmann::json & project); + // snapshot user-added Run Script phases (PBXShellScriptBuildPhase) that aren't in the template — + // Xcode users attach these via Editor → Add Build Phase → New Run Script Phase and lose them + // when the template pbxproj is copied over on regen. + void snapshotShellScriptPhases(); + // re-attach those phases to their original target (matched by name) in the fresh project. + void restoreShellScriptPhases(nlohmann::json & project); bool readAppleSettings(); void mergeAppleSettings(nlohmann::json & project); void mergeSystemReferences(nlohmann::json & project); void saveAppleSettings(); nlohmann::json appleProject, appleFiles, appleState, appleDefaults, nextAppleDefaults, appleMinimums; + // per-file settings carried across regens (key: resolved relative path like "src/Apps/ofApp.cpp"). + std::map appleFileSettings; + // user-added Run Script phases carried across regens; each entry: { targetName, phase (raw PBXShellScriptBuildPhase json) }. + std::vector appleShellScriptPhases; std::set appleSDKKeys; std::vector savedMakeDefines; bool debugCommands = false; diff --git a/frontend/npm-shrinkwrap.json b/frontend/npm-shrinkwrap.json index 2198ff73..6122ceef 100644 --- a/frontend/npm-shrinkwrap.json +++ b/frontend/npm-shrinkwrap.json @@ -1,12 +1,12 @@ { "name": "projectGenerator", - "version": "0.123.0", + "version": "0.124.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "projectGenerator", - "version": "0.123.0", + "version": "0.124.0", "license": "MIT", "dependencies": { "@types/jquery": "^3.5.34", diff --git a/frontend/package.json b/frontend/package.json index 97591fcc..45a62051 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "projectGenerator", "productName": "projectGenerator", - "version": "0.123.0", + "version": "0.124.0", "description": "Build electron GUI openFrameworks projectGenerator", "license": "MIT", "main": "index.js",