Changeset 5996


Ignore:
Timestamp:
Aug 26, 2020, 7:45:23 AM (15 months ago)
Author:
Nicklas Nordborg
Message:

References #1263: The ScriptBuilder? API should perform simple indentation automatically

Indentation is now automatically performed for the following commands: if, elif, else, case, while, until, for

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.opengrid/trunk/src/net/sf/basedb/opengrid/ScriptBuilder.java

    r4270 r5996  
    11package net.sf.basedb.opengrid;
    22
     3import java.util.Arrays;
     4import java.util.Collection;
    35import java.util.HashMap;
     6import java.util.HashSet;
    47import java.util.Map;
     8import java.util.Set;
    59
    610
     
    1822  private Map<String, String> bgProcesses;
    1923 
     24  private int indentLevel;
     25  private String indentString;
     26  private Set<String> autoIndent;
     27  private Set<String> autoUnindent;
    2028 
    2129  /**
     
    3947    this.script = new StringBuilder();
    4048    this.logFolder = logFolder == null ? "${WD}" : logFolder;
     49    this.indentLevel = 0;
     50    this.indentString = "";
     51    this.autoIndent = new HashSet<String>(Arrays.asList("if", "elif", "else", "case", "while", "until", "for"));
     52    this.autoUnindent = new HashSet<String>(Arrays.asList("elif", "else", "fi", "esac", "done"));
    4153  }
    4254
     
    4759  public void comment(String comment)
    4860  {
    49     script.append("# ").append(comment).append("\n");
     61    script.append(indentString).append("# ").append(comment).append("\n");
    5062  }
    5163 
     
    6476  public void time(String comment)
    6577  {
    66     script.append("echo \"`date +%H:%M:%S` ").append(comment).append("\" >> ").append(logFolder).append("/time.log\n");
     78    script.append(indentString).append("echo \"`date +%H:%M:%S` ").append(comment).append("\" >> ").append(logFolder).append("/time.log\n");
    6779  }
    6880 
     
    7486  public void progress(int progress, String status)
    7587  {
    76     script.append("echo ").append(progress).append(" \"").append(status).append("\" > ").append(logFolder).append("/progress\n");
     88    script.append(indentString).append("echo ").append(progress).append(" \"").append(status).append("\" > ").append(logFolder).append("/progress\n");
    7789  }
    7890 
     
    8294  public void cmd(String cmd)
    8395  {
    84     script.append(cmd).append("\n");
     96    checkIndent(cmd, autoUnindent, -1);
     97    script.append(indentString).append(cmd).append("\n");
     98    checkIndent(cmd, autoIndent, 1);
     99  }
     100 
     101  /**
     102    If cmd starts with one of the given keys, apply delta to the
     103    indentation level and create a new indent-string.
     104  */
     105  private void checkIndent(String cmd, Collection<String> keys, int delta)
     106  {
     107    for (String k : keys)
     108    {
     109      if (cmd.startsWith(k))
     110      {
     111        indentLevel = Math.max(0, indentLevel+delta);
     112        indentString = " ".repeat(indentLevel*3);
     113        return;
     114      }
     115    }
    85116  }
    86117 
     
    105136    nextBgPid++;
    106137    bgProcesses.put(pid, cmd);
    107     script.append(cmd).append(" &\n");
    108     script.append(pid).append("=$!\n");
     138    script.append(indentString).append(cmd).append(" &\n");
     139    script.append(indentString).append(pid).append("=$!\n");
    109140    return pid;
    110141  }
     
    142173    }
    143174    bgProcesses.remove(pid);
    144     script.append("wait $").append(pid).append("\n");
     175    script.append(indentString).append("wait $").append(pid).append("\n");
    145176  }
    146177
Note: See TracChangeset for help on using the changeset viewer.