Skip to content

Instantly share code, notes, and snippets.

@sayedihashimi
Last active July 26, 2022 19:53
Show Gist options
  • Save sayedihashimi/64736331b273162a1e76 to your computer and use it in GitHub Desktop.
Save sayedihashimi/64736331b273162a1e76 to your computer and use it in GitHub Desktop.
Samples showing how to download nuget in a scrpit to avoid checking it in
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GetNuget">
<!--
This is an MSBuild snippet that can be used to download nuget to the path
in the property NuGetExePath property.
Usage:
1. Copy and paste this into your build script
2. Call the GetNuGet target before you use nuget.exe from $(NuGetExePath)
-->
<PropertyGroup>
<NuGetExePath Condition=" '$(NuGetExePath)'=='' ">$(localappdata)\LigerShark\AzureJobs\tools\nuget.exe</NuGetExePath>
<NuGetDownloadUrl Condition=" '$(NuGetDownloadUrl)'=='' ">http://nuget.org/nuget.exe</NuGetDownloadUrl>
</PropertyGroup>
<Target Name="GetNuget" Condition="!Exists('$(NuGetExePath)')">
<Message Text="Downloading nuget from [$(NuGetDownloadUrl)] to [$(NuGetExePath)]" Importance="high"/>
<ItemGroup>
<_nugetexeitem Include="$(NuGetExePath)" />
</ItemGroup>
<MakeDir Directories="@(_nugetexeitem->'%(RootDir)%(Directory)')"/>
<DownloadFile
Address="$(NuGetDownloadUrl)"
FileName="$(NuGetExePath)" />
</Target>
<PropertyGroup Condition=" '$(ls-msbuildtasks-path)'=='' ">
<ls-msbuildtasks-path>$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll</ls-msbuildtasks-path>
<ls-msbuildtasks-path Condition=" !Exists('$(ls-msbuildtasks-path)')">$(MSBuildFrameworkToolsPath)\Microsoft.Build.Tasks.v4.0.dll</ls-msbuildtasks-path>
<ls-msbuildtasks-path Condition=" !Exists('$(ls-msbuildtasks-path)')">$(windir)\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll</ls-msbuildtasks-path>
</PropertyGroup>
<UsingTask TaskName="DownloadFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(ls-msbuildtasks-path)">
<!-- http://stackoverflow.com/a/12739168/105999 -->
<ParameterGroup>
<Address ParameterType="System.String" Required="true"/>
<FileName ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System" />
<Code Type="Fragment" Language="cs">
<![CDATA[
new System.Net.WebClient().DownloadFile(Address, FileName);
]]>
</Code>
</Task>
</UsingTask>
</Project>
<#
.SYNOPSIS
You can use this to get the path of nuget.exe. If nuget.exe is not in
the $toolsDir specfied it will be downloaded from $nugetDownloadUrl.
To use this in your script just copy/paste the Get-NuGet function below
and then call Get-NuGet to return the path to the .exe. It will take care
of downloading it if it's missing.
#>
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\AzureJobs\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
<#
.SYNOPSIS
If nuget is not in the tools
folder then it will be downloaded there.
#>
function Get-Nuget{
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
process{
if(!(Test-Path $toolsDir)){
New-Item -Path $toolsDir -ItemType Directory -Force | Out-Null
}
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
'Downloading nuget.exe' | Write-Verbose
(New-Object System.Net.WebClient).DownloadFile($nugetDownloadUrl, $nugetDestPath)
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
}
}
# return the path of the file
$nugetDestPath
}
}
Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment