简体   繁体   中英

How can I tidy DBIx::Class::Schema::Loader's output?

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, ie 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM