将目录及其子目录的内容转换为JSON

问题描述 投票:1回答:1

我有来自this堆栈交换问题的以下perl脚本,该问题将目录的内容转换为JSON。

use File::Find; 
use JSON; 
use strict; 
use warnings; 

my $dirs={}; 
my $encoder = JSON->new->ascii->pretty; 
find({wanted => \&process_dir, no_chdir => 1 }, "."); 

print $encoder->encode($dirs); 
sub process_dir { 
    return if !-d $File::Find::name; 
    my $ref=\%$dirs; 

    for(split(/\//, $File::Find::name)) { 
        $ref->{$_} = {} 
        if(!exists $ref->{$_}); 
        $ref = $ref->{$_}; 
  } 
}

我正在Android 6.0上使用Termux运行脚本。

请考虑以下目录:

.
 |--server .
 |         | - File 1.tmp
 |         | - File 2.sql
 |         | - File 3.sql
 |-- js .
 |         | - File 1.js
 |         | - File 2.js
 |         | - File 3.js
 | -css .
 |         | - File 1.js
 |         | - File 2.js
 |         | - File 3.js
 | -assets .
 |         | - Font-awesome 
 |         |        .
 |         |        | - webfont.
 |         |        |          | - File 2.woff
 |         |        |          | - File 2.woff
 |         |        |          | - File 3.woff
 |         |        | - css .
 |         |        |       | - File 2.css
 |         |        |       | - File 2.css
 |         |        |       | - File 3.css
 |         | - Fonts
 |         |        | - ps  .
 |         |        |       | - File 2.woff
 |         |        |       | - File 2.css
 |         |        |       | - File 3.txt
 |         | - images 
 |         |        | - File 1.png
 |         |        | - File 2.png
 |         |        |   - File 3.svg

如果在以上目录中执行脚本,脚本将返回以下内容:

{
"." : {
      "server" : {},                               
      "js" : {},
      "css" : {},
      "assets" : {
         "fonts" : {                                       
            "ps":{}
         },
         "images" : {},
         "font-awesome" : {
            "css" :{},                                     
            "webfonts" : {}
         }
      }                                 
     }                                            
    }

我正在尝试编辑脚本以将每个子目录中的文件另外包含为Js数组(如果可以使用perl + JSON Module,或者仅使用Perl)

Eg [Edit]

{
"." : {
      "server" : {["File1", "File2", "File n"]},
      /* Or just "server" : ["File1", "File2", "File n"], ... */
      "js" : {["File1", "File2", "File n"]},                                
      "assets" : {
         "fonts" : {                                       
             "ps":{["File1", "File2", "File n"]}
          },
         "images" : ["File1", "File2", "File n"]
         "font-awesome" : {
         "css" : {["File1", "File2", "File n"]}                    
         "webfonts" : {["File1", "File2", "File n"]}
     }
  }     
 }                                            
}

这可以实现吗?如果是这样,我应该怎么做?我对perl来说还比较陌生,我很难解决这个问题。

json perl directory find text-processing
1个回答
0
投票

我尝试过,并决定使用特殊键__files__将文件名移至条目。如果您有一个像这样的目录,它将中断。

use File::Find; 
use JSON; 
use strict; 
use warnings;
use 5.12.0;
use Data::Dumper;
my $dirs={}; 
my $encoder = JSON->new->ascii->pretty; 
find({wanted => \&process_dir, no_chdir => 1 }, "."); 
print $encoder->encode($dirs); 
sub process_dir { 
    my $full_name = $File::Find::name;
    my $ref=$dirs; 
    if (-d $full_name ) {
        for (split(/\//, $full_name)) {
            $ref->{$_} ||= {};
            $ref = $ref->{$_};
        }
    } else {
        for (split(/\//, $full_name)){
            if (exists $ref->{$_}) {
                $ref = $ref->{$_}
            } else {
                push @{$ref->{__files__}}, $_;
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.