Advertisement
happy-barney

PPI vs CSI (planned)

Jun 18th, 2020
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.53 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use feature 'say';
  6.  
  7. use PPI;
  8. use HPB::PPIx::Utils;
  9.  
  10. for my $file (@ARGV) {
  11.     my $document = PPI::Document->new ($file);
  12.  
  13.     set_test_deep_import_version ($document, $file);
  14.  
  15.     $document->save ($file);
  16. }
  17.  
  18. sub set_test_deep_import_version {
  19.     my ($document, $file) = @_;
  20.  
  21.     return unless grep $_->content eq 'obj_isa', @{ $document->find ('PPI::Token::Word') };
  22.  
  23.     for my $include (@{ $document->find ('PPI::Statement::Include') // [] }) {
  24.         next if $include->pragma;
  25.         next unless $include->module eq 'Test::Deep';
  26.  
  27.         my $version = $include->module_version;
  28.         if ($version && $version->literal < 0.111) {
  29.             $version->previous_sibling->remove
  30.                 unless $version->previous_sibling->significant;
  31.             $version->remove;
  32.             $version = undef;
  33.         }
  34.  
  35.         unless ($version) {
  36.             my $insert_before = $include->schild(2);
  37.  
  38.             $insert_before->insert_before (PPI::Token::Whitespace->new (' '));
  39.             $insert_before->insert_before (PPI::Token::Number::Version->new ('v0.111'));
  40.  
  41.             $include->insert_after (PPI::Token::Coment->new ('# 0.111 => obj_isa'));
  42.             $include->insert_after (PPI::Token::Whitespace->new (' '));
  43.         }
  44.     }
  45. }
  46.  
  47. __END__
  48.  
  49. for my $file (@ARGV) {
  50.     my $document = CSI::Document->new (Perl => $file);
  51.  
  52.     $document->(
  53.         find  { '::Statement::Include' },
  54.         where { $_->module eq 'Test::Deep' },
  55.         where { $_->module_version < v0.111 },
  56.         apply { $_->module_version (v0.111) },
  57.         apply { $_->statement_comment ('0.111 => obj_isa') },
  58.     );
  59.  
  60.     $document->save if $document->modified;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement