Advertisement
Guest User

Extract Xib files from Storyboard script

a guest
Oct 6th, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.67 KB | None | 0 0
  1. #!/usr/bin/perl -W
  2. # 10/5/13
  3. # script to covert a storyboard to a collection of xib files
  4.  
  5. # NOTE: this is experimental and is not perfect!
  6.  
  7. # 1) find each view ("<objects>" node) and its name
  8. # 2) write out each xib individually
  9. # - using previously-found name
  10. # - resizing; otherwise the xibs come out too wide/short
  11.  
  12. use strict;
  13.  
  14. my $inFile = shift;
  15. if(!$inFile) {
  16.     die "Argument needed: path to storyboard file.\n";
  17. }
  18.  
  19. open(INFILE, $inFile) or die "$!\n";
  20.  
  21. my $sceneStarted = 0;
  22. my $sceneStr = '';
  23.  
  24. my $xibBeginStr = qq^<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  25. <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="4510" systemVersion="12F37" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
  26. <dependencies>
  27. <deployment defaultVersion="1280" identifier="iOS"/>
  28. <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3742"/>
  29. </dependencies>^;
  30. my $xibEndStr = "</document>";
  31.  
  32. #we need to resize the items
  33. my $newSizeStr = qq^<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>^;
  34. my $sizeSeen = 0;
  35.  
  36. my %scenes;
  37. my $currentIndex = 0;
  38. my $xibName = '';
  39. while(<INFILE>) {
  40.     if(/<!--([\s\S]+)-->/) {
  41.         print "Found scene '$1'\n";
  42.         $xibName = $1;
  43.         #remove spaces
  44.         $xibName =~ s/\s//g;
  45.         next;
  46.     }
  47.    
  48.     if(/<objects>/) {
  49.         #<scene sceneID="[\s\S]+">/i) {
  50.         $sceneStarted = 1;
  51.         $sceneStr = $_;
  52.     }
  53.     elsif(/<\/objects>/) {
  54.      #<\/scene>/i) {
  55.         $sceneStr .= $_;
  56.         ##push(@scenes, $sceneStr);
  57.         if(!$xibName) {
  58.             print "WARNING: no name found for this xib\n";
  59.             $xibName = "xib$currentIndex";
  60.             $currentIndex++;
  61.         }
  62.        
  63.         $scenes{$xibName} = $sceneStr;
  64.         $sceneStr = '';
  65.         $sceneStarted = 0;
  66.         $xibName = '';
  67.         $sizeSeen = 0;
  68.     }
  69.     elsif($sceneStarted) {
  70.        
  71.         #resize this view
  72.         if(!$sizeSeen && /<rect/) {
  73.             print "\treplacing '$_' with '$newSizeStr'\n";
  74.             $_ = $newSizeStr;
  75.             $sizeSeen = 1;
  76.         }
  77.        
  78.         $sceneStr .= $_;
  79.     }
  80. }
  81.  
  82. close INFILE;
  83.  
  84. my $count = scalar (keys %scenes);
  85. print "$count scenes found.";
  86. my $currentIndex = 1;
  87. my $xibName = '';
  88. foreach(keys %scenes) {
  89.     $xibName = "$_.xib";
  90.     #"$currentIndex.xib"
  91.     print "Creating xib file $xibName\n";
  92.     open (OUTFILE, ">$xibName") or die "Could not create $xibName. ($!)\n";
  93.     my $_xibStr = $scenes{$_};
  94.     my $xibStr = "$xibBeginStr\n$_xibStr$xibEndStr";
  95.     print OUTFILE $xibStr;
  96.     close OUTFILE;
  97.     $currentIndex++;
  98. }
  99.  
  100. print "DONE\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement