/[couchdb]/design/design-couch.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /design/design-couch.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations)
Sun Apr 26 22:33:20 2009 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 1833 byte(s)
detach (mirror) files, better detection of content type,
dump unsupported objects as .json dump
1 dpavlin 29 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     use LWP::UserAgent;
7     use JSON;
8     use Data::Dump qw/dump/;
9     use File::Path qw/mkpath/;
10     use File::Slurp qw//;
11    
12     # design-couch.pl
13     #
14     # 04/26/09 21:12:28 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
15    
16 dpavlin 30 my ( $database, $design ) = @ARGV;
17     die "usage: $0 database design\n" unless $database && $design;
18    
19 dpavlin 29 my $ua = LWP::UserAgent->new;
20    
21 dpavlin 30 my $url = "http://llin.lan:5984/$database/_design/$design";
22 dpavlin 29
23 dpavlin 30 warn "# get $url\n";
24 dpavlin 29 my $response = $ua->get( $url );
25    
26     die $response->status_line if $response->is_error;
27    
28 dpavlin 32 sub create_path {
29     my $path = shift;
30 dpavlin 29 if ( $path =~ m{/} ) {
31     my $dir = $path;
32     $dir =~ s{/[^/]+$}{};
33     mkpath $dir if ! -e $dir;
34 dpavlin 30 #warn "# dir $dir";
35 dpavlin 29 }
36 dpavlin 32 }
37     sub write_file {
38     my ( $path, $content ) = @_;
39     $path =~ s{^/+}{};
40     create_path $path;
41 dpavlin 29 File::Slurp::write_file $path, $content;
42 dpavlin 32 print "$path ", -s $path, " bytes\n";
43 dpavlin 29 }
44    
45 dpavlin 32 sub write_attachment {
46     my ( $path ) = @_;
47     my $file = "_attachemnts/$path";
48     create_path $file;
49     $ua->mirror( "$url/$path", $file );
50     print "detached $file ", -s $file, " bytes\n";
51     }
52    
53 dpavlin 29 sub unroll {
54     my ( $tree, $path ) = @_;
55    
56     my $ref = ref $tree;
57     if ( $ref eq 'HASH' ) {
58 dpavlin 32 foreach my $child ( keys %$tree ) {
59     if ( $child eq '_attachments' ) {
60     write_attachment $_ foreach keys %{ $tree->{$child} };
61     } else {
62     unroll( $tree->{$child}, "$path/$child" );
63     }
64     }
65     } elsif ( $ref ) {
66     warn "UNSUPPORTED $path $ref ", dump( $tree );
67     write_file "$path.json", to_json $tree;
68 dpavlin 29 } elsif ( $ref eq '' ) {
69    
70 dpavlin 32 if ( $tree =~ m[^\s*(function(.*){.*}|/\*|var)]is ) {
71 dpavlin 31 $path .= '.js';
72 dpavlin 32 } elsif ( $tree =~ m{<%=.*%>} ) { # couchapp template
73 dpavlin 31 $path .= '.html';
74 dpavlin 32 } else {
75     warn "can't detect type of $path!";
76 dpavlin 29 }
77    
78 dpavlin 31 write_file $path, $tree;
79 dpavlin 29 }
80    
81     }
82    
83 dpavlin 30 my $json = $response->decoded_content;
84 dpavlin 31 write_file "../$database-$design.js", $json;
85 dpavlin 30
86 dpavlin 32 unroll( from_json $json, '' );
87 dpavlin 30

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26