|
| 1 | +/* |
| 2 | + * zedProject.cpp |
| 3 | + * |
| 4 | + * Created on: 04/01/2025 |
| 5 | + * Author: Dimitre Lima |
| 6 | + */ |
| 7 | + |
| 8 | +#include "zedProject.h" |
| 9 | +#include "ofLog.h" |
| 10 | +#include "Utils.h" |
| 11 | +#include <nlohmann/json.hpp> |
| 12 | + |
| 13 | + |
| 14 | +using json = nlohmann::json; |
| 15 | + |
| 16 | +struct fileJson { |
| 17 | + fs::path fileName; |
| 18 | + json data; |
| 19 | + |
| 20 | + // only works for workspace |
| 21 | + void addPath(fs::path folder) { |
| 22 | + std::string path = folder.is_absolute() ? folder.string() : "${workspaceRoot}/../" + folder.string(); |
| 23 | + json object; |
| 24 | + object["path"] = path; |
| 25 | + json::json_pointer p = json::json_pointer("/folders"); |
| 26 | + data[p].emplace_back( object ); |
| 27 | + } |
| 28 | + |
| 29 | + void addToArray(string pointer, fs::path value) { |
| 30 | + json::json_pointer p = json::json_pointer(pointer); |
| 31 | + if (!data[p].is_array()) { |
| 32 | + data[p] = json::array(); |
| 33 | + } |
| 34 | + |
| 35 | + std::string path = fs::path(value).is_absolute() ? value.string() : "${workspaceRoot}/" + value.string(); |
| 36 | + data[p].emplace_back( path ); |
| 37 | + } |
| 38 | + |
| 39 | + void load() { |
| 40 | + if (!fs::exists(fileName)) { |
| 41 | + ofLogError(zedProject::LOG_NAME) << "JSON file not found " << fileName.string(); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + std::ifstream ifs(fileName); |
| 46 | + try { |
| 47 | + data = json::parse(ifs); |
| 48 | + } catch (json::parse_error& ex) { |
| 49 | + ofLogError(zedProject::LOG_NAME) << "JSON parse error at byte" << ex.byte; |
| 50 | + ofLogError(zedProject::LOG_NAME) << "fileName" << fileName.string(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void save() { |
| 55 | +// alert ("saving now " + fileName.string(), 33); |
| 56 | +// std::cout << data.dump(1, '\t') << std::endl; |
| 57 | + std::ofstream jsonFile(fileName); |
| 58 | + try { |
| 59 | + jsonFile << data.dump(1, '\t'); |
| 60 | + } catch(std::exception & e) { |
| 61 | + ofLogError(zedProject::LOG_NAME) << "Error saving json to " << fileName.string() << ": " << e.what(); |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +fileJson workspace; |
| 67 | +fileJson cppProperties; |
| 68 | +std::string zedProject::LOG_NAME = "zedProject"; |
| 69 | + |
| 70 | +bool zedProject::createProjectFile(){ |
| 71 | + |
| 72 | + if (backupProjectFiles) { |
| 73 | + createBackup({ projectDir / ".vscode" / "c_cpp_properties.json" }, projectDir); |
| 74 | + createBackup({ projectDir / ".vscode" / "extensions.json" }, projectDir); |
| 75 | + createBackup({ projectDir / ".vscode" / "launch.json" }, projectDir); |
| 76 | + createBackup({ projectDir / ".vscode" / "tasks.json" }, projectDir); |
| 77 | + createBackup({ projectDir / (projectName + ".code-workspace") }, projectDir); |
| 78 | + createBackup({ projectDir / "addons.make" }, projectDir); |
| 79 | + createBackup({ projectDir / "config.make" }, projectDir); |
| 80 | + createBackup({ projectDir / "Makefile" }, projectDir); |
| 81 | + } |
| 82 | + |
| 83 | +#if defined(__MINGW32__) || defined(__MINGW64__) |
| 84 | + try { |
| 85 | + fs::remove_all(projectDir / ".vscode"); |
| 86 | + } catch(fs::filesystem_error& e) { |
| 87 | + ofLogError(LOG_NAME) << "error removing folder .vscode " << e.what(); |
| 88 | + return false; |
| 89 | + } |
| 90 | +#endif |
| 91 | + |
| 92 | + try { |
| 93 | + fs::copy(templatePath / ".vscode", projectDir / ".vscode", fs::copy_options::update_existing | fs::copy_options::recursive); |
| 94 | + } catch(fs::filesystem_error& e) { |
| 95 | + ofLogError(LOG_NAME) << "error copying folder " << templatePath.string() << " : " << projectDir.string() << " : " << e.what(); |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + workspace.fileName = fs::path { |
| 101 | + projectDir / (projectName + ".code-workspace")}; |
| 102 | + cppProperties.fileName = fs::path { |
| 103 | + projectDir / ".vscode/c_cpp_properties.json" |
| 104 | + }; |
| 105 | + |
| 106 | + copyTemplateFiles.push_back({ fs::path { templatePath / "Makefile" }, |
| 107 | + fs::path { projectDir / "Makefile" } |
| 108 | + }); |
| 109 | + copyTemplateFiles.push_back({ fs::path { templatePath / "config.make" }, |
| 110 | + fs::path { projectDir / "config.make" } |
| 111 | + }); |
| 112 | + copyTemplateFiles.push_back({ fs::path { templatePath / "emptyExample.code-workspace" }, |
| 113 | + fs::path { projectDir / workspace.fileName } |
| 114 | + }); |
| 115 | + |
| 116 | + for (auto & c : copyTemplateFiles) { |
| 117 | + try { |
| 118 | + c.run(); |
| 119 | + } catch (const std::exception& e) { |
| 120 | + std::cerr << "Error running copy template files: " << e.what() << std::endl; |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +bool zedProject::loadProjectFile(){ |
| 130 | + workspace.load(); |
| 131 | + cppProperties.load(); |
| 132 | + return true; |
| 133 | +} |
| 134 | + |
| 135 | +void zedProject::addAddonBegin(const ofAddon& addon) { |
| 136 | +// alert("zedProject::addAddon() " + addon.name, 35); |
| 137 | + |
| 138 | + workspace.addPath(addon.addonPath); |
| 139 | + |
| 140 | + // examples of how to add entries to json arrays |
| 141 | +// cppProperties.addToArray("/env/PROJECT_ADDON_INCLUDES", addon.addonPath); |
| 142 | +// cppProperties.addToArray("/env/PROJECT_EXTRA_INCLUDES", addon.addonPath); |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +bool zedProject::saveProjectFile(){ |
| 148 | + workspace.data["openFrameworksProjectGeneratorVersion"] = getPGVersion(); |
| 149 | + workspace.save(); |
| 150 | + cppProperties.save(); |
| 151 | + return true; |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +void zedProject::addSrc(const fs::path & srcName, const fs::path & folder, SrcType type){ |
| 156 | +// alert ("addSrc " + srcName.string(), 33); |
| 157 | +} |
| 158 | + |
| 159 | +void zedProject::addInclude(const fs::path & includeName){ |
| 160 | +// alert ("addInclude " + includeName, 34); |
| 161 | + cppProperties.addToArray("/env/PROJECT_EXTRA_INCLUDES", includeName); |
| 162 | +} |
| 163 | + |
| 164 | +void zedProject::addLibrary(const LibraryBinary & lib){ |
| 165 | +// alert ("addLibrary " + lib.path, 35); |
| 166 | +} |
0 commit comments