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

3

8

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.

Parlor answered 20/1, 2010 at 14:54 Comment(0)
W
3

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.

Watson answered 1/2, 2010 at 12:50 Comment(0)
J
3

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.

Jacquard answered 20/1, 2010 at 18:35 Comment(0)
W
3

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.

Watson answered 1/2, 2010 at 12:50 Comment(0)
T
2

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.

Topper answered 13/12, 2012 at 2:48 Comment(1)
This is brilliant, thanks!Deaton

© 2022 - 2024 — McMap. All rights reserved.