Replacing javascript nashorn:mozilla_compat.js use in ant script - javascript

Oracle has depreciated nashorn and I use it in my apache ant build scripts.
Here is a short example;
try{load("nashorn:mozilla_compat.js");}catch(e){;}
importClass(java.io.File);
var sourceName = project.getProperty("build.source.dir") + "/" +project.getProperty("teiFile") + ".xml";
var targetName = project.getProperty("build.search.dir") + "/" + project.getProperty("teiFile") + ".xml";
var sourceFile = new File("", sourceName);
var targetFile = new File("", targetName);
var uptodate = targetFile.exists() && sourceFile.lastModified() < targetFile.lastModified();
var sourcePrefix = project.getProperty("teiFile");
if(!uptodate & !sourcePrefix.startsWith("G")) {
......
}
}
I've heard mentions of Rhino or graalVM as a replacement, but each seems to have a fairly long list of deficiencies.Suggestions for a library that is currently active and stable.
thanks, scott

GraalVM is currently active, stable, has a Nashorn compatibility mode.
You can run your ant scripts with GraalVM, and the JS engine is included by default. Or you can include GraalVM's JavaScript as a few dependencies and run on OpenJDK, which will be slower, and might be an untested combination of OpenJDK and GraalVM's JavaScript, but can also work.

Related

Java and Javascript - Lastmodified on Linux

I wrote a script on the Jmeter Web Driver in javascript and Java .
On a Windows system , the script is running perfectly . But on a Linux System , i have a weird problem . The lastmodifed then i obtain is not good. front_end.jtl has been modified yesterday , but the script say today at 10:00 in milliseconds timestamp .
Please help me.
// Importing packages (and all classes in package) from Java into Javascript var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
// We don't use wait in this very simple test, but here is way to access for more realistic testing
var wait=new support_ui.WebDriverWait(WDS.browser, 5)
// Start recording the time for this request
WDS.sampleResult.sampleStart();
// Let's get a page
var baseUrl = "cnyw${FE}.mycore.core-cloud.net"
WDS.browser.get("https://"+ baseUrl)
var frontjtl = new java.io.File('/home/mycore/front_end.jtl')
var frontlog = new java.io.File('/home/mycore/jmeter_front_end.log')
var lastmodifjtl = frontjtl.lastModified()
var lastmodiflog = frontlog.lastModified()
if ( lastmodifjtl = lastmodiflog ) {
var screenshot = WDS.browser.getScreenshotAs(pkg.OutputType.FILE)
screenshot.renameTo(new java.io.File('/home/mycore/screenshots/tools/screenshot_cnyw${FE}.png'))
}
// Record the time of the request
WDS.sampleResult.sampleEnd();
I would recommend using Files.getLastModifiedTime() instead, something like:
var lastmodifjtl = java.nio.file.Files.getLastModifiedTime(java.nio.file.Paths.get(frontjtl.toURI()))
should do the trick for you.
Just in case see The WebDriver Sampler: Your Top 10 Questions Answered article for more information
I found an explanation : https://bugs.openjdk.java.net/browse/JDK-8177809
It's a confirmed bug ...
i have rewrite the script in Java directly. All is okay now.

Ionic 3 Prod Build With Version Number

I use the following command when building an ionic project for desktop
ionic cordova build browser --prod
Which results in the following file being generated
build/main.js
However I would like to be able to add a version number to the generated file automatically as part of the build process. So would end up with something like
build/main.js?version=1.00
as to avoid needing to clear the browser cache after every prod build.
Is there a flag for this, or is it something I must do manually?
Any advice would be great!
EDIT:
My solution is on GitHub for anyone interested!
https://github.com/RichardM99/ionic-3-version-build-file-hook
Here's some advice - You can create a cordova hook.
Hooks are scripts that you want to be executed at different stages of the build process. In your case, you are looking at a script which renames the main.js file after the build event is finished, or in other words a 'after_build' type hook.
The script will usually be a Node.js file, although you can have other types of scripts executed as well.
One more thing. Since you want to get around cache, you wont be renaming the file itself. What you will want to do is rather replace the reference to "main.js" in you "index.html" to include a random or maybe your actual version number.
I have pointed you in a direction, but won't spoonfeed. Look up documentation on cordova hooks. They are super simple if you understand Javascript/Node
Something like this should get the job done:
var index_orig = fs.readFileSync(path-to-index.html, 'utf8');
var index_new = index_orig.replace("main.js", "main.js?version="+version_num);
fs.writeFileSync(path-to-index.html, index_new, 'utf8');
If you want the actual build number, you can read your config.xml and parse it to get it's value.
Hope it helps.
I wrote blog long time ago
In my build pipeline i have command to set version
version "$(app.versionPrefix)$(Build.BuildNumber)"
$(app.versionPrefix) - is a prefix version such as 0.1.
$(Build.BuildNumber) - is build version
Then I have environment file
export const environment = {
apiUrl: 'https://....',
production: true,
version: '0.0.57'
}
Then i have js script to update version in environment and config.xml
var replace = require('replace-in-file');
var package = require("./package.json");
var buildVersion = package.version;
const options = {
files: ['config.xml'],
from: /" version="([0-9]*.[0-9]*.[0-9]*)"/g,
to: "\" version=\""+ buildVersion + "\"",
allowEmptyPaths: false,
};
const optionsEnv = {
files: ['src/environments/environment.prod.ts'],
from: /version: '(.*)'/g,
to: "version: '"+ buildVersion + "' ",
allowEmptyPaths: false,
};
try {
let changedFiles = replace.sync(options);
if (changedFiles == 0) {
throw "Please make sure that file '" + options.files + "' has \"version: ''\"";
}
changedFiles = replace.sync(optionsEnv);
if (changedFiles == 0) {
throw "Please make sure that file '" + optionsEnv.files + "' has \"version: ''\"";
}
console.log('Build version set: "' + options.to + '"');
}
catch (error) {
console.error('Error occurred:', error);
throw error
}
NOTE: you need to install plugin replace-in-file
Then in build pipe line I am running this script
node ./replace.build.js
In your case if you need only for browser you can tune script.

MarkLogic JavaScript scheduled task

I try to schedule a script using the 'Scheduled Tasks' in ML8. The documentation explains this a bit but only for xQuery.
Now I have a JavaScript file I'd like to schedule.
The error in the log file:
2015-06-23 19:11:00.416 Notice: TaskServer: XDMP-NOEXECUTE: Document is not of executable mimetype. URI: /scheduled/cleanData.js
2015-06-23 19:11:00.416 Notice: TaskServer: in /scheduled/cleanData.js [1.0-ml]
My script:
/* Scheduled script to delete old data */
var now = new Date();
var yearBack = now.setDate(now.getDate() - 65);
var date = new Date(yearBack);
var b = cts.jsonPropertyRangeQuery("Dtm", "<", date);
var c = fn.subsequence(cts.uris("", [], b), 1, 10);
while (true) {
var uri = c.next();
if (uri.done == true){
break;
}
xdmp.log(uri.value, "info"); // log for testing
}
Try the *.sjs extension (Server-side JavaScript).
The *.js extension can be used for static JavaScript resources to return to the client instead of executed on the server.
Hoping that helps,
I believe that ehennum found the issue for you (the extension - which is what the mime-type error is complaining about.
However, on the same subject, not all items in ML work quite as you would expect for Serverside Javascript. For example, using sjs as a target of a trigger is (or recently) did not work. So for things like that, it is also possible to wrap the sjs call inside of xqy using xdmp-invoke.

Create ActiveXObject from GUID

I have the following line in my code:
var ScreenRecorder = new ActiveXObject('CCScreenRecorder.ScreenRecorder');
The problem is that I have 2 entries of this
(one for each version, don't ask me why - this is a fact I need to deal with),
So I want to create the ActiveXObject from the GUID.
I tried to do:
document.createElement('<OBJ' + 'ECT ID="ScreenRecorderWrapper" CLA' + 'SSID="CL' + 'SID:37CCF998-3BB7-' + '4F8A-9D9F-EF391543E94A"></OB' + 'JECT>');
var ScreenRecorder = ScreenRecorderWrapper;
but the problem is that ScreenRecorderWrapper will be defined only after SetTimeout or some other manipulation.
Can I get the ActiveXObject from it's GUID and not from it's name?
Something like:
var ScreenRecorder = new ActiveXObject('37CCF668-3BB7-4F8A-9D9F-EF391543E94A');
ActiveXObject requires a ProgID, however, you can use a version-dependent ProgID rather than a version-independent ProgID.
Version-dependent ProgIDs typically have a version number appended to the version-independent ProgID.
In this case, the version-independent ProgID is 'CCScreenRecorder.ScreenRecorder', and the version-dependent ProgID would be something like 'CCScreenRecorder.ScreenRecorder.1' or 'CCScreenRecorder.ScreenRecorder.2'.
Note that using a version-dependent ProgID can fail if the wrong version is installed. You might want to try the version-dependent ProgID first, and fall back to the version-independent ProgID.
Use the registry for obtaining the ProgId from the CLSID.
Reading the registry in JS is possible through the "Shell" ActiveX
var shellObj = new ActiveXObject("WScript.Shell");
var clsid = "37CCF668-3BB7-4F8A-9D9F-EF391543E94A";
var progid = shellObj.RegRead("HKEY_CLASSES_ROOT\\CLSID\\{"+clsid+"}\\ProgID\\");
var ScreenRecorder = new ActiveXObject( progid );

WIX: Where and how should my CustomAction create and read a temporary file?

I have a script CustomAction (Yes, I know all about the opinions that say don't use script CustomActions. I have a different opinion.)
I'd like to run a command, and capture the output. I can do this using the WScript.Shell COM object, then invoking shell.Exec(). But, this flashes a visible console window for the executed command.
To avoid that, I understand I can use the shell.Run() call, and specify "hidden" for the window appearance. But .Run() doesn't give me access to the StdOut of the executed process, so that means I'd need to create a temporary file and redirect the exe output to the temp file, then later read that temp file in script.
Some questions:
is this gonna work?
How do I generate a name for the temporary file? In .NET I could use a static method in the System.IO namespace, but I am using script here. I need to insure that the use has RW access, and also that no anti-virus program is going to puke on this.
Better ideas? I am trying very hard to avoid C/C++.
I could avoid all this if there were a way to query websites in IIS7 from script, without resorting to the IIS6 Compatibility pack, without using .NET (Microsoft.Web.Administration.ServerManager), and without execing a process (appcmd list sites).
I already asked a separate question on that topic; any suggestions on that would also be appreciated.
Answering my own question...
yes, this is going to work.
Use the Scripting.FileSystemObject thing within Javascript. There's a GetTempName() method that produces a file name suitable for temporary use, and a GetSpecialFolder() method that gets the location of the temp folder. There's even a BuildPath() method to combine them.
so far I don't have any better ideas.
Here's the code I used:
function GetWebSites_IIS7_B()
{
var ParseOneLine = function(oneLine) {
...regex parsing of output...
};
LogMessage("GetWebSites_IIS7_B() ENTER");
var shell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var tmpdir = fso.GetSpecialFolder(SpecialFolders.TemporaryFolder);
var tmpFileName = fso.BuildPath(tmpdir, fso.GetTempName());
var windir = fso.GetSpecialFolder(SpecialFolders.WindowsFolder);
var appcmd = fso.BuildPath(windir,"system32\\inetsrv\\appcmd.exe") + " list sites";
// use cmd.exe to redirect the output
var rc = shell.Run("%comspec% /c " + appcmd + "> " + tmpFileName, WindowStyle.Hidden, true);
// WindowStyle.Hidden == 0
var ts = fso.OpenTextFile(tmpFileName, OpenMode.ForReading);
var sites = [];
// Read from the file and parse the results.
while (!ts.AtEndOfStream) {
var oneLine = ts.ReadLine();
var line = ParseOneLine(oneLine);
LogMessage(" site: " + line.name);
sites.push(line);
}
ts.Close();
fso.DeleteFile(tmpFileName);
return sites;
}

Categories

Resources