File Coverage

blib/lib/Minilla/Migrate.pm
Criterion Covered Total %
statement 45 141 31.9
branch 0 36 0.0
condition 0 3 0.0
subroutine 15 24 62.5
pod 0 8 0.0
total 60 212 28.3


line stmt bran cond sub pod time code
1             package Minilla::Migrate;
2 1     1   2407 use strict;
  1         3  
  1         30  
3 1     1   5 use warnings;
  1         3  
  1         24  
4 1     1   5 use utf8;
  1         2  
  1         4  
5              
6 1     1   24 use File::pushd;
  1         2  
  1         62  
7 1     1   7 use CPAN::Meta;
  1         2  
  1         19  
8 1     1   5 use File::Find ();
  1         1  
  1         44  
9 1     1   7 use TOML 0.92 qw(to_toml);
  1         20  
  1         53  
10 1     1   7 use Config;
  1         3  
  1         49  
11              
12 1     1   7 use Minilla::Gitignore;
  1         2  
  1         40  
13 1         83 use Minilla::Util qw(
14             slurp spew require_optional
15             cmd cmd_perl slurp_utf8 spew_utf8
16             slurp_raw spew_raw
17 1     1   6 );
  1         2  
18 1     1   8 use Minilla::Logger;
  1         2  
  1         59  
19 1     1   8 use Minilla::Git;
  1         2  
  1         84  
20 1     1   7 use Minilla::Project;
  1         2  
  1         18  
21              
22 1     1   5 use Moo;
  1         2  
  1         6  
23              
24             has project => (
25             is => 'lazy',
26             );
27              
28 1     1   425 no Moo;
  1         2  
  1         5  
29              
30             sub _build_project {
31 0     0     my $self = shift;
32 0           Minilla::Project->new();
33             }
34              
35             sub run {
36 0     0 0   my $self = shift;
37              
38 0 0         if (!-d '.git') {
39             # init git repo
40 0           infof("Initializing git\n");
41 0           cmd('git', 'init');
42             }
43              
44 0           my $guard = pushd($self->project->dir);
45              
46             # Generate cpanfile from Build.PL/Makefile.PL
47 0 0         unless (-f 'cpanfile') {
48 0           $self->migrate_cpanfile();
49             }
50              
51 0           $self->generate_license();
52              
53             # TODO move top level *.pm to lib/?
54              
55 0 0         if (-f 'dist.ini') {
    0          
56 0           $self->dist_ini2minil_toml();
57             } elsif (!-f 'minil.toml') {
58 0           $self->project->generate_minil_toml('Default');
59             }
60              
61 0           $self->remove_unused_files();
62 0           $self->migrate_gitignore();
63 0           $self->project->regenerate_files();
64 0           $self->migrate_changes();
65              
66 0           git_add('.');
67             }
68              
69             sub migrate_changes {
70 0     0 0   my $self = shift;
71              
72 0 0         if (-f 'Changes') {
73             # Q. Why :raw?
74             # A. It's for windows. See dzil.
75 0           my $content = slurp_raw('Changes');
76 0 0         unless ($content =~ qr!\{\{\$NEXT\}\}!) {
77 0           $content =~ s!^(Revision history for Perl extension \S+\n\n)!$1\{\{\$NEXT\}\}\n\n!;
78             }
79 0           spew_raw('Changes', $content);
80             } else {
81             # Q. Why :raw?
82             # A. It's for windows. See dzil.
83 0           require Minilla::Profile::Default;
84 0           Minilla::Profile::Default->new_from_project(
85             $self->project
86             )->render('Changes');
87             }
88             }
89              
90             sub rm {
91 0     0 0   my ($self, $file) = @_;
92             }
93              
94             sub dist_ini2minil_toml {
95 0     0 0   my $self = shift;
96              
97 0           infof("Converting dist.ini to minil.toml\n");
98              
99 0           require_optional( 'Config/MVP/Reader/INI.pm', 'Migrate dzil repo' );
100 0           require_optional( 'Dist/Zilla/MVP/Assembler.pm', 'Migrate dzil repo' );
101 0           require_optional( 'Dist/Zilla/Chrome/Term.pm', 'Migrate dzil repo' );
102 0           my $sequence = Config::MVP::Reader::INI->read_into_assembler(
103             'dist.ini',
104             Dist::Zilla::MVP::Assembler->new(
105             chrome => Dist::Zilla::Chrome::Term->new(),
106             )
107             );
108 0           my $conf = do {
109             # Note. dist.ini using @Milla does not have '_' section.
110 0           my $section = $sequence->section_named('_');
111 0 0         $section ? $section->payload : +{};
112             };
113              
114 0           my $dst = +{};
115 0           for my $key (qw(name author version license)) {
116 0 0         if ( defined(my $val = $conf->{$key}) ) {
117 0           $dst->{$key} = $val;
118             }
119             }
120 0 0         if (%$dst) {
121 0           my $toml = to_toml($dst);
122 0           spew( 'minil.toml' => $toml );
123 0           git_add('minil.toml');
124             }
125 0           git_rm('--quiet', 'dist.ini');
126              
127 0           $self->project->clear_metadata();
128             }
129              
130             sub generate_license {
131 0     0 0   my ($self) = @_;
132              
133 0 0         unless (-f 'LICENSE') {
134 0           spew_raw('LICENSE', $self->project->metadata->license->fulltext());
135 0           git_add(qw(-f LICENSE));
136             }
137             }
138              
139             sub migrate_cpanfile {
140 0     0 0   my ($self) = @_;
141              
142 0           my $metafile;
143 0 0         if (-f 'Build.PL') {
    0          
    0          
144 0 0         if (slurp('Build.PL') =~ /Module::Build::Tiny/) {
145 0           infof("M::B::Tiny was detected. I hope META.json is already exists here\n");
146 0           $metafile = 'META.json';
147             } else {
148 0           cmd_perl('Build.PL');
149 0           $metafile = 'MYMETA.json';
150             }
151             } elsif (-f 'Makefile.PL') {
152             # ExtUtils::MakeMaker or Module::Install's
153 0           cmd_perl('Makefile.PL');
154 0           cmd($Config{make}, 'metafile');
155 0           $metafile = 'MYMETA.json';
156             } elsif (-f 'dist.ini') {
157 0           my %orig = map { $_ => 1 } glob('*/META.yml');
  0            
158 0           cmd_perl('-S', 'dzil', 'build');
159 0           ($metafile) = grep { !$orig{$_} } glob('*/META.yml');
  0            
160             } else {
161 0           errorf("There is no Build.PL/Makefile.PL/dist.ini: %s\n", Cwd::getcwd());
162             }
163              
164 0 0 0       unless (defined($metafile) && -f $metafile) {
165 0           errorf("Build.PL/Makefile.PL does not generates %s\n", $metafile);
166             }
167              
168 0           my $meta = CPAN::Meta->load_file($metafile);
169 0           my $prereqs = $meta->effective_prereqs->as_string_hash;
170              
171 0           infof("Using Module::Build (Because this distribution uses xs)\n");
172 0           delete $prereqs->{configure}->{requires}->{'ExtUtils::MakeMaker'};
173 0           delete $prereqs->{configure}->{requires}->{'Module::Build'};
174 0           delete $prereqs->{configure}->{requires}->{'Module::Build::Tiny'};
175              
176 0           my $cpanfile = Module::CPANfile->from_prereqs($prereqs);
177 0           spew('cpanfile', $cpanfile->to_string);
178              
179 0           git_add('cpanfile');
180             }
181              
182             sub remove_unused_files {
183 0     0 0   my $self = shift;
184              
185             # Some users put a README.pod symlink for main module.
186             # But it's duplicated to README.md created by Minilla.
187              
188             # remove some unusable files
189 0           for my $file (qw(
190             Makefile.PL
191             MANIFEST
192             MANIFEST.SKIP
193             .shipit
194             xt/97_podspell.t
195             xt/99_pod.t
196             xt/01_podspell.t xt/03_pod.t xt/05_cpan_meta.t
197             xt/04_minimum_version.t xt/06_meta_author.t
198             xt/podspell.t
199             MANIFEST.SKIP.bak
200             MANIFEST.bak
201             README.pod
202             META.yml
203             README
204             MYMETA.json
205             MYMETA.yml
206             inc/Module/Install.pm
207             ), glob('inc/Module/Install/*.pm')) {
208 0 0         if (-e $file) {
209 0 0         if (grep { $_ eq $file } git_ls_files()) {
  0            
210             # committed file
211 0           git_rm('--quiet', $file);
212             } else {
213 0           unlink $file;
214             }
215             }
216             }
217             }
218              
219             sub migrate_gitignore {
220 0     0 0   my ($self) = @_;
221              
222 0           my @lines;
223              
224 0 0         my $gitignore = (
225             -f '.gitignore'
226             ? Minilla::Gitignore->load('.gitignore')
227             : Minilla::Gitignore->new()
228             );
229 0           $gitignore->remove('META.json');
230 0           $gitignore->remove('/META.json');
231 0           $gitignore->remove('LICENSE');
232              
233             # Add some lines
234 0           $gitignore->add(sprintf('/%s-*', $self->project->dist_name));
235 0           for my $fname (qw(
236             /.build
237             /_build_params
238             /Build
239             /Build.bat
240             !Build/
241             !META.json
242             !LICENSE
243             )) {
244 0           $gitignore->add($fname);
245             }
246              
247 0           $gitignore->save('.gitignore');
248              
249 0           git_add(qw(.gitignore));
250             }
251              
252              
253              
254             1;
255