Download
47 lines 1.7 KiBNomsuModified 2019-03-27executable raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
#!/usr/bin/env nomsu -V7.0.0
###
    Tool to automatically update code from old versions of Nomsu. Usage:
    nomsu tools/upgrade.nom [-i] file1 file2 directory1 ...
    If "-i" is the first argument, upgrades will be performed in-place. Otherwise, the
    upgraded code will be printed.
    
use "compatibility"
use "filesystem"
use "consolecolor"
use "commandline"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

command line program with $args:
    $inplace = ($args.i or $args.inplace)
    $start_version = $args."upgrade-from"
    if $start_version:
        try:
            use $start_version
    
    $version = ($args."upgrade-to" or $(NOMSU VERSION))
    $test = ($args.t or $args.test)
    for $filename in $args.extras:
        $file = (read file $filename)
        unless $file:
            fail "File does not exist: \$filename"
        $leading_indent = ($file, matching "\n*([ ]*)")
        $code = (NomsuCode from (Source $filename 1 #$file) $file)
        $tree = ($code parsed $start_version)
        $uptree =
            $tree upgraded from ($start_version or ($tree.version or $(NOMSU VERSION))) to
                $version
        
        $text = "\$leading_indent\($uptree as nomsu, text, with "\n" -> "\n\$leading_indent")"
        when:
            $inplace:
                say "Upgraded \$filename"
                write $text to file $filename
            
            $test:
                if ($uptree == $tree):
                    say (dim "\$filename will not be changed")
                ..else:
                    say (bright "\$filename will be changed")
            
            else:
                say $text inline