blib/lib/Dist/Zilla/Plugin/TemplateXS.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 91.0% 71 / 78 CC 4 SCAR 3.8
CC
4
Cov
95%
filename
3.1
gather_files
2.0
BEGIN
1.0
CRAP 1.5
↑ Summary
1 package Dist::Zilla::Plugin::TemplateXS;
2 1 $Dist::Zilla::Plugin::TemplateXS::VERSION = '0.006';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 with qw(Dist::Zilla::Role::FileGatherer Dist::Zilla::Role::TextTemplate);
5  
6 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Sub::Exporter::ForMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Section 0.200002 { installer => Sub::Exporter::ForMethods::method_installer }, '-setup';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Dist::Zilla::File::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use MooseX::Types::Moose qw/Str Bool ArrayRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶sub mvp_multivalue_args($class) {
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
19 2 return 'includes';
20 }
21  
22 2 ▶sub mvp_aliases($class) {
Subroutine mvp_aliases: called CC=1 SCAR=0.0
Pod undocumented
23 2 return { include => 'includes' };
24 }
25  
26 1 has template => (
27 is => 'ro',
28 isa => Str,
29 predicate => 'has_template',
30 );
31  
32 1 has style => (
33 is => 'ro',
34 isa => enum(['MakeMaker', 'ModuleBuild']),
35 required => 1,
36 );
37  
38 has includes => (
39 isa => ArrayRef[Str],
40 traits => ['Array'],
41 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 handles => {
43 includes => 'elements',
44 },
45 );
46  
47 1 has prototypes_line => (
48 is => 'ro',
49 isa => Bool,
50 lazy => 1,
51 builder => '_build_prototypes_line',
52 );
53  
54 2 ▶sub _build_prototypes_line($self) {
Subroutine _build_prototypes_line: called CC=1 SCAR=0.0
55 2 return $self->style eq 'MakeMaker';
56 }
57  
58 2 ▶sub filename($self, $name) {
Subroutine filename: called CC=3 SCAR=11.3
Pod undocumented
59 2 my @module_parts = split /::/, $name;
60 2 ▶ if ($self->style eq 'MakeMaker') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->style eq 'MakeMaker') { } 1 1 2
elsif ($self->style eq 'ModuleBuild') { } 1 0 1
61 1 return $module_parts[-1] . '.xs';
62 }
63 elsif ($self->style eq 'ModuleBuild') {
64 1 return path('lib', @module_parts) . '.xs';
65 }
66 else {
67 0 confess 'Invalid style for XS file generation';
68 }
69 }
70  
71 2 ▶sub gather_files($self) {
Subroutine gather_files: called CC=2 SCAR=6.9
Pod undocumented
72 2 my $module = $self->zilla->name =~ s/-/::/gr;
73 2 my $filename = $self->filename($module);
74 2 my $includes = join "\n", map { qq{#include "$_"}} $self->includes;
75  
76 2 ▶ my $template = $self->has_template ? path($self->template)->slurp_utf8 : ${ $self->section_data('Module.xs') };
Branch: 1 branch
BranchTrueFalseTotal
$self->has_template ? : 0 2 2
77 2 my $content = $self->fill_in_string($template, {
78 module => $module,
79 includes => $includes,
80 prototypes_line => $self->prototypes_line,
81 });
82  
83 2 $self->add_file(Dist::Zilla::File::InMemory->new({ name => $filename, content => $content }));
84 2 return;
85 }
86  
87 1 __PACKAGE__->meta->make_immutable;
88  
89 1 1;
90  
91 # ABSTRACT: A simple xs-file-from-template plugin
92  
93 =pod
94  
95 =encoding UTF-8
96  
97 =head1 NAME
98  
99 Dist::Zilla::Plugin::TemplateXS - A simple xs-file-from-template plugin
100  
101 =head1 VERSION
102  
103 version 0.006
104  
105 =head1 SYNOPSIS
106  
107 ; In your profile.ini
108 [TemplateXS]
109 style = MakeMaker
110 include = ppport.h
111  
112 =head1 DESCRIPTION
113  
114 This is a L<FileGatherer|Dist::Zilla::Role::FileGatherer> used for creating new XS files when minting a new dist with C<dzil new>. It uses L<Text::Template> (via L<Dist::Zilla::Role::TextTemplate>) to render a template into a XS file. The template is given three variables for use in rendering: C<$name>, the module name; C<$dist>, the Dist::Zilla object, and C<$style>, the C<style> attribute that determines the location of the new file.
115  
116 =head1 ATTRIBUTES
117  
118 =head2 style
119  
120 This B<mandatory> argument affects the location of the new XS file. Possible values are:
121  
122 =over 4
123  
124 =item * MakeMaker
125  
126 This will cause the XS file for Foo::Bar to be written to F<Bar.xs>.
127  
128 =item * ModuleBuild
129  
130 This will cause the XS file for Foo::Bar to be written to F<lib/Foo/Bar.xs>.
131  
132 =back
133  
134 =head2 prototypes_line
135  
136 If enabled, a prototypes lines will be emitted. This is necessary when using L<ExtUtils::MakeMaker>, but when using L<Module::Build> or L<Module::Build::Tiny>, so it's enabled by default only when C<style> is C<MakeMaker>.
137  
138 =head2 template
139  
140 This contains the B<path> to the template that is to be used. If not set, a default template will be used that looks something like this:
141  
142 #define PERL_NO_GET_CONTEXT
143 #include "EXTERN.h"
144 #include "perl.h"
145 #include "XSUB.h"
146
147 {{ $includes }}
148  
149 MODULE = {{ $module }} PACKAGE = {{ $module }}
150  
151 {{ $prototypes_line ? "PROTOTYPES: DISABLE\n" : '' }}
152  
153 =head1 AUTHOR
154  
155 Leon Timmermans <leont@cpan.org>
156  
157 =head1 COPYRIGHT AND LICENSE
158  
159 This software is copyright (c) 2014 by Leon Timmermans.
160  
161 This is free software; you can redistribute it and/or modify it under
162 the same terms as the Perl 5 programming language system itself.
163  
164 =cut
165  
166 __DATA__
↑ Summary