COOKIES! This blog uses cookies!
I am completely out of control of cookies here, otherwise I would have disabled them (it is controlled by the platform).
If you don't like cookies and being tracked please leave this blog immediately.

Friday, 7 April 2017

Windows Creators Update and compatibility of Linux subsystem with Windows binaries

Windows 10 “Creators Update” is finally available to install outside from “insiders programme”, it will soon land on most of windows 10 machine, and those who impatient can install it directly from the website: https://www.microsoft.com/en-us/software-download/windows10

Most valuable thing in this update for me is update of linux subsystem to the Ubuntu 16 and compatibility of this subsystem with Windows binaries, so you now can call pretty much any windows binary from the windows bash. (it is available in build Builds above 14951)


Unfortunately the update of subsystem is not 100% smooth, the subsystem has to be installed and re-installed back (hopefully will not be required soon).


  • (option for impatient) download “Update Now” from the https://www.microsoft.com/en-us/software-download/windows10 and install the update, the system will restart a few times.
  • After update is installed open “bash” and do “lsb_release -a” to check that you’re still on Ubuntu 14;
  • Backup any files and config from the subsystem, for example copy your ~/.bashrc to outside of the subsystem with `cp ~/.bashrc /mnt/c/temp/`
  • Exit the bash and go to windows cmd
  • Uninstall subsystem with `lxrun /uninstall`
  • Re-install linux subsystem with `lxrun /install`
  • Go to the `bash`
  • Ensure that it is now Ubuntu 16 with `lsb_release -a`
  • Try running Windows binary from the bash for example `/mnt/c/Windows/System32/ipconfig.exe /all`

This ability to seamlessly run windows binaries gives many useful opportunities, for example controlling VirtualBox virtual machines from vagrant running inside of linux subsystem.

Wednesday, 15 February 2017

Spring's Autowired things to be available in constructor of JPA entity


Entities are normally not eligible for Spring-driven configuration, however it is possible to make them eligible by adding `@org.springframework.beans.factory.annotation.Configurable` annotation.

Unfortunately autowired things are not available in constructor, because they are injected after the object is constructed. Luckely `Configurable` has very useful `boolean preConstruction() default false` option to make autowired fields available in constructor. Here's the example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationEventPublisher;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Configurable(preConstruction = true)
public class MyEntity {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    private String field;

    @Autowired
    private transient ApplicationEventPublisher publisher;

    public MyEntity() {
        System.out.println("do something...");
        // publisher will be null at this point
        // if preConstruction = true is not used
        publisher.publishEvent("Publish something");
        /* ... more code ... */
    }

    /* ... your code here ... */
}

Thursday, 6 October 2016

Constantly ignore file changes in git without changing .gitignore

(original from "eckes" on stackoverflow: http://stackoverflow.com/questions/10879783/git-doesnt-ignore-2-specifically-named-files?answertab=active#tab-top)
You'll need to use git update-index:
git update-index --assume-unchanged build/conf/a.conf
git update-index --assume-unchanged build/conf/b.conf
will achieve what you want: the files are always assumed unchanged.
If you want to track changes in these files again, use --no-assume-unchanged.
Finally, if you want to know which files are currently in the --assume-unchanged mode, ask git for
git ls-files -v | grep -e "^[hsmrck]"

Friday, 29 July 2016

Take a screenshot of whole app in the Electron

First you need to enable usermedia-screen-capturing in your Chromium Electron,
add the following string into your main.js:
app.commandLine.appendSwitch('enable-usermedia-screen-capturing');

After that you can use the following function to take a PNG blob
/**
 * A simplified function which takes a screenshot with webkitGetUserMedia
 * and returns this screenshot as a PNG blob into the callback
 * @param callback (pngData: Blob) => void
 * @returns void
 */
function takeScreenShot (callback) {
    let screenConstraints = {
        mandatory: {
            chromeMediaSource: "screen",
            maxHeight: 1080,
            maxWidth: 1920,
            minAspectRatio: 1.77
        },
        optional: []
    };

    let session = {
        audio: false,
        video: screenConstraints
    };

    let streaming = false;
    let canvas = document.createElement("canvas");
    let video = document.createElement("video");
    document.body.appendChild(canvas);
    document.body.appendChild(video);
    let width = screen.width;
    let height = 0;

    video.addEventListener("canplay", function(){
        if (!streaming) {
            height = video.videoHeight / (video.videoWidth / width);

            if (isNaN(height)) {
                height = width / (4 / 3);
            }

            video.setAttribute("width", width.toString());
            video.setAttribute("height", height.toString());
            canvas.setAttribute("width", width.toString());
            canvas.setAttribute("height", height.toString());
            streaming = true;

            let context = canvas.getContext("2d");
            if (width && height) {
                canvas.width = width;
                canvas.height = height;
                context.drawImage(video, 0, 0, width, height);

                canvas.toBlob(function (data) {
                    video.pause();
                    video.src = "";
                    document.body.removeChild(video);
                    document.body.removeChild(canvas);
                    callback(data); // here the png blob returned to the callback
                });
            }
        }
    }, false);

    navigator.webkitGetUserMedia(session, function (stream) {
        video.src = window.webkitURL.createObjectURL(stream);
        video.play();
    }, function () {
        console.error("Can't take a screenshot");
    });
}

Wednesday, 6 July 2016

Electron and ReactJS performance hint

If process.env.NODE_ENV is not set to 'production' react will do some performance consuming debug stuff. Set process.env.NODE_ENV to 'production' and it will bump the app performance.

Monday, 30 May 2016

Produce html diff with diff2html-cli

It is handy to do with one nice JavaScript tool, which works on any platform:
https://www.npmjs.com/package/diff2html-cli

> npm install diff2html-cli
> diff -u fileA.txt fileB.txt | diff2html -F diff.html -i stdin


It is important to have -u option for diff command you pipe into diff2html, because it produces unified diff and diff2html expects unified diff.

It is also possible to do these two commands separatedly, without piping:

Write diff of a and b into a-b.diff file:
> diff -u a.txt b.txt > a-b.diff

Produce HTML file from the diff file:
> diff2html -F a-b.html -i file -- a-b.diff

Tuesday, 26 April 2016

Call Rust from NodeJS via cross-platform C ABI with RuNo bridge

The RuNo bridge is a command line tool which generates C++ code for NodeJS addon from Rust code or from JSON definition (with JSON definition it should work with any C ABI compatible library, of course when implemented functionality is enough).

I've implemeted this tool after my last research on calling Rust from Node JS.

The parser of RuNo bridge does not do magical deep analysis of code, it just detects the following signatures in your code:

#[no_mangle]

pub extern "C" fn ...


it does not require any C++ knowledge from developer if you use primitives mentioned above and your Rust ABI interface complies with simple requirements:

  • All your ABI functoins should be listed in one Rust file;
  • Your library should use crate libc;
  • Each ABI function should be preceeded with #[no_mangle];
  • Each ABI function should be prefixed with pub extern "C";
  • ABI Functions should only take params of c_int,c_float,c_double or *c_char (as a C string with EOF);
  • ABI Functions should return either one of c_int,c_float,c_double or *c_char (as a C string with EOF)
It is tested on Windows, Mac OS and Ubuntu, however it has some limitations developer should know:


The package itself does not need Rust or C++ with node-gyp, it just emits a C++ source file.

However in order to build the source code, rust and C++ compiler should be compatible with NodeJS version installed. It is particularly important on Windows, where Rust target should be MSVC not GNU. For example, if one using 32 bit NodeJS on Windows this one should use target i686-pc-windows-msvc, if 64 bit Node then Rust should be configured with x86_64-pc-windows-msvc compile target. The same about C++: Everything is mostrly smooth on platforms with GCC, and a bit painful with MS Visual C++, please refer to node-gyp installation instructions for details.

You can find simple usage examples on the github: https://github.com/andruhon/runo-bridge-example

I will appreciate any comments or contribution.