Sunday, June 12, 2016

Run Powershell script from Gulp.js

Installation Requirement

  1. If your Visual Studio is 2013 or order install Task Runner Explorer - Download and install- https://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708
  2. Node.js- Recommended for most users-Check this download link-https://nodejs.org/en/
 
  • Run from the cmd command from this solution or class library location.
  • Ensure you keep the gulp.js in location where the actions /events are required.
  • Run command below from Class Library or website folder: Npm I gulp -g

Change Environment variable path

Gulp .js can be run from Task Runner VS2015

var gulp = require("gulp");
var childProcess = require('child_process');

gulp.task('deploy-static-files', function (callback) {
    console.log("---------Start--------------------------");
    return childProcess.exec('Powershell.exe  -executionpolicy remotesigned -File  DeployStaticFiles.ps1', function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        if (err) console.log(err);

        console.log("Finished");
    });
})

Powershell script :Deploy Static file code

####----Copy files from source to destination-----
$publishPathDestination      =   "Some Path"
$scriptpathSource            = Split-Path $MyInvocation.MyCommand.Path
Copy-Item  "$scriptpath"     $publishPathDestination   -Recurse -Force;


Get description Attributes from Enum.

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
         //Get Description
            Console.WriteLine(GetEnumDescription<MyEnum>(2));

            //Get Value
            Console.WriteLine(MyEnum.Hear);

            //Get Name
            Console.WriteLine(Enum.GetName(typeof(MyEnum), 1));

            //Get Enum Descriptions values
            var myEnumDescriptions =
                Enum.GetValues(typeof(MyEnum))
                    .Cast<MyEnum>()
                    .Select(n => new { ID = (int)n, Name = Enum.GetName(typeof(MyEnum), n), Description = GetEnumDescription(n) });

            //Get Name, Description, Value
            foreach (var item in myEnumDescriptions)
            {
                Console.WriteLine(item.Name);
            }

            //Iterate and fetch Description
            foreach (int iValues in Enum.GetValues(typeof(MyEnum)))
            {
                Console.WriteLine(GetEnumDescription <MyEnum>(iValues));
            }
        }

        public enum MyEnum
        {
            [Description("You can Walk")]
            Walk = 1,
            [Description("You can Talk")]
            Talk = 2,
            [Description("You can Hear")]
            Hear = 3
        }

        public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

            if (attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        public static string GetEnumDescription <T>(int value)
        {
            return GetEnumDescription((Enum)(object)((T)(object)value));
        }
    }


}