24 Oct 2017

Configuring Database Mail Using T-SQL Script

Configuring Database Mail Using T-SQL Script


--Enabling Database Mail
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'Database Mail XPs',1
reconfigure
go


--Creating a Profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'SQLMailProfile',
@description = 'Mail Service for SQL Server' ;
go


-- Create a Mail account for gmail. We have to use our company mail account.
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'SQL_Email_Account',
@email_address = 'computertrik@gmail.com',
@mailserver_name = 'smtp.gmail.com',
@port=587,
@enable_ssl=1,
@username='computertrik@gmail.com',
@password='Emailid password'
go


-- Adding the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'SQLMailProfile',
@account_name = 'SQL_Email_Account',
@sequence_number =1 ;
go


-- Granting access to the profile to the DatabaseMailUserRole of MSDB
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = 'SQLMailProfile',
@principal_id = 0,
@is_default = 1 ;
go

--Sending Test Mail
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLMailProfile',
@recipients = 'youremailid@xxxx.com',
@body = 'Database Mail Testing...',
@subject = 'Databas Mail from SQL Server';
go

--Verifying, check status column
select * from sysmail_allitems
go

Summary 
Database Mail feature was introduced in SQL Server 2005 version, which can be used to notify the administrators/operators. It provides better performance as well as cluster aware feature.     

20 Oct 2017

Difference between Abstraction and Encapsulation

Difference between Abstraction and Encapsulation :-



AbstractionEncapsulation
Abstraction solves the problem in the design level.Encapsulation solves the problem in the implementation level.
Abstraction is used for hiding the unwanted data and giving relevant data.Encapsulation means hiding the code and data into a single unit to protect the data from outside world.
Abstraction lets you focus on what the object does instead of how it does it.Encapsulation means hiding the internal details or mechanics of how an object does something.
Abstraction- Outer layout, used in terms of design. For Example:- Outer Look of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.Encapsulation- Inner layout, used in terms of implementation. For Example:- Inner Implementation detail of a Mobile Phone, how keypad button and Display Screen are connect with each other using circuits.



The easier way to understand Abstraction and encapsulation is as follows:-

Real World Example:- 

Take an example of Mobile Phone:- 

You have a Mobile Phone, you can dial a number using keypad buttons. Even you don't know how these are working internally. This is called Abstraction. You have the only information that is needed to dial a number. But not its internal working of mobile.

But how the Mobile Phone internally working?, how keypad buttons are connected with internal circuit? is called Encapsulation.

Summary:

"Encapsulation is accomplished by using Class. - Keeping data and methods that accesses that data into a single unit"
"Abstraction is accomplished by using Interface. - Just giving the abstract information about what it can do without specifying the back ground details"
"Information/Data hiding is accomplished by using Modifiers - By keeping the instance variables private or protected."

Polymorphism - OOPS Programming

Polymorphism: 

Polymorphism means one name many forms. One function behaves different forms. In other words, "Many forms of a single object is called Polymorphism."

Real World Example of Polymorphism:
Example-1: 

A Teacher behaves to student.
A Teacher behaves to his/her seniors.
Here teacher is an object but attitude is different in different situation.

Example-2: 

Person behaves SON in house at the same time that person behaves EMPLOYEE in office.

Example-3: 

Your mobile phone, one name but many forms
As phone
As camera
As mp3 player
As radio

Inheritance - OOPS Programming

Inheritance: 

When a class acquire the property of another class is known as inheritance. Inheritance is process of object re-usability. For example, A Child acquire property of Parents.

public class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("Parent Constructor.");
        }
        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }
    public class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Constructor.");
        }
        public static void Main()
        {
            ChildClass child = new ChildClass();
            child.print();
        }
    }



Output:
    Parent Constructor.
    Child Constructor.
    I'm a Parent Class.

Encapsulation - OOPS Programming

Encapsulation: 

Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    class Bag
    {
        book;
        pen;
        ReadBook();
    }


Encapsulation means hiding the internal details of an object, i.e. how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from the other object.

Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.
So, when you access the property you can validate the data and set it.

Example:

class Demo
{
   private int _mark;
   public int Mark
   {
     get { return _mark; }
     set { if (_mark > 0) _mark = value; else _mark = 0; }
   }
 }

Real world Example of Encapsulation:-

Let's take example of Mobile Phone and Mobile Phone Manufacturer
Suppose you are a Mobile Phone Manufacturer and you designed and developed a Mobile Phone design(class), now by using machinery you are manufacturing a Mobile Phone(object) for selling, when you sell your Mobile Phone the user only learn how to use the Mobile Phone but not that how this Mobile Phone works.

This means that you are creating the class with function and by making object (capsule) of it you are making availability of the functionality of you class by that object and without the interference in the original class.

Example-2: 

TV operation
It is encapsulated with cover and we can operate with remote and no need to open TV and change the channel.
Here everything is in private except remote so that anyone can access not to operate and change the things in TV.

Abstraction - OOPS Programming

Abstraction:

Abstraction is "To represent the essential feature without representing the back ground details." Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or object by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.

Real world Example of Abstraction: - 

Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as following:-

Nokia 1400 (Features:- Calling, SMS)
Nokia 2700 (Features:- Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:-Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (Necessary and Common Information) for the object "Mobile Phone" is make a call to any number and can send SMS."

so that, for mobile phone object you will have abstract class like following:-

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }
    public class Nokia1400 : MobilePhone
    {
    }
    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }
    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();
    }

Abstraction means putting all the variables and methods in a class which are necessary.
For example: - Abstract class and abstract method.
Abstraction is the common thing.

Example: 

If somebody in your collage tell you to fill application form, you will fill your details like name, address, data of birth, which semester, percentage you have got etc.
If some doctor gives you an application to fill the details, you will fill the details like name, address, date of birth, blood group, height and weight.
See in the above example what is the common thing?
Age, name, address so you can create the class which consist of common thing that is called abstract class.
That class is not complete and it can inherit by other class.

OOPS Features

OOPS Features


The object oriented programming (OOP) is a programming model where Programs are organized around object and data rather than action and logic. OOP allow decomposition of a problem into a number of entities called Object and then builds data and function around these objects.

The Program is divided into number of small units called Object. The data and function are build around these objects.

  • The data of the objects can be accessed only by the functions associated with that object.
  • The functions of one object can access the functions of other object.


OOP has the following important features.

 Class:

A class is the core of any modern Object Oriented Programming language such as C#.
In OOP languages it is must to create a class for representing data.
Class is a blueprint of an object that contains variables for storing data and functions to performing operations on these data.
Class will not occupy any memory space and hence it is only logical representation of data.
To create a class, you simply use the keyword "class" followed by the class name:

class Employee
{
}

Object: 


Objects are the basic run-time entities in an object oriented system.They may represent a person,a place or any item that the program has to handle.


  • "Object is a Software bundle of related variable and methods."
  • “Object is an instance of a class”




Class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class, which is called as an object.
When an object is created by using the keyword new, then memory will be allocated for the class in heap memory area, which is called as an instance and its starting address will be stored in the object in stack memory area.
 When an object is created without the keyword new, then memory will not be allocated in heap I.e. instance will not be created and object in the stack contains the value null.
When an object contains null, then it is not possible to access the members of the class using that object.

class Employee
{
}

Syntax to create an object of class Employee:-

Employee objEmp = new Employee();

All the programming languages supporting Object Oriented Programming will be supporting these three main concepts:


  1. Encapsulation
  2. Inheritance
  3. Polymorphism

16 Oct 2017

OOPS : Abstraction, Encapsulation, Inheritance, Polymorphism

OOPS : Abstraction, Encapsulation, Inheritance, Polymorphism



OOPS Features


The object oriented programming (OOP) is a programming model where Programs are organized around object and data rather than action and logic. OOP allow decomposition of a problem into a number of entities called Object and then builds data and function around these objects.

The Program is divided into number of small units called Object. The data and function are build around these objects.

  • The data of the objects can be accessed only by the functions associated with that object.
  • The functions of one object can access the functions of other object.


OOP has the following important features.

 Class:

A class is the core of any modern Object Oriented Programming language such as C#.
In OOP languages it is must to create a class for representing data.
Class is a blueprint of an object that contains variables for storing data and functions to performing operations on these data.
Class will not occupy any memory space and hence it is only logical representation of data.
To create a class, you simply use the keyword "class" followed by the class name:

class Employee
{
}

Object: 


Objects are the basic run-time entities in an object oriented system.They may represent a person,a place or any item that the program has to handle.


  • "Object is a Software bundle of related variable and methods."
  • “Object is an instance of a class”




Class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class, which is called as an object.
When an object is created by using the keyword new, then memory will be allocated for the class in heap memory area, which is called as an instance and its starting address will be stored in the object in stack memory area.
 When an object is created without the keyword new, then memory will not be allocated in heap I.e. instance will not be created and object in the stack contains the value null.
When an object contains null, then it is not possible to access the members of the class using that object.

class Employee
{
}

Syntax to create an object of class Employee:-

Employee objEmp = new Employee();

All the programming languages supporting Object Oriented Programming will be supporting these three main concepts:


  1. Encapsulation
  2. Inheritance
  3. Polymorphism


Abstraction:

Abstraction is "To represent the essential feature without representing the back ground details." Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or object by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.

Real world Example of Abstraction: - 

Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as following:-

Nokia 1400 (Features:- Calling, SMS)
Nokia 2700 (Features:- Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:-Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (Necessary and Common Information) for the object "Mobile Phone" is make a call to any number and can send SMS."

so that, for mobile phone object you will have abstract class like following:-

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }
    public class Nokia1400 : MobilePhone
    {
    }
    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }
    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();
    }

Abstraction means putting all the variables and methods in a class which are necessary.
For example: - Abstract class and abstract method.
Abstraction is the common thing.

Example: 

If somebody in your collage tell you to fill application form, you will fill your details like name, address, data of birth, which semester, percentage you have got etc.
If some doctor gives you an application to fill the details, you will fill the details like name, address, date of birth, blood group, height and weight.
See in the above example what is the common thing?
Age, name, address so you can create the class which consist of common thing that is called abstract class.
That class is not complete and it can inherit by other class.

Encapsulation: 

Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    class Bag
    {
        book;
        pen;
        ReadBook();
    }


Encapsulation means hiding the internal details of an object, i.e. how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from the other object.

Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.
So, when you access the property you can validate the data and set it.

Example:

class Demo
{
   private int _mark;
   public int Mark
   {
     get { return _mark; }
     set { if (_mark > 0) _mark = value; else _mark = 0; }
   }
 }

Real world Example of Encapsulation:-

Let's take example of Mobile Phone and Mobile Phone Manufacturer
Suppose you are a Mobile Phone Manufacturer and you designed and developed a Mobile Phone design(class), now by using machinery you are manufacturing a Mobile Phone(object) for selling, when you sell your Mobile Phone the user only learn how to use the Mobile Phone but not that how this Mobile Phone works.

This means that you are creating the class with function and by making object (capsule) of it you are making availability of the functionality of you class by that object and without the interference in the original class.

Example-2: 

TV operation
It is encapsulated with cover and we can operate with remote and no need to open TV and change the channel.
Here everything is in private except remote so that anyone can access not to operate and change the things in TV.


Inheritance: 

When a class acquire the property of another class is known as inheritance. Inheritance is process of object re-usability. For example, A Child acquire property of Parents.

public class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("Parent Constructor.");
        }
        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }
    public class ChildClass : ParentClass
    {
        public ChildClass()
        {
            Console.WriteLine("Child Constructor.");
        }
        public static void Main()
        {
            ChildClass child = new ChildClass();
            child.print();
        }
    }



Output:
    Parent Constructor.
    Child Constructor.
    I'm a Parent Class.

Polymorphism: 

Polymorphism means one name many forms. One function behaves different forms. In other words, "Many forms of a single object is called Polymorphism."

Real World Example of Polymorphism:
Example-1: 

A Teacher behaves to student.
A Teacher behaves to his/her seniors.
Here teacher is an object but attitude is different in different situation.

Example-2: 

Person behaves SON in house at the same time that person behaves EMPLOYEE in office.

Example-3: 

Your mobile phone, one name but many forms
As phone
As camera
As mp3 player
As radio
To Read Polmorphism in Detail click following link:-

Polymorphism in .Net

Difference between Abstraction andEncapsulation :-


Abstraction Encapsulation
Abstraction solves the problem in the design level. Encapsulation solves the problem in the implementation level.
Abstraction is used for hiding the unwanted data and giving relevant data. Encapsulation means hiding the code and data into a single unit to protect the data from outside world.
Abstraction lets you focus on what the object does instead of how it does it. Encapsulation means hiding the internal details or mechanics of how an object does something.
Abstraction- Outer layout, used in terms of design. For Example:- Outer Look of a Mobile Phone, like it has a display screen and keypad buttons to dial a number. Encapsulation- Inner layout, used in terms of implementation. For Example:- Inner Implementation detail of a Mobile Phone, how keypad button and Display Screen are connect with each other using circuits.



The easier way to understand Abstraction and encapsulation is as follows:-

Real World Example:- 

Take an example of Mobile Phone:- 

You have a Mobile Phone, you can dial a number using keypad buttons. Even you don't know how these are working internally. This is called Abstraction. You have the only information that is needed to dial a number. But not its internal working of mobile.

But how the Mobile Phone internally working?, how keypad buttons are connected with internal circuit? is called Encapsulation.

Summary:

"Encapsulation is accomplished by using Class. - Keeping data and methods that accesses that data into a single unit"
"Abstraction is accomplished by using Interface. - Just giving the abstract information about what it can do without specifying the back ground details"
"Information/Data hiding is accomplished by using Modifiers - By keeping the instance variables private or protected."


Constructor types with example programs in C#.NET


Definition:

In simple terms, Constructor is a special kind of method with class name as method name and gets executed when its (class) object is created.
This article will give you a detailed explanation on C# constructors and its types.
Now, when we look at the above definition in a broader sense, a constructor is a class method that gets automatically executed whenever class’s object is created.


A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.

Constructors can be classified into 5 types


  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor


Default Constructor : A constructor without any parameters is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values and it is not possible to initialize each instance of the class to different values.

Example for Default Constructor

Parameterized Constructor : A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.


Example for Parameterized Constructor

using System;
namespace ProgramCall
{
    class Test1
    {
        //Private fields of class
        int A, B;
        //default Constructor
        public Test1()
        {
            A = 10;
            B = 20;
        }
        //Paremetrized Constructor
        public Test1(int X, int Y)
        {
            A = X;
            B = Y;
        }
        //Method to print
        public void Print()
        {
            Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);
        }   
           
    }
    class MainClass
    {
        static void Main()
        {
            Test1 T1 = new Test1();  //Default Constructor is called
            Test1 T2 = new Test1(80, 40); //Parameterized      Constructor is called     
            T1.Print();
            T2.Print();
            Console.Read();     
        }
    }
}
Output
A  =  10        B  =  20
A  =  80        B  =  40


Copy Constructor : A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.

Example for Copy Constructor


using System;
namespace ProgramCall
{
    class Test2
    {
        int A, B;
        public Test2(int X, int Y)
        {
            A = X;
            B = Y;
        }
        //Copy Constructor
        public Test2(Test2 T)
        {
            A = T.A;
            B = T.B;
        }

   
        public void Print()
        {
            Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);
        }
    }
    class CopyConstructor
    {
        static void Main()
        {     
            Test2 T2 = new Test2(80, 90);
            //Invoking copy constructor
            Test2 T3 = new Test2(T2);       
            T2.Print();
            T3.Print();
            Console.Read();
        }
    }
}
Output
A  =  80        B  =  90
A  =  80        B  =  90


Static Constructor : You can create a constructor as static and when a constructor is created as static, it will be invoked only once for any number of instances of the class and it is during the creation of first instance of the class or the first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Example for Static Constructor

using System;
namespace ProgramCall
{
    class Test3
    {
        public Test3()
        {
            Console.WriteLine("Instance  Constructor");
        }
        static Test3()
        {
            Console.WriteLine("Static  Constructor");
        }
    }
    class StaticConstructor
    {
        static void Main()
        {
            //Static Constructor and instance constructor, both are invoked for first instance.
            Test3 T1 = new Test3();
            //Only instance constructor is invoked.
            Test3 T2 = new Test3();
            Console.Read();
        }
    }
}
Output
Static  Constructor
Instance  Constructor
Instance  Constructor




Private Constructor : You can also create a constructor as private. When a class contains at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated when it contains every member as static.

Some unique points related to constructors are as follows


  • A class can have any number of constructors.
  • A constructor doesn’t have any return type even void.
  • A static constructor can not be a parameterized constructor.
  • Within a class you can create only one static constructor.


Note


  • A static constructor should not be declared with any access modifier.
  • A static constructor does not accept parameters
  • A static constructor is called automatically.
  • There is no way to call a static constructor directly.
  • Can’t stop the execution of Static constructor


Points to Remember


  • Constructor is nothing but a special method, which initializes the class or its task to initialize the object of it class.
  • Its name must be same as the name of class
  • This is a special method as constructors do not have return types, not even void
  • Constructor cannot return any value because they didn’t have any return type.
  • Constructor can’t be get inherited, although a derived class can class the base class constructor.
  • A class has atleast one constructor also known as default constructor [a constructor without parameter]
  • You have to explicitly write a default constructor while overloading constructors.
  • Concept declaring multiple constructors of a class with different sets of parameters known as Constructor overloading.
  • A constructor can be called another constructor using this()

6 Oct 2017

Visual Studio 2015 Key

VS 2015 Key HMGNV-WCYXV-X7G9W-YCX63-B98R2

Gulp: Advance in Gulp

We have learned a lot of Gulp and created a workflow that's able to compile Sass into CSS while watching HTML and JS files for changes at the same time. We can run this task with the gulp command in the command line.



We've also built a second task, build, that creates a dist folder for the production website. We compiled Sass into CSS, optimized all our assets, and copied the necessary folders into the dist folder. To run this task, we just have to type gulp build into the command line.

Lastly, we have a clean task that clears away from the generated dist folder any image caches that are created, allowing us to remove any old files that were inadvertently kept in dist.

We've created a robust workflow so far that's capable enough for most web developers. There's a lot more to Gulp and workflows that we can explore to make this process even better. Here are some ideas for you:

For development:




For optimization:


  • Removing unused CSS with unCSS
  • Further optimizing CSS with CSSO
  • Generating inline CSS for performance with Critical
  • In addition to development or optimization processes, you can also add write JavaScript unit tests with gulp-jasmine and even deploy your dist folder onto your production server automatically with gulp-rync.


As you can see, even though the workflow we've created does quite a few things, there's a lot more that can be done. Creating a workflow that suits your needs can be extremely exciting and fulfilling, but it can be a lot to take in if you're new to this.

Table of Contents:  "Table of Contents to learn"

Gulp: Combining Gulp tasks

Let's summarise what we've done. So far, we have created two distinct sets of Gulp tasks.



The first set is for a development process, where we compiled Sass to CSS, watched for changes, and reloaded the browser accordingly.

The second set is for an optimization process, where we ready all files for the production website. We optimized assets like CSS, JavaScript, and images in this process and copied fonts over from app to dist.

We've already grouped the first set of tasks together into a simple workflow with the gulp watch command:


gulp.task('watch', ['browserSync', 'sass'], function (){
  // ... watchers
});

The second set consists of tasks that we need to run to create the production website. This includes clean:dist, sass, useref, images and fonts.

If we went by the same train of thought, we could create a build task to combine everything together.


gulp.task('build', [`clean`, `sass`, `useref`, `images`, `fonts`], function (){
  console.log('Building files');
});

Unfortunately, we wouldn't be able to write the build task this way because Gulp activates all tasks in the second argument simultaneously.

There's a possibility that useref, images, or even fonts gets completed before clean does, which means the entire `dist` folder gets deleted.

So, to ensure that cleans get completed before the rest of the tasks, we need to use an extra plugin called Run Sequence.

$ npm install run-sequence --save-dev

Here's the syntax of a task sequence with run sequence:


var runSequence = require('run-sequence');

gulp.task('task-name', function(callback) {
  runSequence('task-one', 'task-two', 'task-three', callback);
});

When task-name is called, Gulp will run task-one first. When task-one finishes, Gulp will automatically start task-two. Finally, when task-two is complete, Gulp will run task-three.

Run Sequence also allows you to run tasks simultaneously if you place them in an array:


gulp.task('task-name', function(callback) {
  runSequence('task-one', ['tasks','two','run','in','parallel'], 'task-three', callback);=
});

In this case, Gulp first runs task-one. When task-one is completed, Gulp runs every task in the second argument simultaneously. All tasks in this second argument must be completed before task-three is run.

So we can now create a task that ensures that clean:dist runs first, followed by all the other tasks:


gulp.task('build', function (callback) {
  runSequence('clean:dist',
    ['sass', 'useref', 'images', 'fonts'],
    callback
  )
});

To make things consistent, we can also build the same sequence with the first group. Let's use default as the task name this time:


gulp.task('default', function (callback) {
  runSequence(['sass','browserSync', 'watch'],
    callback
  )
});

Why default? Because when you have a task named default, you can run it simply by typing the gulp command, which saves some keystrokes.

Finally, here's a github repo for all the work we've done!
Click here for For next Step:  "Advance in Gulp"

Table of Contents:  "Table of Contents to learn"

Gulp: Cleaning up generated files automatically


Since we're generating files automatically, we'll want to make sure that files that are no longer used don't remain anywhere without us knowing.

This process is called cleaning (or in simpler terms, deleting files).

We'll have to use del to help us with cleaning.

npm install del --save-dev


var del = require('del');

The del function takes in an array of node globs which tells it what folders to delete.

Setting it up with a Gulp task is almost like the first "hello" example we had.


gulp.task('clean:dist', function() {
  return del.sync('dist');
});

Now Gulp will delete the `dist` folder for you whenever gulp clean:dist is run.

Note: We don't have to worry about deleting the dist/images folder because gulp-cache has already stored the caches of the images on your local system.

To clear the caches off your local system, you can create a separate task that's named `cache:clear`


gulp.task('cache:clear', function (callback) {
return cache.clearAll(callback)
});

Phew, that's a mouthful. Let's combine all our tasks together now!
Click here for For next Step:  "Combining Gulp tasks"

Table of Contents:  "Table of Contents to learn"

Gulp: Copying Fonts to Dist


Since font files are already optimized, there's nothing more we need to do. All we have to do is to copy the fonts into dist.

We can copy files with Gulp simply by specifying the gulp.src and gulp.dest without any plugins.


gulp.task('fonts', function() {
  return gulp.src('app/fonts/**/*')
  .pipe(gulp.dest('dist/fonts'))
})

Now Gulp will copy 'fonts' from 'app' to 'dist' whenever you run gulp fonts.


We have 6 different tasks in the gulpfile now, and each of them has to be called individually with the command line, which is kinda cumbersome so we want to tie everything together into one command.

Before we do that though, let's look at how to clean up generated files automatically.
Click here for For next Step:  "Cleaning up generated files automatically"

Table of Contents:  "Table of Contents to learn"

Gulp: Optimizing Images


You've probably guessed it by now; we need to use gulp-imagemin to help us with optimizing images.




$ npm install gulp-imagemin --save-dev


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

We can minify png, jpg, gif and even svg with the help of gulp-imagemin. Let's create an images task for this optimization process.


gulp.task('images', function(){
  return gulp.src('app/images/**/*.+(png|jpg|gif|svg)')
  .pipe(imagemin())
  .pipe(gulp.dest('dist/images'))
});

Since different file types can be optimized differently, you might want to add options to imagemin to customize how each file is optimized.

For example, you can create interlaced GIFs by setting the interlaced option key to true.


gulp.task('images', function(){
  return gulp.src('app/images/**/*.+(png|jpg|jpeg|gif|svg)')
  .pipe(imagemin({
      // Setting interlaced to true
      interlaced: true
    }))
  .pipe(gulp.dest('dist/images'))
});

You can play around with other options if you want to as well.

Optimizing images however, is an extremely slow process that you'd not want to repeat unless necessary. To do so, we can use the gulp-cache plugin.

$ npm install gulp-cache --save-dev


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

gulp.task('images', function(){
  return gulp.src('app/images/**/*.+(png|jpg|jpeg|gif|svg)')
  // Caching images that ran through imagemin
  .pipe(cache(imagemin({
      interlaced: true
    })))
  .pipe(gulp.dest('dist/images'))
});

We're almost done with the optimization process. There's one more folder that we need to transfer from the `app` directory into `dist`, the fonts directory. Let's do that now.
Click here for For next Step:  "Copying Fonts to Dist"

Table of Contents:  "Table of Contents to learn"

Gulp: Optimizing CSS and JavaScript files



Developers have two tasks to perform when we try to optimize CSS and JavaScript files for production: minification and concatenation.



One problem developers face when automating this process is that it's difficult to concatenate your scripts in the correct order.

Say we have included 3 script tags in index.html.


<body>
  <!-- other stuff -->
  <script src="js/lib/a-library.js"></script>
  <script src="js/lib/another-library.js"></script>
  <script src="js/main.js"></script>
</body>

These scripts are located in two different directories. It'll be difficult to concatenate them with traditional plugins like gulp-concatenate.

Thankfully, there's a useful Gulp plugin, gulp-useref that solves this problem.

Gulp-useref concatenates any number of CSS and JavaScript files into a single file by looking for a comment that starts with "<!--build:" and ends with "<!--endbuild-->". Its syntax is:


<!-- build:<type> <path> -->
... HTML Markup, list of script / link tags.
<!-- endbuild -->

<type> can either be js, css, or remove. It's best to set type to the type of file that you're trying to concatenate. If you set type to remove, Gulp will remove the entire build block without generating a file.

<path> here refers to the target path of the generated file.

We'll want the final JavaScript file to be generated in the js folder, as main.min.js. Hence, the markup would be:


<!--build:js js/main.min.js -->
<script src="js/lib/a-library.js"></script>
<script src="js/lib/another-library.js"></script>
<script src="js/main.js"></script>
<!-- endbuild -->

Now let's configure the gulp-useref plugin in the gulpfile. We'll have to install the plugin and require it in the gulpfile.

$ npm install gulp-useref --save-dev


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

Setting up the useref task is similar to the other tasks we've done so far. Here's the code:


gulp.task('useref', function(){
  return gulp.src('app/*.html')
    .pipe(useref())
    .pipe(gulp.dest('dist'))
});

Now if you run this useref task, Gulp will take run through the 3 script tags and concatenate them into dist/js/main.min.js.



The file however, isn't minified right now. We'll have to use the gulp-uglify plugin to help with minifying JavaScript files. We also need a second plugin called gulp-if to ensure that we only attempt to minify JavaScript files.


$ npm install gulp-uglify --save-dev



// Other requires...
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');


gulp.task('useref', function(){
  return gulp.src('app/*.html')
    .pipe(useref())
    // Minifies only if it's a JavaScript file
    .pipe(gulpIf('*.js', uglify()))
    .pipe(gulp.dest('dist'))
});

Gulp should now automatically minify the `main.min.js` file whenever you run the useref task.

One neat thing I've yet to reveal with Gulp-useref is that it automatically changes all the scripts within "<!--build:" and "<!--endbuild-->" into one single JavaScript file that points to `js/main.min.js`.



Wonderful, isn't it?

We can use the same method to concatenate any CSS files (if you decided to add more than one) as well. We'll follow the same process and add a build comment.


<!--build:css css/styles.min.css-->
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="css/another-stylesheet.css">
<!--endbuild-->

We can also minify the concatenated CSS file as well. We need to use a package called gulp-cssnano plugin to help us with minification.


$ npm install gulp-cssnano



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

gulp.task('useref', function(){
  return gulp.src('app/*.html')
    .pipe(useref())
    .pipe(gulpIf('*.js', uglify()))
    // Minifies only if it's a CSS file
    .pipe(gulpIf('*.css', cssnano()))
    .pipe(gulp.dest('dist'))
});

Now you'd get one optimized CSS file and one optimized JavaScript file whenever you run the useref task.

Let's move on and optimize images next.
Click here for For next Step:  "Optimizing Images"

Table of Contents:  "Table of Contents to learn"

Gulp: Live-reloading with Browser Sync


Browser Sync helps make web development easier by spinning up a web server that helps us do live-reloading easily. It has other features, like synchronizing actions across multiple devices, as well.

We'll first have to install Browser Sync:

$ npm install browser-sync --save-dev

You may have noticed that there isn't a gulp- prefix when we install Browser Sync. This is because Browser Sync works with Gulp, so we don't have to use a plugin.

To use Browser Sync, we'll have to require Browser Sync.


var browserSync = require('browser-sync').create();

We need to create a browserSync task to enable Gulp to spin up a server using Browser Sync. Since we're running a server, we need to let Browser Sync know where the root of the server should be. In our case, it's the `app` folder:


gulp.task('browserSync', function() {
  browserSync.init({
    server: {
      baseDir: 'app'
    },
  })
});

We also have to change our sass task slightly so Browser Sync can inject new CSS styles (update the CSS) into the browser whenever the sass task is ran.


gulp.task('sass', function() {
  return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss
    .pipe(sass())
    .pipe(gulp.dest('app/css'))
    .pipe(browserSync.reload({
      stream: true
    }))
});

We're done with configuring Browser Sync. Now, we have to run both the watch and browserSync tasks at the same time for live-reloading to occur.

It'll be cumbersome to open up two command line windows and run gulp browserSync and gulp watch separately, so let's get Gulp to run them together by telling the watch task that browserSync must be completed before watch is allowed to run.

We can do so by adding a second argument to the watch task. The syntax is:


gulp.task('watch', ['array', 'of', 'tasks', 'to', 'complete','before', 'watch'], function (){
  // ...
});

And in this case we're adding the browserSync task.


gulp.task('watch', ['browserSync'], function (){
  gulp.watch('app/scss/**/*.scss', ['sass']);
  // Other watchers
});

We'll also want to make sure sass runs before watch so the CSS will already be the latest whenever we run a Gulp command.


gulp.task('watch', ['browserSync', 'sass'], function (){
  gulp.watch('app/scss/**/*.scss', ['sass']);
  // Other watchers
});

Now, if you run gulp watch in the command line, Gulp should start both the sass and browserSync tasks concurrently. When both tasks are completed, watch will run.



At the same time, a browser window that points to app/index.html would also pop up. If you change the styles.scss file, you'll see that the browser reloads automatically.



There's one more thing before we end this live-reloading section. Since we're already watching for .scss files to reload, why not go a step further and reload the browser if any HTML or JavaScript file gets saved?

We can do so by adding two more watch processes, and calling the browserSync.reload function when a file gets saved:


gulp.task('watch', ['browserSync', 'sass'], function (){
  gulp.watch('app/scss/**/*.scss', ['sass']);
  // Reloads the browser whenever HTML or JS files change
  gulp.watch('app/*.html', browserSync.reload);
  gulp.watch('app/js/**/*.js', browserSync.reload);
});

So far in this tutorial we've taken care of three things:

Spinning up a web server for development
Using the Sass preprocessor
Reloading the browser whenever a file is saved
Let's cover the part on optimizing assets in the next section. We'll start with optimizing CSS and JavaScript files.
Click here for For next Step:  "Optimizing CSS and JavaScript files"

Table of Contents:  "Table of Contents to learn"

Gulp: Watching Sass files for changes


Gulp provides us with a watch method that checks to see if a file was saved. The syntax for the watch method is:


// Gulp watch syntax
gulp.watch('files-to-watch', ['tasks', 'to', 'run']);

If we want to watch all Sass files and run the sass task whenever a Sass file is saved, we just have to replace files-to-watch with the app/scss/**/*.scss, and ['tasks', 'to', 'run'] with ['sass']:


// Gulp watch syntax
gulp.watch('app/scss/**/*.scss', ['sass']);

More often though, we'll want to watch more than one type of file at once. To do so, we can group together multiple watch processes into a watch task:


gulp.task('watch', function(){
  gulp.watch('app/scss/**/*.scss', ['sass']);
  // Other watchers
})

If you ran the gulp watch command right now, you'll see that Gulp starts watching immediately.




And that it automatically runs the sass task whenever you save a .scss file.




Let's take it a step further and make Gulp reload the browser whenever we save a .scss file with the help of Browser Sync.
Click here for For next Step:  "Live-reloading with Browser Sync"

Table of Contents:  "Table of Contents to learn"

9 Apr 2017

Gulp: Globbing in Node

Globs are matching patterns for files that allow you to add more than one file into gulp.src. It's like regular expressions, but specifically for file paths.



When you use a glob, the computer checks file names and paths for the specified pattern. If the pattern exists, then a file is matched.

Most workflows with Gulp tend to only require 4 different globbing patterns:

  1. *.scss: The * pattern is a wildcard that matches any pattern in the current directory. In this case, we’re matching any files ending with .scss in the root folder (project).
  2. **/*.scss: This is a more extreme version of the * pattern that matches any file ending with .scss in the root folder and any child directories.
  3. !not-me.scss: The ! indicates that Gulp should exclude the pattern from its matches, which is useful if you had to exclude a file from a matched pattern. In this case, not-me.scss would be excluded from the match.
  4. *.+(scss|sass): The plus + and parentheses () allows Gulp to match multiple patterns, with different patterns separated by the pipe | character. In this case, Gulp will match any file ending with .scss or .sass in the root folder.


Since we know about globbing now, we can replace app/scss/styles.scss with a scss/**/*.scss pattern, which matches any file with a .scss extension in app/scss or a child directory.


gulp.task('sass', function() {
  return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss and children dirs
    .pipe(sass())
    .pipe(gulp.dest('app/css'))
});

Any other Sass file that's found within app/scss would automatically be included into the sass task with this change. If you add a print.scss file into the project, you'll see that print.css will be generated in app/css.



We've now managed to compile all Sass files into CSS files with a single command. The question is, what good does it do if we have to run gulp sass manually every time we want to compile Sass into CSS?

Luckily, we can tell Gulp to automatically run the sass task whenever a file is saved through a process called "watching".

Click here for For next Step:  "Gulp: Watching Sass files for changes"

Table of Contents:  "Table of Contents to learn"

Preprocessing with Gulp

We can compile Sass to CSS in Gulp with the help of a plugin called gulp-sass. You can install gulp-sass into your project by using the npm install command like we did for gulp.



We'd also want to use the --save-dev flag to ensure that gulp-sass gets added to devDependencies in package.json.

$ npm install gulp-sass --save-dev

We have to require gulp-sass from the node_modules folder just like we did with gulp before we can use the plugin.


var gulp = require('gulp');
// Requires the gulp-sass plugin
var sass = require('gulp-sass');

We can use gulp-sass by replacing aGulpPlugin() with sass(). Since the task is meant to compile Sass into CSS, let's name it sass.


gulp.task('sass', function(){
  return gulp.src('source-files')
    .pipe(sass()) // Using gulp-sass
    .pipe(gulp.dest('destination'))
});

We'll need to provide the sass task with source files and a destination for the task to work, so let's create a styles.scss file in the app/scss folder. This file will be added to the sass task in gulp.src.

We want to output the eventual styles.css file to the `app/css` folder, which would be the destination for gulp.dest.


gulp.task('sass', function(){
  return gulp.src('app/scss/styles.scss')
    .pipe(sass()) // Converts Sass to CSS with gulp-sass
    .pipe(gulp.dest('app/css'))
});

We'll want to test that the sass task is working as we want it to. To do so, we can add a Sass function within styles.scss.


// styles.scss
.testing {
  width: percentage(5/7);
}

If you run gulp sass in the command line, you should now be able to see that a styles.css file was created in app/css. In addition, it has the code where percentage(5/7) was evaluted into 71.42857%.


/* styles.css */
.testing {

  width: 71.42857%;
}

That's how we know that the sass task works!

Sometimes we need the ability to compile more than one .scss file into CSS at the same. We can do so with the help of Node globs.

FYI: Gulp-sass uses LibSass to convert Sass into CSS. It's much quicker than Ruby-based methods. If you want, you can still use Ruby methods with Gulp by using gulp-ruby-sass or gulp-compass instead.

Click here for For next Step:  "Gulp: Globbing in Node"

Table of Contents:  "Table of Contents to learn"

Writing Your First Gulp Task



The first step to using Gulp is to require it in the gulpfile.


var gulp = require('gulp');


The require statement tells Node to look into the node_modules folder for a package named gulp. Once the package is found, we assign its contents to the variable gulp.

We can now begin to write a gulp task with this gulp variable. The basic syntax of a gulp task is:


gulp.task('task-name', function() {
  // Stuff here
});


task-name refers to the name of the task, which would be used whenever you want to run a task in Gulp. You can also run the same task in the command line by writing gulp task-name.

To test it out, let's create a hello task that says Hello Zell!.


gulp.task('hello', function() {
  console.log('Hello Zell');
});

We can run this task with gulp hello in the command line.

$ gulp hello

The command line will return a log that says Hello Zell!.


Gulp tasks are usually a bit more complex than this. It usually contains two additional Gulp methods, plus a variety of Gulp plugins.

Here's what a real task may look like:


gulp.task('task-name', function () {

  return gulp.src('source-files') // Get source files with gulp.src

    .pipe(aGulpPlugin()) // Sends it through a gulp plugin

    .pipe(gulp.dest('destination')) // Outputs the file in the destination folder

});

As you can see, a real task takes in two additional gulp methods — gulp.src and gulp.dest.

gulp.src tells the Gulp task what files to use for the task, while gulp.dest tells Gulp where to output the files once the task is completed.

Let's try building a real task where we compile Sass files into CSS files.

Click here for For next Step:  "Preprocessing with Gulp"

Table of Contents:  "Table of Contents to learn"

Determining Folder Structure in gulp

Gulp is flexible enough to work with any folder structure. You'll just have to understand the inner workings before tweaking it for your project.


For this article, we will use the structure of a generic webapp:


  |- app/

      |- css/

      |- fonts/

      |- images/

      |- index.html

      |- js/

      |- scss/

  |- dist/

  |- gulpfile.js

  |- node_modules/

  |- package.json


In this structure, we'll use the app folder for development purposes, while the dist (as in "distribution") folder is used to contain optimized files for the production site.

Since app is used for development purposes, all our code will be placed in app.

We'll have to keep this folder structure in mind when we work on our Gulp configurations. Now, let's begin by creating your first Gulp task in gulpfile.js, which stores all Gulp configurations.
Click here for For next Step:  "Writing Your First Gulp Task"

Table of Contents:  "Table of Contents to learn"

Creating a Gulp Project

First, we'll create a folder called project to server as our project root as we move through this tutorial. Run the npm init command from inside that directory:



# ... from within our project folder
$ npm init


The npm init command creates a package.json file for your project which stores information about the project, like the dependencies used in the project (Gulp is an example of a dependency).

npm init will prompt you:



Once the package.json file is created, we can install Gulp into the project by using the following command:

$ npm install gulp --save-dev

This time, we're installing Gulp into project instead of installing it globally, which is why there are some differences in the command.

You'll see that the sudo keyword isn't required because we're not installing Gulp globally, so -g is also not necessary. We've added --save-dev, which tells the computer to add gulp as a dev dependency in package.json.





If you check the project folder when the command has finished executing, you should see that Gulp has created a node_modules folder. You should also see a gulp folder within node_modules.




We're almost ready to start working with Gulp. Before we do so, we have to be clear on how we're going to use Gulp for the project, and part of that is deciding on a directory structure.

Click here for For next Step:  "Determining Folder Structure in gulp"

Table of Contents:  "Table of Contents to learn"

Installing Gulp

You need to have Node.js (Node) installed onto your computer before you can install Gulp.




If you do not have Node installed already, you can get it by downloading the package installer from Node's website.

When you're done with installing Node, you can install Gulp by using the following command in the command line:



$ sudo npm install gulp -g


Note: Only Mac users need the sudo keyword. (See the first comment by Pawel Grzybek if you don't want to use sudo). And remember the "$" in the code above just symbolizes the command prompt. That's not actually part of the command you run.


The npm install command we've used here is a command that uses Node Package Manager (npm) to install Gulp onto your computer.

The -g flag in this command tells npm to install Gulp globally onto your computer, which allows you to use the gulp command anywhere on your system.

Mac users need the extra sudo keyword in the command because they need administrator rights to install Gulp globally.

Now that you have Gulp installed, let's make a project that uses Gulp.

Click here for For next Step:  "Creating a Gulp Project"

Table of Contents:  "Table of Contents to learn"