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.