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.

// before you use this, add a RAM disk cleaner
sbool javaCompileToJar_useRAMDisk;

static new ThreadLocal<L<File>> javaCompileToJar_localLibraries;
static new ThreadLocal<IVF1<File>> javaCompileToJar_addMoreFiles;

static File javaCompileToJar_optionalRename(S src, File destJar, S progIDForRename) {
  ret javaCompileToJar_optionalRename(src, "", destJar, progIDForRename);
}

static synchronized File javaCompileToJar_optionalRename(S src, S dehlibs, File destJar, S progIDForRename) {
  ret javaCompileToJar_optionalRename(src, dehlibs, destJar, progIDForRename, null);
}

// returns path to jar
static synchronized File javaCompileToJar_optionalRename(S src, S dehlibs, File destJar, S progIDForRename, S progID) {
  S javaTarget = null; // use default target
  
  //print("Compiling " + l(src) + " chars");
  S dummyClass = "main";
  if (progIDForRename != null) {
    dummyClass = dummyMainClassName(progIDForRename);
    src += "\nclass " + dummyClass + "{}";
  }
  S md5 = md5(src);
  File jar = destJar;

  Class j = getJavaX();
  if (javaTarget != null)
    setOpt(j, "javaTarget", javaTarget);
  //setOpt(j, "verbose", true);
  File srcDir = tempDir();
  S fileName = dummyClass + ".java";
  
  // deriver name of main Java file from source
  LS tok = javaTok(src);
  S packageName = tok_packageName(tok);
  if (packageName != null)
    fileName = packageName.replace(".", "/") + "/" + tok_firstClassName(tok) + ".java";
  File mainJava = new File(srcDir, fileName);
  
  //print("main java: " + mainJava.getAbsolutePath());
  saveTextFile(mainJava, src);
  File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir();
  //print("Compiling to " + f2s(classesDir));
  try {
    L<File> libraries = cloneList(getAndClearTL(javaCompileToJar_localLibraries));
    
    Matcher m = Pattern.compile("\\d+").matcher(dehlibs);
    while (m.find()) {
      S libID = m.group();
      //print("libID=" + quote(libID));
      assertTrue(isSnippetID(libID));
      //print("Adding library " + libID);
      libraries.add(loadLibraryOrSrcLib(libID));
    }
    
    libraries.add(pathToJavaxJar());
      
    S compilerOutput;
    try {
      compilerOutput = (S) call(j, "compileJava", srcDir, libraries, classesDir);
    } catch (Throwable e) {
      compilerOutput = (S) get(getJavaX(), "javaCompilerOutput");
      //fail("Compile Error. " + cleanJavaCompilerOutput(compilerOutput) + " " + e);
      //compilerOutput = indentx("> ", cleanJavaCompilerOutput(compilerOutput));
      //throw JavaCompilerException(compilerOutput, e);
      if (!swic(e.getMessage(), "Java compiler returned errors."))
        compilerOutput = appendWithNewLine(compilerOutput, str(e));
      //printStackTrace(e);
      fail(compilerOutput, e);
    }
    
    compilerOutput = cleanJavaCompilerOutput("Annotation processing got disabled, since it requires a 1.6 compliant JVM");

    if (nempty(compilerOutput)) {
      print("Compiler said: " + compilerOutput);
      //fail("Compile Error. " + compilerOutput);
    }
  
    // sanity test
    if (!anyFileWithExtensionInDir(classesDir, ".class")) {
      printWithIndent("SRC> ", src);
      fail("No classes generated (was compiling " + nChars(src) + ")");
    }
        
    // add sources to .jar
    saveTextFile(new File(classesDir, "main.java"), src);
    
    // add information about libraries to jar
    if (nempty(dehlibs))
      saveTextFile(new File(classesDir, "libraries"), dehlibs);
      
    // add prog id to jar
    saveTextFile(new File(classesDir, "progID"), progID);

    // save pointer to main Java source    
    //saveTextFile(new File(classesDir, "main-src"), fileName);
    
    callF(javaCompileToJar_addMoreFiles!, classesDir);
  
    //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath());
    dir2zip_recurse_verbose = false;
    int n = dir2zip_recurse(classesDir, jar); // cache on success only
    //print("Files zipped: " + n);
  
    ret jar;
  } finally {
    if (isInRAMDisk(classesDir)) deleteDirectory(classesDir);
  }
}

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: 697 / 910
Version history: 36 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1033529 - CompileJavaToJar [Java compiler stage 2] - successor of javaCompileToJar_optionalRename