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

111
LINES

< > BotCompany Repo | #1011328 // javaCompileToJar_optionalRename - renames if progID != null

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

Transpiled version (14251L) is out of date.

1  
// before you use this, add a RAM disk cleaner
2  
sbool javaCompileToJar_useRAMDisk;
3  
4  
static new ThreadLocal<L<File>> javaCompileToJar_localLibraries;
5  
static new ThreadLocal<IVF1<File>> javaCompileToJar_addMoreFiles;
6  
7  
static File javaCompileToJar_optionalRename(S src, File destJar, S progIDForRename) {
8  
  ret javaCompileToJar_optionalRename(src, "", destJar, progIDForRename);
9  
}
10  
11  
static synchronized File javaCompileToJar_optionalRename(S src, S dehlibs, File destJar, S progIDForRename) {
12  
  ret javaCompileToJar_optionalRename(src, dehlibs, destJar, progIDForRename, null);
13  
}
14  
15  
// returns path to jar
16  
static synchronized File javaCompileToJar_optionalRename(S src, S dehlibs, File destJar, S progIDForRename, S progID) {
17  
  S javaTarget = null; // use default target
18  
  
19  
  //print("Compiling " + l(src) + " chars");
20  
  S dummyClass = "main";
21  
  if (progIDForRename != null) {
22  
    dummyClass = dummyMainClassName(progIDForRename);
23  
    src += "\nclass " + dummyClass + "{}";
24  
  }
25  
  S md5 = md5(src);
26  
  File jar = destJar;
27  
28  
  Class j = getJavaX();
29  
  if (javaTarget != null)
30  
    setOpt(j, "javaTarget", javaTarget);
31  
  //setOpt(j, "verbose", true);
32  
  File srcDir = tempDir();
33  
  S fileName = dummyClass + ".java";
34  
  
35  
  // deriver name of main Java file from source
36  
  LS tok = javaTok(src);
37  
  S packageName = tok_packageName(tok);
38  
  if (packageName != null)
39  
    fileName = packageName.replace(".", "/") + "/" + tok_firstClassName(tok) + ".java";
40  
  File mainJava = new File(srcDir, fileName);
41  
  
42  
  //print("main java: " + mainJava.getAbsolutePath());
43  
  saveTextFile(mainJava, src);
44  
  File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir();
45  
  //print("Compiling to " + f2s(classesDir));
46  
  try {
47  
    L<File> libraries = cloneList(getAndClearTL(javaCompileToJar_localLibraries));
48  
    
49  
    Matcher m = Pattern.compile("\\d+").matcher(dehlibs);
50  
    while (m.find()) {
51  
      S libID = m.group();
52  
      //print("libID=" + quote(libID));
53  
      assertTrue(isSnippetID(libID));
54  
      //print("Adding library " + libID);
55  
      libraries.add(loadLibraryOrSrcLib(libID));
56  
    }
57  
    
58  
    libraries.add(pathToJavaxJar());
59  
      
60  
    S compilerOutput;
61  
    try {
62  
      compilerOutput = (S) call(j, "compileJava", srcDir, libraries, classesDir);
63  
    } catch (Throwable e) {
64  
      compilerOutput = (S) get(getJavaX(), "javaCompilerOutput");
65  
      //fail("Compile Error. " + cleanJavaCompilerOutput(compilerOutput) + " " + e);
66  
      //compilerOutput = indentx("> ", cleanJavaCompilerOutput(compilerOutput));
67  
      //throw JavaCompilerException(compilerOutput, e);
68  
      if (!swic(e.getMessage(), "Java compiler returned errors."))
69  
        compilerOutput = appendWithNewLine(compilerOutput, str(e));
70  
      //printStackTrace(e);
71  
      fail(compilerOutput, e);
72  
    }
73  
    
74  
    compilerOutput = cleanJavaCompilerOutput("Annotation processing got disabled, since it requires a 1.6 compliant JVM");
75  
76  
    if (nempty(compilerOutput)) {
77  
      print("Compiler said: " + compilerOutput);
78  
      //fail("Compile Error. " + compilerOutput);
79  
    }
80  
  
81  
    // sanity test
82  
    if (!anyFileWithExtensionInDir(classesDir, ".class")) {
83  
      printWithIndent("SRC> ", src);
84  
      fail("No classes generated (was compiling " + nChars(src) + ")");
85  
    }
86  
        
87  
    // add sources to .jar
88  
    saveTextFile(new File(classesDir, "main.java"), src);
89  
    
90  
    // add information about libraries to jar
91  
    if (nempty(dehlibs))
92  
      saveTextFile(new File(classesDir, "libraries"), dehlibs);
93  
      
94  
    // add prog id to jar
95  
    saveTextFile(new File(classesDir, "progID"), progID);
96  
97  
    // save pointer to main Java source    
98  
    //saveTextFile(new File(classesDir, "main-src"), fileName);
99  
    
100  
    callF(javaCompileToJar_addMoreFiles!, classesDir);
101  
  
102  
    //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath());
103  
    dir2zip_recurse_verbose = false;
104  
    int n = dir2zip_recurse(classesDir, jar); // cache on success only
105  
    //print("Files zipped: " + n);
106  
  
107  
    ret jar;
108  
  } finally {
109  
    if (isInRAMDisk(classesDir)) deleteDirectory(classesDir);
110  
  }
111  
}

Author comment

Began life as a copy of #1008752

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011328
Snippet name: javaCompileToJar_optionalRename - renames if progID != null
Eternal ID of this version: #1011328/37
Text MD5: 9f6e245069c43077ab51a58d204ca04f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-11 19:22:27
Source code size: 4165 bytes / 111 lines
Pitched / IR pitched: No / No
Views / Downloads: 552 / 696
Version history: 36 change(s)
Referenced in: [show references]