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

203
LINES

< > BotCompany Repo | #1020140 // Jar Web Bot [LIVE at botcompany.de/jar]

JavaX module (desktop) [tags: butter use-pretranspiled] - homepage

Download Jar. Uses 3310K of libraries. Click here for Pure Java version (18608L/116K).

!7

sS standardCredentials() { ret ""; } // public snippets only

// (transpilation md5, snippetID) => jar file
//static new SerializeComputations<PairS, File> computations;
// transpilation md5 => jar file
static new SerializeComputations<S, File> computations;

// jar file => null (not used yet)
static new SerializeComputations<File, Void> packagingComputations;

static File compileSrcLib(S srcLibID) {
  new CompilerBot2 subBot;
  subBot.dontCompile = true;
  ret assertFileExists(subBot.compileSnippet(srcLibID));
}

static Lock lock = lock();

html {
  // lock lock; // now using SerializeComputations
  
  uri = dropPrefixSlash(uri);
  S snippetID = fsI(uri);
  if (!isSnippetPublic(snippetID)) fail("Snippet not public: " + snippetID);
  S transpiledSrc = getServerTranspiled2(snippetID);
  
  // md5 check (if parameter supplied)
  
  S md5 = params.get('md5);
  S srcMD5 = md5(transpiledSrc);
  if (md5 != null && !eq(md5, srcMD5)) ret "md5 mismatch";
  bool forceCompilation = eq(params.get("forceCompilation"), "1");
  S mainClassForManifest = params.get("mainClassForManifest");
  
  File jar;
  try {
    jar = computations.get(srcMD5/*, snippetID)*/, -> {
      print("Entering compilation (forceCompilation=" + forceCompilation + ")");
      new CompilerBot2 bot;
      //bot.mainClassForManifest = mainClassForManifest;
      bot.useRenaming = !sameSnippetID(snippetID, #1001638);
      bot.forceCompilation = forceCompilation;
      bot.compileSrcLib = lambda1 compileSrcLib;
      ret bot.compileSnippet(snippetID);
    });
  } catch print e {
    // dexcompile.php can't handle code 500
    ret /*subBot_serve500*/("Error. " + e);
  }
  
  bool forceRepackage = eq("1", params.get("repackage"));
  bool withLibs = eq("1", params.get("withLibs"));
  bool noCompiler = eq("1", params.get("noCompiler"));
  bool withX30 = eq("1", params.get("withX30"));
  bool x30UtilOnly = eq("1", params.get("x30UtilOnly"));
  bool noSrc = eq("1", params.get("noSrc"));
  bool dynamicObjectOnly = eq("dynamicObjectOnly", params.get("mode"));
  // currently unsupported
  //bool uncompressed = eq("1", params.get("uncompressed"));
  S nameForDelivery = params.get("name");
  S fileToExtract = params.get("fileToExtract");
  
  L<Map> existingFilesJSON = (L) jsonDecode(params.get("existingFiles"));
  S existingFilesComp = params.get("existingFilesComp");
  if (nempty(existingFilesComp))
    existingFilesJSON = (L) jsonDecode(gunzipToText(base64decode(existingFilesComp)));
  
  File jar2 = jar;
  if (withLibs) jar2 = appendToBaseFileName(jar2, ".with-libs"); 
  if (noCompiler) jar2 = appendToBaseFileName(jar2, ".no-compiler"); 
  if (dynamicObjectOnly) jar2 = appendToBaseFileName(jar2, ".dynamicObjectOnly"); 
  if (noSrc) jar2 = appendToBaseFileName(jar2, ".noSrc"); 
  //if (uncompressed) jar2 = appendToBaseFileName(jar2, ".uncomp"); 
  
  bool withSrc = !noSrc;
      
  IPred<S> generalFileFilter = f -> 
       (withSrc || !endsWith(f, ".java"))
    && !startsWithOneOf(f, "META-INF/versions/", "META-INF/LICENSE"); // see flatlaf jar
  
  if (!eq(jar, jar2)) {
    lock lock;
    
    print(+jar2);
  
    if (fileLength(jar2) <= 22 || forceRepackage) {
      print("Repackaging.");
      
      for (File f : listFilesStartingWith(fileNameWithoutExtension(jar) + ".", dirOfFile(jar))) {
        if (!eq(f, jar))
          deleteFileVerbose(f);
      }
      
      File tempZip = createTempFileWithExtension(".zip");
      {
        temp tempZipOut = zipOutputStream(tempZip);
        text2zip(tempZipOut, "source.txt",
          renderColonProperties(
            jar := f2s(jar2),
            packagingDate := dateWithSecondsUTC()));
        if (nempty(mainClassForManifest))
          text2zip(tempZipOut, manifestPathInJar(), manifestTextForMainClass(mainClassForManifest));
      }

      temp Zip2Zip_Zip4j z2z = new(jar2);
      
      z2z.inZip = tempZip;
      z2z.run();
      
      z2z.inZip = jar;
      z2z.predicate = generalFileFilter;
      /*if (uncompressed) z2z.modifyZipEntry = e -> {
        e.setMethod(ZipOutputStream.STORED);
        e.setCompressedSize(e.getSize());
      };*/
      z2z.run();
      
      if (withLibs) {
        Pair<S, LS> srcAndLibs = extractLibsFromTranspilation(transpiledSrc);
        fOr (S libID : srcAndLibs.b) {
          File fixer = compilerBotDestDir("fixer" + psI(libID) + ".jar");
  
          if (fileExists(fixer)) {
            println("APPLYING FIXER: " + fixer);
            z2z.inZip = fixer;
            z2z.predicate = null;
            z2z.run();
          }
            
          z2z.inZip = loadLibraryOrSrcLib(libID, lambda1 compileSrcLib);
          z2z.run();
        }
      }
      
      z2z.inZip = pathToJavaxJar();
      
      if (withX30) {
        z2z.predicate = null;
        z2z.run();
      } else if (x30UtilOnly) {
        z2z.predicate = name -> startsWithOneOf(name, "x30_pkg");
        z2z.run();
      } else if (noCompiler) {
        z2z.predicate = name -> !
          startsWithOneOf(name, "org/eclipse/", "META-INF/", "ecj.", "about.html");
        z2z.run();
      } else if (dynamicObjectOnly) {
        z2z.predicate = name -> contains(name, "DynamicObject");
        z2z.run();
      }
      
      z2z.finish();
    }
    
    jar = jar2;
  }
  
  if (nempty(existingFilesJSON)) {
    lock lock;
    print(+jar);
    L<Map> newFingerprints = zipFileToJSONFingerprint_md5(jar);
    new SS map;
    for (Map m : existingFilesJSON)
      map.put((S) m.get("name"), (S) m.get("md5"));
    
    jar2 = createTempFile("partial", ".jar");
    {
      temp ZipOutputStream outJar = zipOutputStream(jar2);
      temp ZipFile jarIn = new ZipFile(jar);
      new LS toKeep;
      for (Map fp : newFingerprints) {
        S name = (S) fp.get("name");
        S oldMD5 = map.get(name);
        if (!eq(oldMD5, fp.get("md5"))) {
          outJar.putNextEntry(new ZipEntry(name));
          ZipEntry entry = jarIn.getEntry(name);
          copyStreamAndCloseInput(jarIn.getInputStream(entry), outJar);
        } else
          toKeep.add(name);
      }
      text2zip(outJar, "to-keep", lines_rtrim(toKeep));
    }
    
    jar = jar2;
  }
  
  if (forceRepackage)
    sendToSnippetUpdatesBot("/transpileOK/" + psI(snippetID));

  print("Serving jar for " + snippetID + ": " + renderFileInfo(jar));
  if (eq(params.get("makeJarOnly"), "1"))
    ret "OK: " + f2s(jar);
  else if (nempty(fileToExtract)) {
    // serve one file out of jar file
    ret "TODO";
  } else
    // serve the whole jar file
    ret subBot_serveFileWithName(jar,
      or2(nameForDelivery, psI(snippetID) + ".jar"),
      "application/java-archive");
}

Author comment

Began life as a copy of #1018308

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1020140
Snippet name: Jar Web Bot [LIVE at botcompany.de/jar]
Eternal ID of this version: #1020140/105
Text MD5: fb1f93c60ad367ab74404915e49bf608
Transpilation MD5: 4e1b3f491f8e124067c8a5bdbcc3d41e
Author: stefan
Category: javax / web
Type: JavaX module (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-12 18:33:33
Source code size: 6837 bytes / 203 lines
Pitched / IR pitched: No / No
Views / Downloads: 952 / 3150
Version history: 104 change(s)
Referenced in: [show references]