Created packaging files for Nuget and Chocolatey (#2281)

* scripts for packaging a nuget package

* added chocolatey packaging

* Updated nuget package version to 1.0.1
This commit is contained in:
Garrett Serack 2017-05-22 14:31:15 -07:00 коммит произвёл GitHub
Родитель 75f5fadab2
Коммит 17a80f79e6
12 изменённых файлов: 380 добавлений и 1 удалений

5
.gitignore поставляемый
Просмотреть файл

@ -215,4 +215,7 @@ src/extension/old/**/*
src/bootstrapper
src/extension/out
src/next-gen
src/next-gen
package/nuget/tools
package/chocolatey/*.nupkg

Просмотреть файл

@ -0,0 +1,21 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>autorest</id>
<version>1.0.0</version>
<title>AutoRest</title>
<authors>Microsoft</authors>
<owners>azure-sdk, Microsoft</owners>
<licenseUrl>https://raw.githubusercontent.com/Microsoft/dotnet/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/Azure/AutoRest</projectUrl>
<iconUrl>https://github.com/Azure/autorest/raw/master/docs/images/logo.png</iconUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>The AutoRest tool generates client libraries for accessing REST APIs that are described using the Swagger format.</description>
<summary>AutoRest code generation of REST API client libraries.</summary>
<copyright>Copyright (c) Microsoft Corporation</copyright>
<tags>Microsoft AutoRest</tags>
<dependencies>
<dependency id="nodejs.install" version="[7.10,)" />
</dependencies>
</metadata>
</package>

Просмотреть файл

@ -0,0 +1 @@
npm install -g autorest

Просмотреть файл

@ -0,0 +1 @@
npm uninstall -g autorest

210
package/nuget/Program.cs Normal file
Просмотреть файл

@ -0,0 +1,210 @@
using System;
using System.Diagnostics;
using Microsoft.DotNet.PlatformAbstractions;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace autorest_nuget
{
class Program
{
private static string Os
{
get
{
switch (RuntimeEnvironment.OperatingSystemPlatform)
{
case Platform.Windows:
return "win";
case Platform.Linux:
return "linux";
case Platform.Darwin:
return "osx";
default:
return "unknown";
}
}
}
public static string EscapeAndConcatenateArgArrayForProcessStart(IEnumerable<string> args)
{
return string.Join(" ", EscapeArgArray(args));
}
private static IEnumerable<string> EscapeArgArray(IEnumerable<string> args)
{
var escapedArgs = new List<string>();
foreach (var arg in args)
{
escapedArgs.Add(EscapeArg(arg));
}
return escapedArgs;
}
private static string EscapeArg(string arg)
{
var sb = new StringBuilder();
var quoted = ShouldSurroundWithQuotes(arg);
if (quoted) sb.Append("\"");
for (int i = 0; i < arg.Length; ++i)
{
var backslashCount = 0;
// Consume All Backslashes
while (i < arg.Length && arg[i] == '\\')
{
backslashCount++;
i++;
}
// Escape any backslashes at the end of the arg
// This ensures the outside quote is interpreted as
// an argument delimiter
if (i == arg.Length)
{
sb.Append('\\', 2 * backslashCount);
}
// Escape any preceding backslashes and the quote
else if (arg[i] == '"')
{
sb.Append('\\', (2 * backslashCount) + 1);
sb.Append('"');
}
// Output any consumed backslashes and the character
else
{
sb.Append('\\', backslashCount);
sb.Append(arg[i]);
}
}
if (quoted) sb.Append("\"");
return sb.ToString();
}
internal static bool ShouldSurroundWithQuotes(string argument)
{
// Don't quote already quoted strings
if (argument.StartsWith("\"", StringComparison.Ordinal) &&
argument.EndsWith("\"", StringComparison.Ordinal))
{
return false;
}
// Only quote if whitespace exists in the string
if (argument.Contains(" ") || argument.Contains("\t") || argument.Contains("\n"))
{
return true;
}
return false;
}
private static string AutoRestPath
{
get
{
var current = path;
string newPath = string.Empty;
while (!string.IsNullOrEmpty(current))
{
if (System.IO.Directory.Exists(System.IO.Path.GetFullPath($"{current}/node_modules/autorest")))
{
return System.IO.Path.GetFullPath($"{current}/node_modules/autorest/app.js");
}
newPath = System.IO.Directory.GetParent(current)?.FullName;
if (newPath == current)
{
return null;
}
current = newPath;
}
return null;
}
}
private static string NodePath
{
get
{
var current = path;
string newPath = string.Empty;
while (!string.IsNullOrEmpty(current))
{
if (System.IO.Directory.Exists(System.IO.Path.GetFullPath($"{current}/tools/{Os}-{RuntimeEnvironment.RuntimeArchitecture}")))
{
return System.IO.Path.GetFullPath($"{current}/tools/{Os}-{RuntimeEnvironment.RuntimeArchitecture}/node");
}
newPath = System.IO.Directory.GetParent(current)?.FullName;
if (newPath == current)
{
return null;
}
current = newPath;
}
return null;
}
}
private static string path = new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase).LocalPath;// Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
static void Main(string[] args)
{
Process P = null;
Console.CancelKeyPress += new ConsoleCancelEventHandler((sender, e) =>
{
if (P != null && !P.HasExited)
{
P.Kill();
}
Environment.Exit(0);
});
var rgs = string.Empty;
try
{
var nodePath = NodePath;
if (string.IsNullOrEmpty(nodePath))
{
Console.Error.WriteLine($"Unable to find node.js executable in the tools path (should contain `tools/{Os}-{RuntimeEnvironment.RuntimeArchitecture}`) ");
Environment.Exit(1);
}
var autorest = AutoRestPath;
if (string.IsNullOrEmpty(autorest))
{
Console.Error.WriteLine($"Unable to find autorest/app.js in the node_modules path");
Environment.Exit(2);
}
rgs = EscapeAndConcatenateArgArrayForProcessStart(new[] { autorest }.Concat(args));
P = System.Diagnostics.Process.Start(new ProcessStartInfo(NodePath)
{
Arguments = rgs
});
P.WaitForExit();
Environment.Exit(P.ExitCode);
}
catch
{
// who cares what went wrong.
}
if (P != null && !P.HasExited)
{
P.Kill();
}
Console.Error.WriteLine($"Error attempting to run AutoRest.\n> {NodePath} {rgs}");
Environment.Exit(3);
}
}
}

7
package/nuget/build.ps1 Normal file
Просмотреть файл

@ -0,0 +1,7 @@
cmd /c rmdir /s /q "$ENV:HOME\.nuget\packages\.tools\autorest\"
cmd /c rmdir /s /q "$ENV:HOME\.nuget\packages\autorest\"
dotnet build --configuration release
dotnet publish --configuration release
copy bin/release/netcoreapp1.0/publish/* tools/
dotnet pack --configuration release

Просмотреть файл

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageType>DotnetCliTool</PackageType>
<IsTool>false</IsTool>
<ContentTargetFolders>.</ContentTargetFolders>
<PackageVersion>1.0.1</PackageVersion>
<PackageId>AutoRest</PackageId>
<Title>AutoRest Code Generation Tool</Title>
<Authors>Microsoft, Azure-SDK</Authors>
<Description>The AutoRest tool generates client libraries for accessing RESTful web services. Input to AutoRest is a spec that describes the REST API using the Open API Initiative format.</Description>
<Copyright>(C) 2017 Microsoft Corporation</Copyright>
<PackageLicenseUrl>https://github.com/Azure/autorest/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://aka.ms/autorest</PackageProjectUrl>
<PackageTags>Swagger;OpenAPI;AutoRest;CodeGenerator</PackageTags>
<TargetFramework>netcoreapp1.0</TargetFramework>
<AssemblyName>dotnet-autorest</AssemblyName>
<OutputType>Exe</OutputType>
<RuntimeIdentifiers>win7-x64;win7-x86;osx.10.10-x64;osx.10.11-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;centos.7-x64;rhel.7.2-x64;debian.8-x64;fedora.23-x64;opensuse.13.2-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="1.0.3"/>
<Content Include="node_modules/**/*" />
<Content Include="tools/**" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,85 @@
<#
.SYNOPSIS
Downloads build tools and a particular version of Node.js
for purposes of later building a package.
.PARAMETER Version
The version of Node.js to download.
#>
$progressPreference = 'silentlyContinue'
$Version = "7.10.0"
$DistRootUri = "https://nodejs.org/dist/v$Version"
$LayoutRoot = "$PSScriptRoot\"
if (!(Test-Path $LayoutRoot)) { $null = mkdir $LayoutRoot }
function Expand-ZIPFile($file, $destination) {
$shell = new-object -com shell.application
$zip = $shell.NameSpace((Resolve-Path $file).Path)
foreach ($item in $zip.items()) {
$shell.Namespace((Resolve-Path $destination).Path).copyhere($item)
}
}
if (!(Test-Path $PSScriptRoot\obj)) { $null = mkdir $PSScriptRoot\obj }
$unzipTool = "$PSScriptRoot\obj\7z\7za.exe"
if (!(Test-Path $unzipTool)) {
$zipToolArchive = "$PSScriptRoot\obj\7za920.zip"
if (!(Test-Path $zipToolArchive)) {
Invoke-WebRequest -Uri http://7-zip.org/a/7za920.zip -OutFile $zipToolArchive
}
if (!(Test-Path $PSScriptRoot\obj\7z)) { $null = mkdir $PSScriptRoot\obj\7z }
Expand-ZIPFile -file $zipToolArchive -destination $PSScriptRoot\obj\7z
}
Function Get-NetworkFile {
Param(
[uri]$Uri,
[string]$OutDir
)
if (!(Test-Path $OutDir)) {
$null = mkdir $OutDir
}
$OutFile = Join-Path $OutDir $Uri.Segments[$Uri.Segments.Length - 1]
if (!(Test-Path $OutFile)) {
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
}
$OutFile
}
Function Get-NixNode($os, $arch, $osBrand) {
$tgzPath = Get-NetworkFile -Uri $DistRootUri/node-v$Version-$os-$arch.tar.gz -OutDir "$PSScriptRoot\obj"
$tarName = [IO.Path]::GetFileNameWithoutExtension($tgzPath)
$tarPath = Join-Path $PSScriptRoot\obj $tarName
$null = & $unzipTool -y -o"$PSScriptRoot\obj" e $tgzPath $tarName
$null = & $unzipTool -y -o"$PSScriptRoot\obj" e $tarPath "node-v$Version-$os-$arch\bin\node"
if (!$osBrand) { $osBrand = $os }
$targetDir = "$LayoutRoot\tools\$osBrand-$arch"
if (!(Test-Path $targetDir)) {
$null = mkdir $targetDir
}
Copy-Item $PSScriptRoot\obj\node $targetDir
Remove-Item $PSScriptRoot\obj\node
}
Function Get-WinNode($arch) {
$nodePath = Get-NetworkFile -Uri https://nodejs.org/dist/v$Version/win-$arch/node.exe -OutDir "$PSScriptRoot\obj\win-$arch-$Version"
$targetDir = "$LayoutRoot\tools\win-$arch"
if (!(Test-Path $targetDir)) {
$null = mkdir $targetDir
}
Copy-Item $nodePath $targetDir
}
Get-NixNode 'linux' x64
Get-NixNode 'linux' x86
Get-NixNode 'darwin' x64 -osBrand 'osx'
Get-WinNode x86
Get-WinNode x64
npm install

Просмотреть файл

@ -0,0 +1,14 @@
{
"name": "autorest-nuget",
"version": "1.0.1",
"description": "AutoRest Nuget Package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"autorest": "^0.13.3"
}
}

Просмотреть файл

@ -0,0 +1,6 @@
cmd /c rmdir /s /q bin obj node_modules
cmd /c rmdir /s /q "$ENV:HOME\.nuget\packages\.tools\autorest\"
cmd /c rmdir /s /q "$ENV:HOME\.nuget\packages\autorest\"
dotnet restore
./get-node.ps1

Просмотреть файл

@ -0,0 +1,2 @@
#!/bin/sh
dotnet "`dirname $0`/dotnet-autorest.dll" $*

Просмотреть файл

@ -0,0 +1 @@
@dotnet %~dp0/dotnet-autorest.dll %*