如何整理 DBIx::Class::Schema::Loader 的输出?

发布于 2024-08-18 08:18:30 字数 512 浏览 3 评论 0原文

目前,我们正在团队中引入 DBIx::Class,我们希望从 DBIx::Class::Schema::Loader 开始。但是,我们对代码风格有严格的要求,即我们将 Perl::Tidy 作为我们 pre-commit 脚本的一部分,因为我们还没有生成任何代码之前的代码。现在,我们必须确保 Schema::Loader 生成的代码干净整洁。我们无法在提交之前对代码运行 perltidy,因为它会破坏 DBIC 的 MD5 哈希。因此,将后处理器集成到 Schema::Loader 中将是我的首选,也可能是唯一可行的解​​决方案。但仍然:你会如何处理这个问题?

编辑我不妨修补DBIx::Class::Schema::Loader::Base以使用perltidy preprocess 参数(如果有)。

We are currently introducing DBIx::Class in our team and we would like to start out with DBIx::Class::Schema::Loader. However, we have hard requirements on code style, i.e. we've got Perl::Tidy as part of our pre-commit script, since we haven't had any generated code before. Now, we'd have to make sure that the code that Schema::Loader generates is clean and tidy. We can't run perltidy over the code before commit, since it screws up DBIC's MD5 hashing. So a post-processor integrated into Schema::Loader would be my preferred and probably the only feasible solution. But still: how would you handle this problem?

EDIT I might as well patch DBIx::Class::Schema::Loader::Base to use a perltidy preprocess parameter if it gets one.

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

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

发布评论

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

评论(3

浪漫之都 2024-08-25 08:18:31

这个问题是不久前提出的,但我今天必须处理这个问题,所以我想我会根据暂时对该模块所做的更改来分享我的解决方案。如果您扫描 PerlTidy 文档中的 --format-skipping,您会发现您可以向 PerlTidy 提供有关不应整理哪些代码的说明。开始和结束标记是 #<<<和#>>分别。所以,默认设置看起来像这样:

# tidy my code
my $foo = 'bar';

#<<<
# don't tidy the code below
my $baz   =     'foo';

# start to tidy again
#>>>

$foo .= 'stuff';

这很简单。现在您只需要让加载器用这些标记包装生成的代码即可。这可能看起来像这样:

my %args = (                                                                                     
    components            => [ 'InflateColumn::DateTime', 'TimeStamp' ],                                                 
    debug                 => 1,                                                                  
    dump_directory        => './lib',                                                            
    filter_generated_code => sub {                                                               
        my ( $type, $class, $text ) = @_;                                                        
        return "#<<<\n$text#>>>";                                                                
    },                                                                                           
    generate_pod            => 0,                                                                
    naming                  => 'current',                                                        
    overwrite_modifications => 0,                                                                
    skip_load_external      => 1,                                                                
    use_moose               => 1,                                                                
    use_namespaces          => 1,                                                                
);                                                                                               

make_schema_at( 'My::Schema', \%args, [$dsn, $user, $pass] ); 

重要的部分是 filter_ generated_code,它允许您包装生成的代码。现在您可以生成模式文件并仍然 PerlTidy 它们。这将允许您整理在生成的文件底部添加的自定义代码,而不会遇到当生成的代码被 make_schema_at() 以外的其他内容更改时发生的错误。

就我而言,我决定关闭 generate_pod,因为 PerlTidy 仍然(出于某种原因)在生成的 Pod 中插入一些换行符。我还不太明白为什么会这样,但是关闭 Pod 就可以解决这个问题,没有它我也能生活。

This question was asked a while ago, but I had to deal with this today, so I thought I'd share my solution, based on the changes made to this module in the time being. If you scan the PerlTidy docs for --format-skipping, you'll see that you can give PerlTidy instructions about which code should not be tidied. The start and end markers are #<<< and #>>> respectively. So, the default settings would look something like this:

# tidy my code
my $foo = 'bar';

#<<<
# don't tidy the code below
my $baz   =     'foo';

# start to tidy again
#>>>

$foo .= 'stuff';

That's easy enough. Now you just need to have the Loader wrap the generated code with these markers. That could look something like this:

my %args = (                                                                                     
    components            => [ 'InflateColumn::DateTime', 'TimeStamp' ],                                                 
    debug                 => 1,                                                                  
    dump_directory        => './lib',                                                            
    filter_generated_code => sub {                                                               
        my ( $type, $class, $text ) = @_;                                                        
        return "#<<<\n$text#>>>";                                                                
    },                                                                                           
    generate_pod            => 0,                                                                
    naming                  => 'current',                                                        
    overwrite_modifications => 0,                                                                
    skip_load_external      => 1,                                                                
    use_moose               => 1,                                                                
    use_namespaces          => 1,                                                                
);                                                                                               

make_schema_at( 'My::Schema', \%args, [$dsn, $user, $pass] ); 

The important part is filter_generated_code, which allows you to wrap the generated code. Now you can generate your schema files and still PerlTidy them. This will allow you to tidy the custom code you add at the bottom of the generated files without running into the errors which happen when the generated code gets altered by something other than make_schema_at().

In my case, I decided to turn off generate_pod, because PerlTidy was still (for some reason) inserting some newlines into the generated Pod. I haven't quite figured out why that is, but turning off the Pod fixes it and I can live without it.

孤云独去闲 2024-08-25 08:18:30

0.05000 已经发布(之前是开发版本),它添加了 overwrite_modifications 选项 rbuels。

我很快也会尝试添加 post_process 选项。

0.05000 has been released (previously the development version) it has the overwrite_modifications option rbuels added.

I will try to add a post_process option as well soon.

心安伴我暖 2024-08-25 08:18:30

DBICSL 的开发版本现在有一个 overwrite_modifications 选项,您可以使用它来忽略代码的 md5summed 部分中的更改。这应该让您在提交之前对输出运行 perltidy,并且仍然能够稍后重新转储。

The development version of DBICSL now has an overwrite_modifications option you can use to ignore changes in the md5summed parts of the code. This should let you run perltidy on the output before committing it, and still be able to re-dump later.

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