Generate `avsc` files from `avdl` with pure JS - javascript

I know that it's possible to transform avdl to series of avsc files using java tools provided by Apache.
But despite the website lists plenty of implementations on different languages too, including JS, it seems to be that there is no support for avdl -> avsc conversion in these.
What would be your recommendation how to perform this conversion in pure JavaScript? Does such library even exists or we're forced to go through pure Java implementation always?

Java seems to be the only language that they implemented the compilation from avdl to avsc. The easiest route is probably just to have the avro-tools.jar somewhere and then have your JS code call out to that in some sub process to compile the schemas.
The other option would be to re-implement the IDL compiler in JS. I wouldn't do that, but the Java implementation is pretty much all contained within https://github.com/apache/avro/blob/master/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj if you wanted to take a look.

Related

Is it possible to create library for Java & JavaScript/TypeScript

Im angular developer.
In our front & back exists some magic calculation methods.
Classes same, but when anyone find bug in calculation need to fix it in two different projects.
maybe there is a way to create a generic codebase (maby function) that can be converted to js(or ts) & java and update two libraries based on the two results obtained
You could try to use kotlin.
Kotlin transpiles to JavaScript and also compiles to java bytecode.
However, you can only access kotlin utilities and neither access java or JS/TS types if you want to use the code in both java and ts/js code but you can use the kotlin stdlib.
But if it really is just a calculation, you may not need java/js specific classes/functions.
However, as VLAZ mentioned in the comments, you should consider doing the calculation only once in the backend.
Setting this up in IntelliJ
You can create such a project in IntelliJ by sekecting Kotlin in the New Project Window and using the project template Library.
Make sure you have the targets common, jvm and js. Since you didn't say you would do native stuff, you don't need the native target.
You can then use the kotlin library in both JavaScript (e.g. Angular) and Java projects as a dependency.
From a Java project, you can reference KOTLIN_PROJECT/build/classes/kotlin/jvm/main (this directory contains compiled Java classes).
From a JavaScript (e.g. Angular) project, you can reference KOTLIN_PROJECT/build/js/packages/kotlinToJavaAndJS.
As a proof of concept, I have made this repository on GitHub.

Creating a Scala.js Library - linking the Scala to the Javascript

I'm trying to write a new library that would work in Scala.js. I have written some of the implementations of the classes and methods in Javascript. How do I set it up so that a user can code in Scala.js (Scala)?
I've looked at some Scala.js libraries on GitHub, but these do not show the Javascript code; they all appear to be .scala files.
So how does one actually create a new library for Scala.js?
Edit: The main code for the library must be written in Javascript since it takes advantage of the Javascript audio api.
Somewhat agreeing with the comment from #sjrd, it should be possible to write your JavaScript-targeted API directly within Scala.js (See Calling JavaScript from Scala.js ).
Alternatively, there may also be the possibility for 'importing' or converting your existing JavaScript code into Scala.js '.scala' files in a strongly-typed-Scala manner, so:
Convert your JavaScript file to TypeScript, start by changing the file extension from '.js' to '.ts'.
Process your TypeScript file(s)
created in #1 into valid Scala[JS] files using #sjrd's (!) TypeScript-to-ScalaJS
importer # Github.
Finally, develop your code against Scala.js, using the '.scala'
files generated in #2
As TypeScript is a strongly-typed superset of JavaScript, changing the extension alone might be enough - otherwise, after running and failing step #2, you might need to refine you TypeScript'd library, this post, 'How to compile plain *.js (JavaScript) files with the TypeScript Compiler', should help with that.
The 'DefinitelyTyped' code repository # Github contains a collection of JavaScript libraries updated to TypeScript, the webaudio API is one of them (so this could be converted and used within Scala.JS using some of the process outlined above).
I haven't [yet] personally tested this myself, in anger, I'd be interested in whether you get any mileage out of this tool set/process.
Here are some extra TypeScript resources:
http://en.wikipedia.org/wiki/TypeScript
http://www.sitepen.com/blog/2013/12/31/definitive-guide-to-typescript

CoffeeScript-like language written in Python

Are there any languages targeting JavaScript (like CoffeeScript) and written in Python? I found Pyjamas, but it’s GWT of Python as I see. I want a language that doesn’t need heavy runtime library and is able to be compiled to JavaScript. I found Mascara also, and it very satisfies my requirements except it’s license. CoffeeScript is ideal for me except it’s written in CoffeeScript itself. I have to compile [CoffeeScript-like language] source codes into JavaScript statically in Python application.
You might want to have a look at pyjaco (python to javascript compiler).
Here's an example to get you started with manipulating the DOM in Python using jQuery:
https://github.com/chrivers/pyjaco/tree/devel/examples/jquery
Check this:
PyvaScript: http://www.allbuttonspressed.com/blog/django/2010/07/PyvaScript-Pythonic-syntax-for-your-browser
Pyjs: https://github.com/anandology/pyjs
Pyjamas: http://pyjs.org/
One part of Pyjamas is pyjs, which is decribed this way in the project overview:
pyjs translates Python code to Javascript by walking the Python abstract syntax tree and generating Javascript.
Sounds like it should fit the bill: no need to use the other parts of pyjamas you don't need.

How to use coffeescript in developing web-sites?

How do you use CoffeeScript? It need to be compiled, so - you write code in CoffeScript, compile it, and insert real JavaScript on your site?
Doesn't it take a lot of time? Or is there some another way?
P.S. I've seen another way - to insert in development stage coffeescript in text/coffeescript script-tags with coffeescript.js library (about 150k), and compile only for production version and insert real Javascript.
The answer is yes, you compile it and include the generated JavaScript on your side.
If you're using a web framework (rails, django etc) you should take a look at the following list of coffeescript plugins: https://github.com/jashkenas/coffee-script/wiki/Web-framework-plugins. They will compile your coffeescript to javascript when you deploy your app to a server.
Using the coffee-script plugin with the text/coffeescript tags is another option, gzipped and compressed, its only about 39kB, but that can add up if you include it on pages that get many hits and I don't think is a good idea when you can compile the coffeescript to javascript yourself without needing the plugin.
There is the middleman plugin which will lets you work with CoffeeScript during development, then compile and minify it for deployment.
When you are first learning coffeescript, you will almost certainly want to do your compilation manually during development, because you will probably find, as most of us do, that you need to be able to look at the javascript code in order to debug and find out what's going on. The more fluent and comfortable you become with coffescript, the less often you will need to refer to the javascript code.
I think this will probably be true no matter what your level of expertise in javascript. If you are a javascript pro, you will be dependent on looking at stuff you are familiar with, until you start to figure out how coffeescript works. On the other hand, if you are completely or partially unfamiliar with all the quirks and subtleties of javascript, such as prototypes, the javascript approach to scope and globals, and all the rest, you'll find yourself digging into javascript references, and cross-referencing the javascript code with your coffescript code, until you get familiar with it.

How should I start a new JavaScript project (Testing, Developing, Building)?

I've developing JavaScript since many years but have never really thought about the whole testing, developing and building stuff - but I realized it's pretty much necessary. We've just used a Subversion repository with simple release tagging (switching to git soon). As a new bigger pure JavaScript project (using jQuery) is arriving soon, I'd like to optimize the whole process.
I already did some research but never found a good starting tutorial.
It's definetly a good idea to split classes and separate code blocks into several js-files and not a big one (as Prototype or jQuery do it). These js-files must be "build" into a single file. How do I achieve that?
It's pretty much necessary to Unit-test the stuff me and my colleagues are coding. I found the js-test-driver which has an eclipse plugin that seems to be doing his job quite good. If my developer-folder contains all these src- and src-test-files, how do I integrate this in the building process?
For testing, take a look at this: https://stackoverflow.com/questions/32809/javascript-unit-testing
For merging all of your JavaScript into one file you can use something like YUI Compressor. You need to be looking for a minimizer first, compression second. A minimizer just takes the files and merges them together and gets rid of whitespace. A compressor will actually try to optimize the js for you by changing variable names and removing unnecessary code.
As for unit testing I am unsure of how you will want to do that. There are a few unit test libraries out there. A popular tool for testing is Selenium. I don't currently do unit testing so I am out of my element there..
For setting up your code you could always look at using a JavaScript framework like ExtJS or JavaScriptMVC. Those help you with setting up your code in the proper way and also helps focus your team on the proper standards and coding structure while also writing a lot of the code for you so you don't have to re-invent the wheel.
EDIT: Just a quick after thought. Even if you don't want to use a JavaScript framework, I would suggest checking them out, especially ExtJS, just to see how they organize their code and some of the tricks they do to keep it clean.
I'll answer part of your question:
These js-files must be "build" into a
single file.
This is possible only with server side language - in ASP.NET you have built in tools for that, otherwise build your own "merger" server side file and reference that file instead of the actual .js files.
These js-files must be "build" into a single file. How do I achieve that?
Definitely keep your files separate in version control, and only merge them during the build process.
The YUI compressor mentioned elsewhere is a java-based tool that will not only merge but -- of course! -- compress your files for faster download.
If you just want a simple merge of files, a simple Perl or bash-script (or other preferred scripting language) could concatenate multiple .js files into one for release -- just make sure that the build script also updates all HTML in the release to reference only the single page.

Categories