Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

125
LINES

< > BotCompany Repo | #1033503 // LoadableUtilsExpander

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (13517L/85K).

/*
How to:
-set snippet IDs
-call run()
-Manually add lambdaMapLike etc to includeSnippetID
-Transpile lib & then main
-Check that things still work

-TODO: rewritten class names
*/

sclass LoadableUtilsExpander {
  run { prepare(); doIt(); }
  
  S mainProgramSnippetID = #1033411;
  S loadableUtilsSnippetID = #1033505;
  S includeSnippetID = #1033506;
  S globalSnippetID = #1020737;

  transient Set<S> requestedFunctions, requestedClasses;
  transient Set<S> have, haveClasses;
  transient new Set<S> blockedFunctions;
  transient new Set<S> blockedClasses;
  transient Set<S> newFunctions, newClasses;
  transient new Set<S> imported;
  transient Set<S> importedClasses;
  transient Set<S> toImport, classesToImport;
  transient new SS modifiers;
  transient S transpilerOutput;
  
  void prepare {
    transpilerOutput = transpilerOutput(loadSnippet(mainProgramSnippetID));
    LS transpilerLines = mapLines dropLeadingAngleBracketStuff(transpilerOutput);
    
    requestedFunctions = asSet(startingWithIC_dropAndTrim("Compiling function:", transpilerLines);
    requestedClasses = asSet(startingWithIC_dropAndTrim("Compiling class:", transpilerLines);
    
    print(n2(requestedFunctions, "requested function"));
    print(n2(requestedClasses, "requested class", "requested classes"));
    
    for (PairS p : parseColonPropertyPairs(lines(transpilerLines)))
      if (eqOneOf(p.a, "mapLike", "mapMethodLike", "nuLike", "lambdaMapLike", "curry1Like"))
        modifiers.put(p.b, p.a);
    print(n2(modifiers, "function modifier"));
        
    S text = loadSnippet(loadableUtilsSnippetID);
    have = asSet(lmap tok_lastIdentifier(jextractAll("please include function <id>.", text)));
    haveClasses = asSet(lmap tok_lastIdentifier(jextractAll("please include class <id>.", text)));
    
    print("Have " + n2(have, "function"));
    
    S includeSnippet = joinNemptiesWithEmptyLines(
      loadSnippet(includeSnippetID),
      loadSnippet(globalSnippetID));
    
    new Matches m;
    for (S comment : trimAll(getJavaLineComments(text))) {
      if (find3("don't include function *", comment, m))
        blockedFunctions.add($1);
      if (find3("don't include class *", comment, m))
        blockedClasses.add($1);
    }
        
    for (S comment : trimAll(getJavaLineComments(includeSnippet)))
      if (swic(comment, "don't import "))
        blockedFunctions.add(tok_lastIdentifier(comment));
        
    for (S s : jextractAll(javaTok(includeSnippet), "please include function <id>."))
      blockedFunctions.add(tok_lastIdentifier(s));
      
    print(n2(blockedFunctions, "blocked function") + ": " + blockedFunctions);
    print(n2(blockedClasses, "blocked class", "blocked classes") + ": " + blockedClasses);
    
    newFunctions = setMinusSet(setMinusSet(requestedFunctions, blockedFunctions), have);
    print(n2(newFunctions, "new function"));
    
    newClasses = setMinusSet(setMinusSet(requestedClasses, blockedClasses), haveClasses);
    print(n2(newClasses, "new class", "new classes"));
    
    for (S s : tlft(includeSnippet)) {
      s = simpleSpaces(s);
      if (startsWith(s, "import static "))
        imported.add(tok_lastIdentifier(s));
    }
    print(n2(imported, "imported function"));
    
    importedClasses = asSet(lmap tok_lastIdentifier(jextractAll("import loadableUtils.utils.<id>;", includeSnippet)));
    print(n2(importedClasses, "imported class", "imported classes"));
    
    var negativeSet = joinSets(imported, blockedFunctions);
    var positiveSet = joinSets(have, newFunctions);
    toImport = setMinusSet(positiveSet, negativeSet);
    print(n2(toImport) + " function(s) to import");
    
    classesToImport = setMinusSet(haveClasses, importedClasses);
    print(n2(classesToImport) + " class(es) to import");
  }
  
  // appends to Loadable Utils snippet
  void appendThem {
    if (empty(newFunctions) && empty(newClasses)) ret with print("Nothing to do");
    S text = loadSnippet(loadableUtilsSnippetID);
    text += "\n\n"
      + mapToLines(newFunctions, f -> "please include function " + f + ".")
      + mapToLines(newClasses, c -> "please include class " + c + ".");
    editSnippet(loadableUtilsSnippetID, text);
  }
  
  // appends to Compact Module Include
  void appendImported {
    if (empty(toImport) && empty(classesToImport)) ret with print("Nothing to do");
    S text = loadSnippet(includeSnippetID);
    text += "\n\n"
      + mapToLines(toImport, f -> "import static loadableUtils.utils."
        + appendSpaceIfNempty(modifiers.get(f))
        + f + ";")
      + mapToLines(classesToImport, c -> "import loadableUtils.utils." + c + ";");
    editSnippet(includeSnippetID, text);
  }
  
  void doIt {
    appendThem();
    appendImported();
  }
}

Author comment

Began life as a copy of #1030951

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033503
Snippet name: LoadableUtilsExpander
Eternal ID of this version: #1033503/8
Text MD5: b26d8021d6ac10e6f05fdf8f930d8e86
Transpilation MD5: 7da945cfcb08341a5f8dbbcd9c9b088b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-29 08:07:56
Source code size: 4865 bytes / 125 lines
Pitched / IR pitched: No / No
Views / Downloads: 159 / 257
Version history: 7 change(s)
Referenced in: [show references]