Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Azure/autorest into optional
Browse files Browse the repository at this point in the history
  • Loading branch information
amarzavery committed Apr 26, 2016
2 parents 3dd9e99 + 32d190e commit f82044b
Show file tree
Hide file tree
Showing 73 changed files with 2,708 additions and 288 deletions.
3 changes: 3 additions & 0 deletions AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public void OutputToSingleFile()
};

string path = Path.Combine(settings.OutputDirectory, "test.file.cs");
string existingContents = "this is dummy";
_fileSystem.VirtualStore[path] = new StringBuilder(existingContents);
var codeGenerator = new SampleCodeGenerator(settings);
codeGenerator.Generate(new ServiceClient()).GetAwaiter().GetResult();
Assert.DoesNotContain(existingContents, _fileSystem.VirtualStore[path].ToString());
Assert.Equal(4, _fileSystem.VirtualStore.Count);
Assert.True(_fileSystem.VirtualStore.ContainsKey(path));
Assert.True(_fileSystem.VirtualStore.ContainsKey("AutoRest.json"));
Expand Down
3 changes: 2 additions & 1 deletion AutoRest/AutoRest.Core/ClientModel/KnownPrimaryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum KnownPrimaryType
Boolean,
Credentials,
Uuid,
Base64Url
Base64Url,
UnixTime
}
}
21 changes: 12 additions & 9 deletions AutoRest/AutoRest.Core/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Rest.Generator
public abstract class CodeGenerator
{
public const string EnumObject = "x-ms-enum";
private bool firstTimeWriteSingleFile = true;

protected CodeGenerator(Settings settings)
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public async Task Write(ITemplate template, string fileName)
/// <returns></returns>
public async Task Write(string template, string fileName)
{
string relativeFilePath = null;
string filePath = null;

if (Settings.OutputFileName != null)
{
Expand All @@ -110,17 +111,19 @@ public async Task Write(string template, string fileName)
ErrorManager.ThrowErrors();
}

relativeFilePath = Settings.OutputFileName;
filePath = Path.Combine(Settings.OutputDirectory, Settings.OutputFileName);

if (firstTimeWriteSingleFile)
{
// for SingleFileGeneration clean the file before writing only if its the first time
Settings.FileSystem.DeleteFile(filePath);
firstTimeWriteSingleFile = false;
}
}
else
{
relativeFilePath = fileName;
}
string filePath = Path.Combine(Settings.OutputDirectory, relativeFilePath);

// cleans file before writing unless single file
if (!(Settings.OutputFileName != null && IsSingleFileGenerationSupported))
{
filePath = Path.Combine(Settings.OutputDirectory, fileName);
// cleans file before writing
Settings.FileSystem.DeleteFile(filePath);
}
// Make sure the directory exist
Expand Down
51 changes: 50 additions & 1 deletion AutoRest/AutoRest.Core/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static string EscapeXmlComment(this string comment)
}

/// <summary>
/// Returns true is the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
/// Returns true if the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
Expand All @@ -263,5 +263,54 @@ public static bool IsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
}
return false;
}

/// <summary>
/// Returns true if the <paramref name="type"/> is a PrimaryType with KnownPrimaryType matching <paramref name="typeToMatch"/>
/// or a DictionaryType with ValueType matching <paramref name="typeToMatch"/> or a SequenceType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsOrContainsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
{
if (type == null)
{
return false;
}

if (type.IsPrimaryType(typeToMatch) ||
type.IsDictionaryContainingType(typeToMatch) ||
type.IsSequenceContainingType(typeToMatch))
{
return true;
}
return false;
}

/// <summary>
/// Returns true if the <paramref name="type"/> is a DictionaryType with ValueType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsDictionaryContainingType(this IType type, KnownPrimaryType typeToMatch)
{
DictionaryType dictionaryType = type as DictionaryType;
PrimaryType dictionaryPrimaryType = dictionaryType?.ValueType as PrimaryType;
return dictionaryPrimaryType != null && dictionaryPrimaryType.IsPrimaryType(typeToMatch);
}

/// <summary>
/// Returns true if the <paramref name="type"/>is a SequenceType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsSequenceContainingType(this IType type, KnownPrimaryType typeToMatch)
{
SequenceType sequenceType = type as SequenceType;
PrimaryType sequencePrimaryType = sequenceType?.ElementType as PrimaryType;
return sequencePrimaryType != null && sequencePrimaryType.IsPrimaryType(typeToMatch);
}
}
}
5 changes: 4 additions & 1 deletion AutoRest/AutoRest.Core/Utilities/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@

using System.IO;
using System.Net;
using System.Text;

namespace Microsoft.Rest.Generator.Utilities
{
public class FileSystem : IFileSystem
{
public void WriteFile(string path, string contents)
{
File.WriteAllText(path, contents);
File.WriteAllText(path, contents, Encoding.UTF8);
}

public string ReadFileAsText(string path)
{
using (var client = new WebClient())
{
client.Headers.Add("User-Agent: AutoRest");
client.Encoding = Encoding.UTF8;
return client.DownloadString(path);
}
}
Expand Down
3 changes: 2 additions & 1 deletion AutoRest/AutoRest/AutoRest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="..\..\Tools\AssemblyVersionInfo.cs">
<Link>Properties\AssemblyVersionInfo.cs</Link>
</Compile>
<Compile Include="ExitCode.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="HelpExample.cs" />
<Compile Include="HelpGenerator.cs" />
Expand Down Expand Up @@ -70,4 +71,4 @@
</Content>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
14 changes: 14 additions & 0 deletions AutoRest/AutoRest/ExitCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace Microsoft.Rest.Generator.Cli
{
/// <summary>
/// Available exit codes.
/// </summary>
public enum ExitCode : int
{
Success = 0,
Error = 1
}
}
6 changes: 5 additions & 1 deletion AutoRest/AutoRest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ namespace Microsoft.Rest.Generator.Cli
{
internal class Program
{
private static void Main(string[] args)
private static int Main(string[] args)
{
int exitCode = (int)ExitCode.Error;

try
{
Settings settings = null;
Expand Down Expand Up @@ -69,6 +71,7 @@ private static void Main(string[] args)
{
Console.WriteLine(Resources.GenerationComplete,
settings.CodeGenerator, settings.Input);
exitCode = (int)ExitCode.Success;
}
}

Expand All @@ -91,6 +94,7 @@ private static void Main(string[] args)
Console.Error.WriteLine(Resources.ConsoleErrorMessage, exception.Message);
Console.Error.WriteLine(Resources.ConsoleErrorStackTrace, exception.StackTrace);
}
return exitCode;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ public void LroSadPathTests()
Assert.Equal("Error from the server", exception.Body.Message);
Assert.NotNull(exception.Request);
Assert.NotNull(exception.Response);
exception =
Assert.Throws<CloudException>(() => client.LROSADs.PutNonRetry201Creating400InvalidJson(new Product { Location = "West US" }));
Assert.Null(exception.Body);
Assert.Equal("Long running operation failed with status 'BadRequest'.", exception.Message);
exception =
Assert.Throws<CloudException>(
() => client.LROSADs.PutAsyncRelativeRetry400(new Product {Location = "West US"}));
Expand Down Expand Up @@ -527,8 +531,7 @@ public void AzureODataTests()
Top = 10,
OrderBy = "id"
};
var filterString = Uri.EscapeDataString("id gt 5 and name eq 'foo'");
Assert.Equal(string.Format("$filter={0}&$orderby=id&$top=10", filterString), filter.ToString());
Assert.Equal("$filter=id gt 5 and name eq 'foo'&$orderby=id&$top=10", filter.ToString());
client.Odata.GetWithFilter(filter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ public partial interface ILROSADsOperations
/// </param>
Task<AzureOperationResponse<Product>> BeginPutNonRetry201Creating400WithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse<Product>> PutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse<Product>> BeginPutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Long running put request, service returns a 200 with
/// ProvisioningState=’Creating’. Poll the endpoint indicated in the
/// Azure-AsyncOperation header for operation status
Expand Down
Loading

0 comments on commit f82044b

Please sign in to comment.