changeset 152:07f3bab17e54

fixmodules
author Frank Benoit <benoit@tionex.de>
date Sun, 24 Aug 2008 23:57:08 +0200
parents eb21d3dfc767
children f70d9508c95c
files fixmodule.d packageimport.d
diffstat 2 files changed, 88 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fixmodule.d	Sun Aug 24 23:57:08 2008 +0200
@@ -0,0 +1,87 @@
+module packageimport;
+
+import tango.io.FilePath;
+import tango.io.File;
+import tango.io.Buffer;
+import tango.io.stream.FileStream;
+import tango.io.stream.TextFileStream;
+import tango.util.log.Trace;
+import tango.text.Regex;
+import tango.text.Util;
+import tango.text.stream.LineIterator;
+import tango.text.convert.Format;
+
+void processDir( char[] dir ){
+    auto pack = dir.dup.replace( '/', '.' );
+    auto fp = FilePath(dir);
+    char[][] mods;
+    // read all module names
+    foreach( fileinfo; fp ){
+        if( fileinfo.folder ){
+            processDir( fileinfo.path ~ fileinfo.name );
+            continue;
+        }
+        if( fileinfo.name.length > 2 && fileinfo.name[ $-2 .. $ ] == ".d" ){
+            mods ~= fileinfo.name.dup;
+        }
+    }
+    // foreach module
+    foreach( mod; mods ){
+        auto filename = Format("{}/{}", dir, mod );
+        auto cont = cast(char[])File( filename ).read;
+        char[][] lines = cont.splitLines();
+        int modLine = -1;
+        foreach( uint idx, char[] line; lines ){
+            if( line.length && line.locatePattern( "module dwtx" ) is 0 ){
+        //Trace.formatln( "mod: {} {}", idx, line );
+                modLine = idx;
+                break;
+            }
+        }
+        int impLine = -1;
+        foreach( uint idx, char[] line; lines ){
+            if( line.length && line.locatePattern( "import dwtx" ) is 0 ){
+        //Trace.formatln( "imp: {} {}", idx, line );
+                impLine = idx;
+                break;
+            }
+        }
+        assert( modLine !is -1 );
+        assert( impLine !is -1 );
+        if( modLine > impLine ){
+            Trace.formatln( "{} {} {} {}", filename, modLine, impLine, lines.length );
+            auto moddecl = lines[ modLine .. modLine + 2 ].dup;
+            for( int i = modLine; i >= impLine+1; i-- ){
+                lines[i+1] = lines[i-1];
+            }
+            lines[ impLine .. impLine+2 ] = moddecl;
+            auto output = new TextFileOutput( filename );
+            bool first = true;
+            foreach( line; lines ){
+                if( !first ){
+                    output.write( \n );
+                }
+                first = false;
+                output.write( line );
+            }
+            output.flush();
+            output.close();
+        }
+    }
+        // read content into buffer
+        // search module statement and print to outfile
+        // write package imports
+        // write all remaining lines
+}
+
+void main(){
+    processDir( "dwtx/text" );
+    processDir( "dwtx/jface/text" );
+    processDir( "dwtx/jface/internal/text" );
+}
+
+
+
+
+
+
--- a/packageimport.d	Sun Aug 24 23:55:45 2008 +0200
+++ b/packageimport.d	Sun Aug 24 23:57:08 2008 +0200
@@ -43,7 +43,7 @@
         auto it = new LineIterator!(char)( new Buffer( cont ));
         foreach( line; it ){
             println(line);
-            if( line.locatePattern( "module " ) is 0 ){
+            if( line.length && line.locatePattern( "module " ) is 0 ){
                 found = true;
                 break;
             }