Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
meancrazy committed Oct 9, 2015
1 parent 2e20216 commit 123bd1d
Show file tree
Hide file tree
Showing 23 changed files with 5,332 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,4 @@ FakesAssemblies/

# Visual Studio 6 workspace options file
*.opt
/*.snk
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>
522 changes: 522 additions & 0 deletions Builder/CodeGenerationContext.cs

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions Builder/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace OData4.Builder
{
public class Configuration
{
// The URI of the metadata document. The value must be set to a valid service document URI or a local file path
// eg : "http://services.odata.org/V4/OData/OData.svc/", "File:///C:/Odata.edmx", or @"C:\Odata.edmx"
// ### Notice ### If the OData service requires authentication for accessing the metadata document, the value of
// MetadataDocumentUri has to be set to a local file path, or the client code generation process will fail.
public string MetadataDocumentUri { get; }

// The use of DataServiceCollection enables entity and property tracking. The value must be set to true or false.
public bool UseDataServiceCollection => true;

// The namespace of the client code generated. It replaces the original namespace in the metadata document,
// unless the model has several namespaces.
public string NamespacePrefix { get; }

// This flag indicates whether to enable naming alias. The value must be set to true or false.
public bool EnableNamingAlias => false;

// This flag indicates whether to ignore unexpected elements and attributes in the metadata document and generate
// the client code if any. The value must be set to true or false.
public bool IgnoreUnexpectedElementsAndAttributes => true;

public Configuration(string uri, string namespacePrefix)
{
MetadataDocumentUri = uri;
NamespacePrefix = namespacePrefix;
}
}
}
51 changes: 51 additions & 0 deletions Builder/Customization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Globalization;

namespace OData4.Builder
{
public static class Customization
{
/// <summary>
/// Changes the text to use upper camel case, which upper case for the first character.
/// </summary>
/// <param name="text">Text to convert.</param>
/// <returns>The converted text in upper camel case</returns>
internal static string CustomizeNaming(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}

if (text.Length == 1)
{
return char.ToUpperInvariant(text[0]).ToString(CultureInfo.InvariantCulture);
}

return char.ToUpperInvariant(text[0]) + text.Substring(1);
}

/// <summary>
/// Changes the namespace to use upper camel case, which upper case for the first character of all segments.
/// </summary>
/// <param name="fullNamespace">Namespace to convert.</param>
/// <returns>The converted namespace in upper camel case</returns>
internal static string CustomizeNamespace(string fullNamespace)
{
if (string.IsNullOrEmpty(fullNamespace))
{
return fullNamespace;
}

var segs = fullNamespace.Split('.');
var upperNamespace = string.Empty;
var n = segs.Length;
for (var i = 0; i < n; ++i)
{
upperNamespace += CustomizeNaming(segs[i]);
upperNamespace += (i == n - 1 ? string.Empty : ".");
}

return upperNamespace;
}
}
}
100 changes: 100 additions & 0 deletions Builder/ODataClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;

namespace OData4.Builder
{
/*
OData Client T4 Template ver. 2.4.0
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
internal class ODataClient
{
private readonly Configuration _configuration;

public ODataClient(Configuration configuration)
{
_configuration = configuration;
MetadataDocumentUri = configuration.MetadataDocumentUri;
NamespacePrefix = configuration.NamespacePrefix;
}

public string GenerateCode()
{
var context = new CodeGenerationContext(new Uri(MetadataDocumentUri, UriKind.Absolute), NamespacePrefix)
{
UseDataServiceCollection = _configuration.UseDataServiceCollection,
EnableNamingAlias = _configuration.EnableNamingAlias,
IgnoreUnexpectedElementsAndAttributes = _configuration.IgnoreUnexpectedElementsAndAttributes
};

var template = new ODataClientCSharpTemplate(context);
return template.TransformText();
}

/// <summary>
/// The Uri string to the metadata document.
/// </summary>
public string MetadataDocumentUri
{
get
{
return _metadataDocumentUri;
}

set
{
value = Uri.UnescapeDataString(value);
Uri uri;
if (!Uri.TryCreate(value, UriKind.Absolute, out uri))
{
// ********************************************************************************************************
// To fix this error, if the current text transformation is run by the TextTemplatingFileGenerator
// custom tool inside Visual Studio, update the .odata.config file in the project with a valid parameter
// value then hit Ctrl-S to save the .tt file to refresh the code generation.
// ********************************************************************************************************
throw new ArgumentException($"The value \"{value}\" is not a valid MetadataDocumentUri because is it not a valid absolute Uri. The MetadataDocumentUri must be set to an absolute Uri referencing the $metadata endpoint of an OData service.");
}

if (uri.Scheme == "http" || uri.Scheme == "https")
{
value = uri.Scheme + "://" + uri.Authority + uri.AbsolutePath;
value = value.TrimEnd('/');
if (!value.EndsWith("$metadata"))
{
value += "/$metadata";
}
}

_metadataDocumentUri = value;
}
}

private string _metadataDocumentUri;

/// <summary>
/// The NamespacePrefix is used as the only namespace for types in the same namespace as the default container,
/// and as a prefix for the namespace from the model for everything else. If this argument is null, the
/// namespaces from the model are used for all types.
/// </summary>
public string NamespacePrefix
{
get
{
return _namespacePrefix;
}

set
{
_namespacePrefix = string.IsNullOrWhiteSpace(value) ? null : value;
}
}

private string _namespacePrefix;
}
}
Loading

0 comments on commit 123bd1d

Please sign in to comment.