This document contains basic fundamental of package manager concept and also contains step by step guide to create and publish a package in to package repository .
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Introduction to package manager
1. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Agenda: Creating, Publishing and Consuming NuGet Package.
Introduction to Package Manager.
Packaging Steps.
Creating a NuGet Core package using Visual Studio IDE.
Creating a NuGet Core package using .NET CLI Tool.
Creating a NuGet .NET Framework package using NuGet CLI Tool.
Config file transformations.
Including package dependencies using .nuspec file..
Publishing a Package into NuGet public repository using NuGet.org web interface.
Publish package into NuGet public repository using Dot net core CLI.
Publishing a Package into a Local repository.
How to consume packages available in the Local repository:
Publishing a Package into MyGet repository.
How to consume packages available in the MyGet repository.
Publishing a Package into Azure DevOps repository.
How to create and publish a NPM package?
What is a package manager?
Package Manager is a tool used to simplify the process of installing, configuring, upgrading and removing of
software packages.
Why we need a Package Manager?
When we want to consume any library from our code we generally follow certain steps such as,
Search for the required library.
Install the library.
Add the reference to the DLL.
Configure the library if needed.
After some day if we need to update our library because we found some critical bug or security hole then we
need to follow some steps to remove it from the current project and again follow above repeated steps to refer
the new library. As day by day the amount of libraries available for any given language or technology are
increases and in a given software project we may used hundreds of libraries, so it is very difficult to manually
manage the library packages . So we required a proper tool to manage all our packages.
Popular Package Manager Tool:
NuGet: Package manager for the Microsoft development platform.
NPM: package manager for Node.js and JavaScript
Reference : https://www.nuget.org/ , https://www.npmjs.com/
What is a NuGet Package?
The NuGet package is just a ZIP file with a extension of .nupkg and is contains compiled code ,other
supported file require for the package, and a descriptive manifest that includes information like, the package's
version number, Package Id,author , company etc.
Reference: https://docs.microsoft.com/en-us/nuget/reference/nuspec
Packaging Steps:
There are some basic steps we need to follow for packaging and this process is common for mostly all type
packages like NuGet, npm etc.
Step 1: Write required source code
Write the code to implement the Business requirements and test the library thoroughly to make sure that
its working as expected.
2. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
You are free to create a .NET Framework libraries , .NET Core libraries etc.
Step 2: Create a package.
To create a package you can use any NuGet client tool such as Visual Studio IDE, dotnet.exe or nuget.exe
dotnet.exe tool is for .NET Core libraries.
Step 3: Publish the package into a package repository.
As per your business requirements you can select any type of repository.
Public Repository: If you want that your package should available to everyone then you should chose a
public repository like Nuget.org.
Private Repository: There might be some scenarios where you might want to release packages to only a
limited audience or you want some restriction over the package uses then you should go for a private
repository. It may be a local private repository or remote private repository.
Local Private Repository: Local NuGet package feeds are simply a collections of folder on your local
network.
Remote Private Repository: There are some NuGet hosting providers who provides services for private
repository and popular examples are MyGet and Azure DevOps.
Step 4: Consume the package.
Consumer application can browse the packages using a package manager tool and consume it where ever
required.
Creating a NuGet Core package using Visual Studio IDE
Open Visual Studio, go to File - New and select a .Net Core Class Library project.
Write some sample code.
using System;
namespace SampleLibrary
{
public class Utility
{
public string ToUpper(string input)
{
return input.ToUpper();
}
}
}
3. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Right-click on the project, choose Properties and go to the package tab.
Check the check box at the top and then fill in the package information as required.
Package ID This is a unique identifier used to identify your package uniquely..
Version This is a version of the package like 1.0.0.
Author This field holds authors of the package.
owners Generally Author and owner both contains same value i.e creator of the package.
iconUrl We can specify an icon for the package. The size of the image should be a 32x32 pixel png
image with transparent background.
copyright Specify copyright details for the package.
tags Tags are nothing but keywords and can be used to search the package.
releaseNotes This going to hold the release notes of the project.
description As the name suggests, this is a long description of the package.
Build the class library in the release mode.
Congratulations!! You are ready with your first NuGet package. Now you can publish into any type of
NuGet repository.
Creating a NuGet Core package using .NET CLI Tool
Create a .net core library using CLI
F:> mkdir DemoApp
F:> dotnet new classlib -n SampleUtility -o DemoApp
-n stands for name of the class library and -o stands for output directory
Open the project file in any supported editor such as Visual Studio Code and write your sample code.
using System;
namespace SampleLibrary
{
public class Utility
{
public string ToUpper(string input)
{
4. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
return input.ToUpper();
}
}
}
Build the project using ,
F:DemoApp> dotnet build
Package using below command,
F:DemoApp> dotnet pack --configuration release
Now your NuGet package is ready to be published.
Creating a NuGet .NET Framework package using NuGet CLI Tool
Open Visual Studio, go to File - New and select a .Net Framework Class Library project.
Write below sample code and build it in release mode.
using System;
namespace SampleNetFrameworkLibrary{
public class Utility {
public string ToUpper(string input)
{
return input.ToUpper();
}
}
}
5. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Install NuGet.exe
Create a new folder to hold your package details, let say SampleNetFrameworkPackage.
Inside the new folder create a .nuspec file by using bellow command.
F:SampleNetFrameworkPackage> nuget.exe spec
This command generate a file Package.nuspec and it is a manifest file that uses XML nodes to describe the
package details such as PackageId, Version, Author etc.
Open Package.nuspec file and edit the package details such as PackageId, Version, Author etc as per the
requirements.
Sample Package.nuspes looks like below,
<?xml version="1.0"?>
<package >
<metadata>
<id>Package.Test.Yash</id>
<version>1.0.0</version>
<authors>Yashobanta</authors>
<owners>Yashobanta</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Sample description</description>
<releaseNotes>Sample release notes of the package.</releaseNotes>
<copyright>Copyright 2019</copyright>
</metadata>
</package>
Create another subfolder in the name of "lib" inside "SampleNetFrameworkPackage" and inside the "lib"
folder create a subfolder and name it as per the framework you supported. For an example of you support
.Net4.5 then create a folder name as net45.
Copy your class library project DLL from the release folder into SampleNetFrameworkPackagelibnet45
folder.
Create a read me file that contain instructions and additional help regarding your package and it will be
available to the end user just after installing your package.
Now create the package using the below command
F:SampleNetFrameworkPackage> Nuget.exe pack Package.nuspec
Great! your package is ready now.
Config File Transformations
Some time you may required that when your package is referenced by any application you need to add some
default settings in to the web.config or app.config file, this requirement can be achieved through config file
transformations.
The package creation process will be exactly as mentioned in the above, but we need to add some extra
configuration inside our Package.nuspec file to achieve config transformation.
Follow the steps mention in the above section i.e " Creating a NuGet .NET Framework package using
NuGet CLI Tool".
6. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Create a folder name as "content" inside your parent folder that you earlier created to hold your package
details i.e SampleNetFrameworkPackage, and add both app.config.transform and web.config.transform
file.Add the below XML code in both the files.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TestKey" value="TestValue"/>
</appSettings>
</configuration>
Then modify the Package.nuspes file to include these transformation file in the package.
<?xml version="1.0"?>
<package >
<metadata>
<id>Package.Test.Yash</id>
<version>1.0.0</version>
<authors>Yashobanta</authors>
<owners>Yashobanta</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Sample description</description>
<releaseNotes>Sample release notes of the package.</releaseNotes>
<copyright>Copyright 2019</copyright>
</metadata>
<files>
<file src="contentweb.config.transform" target="content" />
<file src="contentapp.config.transform" target="content" />
</files>
</package>
Now when the NuGet package is install in the clients application the key that you had defined in the
transform file will be added to the App.config/web.config file. When the package is uninstalled, the key
will be removed.
Note:
src: The location of the file or files to include.
target: The relative path to the folder within the package where the source files are placed, which must
begin with lib, content, build, or tools.
Reference : https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package#from-a-
convention-based-working-directory
Including Package Dependencies
If your root package is dependent over any other packages then you can define those packages inside the
dependencies section. So when your root package is installed in the client's system, then all the dependent
packages are automatically installed. You can define your project dependencies inside the .nuspec file using
<dependencies> tag.
7. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
<?xml version="1.0"?>
<package >
<metadata>
<id>Package.Test.Yash</id>
<version>1.0.0</version>
<authors>Yashobanta</authors>
<owners>Yashobanta</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Sample description</description>
<releaseNotes>Sample release of the package.</releaseNotes>
<copyright>Copyright 2019</copyright>
<dependencies>
<dependency id="Newtonsoft.Json" version="12.0.2" />
</dependencies>
</metadata>
</package>
Note: Now when Package.Test.Yash root package is installed in the client's system, then automatically
Newtonsoft.Json package also going to installed.
Publishing a Package into NuGet public repository.
Create an NuGet Account by following instructions available in https://www.nuget.org/.
You can upload packages using NuGet.org web portal or using CLI tools.
Publish package using NuGet.org web portal:
Sign In to your https://www.nuget.org/. account.
Go to Manage packages options, it shows you all the packages you had already published.
8. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
To upload a new package you should select upload package option and it provide a easy UI interface
to browse and upload a package.
Congratulations!! You had successfully uploaded your package in to a public repository.It might take
some time to be available to others for consuming it.
Publish package using Dot net core CLI:
Login to https://www.nuget.org
Select your user name (on the upper right corner), then select API Keys.
If you dont have any API keys then create one API key
Select Create and provide a name for your key, then Select Scopes as Push. Under API Key, enter *
for Glob pattern, then click on Create button to create the API keys.
Execute the following command to push a package in to the remote repository.
dotnet nuget push MyDemoProject.1.0.0.nupkg -k
oy2b6sukbzvh7d5r2wnqz7ghv7s4utcevgb7ekfbtcli -s https://api.nuget.org/v3/index.json
Note: -k stands for API key and -s stands for Source.
Consume a NuGet package
Open your .NET project in visual studio , Go to the solution explorer.
Right click and select Manage NuGet packages.
Search for your package and install it.
9. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Publishing a Package into a Local repository
Local NuGet package feeds are nothing but hierarchical folder structures on local networklocal
system.
Create a new folder in your local system MyLocalNugetRepo and share the folder so that others can
discover this folder.
You can manually copy the nuget packages into this folder or can use below command to add nuget
packages into this local repository.
nuget add MyDemoProject.1.0.1.nupkg -source HPMyLocalNugetRepo
Consume packages available in the Local repository
Open your .NET project in visual studio , Go to the solution explorer.
Right click on your project and select Manage NuGet packages.
Go to the package source settings
Go to package source option and click on the plus icon to add the local repository as a source.
In the Source provide your repository folder location i.e HPMyLocalNugetRepo and press on ok button.
Now onwards all the packages that you had added inside your local repository folders will be available in
your visual studio package manager
10. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
These packages are available only to those who have access to the repository folder i.e in our case
HPMyLocalNugetRepo.
Publishing a Package into MyGet repository
MyGet provides both public and private repository/feed.
So first create an MyGet Account by following instructions available in https://www.myget.org/
Sign in to the account and click on the new feed button to create a new feed.
Provide the name and type of the feed
If you select public type then what ever package you publish can be accessible to anyone. But if you
chose the feed as private then only the authorize candidate can access.
Go to the MyGet home page to see the list of available feed you have added inside your user account.
Select the feed in which you want to publish the package.
11. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Click on add button and select NuGet type package.
Browse the required package and upload it into the MyGet server.
Consume packages available in the MyGet repository:
Open your .NET project in visual studio , Go to the solution explorer.
Right click on your project and select Manage NuGet packages.
Go to the package source settings
Go to package source option and click on the plus icon to add the MyGet repository as a source
In the Source provide your feed url and press on ok button. You can get the feed url from the feed
details section.
12. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Now onwards all the packages that you had added inside your MyGet feed will be available in your
visual studio package manager and any time you can consume it just by installing from the package
manager
If you are trying to access and install any private feed packages then package manager will ask your
credentials to validate. So if feed owner provides you access permission then only you can consume the
private feed packages.
Publishing a Package into Azure DevOps repository
Like MyGet we can also use Azure Devops to create a Remote Private NuGet Repository.
Create an Azure Devops free account by following instructions available in
https://visualstudio.microsoft.com/dev-essentials/.
Sign in to the AzureDevops account and if you have not added any organization then add one
organization and within the organization add a project.
Select the required project.
From the project dashboard select artifacts.
Click on the NewFeed option.
13. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Provide the required details and click on the create button to create a feed.
Click on Connect to feed button and it will show you all the steps required to push a NuGet package in
to the Azure Feed.
Consume packages available in the Azure DevOps repository
In order to consume the packages, open Visual studio go to the package source option and click on the
plus icon to add the Azure Feed repository url as a source
14. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Creating and publishing a NPM package
NPM is a package manager tool for java script related technologies.
Step 1: We need NPM CLI tool to create an NPM package and NPM tool is distributed as a part of NodeJs.
So check whether node is installed in your system or not.
F:> node --version
F:> npm --version
Step 2: Create a project directory
F:> mkdir mytestnpm
F:> cd mytestnpm
Step 3: Initialise the NPM package
F:mytestnpm> npm init
This command will create a package.json file by taking some manifest details. The file looks like below,
{
"name": " mytestnpmUtility ",
"version": "1.0.0",
"description": "This is my test package.",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "Yashobanta",
"license": "ISC"
}
main is the important field in the package.json file, it contains the name of the file that would be
loaded when your package is required by another application. By default the value is index.js.
Name should be unique.
15. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Step 4: Create an index.js file in the root of the project folder and implement the functionality that you want
to share. Whatever methods we exports will be available to the consumer who install our package.
function SayHi() {
return "Wel Come to NPM Demo";
}
module.exports = SayHi;
Step 5: To push NPM package into the NPM repository you first need have an NPM account, you can create
an free account by visiting https://www.npmjs.com.
Login to you account from the command prompt and publish your package using below command in
Administrator mode.
F:mytestnpm> npm login
F:mytestnpm> npm publish
Step 6: Login into you NPM web portal and check your package.
Consume the NPM package
Create a test directory.
F:> mkdir TestStringUtility
F:> cd TestStringUtility
16. Introduction To Package Manager
Yashobanta Bai
yashobanta.bai@gmail.com
Install stringutility package
F:TestStringUtility> npm install stringutility --save
Create a test file,mytest.js and write the bellow sample code to consume the library.
const SayHi = require('stringutility');
console.log(SayHi());
Run the node command to execute the code.
F:TestStringUtility> node ./mytest.js