我应该如何将文本文件库与我的模块捆绑?

发布于 2025-02-07 07:37:02 字数 2977 浏览 0 评论 0原文

我在要构建的模块中的Resources目录中具有以下结构:

resources
|-- examples
    |-- Arrays
    |   |-- file  
    |-- Lists
        |-- file1
        |-- file2

我有以下代码来收集和处理这些文件:

use v6.d;
unit module Doc::Examples::Resources;

class Resource {
    has Str $.name;
    has Resource @.resources;
    has Resource %.resource-index;

    method resource-names() {
        @.resources>>.name.sort
    }

    method list-resources() {
        self.resource-names>>.say;
    }

    method is-resource(Str:D $lesson) {
        $lesson ~~ any self.resource-names;
    }

    method get-resource(Str:D $lesson) {
        if !self.is-resource($lesson) {
            say "Sorry, that lesson does not exist.";
            return;
        }
        return %.resource-index{$lesson};
    }

}

class Lesson is Resource {
    use Doc::Parser;
    use Doc::Subroutines;
    has IO $.file;

    method new(IO:D :$file) {
        my $name = $file.basename;
        self.bless(:$name, :$file)
    }

    method parse() {
        my @parsed = parse-file $.file.path;
        die "Failed parse examples from $.file" if @parsed.^name eq 'Any';
        for @parsed -> $section {
            my $heading = $section<meta>[0] || '';
            my $intro = $section<meta>[1] || '';
            say $heading.uc ~ "\n" if $heading && !$intro;
            say $heading.uc if $heading && $intro;
            say $intro ~ "\n" if $intro;
            for $section<code>.Array {
                die "Failed parse examples from $.file, check it's syntax." if $_.^name eq 'Any';
                das |$_>>.trim;
            }
        }
    }
}

class Topic is Resource {
    method new(IO:D :$dir) {
        my $files = dir $?DISTRIBUTION.content("$dir");
        my @lessons;
        my $name = $dir.basename;
        my %lesson-index;
        for $files.Array -> $file {
            my $lesson = Lesson.new(:$file);
            push @lessons, $lesson;
            %lesson-index{$lesson.name} = $lesson;
        }
        self.bless(:$name, resources => @lessons, resource-index => %lesson-index);
    }

}

class LocalResources is Resource is export {
    method new() {
        my $dirs = dir $?DISTRIBUTION.content('resources/examples');
        my @resources;
        my %resource-index;
        for $dirs.Array -> $dir {
            my $t = Topic.new(:$dir);
            push @resources, $t;
            %resource-index{$t.name} = $t;
        }
        self.bless(:@resources, :%resource-index)
    }

    method list-lessons(Str:D $topic) {
        self.get-resource($topic).list-lessons;
    }

    method parse-lesson(Str:D $topic, Str:D $lesson) {
        self.get-resource($topic).get-resource($lesson).parse;
    }
}

它有效。但是,我被告知这是不可靠的,并且不能保证我的$ files = dir $?distribution.content(“ $ dir”);在模块为后是否有效安装或将继续在未来工作。

那么,哪些更好的选择是将文本文件库与我可以由模块访问和找到的模块捆绑在一起?

I have the following structure in the resources directory in a module I'm building:

resources
|-- examples
    |-- Arrays
    |   |-- file  
    |-- Lists
        |-- file1
        |-- file2

I have the following code to collect and process these files:

use v6.d;
unit module Doc::Examples::Resources;

class Resource {
    has Str $.name;
    has Resource @.resources;
    has Resource %.resource-index;

    method resource-names() {
        @.resources>>.name.sort
    }

    method list-resources() {
        self.resource-names>>.say;
    }

    method is-resource(Str:D $lesson) {
        $lesson ~~ any self.resource-names;
    }

    method get-resource(Str:D $lesson) {
        if !self.is-resource($lesson) {
            say "Sorry, that lesson does not exist.";
            return;
        }
        return %.resource-index{$lesson};
    }

}

class Lesson is Resource {
    use Doc::Parser;
    use Doc::Subroutines;
    has IO $.file;

    method new(IO:D :$file) {
        my $name = $file.basename;
        self.bless(:$name, :$file)
    }

    method parse() {
        my @parsed = parse-file $.file.path;
        die "Failed parse examples from $.file" if @parsed.^name eq 'Any';
        for @parsed -> $section {
            my $heading = $section<meta>[0] || '';
            my $intro = $section<meta>[1] || '';
            say $heading.uc ~ "\n" if $heading && !$intro;
            say $heading.uc if $heading && $intro;
            say $intro ~ "\n" if $intro;
            for $section<code>.Array {
                die "Failed parse examples from $.file, check it's syntax." if $_.^name eq 'Any';
                das |$_>>.trim;
            }
        }
    }
}

class Topic is Resource {
    method new(IO:D :$dir) {
        my $files = dir $?DISTRIBUTION.content("$dir");
        my @lessons;
        my $name = $dir.basename;
        my %lesson-index;
        for $files.Array -> $file {
            my $lesson = Lesson.new(:$file);
            push @lessons, $lesson;
            %lesson-index{$lesson.name} = $lesson;
        }
        self.bless(:$name, resources => @lessons, resource-index => %lesson-index);
    }

}

class LocalResources is Resource is export {
    method new() {
        my $dirs = dir $?DISTRIBUTION.content('resources/examples');
        my @resources;
        my %resource-index;
        for $dirs.Array -> $dir {
            my $t = Topic.new(:$dir);
            push @resources, $t;
            %resource-index{$t.name} = $t;
        }
        self.bless(:@resources, :%resource-index)
    }

    method list-lessons(Str:D $topic) {
        self.get-resource($topic).list-lessons;
    }

    method parse-lesson(Str:D $topic, Str:D $lesson) {
        self.get-resource($topic).get-resource($lesson).parse;
    }
}

It works. However, I'm told that this is not reliable and there there is no guarantee that lines like my $files = dir $?DISTRIBUTION.content("$dir"); will work after the module is installed or will continue to work into the future.

So what are better options for bundling a library of text files with my module that can be accessed and found by the module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

献世佛 2025-02-14 07:37:02

资源下的文件目录将始终作为键 %?资源编译时变量如果您在meta6.json文件中以这种方式声明它们:

"resources": [
    "examples/Array/file",
]

等等。

Files under the resources directory will always be available as keys to the %?RESOURCES compile-time variable if you declare them in the META6.json file this way:

"resources": [
    "examples/Array/file",
]

and so on.

祁梦 2025-02-14 07:37:02

我已经解决了一个解决方案。正如Jjmerelo指出的那样,Meta6.json文件包含资源列表,如果您使用逗号,则将自动为您生成资源列表。

从模块的代码中,可以通过$?分发变量访问资源列表,例如:

my @resources = $?distraction.meta&lt; resources&gt;

从此处,我可以建立我的资源列表。

我发现的内容的一个注释是:$?分发变量无法从测试脚本访问。必须将其放置在lib分布目录中的模块中,并导出。

I've settled on a solution. As pointed out by jjmerelo, the META6.json file contains a list of resources and, if you use the comma IDE, the list of resources is automatically generated for you.

From within the module's code, the list of resources can be accessed via the $?DISTRIBUTION variable like so:

my @resources = $?DISTRIBUTION.meta<resources>

From here, I can build up my list of resources.

One note on something I discovered: the $?DISTRIBUTION variable is not accessible from a test script. It has to be placed inside a module in the lib directory of the distribution and exported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文