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).

1  
/*
2  
How to:
3  
-set snippet IDs
4  
-call run()
5  
-Manually add lambdaMapLike etc to includeSnippetID
6  
-Transpile lib & then main
7  
-Check that things still work
8  
9  
-TODO: rewritten class names
10  
*/
11  
12  
sclass LoadableUtilsExpander {
13  
  run { prepare(); doIt(); }
14  
  
15  
  S mainProgramSnippetID = #1033411;
16  
  S loadableUtilsSnippetID = #1033505;
17  
  S includeSnippetID = #1033506;
18  
  S globalSnippetID = #1020737;
19  
20  
  transient Set<S> requestedFunctions, requestedClasses;
21  
  transient Set<S> have, haveClasses;
22  
  transient new Set<S> blockedFunctions;
23  
  transient new Set<S> blockedClasses;
24  
  transient Set<S> newFunctions, newClasses;
25  
  transient new Set<S> imported;
26  
  transient Set<S> importedClasses;
27  
  transient Set<S> toImport, classesToImport;
28  
  transient new SS modifiers;
29  
  transient S transpilerOutput;
30  
  
31  
  void prepare {
32  
    transpilerOutput = transpilerOutput(loadSnippet(mainProgramSnippetID));
33  
    LS transpilerLines = mapLines dropLeadingAngleBracketStuff(transpilerOutput);
34  
    
35  
    requestedFunctions = asSet(startingWithIC_dropAndTrim("Compiling function:", transpilerLines);
36  
    requestedClasses = asSet(startingWithIC_dropAndTrim("Compiling class:", transpilerLines);
37  
    
38  
    print(n2(requestedFunctions, "requested function"));
39  
    print(n2(requestedClasses, "requested class", "requested classes"));
40  
    
41  
    for (PairS p : parseColonPropertyPairs(lines(transpilerLines)))
42  
      if (eqOneOf(p.a, "mapLike", "mapMethodLike", "nuLike", "lambdaMapLike", "curry1Like"))
43  
        modifiers.put(p.b, p.a);
44  
    print(n2(modifiers, "function modifier"));
45  
        
46  
    S text = loadSnippet(loadableUtilsSnippetID);
47  
    have = asSet(lmap tok_lastIdentifier(jextractAll("please include function <id>.", text)));
48  
    haveClasses = asSet(lmap tok_lastIdentifier(jextractAll("please include class <id>.", text)));
49  
    
50  
    print("Have " + n2(have, "function"));
51  
    
52  
    S includeSnippet = joinNemptiesWithEmptyLines(
53  
      loadSnippet(includeSnippetID),
54  
      loadSnippet(globalSnippetID));
55  
    
56  
    new Matches m;
57  
    for (S comment : trimAll(getJavaLineComments(text))) {
58  
      if (find3("don't include function *", comment, m))
59  
        blockedFunctions.add($1);
60  
      if (find3("don't include class *", comment, m))
61  
        blockedClasses.add($1);
62  
    }
63  
        
64  
    for (S comment : trimAll(getJavaLineComments(includeSnippet)))
65  
      if (swic(comment, "don't import "))
66  
        blockedFunctions.add(tok_lastIdentifier(comment));
67  
        
68  
    for (S s : jextractAll(javaTok(includeSnippet), "please include function <id>."))
69  
      blockedFunctions.add(tok_lastIdentifier(s));
70  
      
71  
    print(n2(blockedFunctions, "blocked function") + ": " + blockedFunctions);
72  
    print(n2(blockedClasses, "blocked class", "blocked classes") + ": " + blockedClasses);
73  
    
74  
    newFunctions = setMinusSet(setMinusSet(requestedFunctions, blockedFunctions), have);
75  
    print(n2(newFunctions, "new function"));
76  
    
77  
    newClasses = setMinusSet(setMinusSet(requestedClasses, blockedClasses), haveClasses);
78  
    print(n2(newClasses, "new class", "new classes"));
79  
    
80  
    for (S s : tlft(includeSnippet)) {
81  
      s = simpleSpaces(s);
82  
      if (startsWith(s, "import static "))
83  
        imported.add(tok_lastIdentifier(s));
84  
    }
85  
    print(n2(imported, "imported function"));
86  
    
87  
    importedClasses = asSet(lmap tok_lastIdentifier(jextractAll("import loadableUtils.utils.<id>;", includeSnippet)));
88  
    print(n2(importedClasses, "imported class", "imported classes"));
89  
    
90  
    var negativeSet = joinSets(imported, blockedFunctions);
91  
    var positiveSet = joinSets(have, newFunctions);
92  
    toImport = setMinusSet(positiveSet, negativeSet);
93  
    print(n2(toImport) + " function(s) to import");
94  
    
95  
    classesToImport = setMinusSet(haveClasses, importedClasses);
96  
    print(n2(classesToImport) + " class(es) to import");
97  
  }
98  
  
99  
  // appends to Loadable Utils snippet
100  
  void appendThem {
101  
    if (empty(newFunctions) && empty(newClasses)) ret with print("Nothing to do");
102  
    S text = loadSnippet(loadableUtilsSnippetID);
103  
    text += "\n\n"
104  
      + mapToLines(newFunctions, f -> "please include function " + f + ".")
105  
      + mapToLines(newClasses, c -> "please include class " + c + ".");
106  
    editSnippet(loadableUtilsSnippetID, text);
107  
  }
108  
  
109  
  // appends to Compact Module Include
110  
  void appendImported {
111  
    if (empty(toImport) && empty(classesToImport)) ret with print("Nothing to do");
112  
    S text = loadSnippet(includeSnippetID);
113  
    text += "\n\n"
114  
      + mapToLines(toImport, f -> "import static loadableUtils.utils."
115  
        + appendSpaceIfNempty(modifiers.get(f))
116  
        + f + ";")
117  
      + mapToLines(classesToImport, c -> "import loadableUtils.utils." + c + ";");
118  
    editSnippet(includeSnippetID, text);
119  
  }
120  
  
121  
  void doIt {
122  
    appendThem();
123  
    appendImported();
124  
  }
125  
}

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: 103 / 180
Version history: 7 change(s)
Referenced in: [show references]