@@ -177,6 +177,7 @@ class EntityFolder {
177
177
178
178
FlattenObject flatten (EntityFolder entityFolder) {
179
179
var names = _getPath (entityFolder);
180
+ names = names.toSet ().toList ();
180
181
// print(names);
181
182
var files = _getFiles (entityFolder);
182
183
// names = _merge(names);
@@ -189,6 +190,7 @@ List<String> _getPath(EntityFolder entityFolder) {
189
190
for (var i in entityFolder.children) {
190
191
if (i.runtimeType == EntityFile ) {
191
192
names.add ("${(i as EntityFile ).fatherPath }/${i .name }" );
193
+ names.add ((i).fatherPath);
192
194
} else {
193
195
if (! (i as EntityFolder ).hasChildren) {
194
196
var s = "${i .fatherPath }/${i .name }" ;
@@ -226,6 +228,11 @@ class FlattenObject {
226
228
List <EntityFile > files;
227
229
228
230
FlattenObject ({this .files = const [], this .path = const []});
231
+
232
+ @override
233
+ String toString () {
234
+ return path.toString () + files.map ((e) => e.toJson ()).toList ().toString ();
235
+ }
229
236
}
230
237
231
238
EntityFolder ? toStructured (FlattenObject object,
@@ -250,15 +257,15 @@ EntityFolder? toStructured(FlattenObject object,
250
257
int maxDepth = 0 ;
251
258
for (var s in object.path) {
252
259
if (! isAFile (s)) {
260
+ // print(s);
253
261
var slist = s.split ("/" );
262
+ // print(s);
254
263
// slist.remove("..");
255
264
// slist.remove("root");
256
- // print(slist.length);
257
265
258
266
for (int i = 2 ; i < slist.length; i++ ) {
259
267
EntityFolder _en;
260
268
if (i == 2 ) {
261
- // print("name:${slist[i]}");
262
269
_en = EntityFolder (
263
270
name: slist[i],
264
271
depth: i - 1 ,
@@ -279,16 +286,14 @@ EntityFolder? toStructured(FlattenObject object,
279
286
name: name, depth: i - 1 , children: [], fatherPath: fatherPath);
280
287
if (maxDepth <= _en.depth) maxDepth = _en.depth;
281
288
}
289
+ // print("[dddd]:${_en.toJson()}");
282
290
if (! allFolders.contains (_en)) allFolders.add (_en);
283
291
}
284
292
}
285
293
}
286
- // print(maxDepth);
287
- // for (final i in object.files) {
288
- // if (i.depth > maxDepth) {
289
- // maxDepth = i.depth;
290
- // }
291
- // }
294
+ // print(allFolders.length);
295
+
296
+ // _depthEntityMap[1] = [entityFolder];
292
297
293
298
Map <int , List <EntityFolder >> _depthEntityMap = {};
294
299
_depthEntityMap[0 ] = [entityFolder];
@@ -299,7 +304,7 @@ EntityFolder? toStructured(FlattenObject object,
299
304
_depthEntityMap[i] = _res;
300
305
}
301
306
302
- // print(" _depthEntityMap:$_depthEntityMap" );
307
+ // print(_depthEntityMap);
303
308
304
309
generateFromMap (_depthEntityMap, maxDepth, object.files);
305
310
// print(jsonEncode(_depthEntityMap[0]![0].toJson()));
@@ -310,32 +315,31 @@ EntityFolder? toStructured(FlattenObject object,
310
315
void generateFromMap (Map <int , List <EntityFolder >> depthEntityMap, int maxDepth,
311
316
List <EntityFile > files) {
312
317
// print(maxDepth);
313
-
314
- for (int index = maxDepth; index > 0 ; index-- ) {
315
- for (var j in depthEntityMap[index]! ) {
316
- // print("j:${j.toJson()}");
317
- for (var i in depthEntityMap[index - 1 ]! ) {
318
- // print("i:${i.toJson()}");
319
- List <EntityFile > caches = [];
320
-
321
- for (var f in files) {
322
- if (f.fatherPath.endsWith (i.name)) {
323
- i.addFile (f);
324
- caches.add (f);
318
+ for (int index = maxDepth; index >= 0 ; index-- ) {
319
+ if (index > 0 ) {
320
+ for (var j in depthEntityMap[index]! ) {
321
+ for (var i in depthEntityMap[index - 1 ]! ) {
322
+ // List<EntityFile> caches = [];
323
+
324
+ for (var f in files) {
325
+ if (f.fatherPath.endsWith ("${j .fatherPath }/${j .name }" )) {
326
+ j.addFile (f);
327
+ // caches.add(f);
328
+ }
325
329
}
326
330
327
- if (f.fatherPath.endsWith (j.name)) {
328
- j.addFile (f);
329
- caches.add (f);
331
+ if (j.fatherPath.endsWith (i.name)) {
332
+ i.children.add (j);
330
333
}
331
334
}
332
-
333
- for (var f in caches) {
334
- files.remove (f);
335
- }
336
-
337
- if (j.fatherPath.endsWith (i.name)) {
338
- i.children.add (j);
335
+ }
336
+ } else {
337
+ for (var j in depthEntityMap[index]! ) {
338
+ for (var f in files) {
339
+ if (f.fatherPath.endsWith ("${j .fatherPath }/${j .name }" )) {
340
+ j.addFile (f);
341
+ // caches.add(f);
342
+ }
339
343
}
340
344
}
341
345
}
@@ -549,3 +553,59 @@ bool isAFile(String s) {
549
553
return _tmp.contains ("." );
550
554
}
551
555
}
556
+
557
+ extension FindObject on EntityFolder {
558
+ Object ? findObject (int dep, String objectName) {
559
+ if (depth == dep && name == objectName) {
560
+ return this ;
561
+ }
562
+
563
+ for (var i in children) {
564
+ if (i is EntityFile ) {
565
+ if (i.depth == dep && i.name == objectName) {
566
+ return i;
567
+ } else {
568
+ continue ;
569
+ }
570
+ }
571
+
572
+ if (i is EntityFolder ) {
573
+ if (i.depth == dep && i.name == objectName) {
574
+ return i;
575
+ } else {
576
+ var r = (i).findObject (dep, objectName);
577
+ if (r != null ) {
578
+ return r;
579
+ } else {
580
+ continue ;
581
+ }
582
+ }
583
+ }
584
+ }
585
+
586
+ return null ;
587
+ }
588
+ }
589
+
590
+ extension FindParent on EntityFile {
591
+ EntityFolder ? findParent (EntityFolder en) {
592
+ if (depth == en.depth + 1 && fatherPath.endsWith (en.name)) {
593
+ return en;
594
+ }
595
+
596
+ for (var i in en.children) {
597
+ if (i is EntityFile ) {
598
+ continue ;
599
+ } else {
600
+ var f = findParent (i as EntityFolder );
601
+ if (f != null ) {
602
+ return f;
603
+ } else {
604
+ continue ;
605
+ }
606
+ }
607
+ }
608
+
609
+ return null ;
610
+ }
611
+ }
0 commit comments