Monday, June 6, 2016

Gulp from command line with error handling


First step is installing the gulp.
Install gulp
Install gulp modules

There will be node-modules directory in your present working directory (pwd).

Create a gulpfile.js in your current working directory which would include the JS functions and gulp tasks.
------------------------------------
Sample gulpfile.js
------------------------------------
function handleError(err) {
    console.log("Error: ", err.toString());
    process.exit(-1);
}

var gulp = require('gulp');
var minifyCss = require('gulp-minify-css');

gulp.task('minify-css', function() {
  return gulp.src('./*.css')
    .pipe(minifyCss({compatibility: 'ie8'}))
    // handle error
    .on("error", handleError)
    .pipe(gulp.dest('CSS'));
});

var uglify = require('gulp-uglify');

gulp.task('minify-js', function() {
  return gulp.src('./*.js')
    .pipe(uglify())
    // handle error
    .on("error", handleError)
    .pipe(gulp.dest('JS'))
    ;
});



Open terminal in the directory where gulpfile.js is located.
Run the following command.
gulp <task-name>
Ex. gulp minify-js


Error handling

Error code returned from the gulp process will be handled in the shell script that invoked the gulp command.

On error, there will be an exit code of -1 from gulp handleError function which can be used to take corresponding action in the shell script.

----------------------------------------------
Sample shell script (powershell)
----------------------------------------------
gulp minify-js
if ($LASTEXITCODE -ne 0) {
    throw "Gulp Failed. Refer gulp log files."
    exit
}
echo ":) Gulp tasks finished successfully."

Export all the stored procedures to a file in SQL Server


Recently, I just wanted to get all the stored procedures, full text, on a file. This will give me power to do lot of things.
So, I came across of material. These are 3 approaches I followed.

Approach 1

Right Click on the database name -> Tasks -> Generate Scripts...



In the Choose Objects step,
Check the Stored Procedures.

Next step choose a destination file. Next. Finish.

Approach 2

This can be achieved by using a query as well, but be warned that there is a pitfall.
SQL Server truncates all the cell contents to 4000 characters. Tested on 2014.

    SELECT
        r.ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES r

You could use the columns ROUTINE_CATALOG, r.ROUTINE_SCHEMA, r.ROUTINE_NAME to get extra details.

Caution: SELECT query truncates cells to 4000 chars.
I have not found a direct and simple solution to this problem. (Please leave a comment if you have one).

Settings: You can access 
Tools -> Query Results -> Results to Grid 
to modify the settings. However, maximum length of Non XML data is 65535 only. Unable to go beyond that.

Approach 3

    SELECT DEFINITION FROM SYS.SQL_MODULES

Thursday, February 11, 2016

Super cool packages for Sublime Text

These are some of the Sublime Text packages I strongly recommend.

First grab on to wbond's Package Control for Sublime Text.
You can access it by "ctrl+shift+p", type "install package".
Then you can type any of the following plugins and install from within sublime text.

1. RegReplace: So many inbuilt regex search-replace ready to use via keybindings. Ex: You can use it to remove trailing whitespaces in your document.

2.  Chain of Commands: One command translates to a chain of multiple commands in sequence. Ex: To create a new copy of the file you can do something like
{ "keys": ["ctrl+shift+n"],
        "command": "chain",
        "args": {
            "commands": [
                ["select_all"],
                ["copy"],
                ["new_file"],
                ["paste"],
                ["save"]
            ]
        }
    }

3.SublimeLinter: Get instant warning/error messages as you make syntax errors while creating a program, say, Javascript program.
    a) SublimeLinter (from install package)
    b) SublimeLinter-jshint (from install package)
    c) Download Node.js msi (for windows) and npm (for linux)
    d) npm install -g jshint
    Restart the Sublime Text and you are good to go.

3.1: JSLint: JSLint is smart and unforgiving. It helps you write better programs in JavaScript by showing warning and errors. It shows the warnings and errors in console once you save your js file.

4. DocBlockr: Give the documentation comments for class, function etc once doc comments are added. @params, arguments are also detected automatically and filled in the docs.

5. PlainTasks: Task and checklist manager inside sublime text editor.

6. Gist: Install Gist (from install package).
https://github.com/settings/tokens
Preferences -> Package settings -> Gist -> Settings - User
{ "token": "<add your generated token>" }

7. ColorPicker: Pick color by pressing ctrl+shift+c.

8. Better Completion: Better auto-completion management.

9. Pretty JSON: Prettify/Minify/Query JSON plugin.

10. Dictionary AutoComplete: This adds dictionary entries to the completions inside comments. For lazy typers! Provides auto completion of dictionary words inside quotes (i.e. strings in programmers terminology).

11. FileDiffs: Shows diffs between the current file, or selection(s) in the current file, and clipboard, another file, or unsaved changes.

12. SubliMerge Pro: The professional, side-by-side tool to diff and merge files and directories right in Sublime Text. Supports Git, SVN and Mercurial.

13. Color Highlighter: This plugin underlays selected hexadecimal colorcodes (like "#FFFFFF", "rgb(255,255,255)", "white", etc.) with their real color. Also, plugin adds color picker to easily modify colors.
Use can also pick color by pressing ctrl+shift+c placing the cursor on the color code.

14. Can I Use: Checking CSS property support.

15. WordCount: Real-time Word, Char, Line and Page counter, in the status-bar for the document, line or selection.

16. Word Wrap Plus: Enhanced “wrap lines” command for Sublime Text 2 or 3.

Saturday, December 26, 2015

Hotspot script for ubuntu

This script was tested on both Ubuntu 12.04


#!/bin/sh
# 20141208
# hotspot access point creation for android mobiles in ubuntu 12.04.4; works for laptop to laptop connection as well

zenity --warning --text "Run this script from terminal. \n ./hotspot_install. \n Close dialog box/ESC if you are running via terminal. Else click ok/ENTER."
if [ $? -eq 0 ]
then
    exit
fi

echo "Is ap-hotspot already installed (y/n) ? "
read ch
if [ "$ch" = "n" ]
then
    sudo add-apt-repository ppa:nilarimogard/webupd8
    sudo apt-get update
    sudo apt-get install ap-hotspot
fi


echo "Is ap-hotspot already configured (y/n) ? "
read ch
if [ "$ch" = "n" ]
then
    echo "configure your hotspot. give an access point name and good password"
    sudo ap-hotspot configure
    echo "delete all hotspot connection. Disconnect connected hotspots. Then Network -> Wireless -> Forget/Remove all hotspot. Then press any key to continue..."
    read key
fi

sudo ap-hotspot start

echo "Are you able to obtain ip address in your android mobile (y/n) ? "
read ch
if [ "$ch" = "n" ]
then
    echo "configuring network manager"
    sudo sed -i 's:^dns=dnsmasq:#dns=dnsmasq:gi' /etc/NetworkManager/NetworkManager.conf
    echo "restarting network manager"
    sudo service network-manager restart
    echo "check if following command does not say nameserver 127.0.0.1"
    cat /etc/resolv.conf
    echo "restarting ap-hotspot"
    sudo ap-hotspot stop
    sudo ap-hotspot start
fi

Friday, December 25, 2015

Ethernet wired connection not detected in Ubuntu 14.04

I have been using only wireless connection for a long time now. Finally a day has come for me to use wired ethernet connection.

Unfortunately, it was not getting detected at all.
The first command that comes to mind for checking connection....

ifconfig
The command didn't show "eth0".

ifconfig - a
This time it showed "eth0", but HWaddr  is all 0's. No valid physical address.


Well, this was the reason the wired connection was not getting detected.

Solution is to edit the rc.local file (/etc/rc.local)

Add these two lines before the "exit 0" line.

ifconfig eth0 hw ether 00:22:68:8f:09:e0
service network-manager restart


Restart and wired connection back.

Weirdly, the connection that got connected is "ifupdown (eth0)" instead of "Wired connection 1". Anyway, I just wanted it to work and that's done.

Wednesday, December 16, 2015

Getting started with Swift on Ubuntu 14.04

Download the swift 2.2 or later from Swift.org offical site.
Extract and set path to the binaries.

export PATH=${PATH}:~/swift-2.2-ubuntu14.04/usr/bin

assuming extracted folder is in home directory (indicated by ~/ ).
Try executing

$ swift --version

If you get error like
libstdc++.so.6: version `GLIBCXX_3.4.18' not found
execute the following commands.
 $ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update
$ sudo apt-get install libstdc++6-4.7-dev



Finally, verify if the swift is working.

$ swift --version
Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Target: x86_64-unknown-linux-gnu



That's all. Enjoy programming in Swift.




Saturday, August 29, 2015

Installing JSHint on Ubuntu using npm


When I tried to install jshint on Ubuntu 12.04 using npm, I got errors. It simply didn't work.

If you are trying to use SublimeLinter-jshint for Sublime Text 3, it won't work unless you install 2 things:
1. node.js
2. jshint

Default version on repo for Ubuntu 12.04
$ /usr/bin/node --version
v0.6.12
$ npm --version
1.1.4

When you try
$ /usr/bin/npm install -g jshint
npm http GET https://registry.npmjs.org/jshint

npm ERR! Error: failed to fetch from registry: jshint
...
...
npm not ok


The important thing to note is you must have recent version to install it properly.

Solution

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository --yes ppa:chris-lea/node.js
$ sudo apt-get install nodejs
$ sudo apt-get update

Check the version to confirm once.
$ npm --version
1.4.28
$ node --version
v0.10.37

and finally...
$ sudo npm install -g jshint
/usr/bin/jshint -> /usr/lib/node_modules/jshint/bin/jshint

Success. It's now installed.

Now if you restart the Sublime text and start with any js file, you can see the jshint will hint you for warnings and errors.a