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

Diff of /design/design-couch.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 31 by dpavlin, Sun Apr 26 21:37:40 2009 UTC revision 34 by dpavlin, Tue Apr 28 15:06:02 2009 UTC
# Line 8  use JSON; Line 8  use JSON;
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
9  use File::Path qw/mkpath/;  use File::Path qw/mkpath/;
10  use File::Slurp qw//;  use File::Slurp qw//;
11    use File::Find;
12    use HTTP::Request::Common;
13    
14  # design-couch.pl  # design-couch.pl
15  #  #
16  # 04/26/09 21:12:28 CEST Dobrica Pavlinusic <dpavlin@rot13.org>  # 04/26/09 21:12:28 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
17    
18  my ( $database, $design ) = @ARGV;  my ( $command, $database, $design ) = @ARGV;
19  die "usage: $0 database design\n" unless $database && $design;  die "usage: $0 [push|pull] database design\n" unless $database && $design;
20    
21  my $ua = LWP::UserAgent->new;  my $ua = LWP::UserAgent->new;
22    
23  my $url = "http://llin.lan:5984/$database/_design/$design";  my $url = "http://llin.lan:5984/$database/_design/$design";
24    
25  warn "# get $url\n";  sub create_path {
26  my $response = $ua->get( $url );          my $path = shift;
   
 die $response->status_line if $response->is_error;  
   
 sub write_file {  
         my ( $path, $content ) = @_;  
27          if ( $path =~ m{/} ) {          if ( $path =~ m{/} ) {
28                  my $dir = $path;                  my $dir = $path;
29                  $dir =~ s{/[^/]+$}{};                  $dir =~ s{/[^/]+$}{};
30                  mkpath $dir if ! -e $dir;                  mkpath $dir if ! -e $dir;
31                  #warn "# dir $dir";                  #warn "# dir $dir";
32          }          }
33    }
34    sub write_file {
35            my ( $path, $content ) = @_;
36            $path =~ s{^/+}{};
37            create_path $path;
38          File::Slurp::write_file $path, $content;          File::Slurp::write_file $path, $content;
39          #warn "# write_file $path ", -s $path, " bytes\n";          print "$path ", -s $path, " bytes\n";
40    }
41    
42    sub write_attachment {
43            my ( $path ) = @_;
44            my $file = "_attachemnts/$path";
45            create_path $file;
46            $ua->mirror( "$url/$path", $file );
47            print "detached $file ", -s $file, " bytes\n";
48  }  }
49    
50  sub unroll {  sub unroll {
# Line 42  sub unroll { Line 52  sub unroll {
52    
53          my $ref = ref $tree;          my $ref = ref $tree;
54          if ( $ref eq 'HASH' ) {          if ( $ref eq 'HASH' ) {
55                  unroll( $tree->{$_}, "$path/$_" ) foreach ( keys %$tree );                  foreach my $child ( keys %$tree ) {
56                            if ( $child eq '_attachments' ) {
57                                    write_attachment $_ foreach keys %{ $tree->{$child} };
58                            } else {
59                                    unroll( $tree->{$child}, $path ? "$path/$child" : $child );
60                            }
61                    }
62            } elsif ( $ref ) {
63                    warn "UNSUPPORTED $path $ref ", dump( $tree );
64                    write_file "$path.json", to_json $tree;
65          } elsif ( $ref eq '' ) {          } elsif ( $ref eq '' ) {
                 $path =~ s{^/+}{};  
66    
67                  if ( $tree =~ m{^\s*function}i ) {                  if ( $tree =~ m[^\s*(function(.*){.*}|/\*|//|var)]is ) {
68                          $path .= '.js';                          $path .= '.js';
69                  } elsif ( $tree =~ m{<\w+>} ) {                  } elsif ( $tree =~ m{<%=.*%>} ) { # couchapp template
70                          $path .= '.html';                          $path .= '.html';
71                    } else {
72                            warn "# can't detect type of $path\n";
73                  }                  }
74    
75                  write_file $path, $tree;                  write_file $path, $tree;
                 print "$path ", -s $path, " bytes\n";  
76          }          }
77    
78  }  }
79    
80  my $json = $response->decoded_content;  if ( $command eq 'pull' ) {
81  write_file "../$database-$design.js", $json;  
82            warn "# get $url\n";
83            my $response = $ua->get( $url );
84            die $response->status_line if $response->is_error;
85    
86            my $json = $response->decoded_content;
87            write_file "../$database-$design.pull.js", $json;
88    
89            unroll( from_json $json, '' );
90    
91  unroll( from_json $json, '/' );  } elsif ( $command eq 'push' ) {
92    
93            my $json;
94    
95            my @attached;
96    
97            find({ no_chdir => 1, wanted => sub {
98                    my $path = $File::Find::name;
99                    return unless -f $path;
100    
101    warn "## $path\n";
102    
103                    $path =~ s{^\./}{};
104    
105                    if ( $path =~ m{_attachemnts} ) {
106                            push @attached, $path;
107                            return;
108                    }
109    
110                    my $data = File::Slurp::read_file( $path );
111                    $path =~ s[/]['}->{']g;
112                    $path =~ s{\.\w+$}{};
113                    my $code = "\$json->{'$path'} = \$data;";
114                    eval $code;
115                    die "ERROR in $code: $@" if $@;
116    #               warn "## json = ",dump( $json );
117            }}, '.' );
118    
119            warn "# $database/_design/$design = ",dump( $json );
120            write_file "../$database-$design.push.js", to_json $json;
121    
122            die "no _id?" unless $json->{_id};
123            delete( $json->{_rev} );
124    
125            warn "# put $url\n";
126            my $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) );
127    
128            if ( $response->code == 409 ) {
129                    warn "## get old $url\n";
130                    my $response = $ua->get( $url );
131                    die $response->status_line if $response->is_error;
132    
133                    my $data = from_json $response;
134                    $json->{$_} = $data->{$_} foreach ( 'rev', '_id' );
135    
136                    $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) );
137                    die $response->status_line if $response->is_error;
138                    warn "push updated $url\n";
139            } else {
140                    die $response->status_line if $response->is_error;
141                    warn "push new $url\n";
142            }
143    
144            warn "FIXME: ignore attachments: ",dump( @attached );
145    
146    } else {
147            die "$0: unknown command $command";
148    }
149    

Legend:
Removed from v.31  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.26