| 1 | package TeamCity::Message; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 2 | our $VERSION = '0.02'; | |||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | use Time::HiRes qw( time ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | ▶ | use Exporter qw( import ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | ## no critic (Modules::ProhibitAutomaticExportation) | ||||||||||||
| 13 | 2 | our @EXPORT = qw( tc_message ); | |||||||||||
| 14 | ## use critic | ||||||||||||
| 15 | 2 | our @EXPORT_OK = ( @EXPORT, 'tc_timestamp' ); | |||||||||||
| 16 | |||||||||||||
| 17 | 6 | ▶ | sub tc_message { | ||||||||||
|
Subroutine
tc_message: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||
| 18 | 6 | my %args = @_; | |||||||||||
| 19 | |||||||||||||
| 20 | 6 | ▶ | my $type = delete $args{type} || 'message'; | ||||||||||
|
Condition50%1/2
delete $args{'type'} || 'message'
MC/DC0%0/1
delete $args{'type'} || 'message'
delete $args{'type'}
Truth table50%1/2
A
A= delete $args{'type'}
| |||||||||||||
| 21 | my $content = delete $args{content} | ||||||||||||
| 22 | 6 | ▶ | or die 'You must provide a content argument to tc_message()'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | |||||||||||||
| 24 | 6 | my $msg = "##teamcity[$type"; | |||||||||||
| 25 | |||||||||||||
| 26 | 6 | ▶ | if ( ref $content ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 4 | for my $name ( sort keys %{$content} ) { | |||||||||||
| 28 | 5 | my $value = $content->{$name}; | |||||||||||
| 29 | 5 | $msg .= qq{ $name='} . _escape($value) . q{'}; | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | $msg .= q{ timestamp='} . tc_timestamp() . q{'} | ||||||||||||
| 33 | 4 | ▶ | unless $content->{timestamp}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | } | ||||||||||||
| 35 | else { | ||||||||||||
| 36 | 2 | ▶ | $msg .= q{ '} . _escape($content) . q{'} or die $!; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 6 | $msg .= "]\n"; | |||||||||||
| 40 | |||||||||||||
| 41 | 6 | return $msg; | |||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | 5 | ▶ | sub tc_timestamp { | ||||||||||
|
Subroutine
tc_timestamp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 45 | 5 | my $now = time; | |||||||||||
| 46 | 5 | my ( $s, $mi, $h, $d, $mo, $y ) = ( gmtime($now) )[ 0 .. 5 ]; | |||||||||||
| 47 | |||||||||||||
| 48 | 5 | my $float = ( $now - int($now) ); | |||||||||||
| 49 | 5 | return sprintf( | |||||||||||
| 50 | '%4d-%02d-%02dT%02d:%02d:%02d.%03d', | ||||||||||||
| 51 | $y + 1900, $mo + 1, $d, | ||||||||||||
| 52 | $h, $mi, $s, | ||||||||||||
| 53 | |||||||||||||
| 54 | # We only need 3 places of precision so if we multiply it by 1,000 we | ||||||||||||
| 55 | # can just treat it as an integer. | ||||||||||||
| 56 | $float * 1000, | ||||||||||||
| 57 | ); | ||||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 7 | ▶ | sub _escape { | ||||||||||
|
Subroutine
_escape: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 7 | my $str = shift; | |||||||||||
| 62 | |||||||||||||
| 63 | 7 | ( my $esc = $str ) =~ s{(['|\]])}{|$1}g; | |||||||||||
| 64 | 7 | $esc =~ s{\n}{|n}g; | |||||||||||
| 65 | 7 | $esc =~ s{\r}{|r}g; | |||||||||||
| 66 | |||||||||||||
| 67 | 7 | return $esc; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 2 | 1; | |||||||||||
| 71 | |||||||||||||
| 72 | # ABSTRACT: Generate TeamCity build messages | ||||||||||||
| 73 | |||||||||||||
| 74 | __END__ | ||||||||||||