File Coverage

blib/lib/Dist/Zilla/Plugin/TemplateXS.pm
Criterion Covered Total %
statement 34 35 97.1
branch 4 6 66.6
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 51 54 94.4


line stmt bran cond sub pod time code
1             $Dist::Zilla::Plugin::TemplateXS::VERSION = '0.003';
2             use Moose;
3 1     1   3011741 with qw(Dist::Zilla::Role::FileGatherer Dist::Zilla::Role::TextTemplate);
  1         3  
  1         7  
4              
5             use Path::Tiny;
6 1     1   6056  
  1         2  
  1         56  
7             use namespace::autoclean;
8 1     1   5  
  1         2  
  1         10  
9             use Sub::Exporter::ForMethods;
10 1     1   81 use Data::Section 0.200002 { installer => Sub::Exporter::ForMethods::method_installer }, '-setup';
  1         2  
  1         11  
11 1     1   195 use Dist::Zilla::File::InMemory;
  1         26  
  1         6  
12 1     1   793 use Moose::Util::TypeConstraints;
  1         2  
  1         31  
13 1     1   6  
  1         2  
  1         9  
14             has template => (
15             is => 'ro',
16             isa => 'Str',
17             predicate => 'has_template',
18             );
19              
20             has style => (
21             is => 'ro',
22             isa => enum(['MakeMaker', 'ModuleBuild']),
23             required => 1,
24             );
25              
26             my ($self, $name) = @_;
27             my @module_parts = split /::/, $name;
28 2     2 1 5 if ($self->style eq 'MakeMaker') {
29 2         9 return $module_parts[-1] . '.xs';
30 2 100       57 }
    50          
31 1         6 elsif ($self->style eq 'ModuleBuild') {
32             return path('lib', @module_parts) . '.xs';
33             }
34 1         6 else {
35             confess 'Invalid style for XS file generation';
36             }
37 0         0 }
38              
39             my ($self, $name) = @_;
40             my $template = $self->has_template ? path($self->template)->slurp_utf8 : ${ $self->section_data('Module.xs') };
41             return $self->fill_in_string($template, { dist => \($self->zilla), name => $name, style => $self->style });
42 2     2 1 60 }
43 2 50       64  
  2         9  
44 2         759 my $self = shift;
45             (my $name = $self->zilla->name) =~ s/-/::/g;
46             $self->add_file(Dist::Zilla::File::InMemory->new({ name => $self->filename($name), content => $self->content($name) }));
47             return;
48 2     2 1 141372 }
49 2         53  
50 2         69 __PACKAGE__->meta->make_immutable;
51 2         3217  
52             1;
53              
54             # ABSTRACT: A simple xs-file-from-template plugin
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             Dist::Zilla::Plugin::TemplateXS - A simple xs-file-from-template plugin
63              
64             =head1 VERSION
65              
66             version 0.003
67              
68             =head1 SYNOPSIS
69              
70             ; In your profile.ini
71             [TemplateXS]
72             style = MakeMaker
73              
74             =head1 DESCRIPTION
75              
76             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.
77              
78             =head1 ATTRIBUTES
79              
80             =head2 style
81              
82             This B<mandatory> argument affects the location of the new XS file. Possible values are:
83              
84             =over 4
85              
86             =item * MakeMaker
87              
88             This will cause the XS file for Foo::Bar to be written to F<Bar.xs>.
89              
90             =item * ModuleBuild
91              
92             This will cause the XS file for Foo::Bar to be written to F<lib/Foo/Bar.xs>.
93              
94             =back
95              
96             =head2 template
97              
98             This contains the path to the template that is to be used. If not set, a default template will be used that looks something like this:
99              
100             #define PERL_NO_GET_CONTEXT
101             #include "EXTERN.h"
102             #include "perl.h"
103             #include "XSUB.h"
104            
105             MODULE = {{ $name }} PACKAGE = {{ $name }}
106            
107             PROTOTYPES: DISABLED
108              
109             =head1 METHODS
110              
111             =head2 filename($module_name)
112              
113             This returns the filename for C<$module_name>, given the specified C<style>.
114              
115             =head2 content($module_name)
116              
117             This returns the appropriate content for C<$module_name>.
118              
119             =head2 gather_files()
120              
121             This adds an XS file for the main module of the distribution.
122              
123             =head1 AUTHOR
124              
125             Leon Timmermans <leont@cpan.org>
126              
127             =head1 COPYRIGHT AND LICENSE
128              
129             This software is copyright (c) 2014 by Leon Timmermans.
130              
131             This is free software; you can redistribute it and/or modify it under
132             the same terms as the Perl 5 programming language system itself.
133              
134             =cut
135              
136             __[ Module.xs ]__
137             #define PERL_NO_GET_CONTEXT
138             #include "EXTERN.h"
139             #include "perl.h"
140             #include "XSUB.h"
141              
142             MODULE = {{ $name }} PACKAGE = {{ $name }}
143              
144             PROTOTYPES: DISABLED
145