1#include <Poco/JSON/Array.h>
2#include <Poco/JSON/Parser.h>
3#include <Poco/SHA2Engine.h>
14 Poco::SHA2Engine engine;
16 return Poco::DigestEngine::digestToHex(engine.digest());
19 const auto text =
path.generic_u8string();
20 return std::string(
text.begin(),
text.end());
22std::string
requiredString(Object::Ptr
object,
const std::string& field) {
23 if (!
object || !
object->has(field) || !
object->get(field).isString())
24 throw std::runtime_error(
"Expected string: " + field);
25 auto value =
object->getValue<std::string>(
field);
26 if (
value.empty() ||
value.size() > 4096)
throw std::runtime_error(
"Invalid string: " + field);
31std::string masked(
const std::string&
text,
bool strings) {
33 bool line =
false, block =
false, escaped =
false,
verbatim =
false;
35 for (
size_t i = 0; i <
text.size(); ++i) {
45 if (
c !=
'\n' &&
c !=
'\r')
out[i] =
' ';
46 if (
c ==
'*' && next ==
'/') {
53 if (strings &&
c !=
'\n' &&
c !=
'\r')
out[i] =
' ';
64 if (strings)
out[i + 1] =
' ';
71 if (
c ==
'/' && (next ==
'/' || next ==
'*')) {
78 if (
c ==
'"' ||
c ==
'\'') {
81 if (strings)
out[i] =
' ';
86std::shared_ptr<const File>
parse(
const std::string&
path, std::string
text,
const std::string&
hash) {
87 auto file = std::make_shared<File>();
91 const auto extension = std::filesystem::path(
path).extension().string();
92 if (extension ==
".json") {
93 Poco::JSON::Parser parser;
95 file->json = parser.parse(
file->text);
96 }
else if (extension ==
".nut") {
97 const auto code = masked(
file->text,
true);
98 const auto comments = masked(
file->text,
false);
100 file->structure = metadata.structure;
101 if (!
file->structure.diagnostics.empty())
file->error =
file->structure.diagnostics.front();
103 file->functions.push_back(
106 static const std::regex
reference(R
"REF(\bdofile\s*\(\s*"([^"\n]+)"\s*\))REF");
107 for (
auto it = std::sregex_iterator(comments.begin(), comments.end(),
reference);
108 it != std::sregex_iterator(); ++it)
109 if (
code[
static_cast<size_t>(it->position())] ==
'd')
file->references.push_back((*it)[1].str());
111 }
catch (
const std::exception& e) {
112 file->error = e.what();
114 file->functions.clear();
115 file->structure = {};
122 std::vector<std::string> newPaths;
124 int parsed = 0,
reused = 0, added = 0, modified = 0,
removed = 0;
125 const std::set<std::string> excluded{
".git",
".eve",
"build",
"third-party",
"node_modules"};
126 for (
auto it = std::filesystem::recursive_directory_iterator(
root);
127 it != std::filesystem::recursive_directory_iterator(); ++it) {
128 const auto& entry = *it;
131 if (entry.is_symlink() || std::filesystem::canonical(entry.path()) != entry.path()) {
132 it.disable_recursion_pending();
135 if (entry.is_directory()) {
137 if (
name.starts_with(
'.') || excluded.contains(
name)) it.disable_recursion_pending();
140 if (!entry.is_regular_file())
continue;
141 const auto extension = entry.path().extension().string();
142 if (extension !=
".json" && extension !=
".nut" && extension !=
".dnut" && extension !=
".md")
continue;
143 const auto size = entry.file_size();
144 total +=
static_cast<size_t>(
size);
145 if (
size > 2 * 1024 * 1024 || total > 32 * 1024 * 1024 ||
next.size() >= 512)
146 throw std::runtime_error(
"Project input budget exceeded");
147 std::ifstream
input(entry.path(), std::ios::binary);
148 if (!
input)
throw std::runtime_error(
"Cannot read project source");
149 std::string
text((std::istreambuf_iterator<char>(
input)), {});
150 if (
input.bad() ||
text.size() !=
size)
throw std::runtime_error(
"Project source changed during read");
154 if (old !=
previous.end() && old->second->hash ==
hash) {
162 auto pending = std::make_shared<File>();
166 newPaths.push_back(
path);
170 Poco::JSON::Array::Ptr renames =
new Poco::JSON::Array();
171 std::map<std::string, std::vector<std::string>> deletedHashes, addedHashes;
172 std::set<std::string> reusedRenames;
176 deletedHashes[
file->hash].push_back(
path);
180 for (
const auto& [
hash,
paths] : deletedHashes) {
182 if (
paths.size() != 1 ||
created == addedHashes.end() ||
created->second.size() != 1)
continue;
183 if (std::filesystem::path(
paths.front()).extension() !=
184 std::filesystem::path(
created->second.front()).extension())
186 Object::Ptr rename =
new Object();
187 rename->set(
"from",
paths.front());
188 rename->set(
"to",
created->second.front());
189 renames->add(rename);
195 reusedRenames.insert(
created->second.front());
197 for (
const auto&
path : newPaths)
198 if (!reusedRenames.contains(
path)) {
202 stats->set(
"parsed", parsed);
204 stats->set(
"added", added);
205 stats->set(
"modified", modified);
207 stats->set(
"renames", renames);
209 stats->set(
"bytes", total);
AuthorityStoreHandleRef reference
std::vector< QuestEvent > pending
std::vector< std::filesystem::path > paths
std::shared_ptr< const OceanSprayFrame > previous
std::map< std::string, std::vector< Function > > functions
std::vector< EquipmentStat > stats
MigrationFunction function
static ScriptMetadata analyze(std::string_view source, std::string_view canonicalUri)
Performs source-only metadata extraction without executing code.
std::string extension(std::string_view name)
Extension.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
std::string pathText(const std::filesystem::path &path)
UTF-8 source path, including Windows project names.
Poco::JSON::Object Object
std::map< std::string, std::shared_ptr< const File > > Files
Files scan(const std::filesystem::path &root, const Files &previous, Object::Ptr stats)
Bounded filesystem scan. Parses only changed files; symlinks are excluded.
std::string requiredString(Object::Ptr object, const std::string &field)
Strict source string fields; invalid manifest shapes throw a diagnostic exception.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
ModelData parse(std::span< const uint8_t > bytes)
Parse.