/[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

Contents of /design/design-couch.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show 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 #!/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 my ( $database, $design ) = @ARGV;
17 die "usage: $0 database design\n" unless $database && $design;
18
19 my $ua = LWP::UserAgent->new;
20
21 my $url = "http://llin.lan:5984/$database/_design/$design";
22
23 warn "# get $url\n";
24 my $response = $ua->get( $url );
25
26 die $response->status_line if $response->is_error;
27
28 sub create_path {
29 my $path = shift;
30 if ( $path =~ m{/} ) {
31 my $dir = $path;
32 $dir =~ s{/[^/]+$}{};
33 mkpath $dir if ! -e $dir;
34 #warn "# dir $dir";
35 }
36 }
37 sub write_file {
38 my ( $path, $content ) = @_;
39 $path =~ s{^/+}{};
40 create_path $path;
41 File::Slurp::write_file $path, $content;
42 print "$path ", -s $path, " bytes\n";
43 }
44
45 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 sub unroll {
54 my ( $tree, $path ) = @_;
55
56 my $ref = ref $tree;
57 if ( $ref eq 'HASH' ) {
58 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 } elsif ( $ref eq '' ) {
69
70 if ( $tree =~ m[^\s*(function(.*){.*}|/\*|var)]is ) {
71 $path .= '.js';
72 } elsif ( $tree =~ m{<%=.*%>} ) { # couchapp template
73 $path .= '.html';
74 } else {
75 warn "can't detect type of $path!";
76 }
77
78 write_file $path, $tree;
79 }
80
81 }
82
83 my $json = $response->decoded_content;
84 write_file "../$database-$design.js", $json;
85
86 unroll( from_json $json, '' );
87

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26