Skip to content

Commit

Permalink
refactor to permit swagger upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed May 9, 2020
1 parent f655943 commit ace64cd
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 40 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
deployNuget: '1'
deployNuget: '0'

steps:
- task: NuGetToolInstaller@1
Expand Down
6 changes: 3 additions & 3 deletions src/NetCore2Blockly/NetCore2Blockly/ActionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ private BindingSourceDefinition ConvertFromBindingSource(BindingSource bs)

};
}
Dictionary<string, (Type type, BindingSourceDefinition bs)> GetParameters(ApiParameterDescription[] parameterDescriptions)
Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)> GetParameters(ApiParameterDescription[] parameterDescriptions)
{
var desc = new Dictionary<string, (Type type, BindingSourceDefinition bs)>();
var desc = new Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)>();

if (parameterDescriptions?.Length == 0)
return desc;
Expand All @@ -64,7 +64,7 @@ private BindingSourceDefinition ConvertFromBindingSource(BindingSource bs)
.Where(parameterDescriptor => parameterDescriptor != null && okBindingSource.Contains(parameterDescriptor.BindingInfo?.BindingSource))
.Distinct()
.ToList()
.ForEach(x => desc.Add(x.Name, (x.ParameterType, ConvertFromBindingSource(x.BindingInfo?.BindingSource ?? BindingSource.Query))));
.ForEach(x => desc.Add(x.Name, (new TypeToGenerateFromCSharp( x.ParameterType), ConvertFromBindingSource(x.BindingInfo?.BindingSource ?? BindingSource.Query))));

if (parameterDescriptions.Length > desc.Count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ActionListExtensions
/// </summary>
/// <param name="list">The list.</param>
/// <returns></returns>
public static Type[] GetAllTypesWithNullBlocklyType(this List<ActionInfo> list)
public static TypeArgument[] GetAllTypesWithNullBlocklyType(this List<ActionInfo> list)
{
return list

Expand Down
4 changes: 2 additions & 2 deletions src/NetCore2Blockly/NetCore2Blockly/IActionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class ActionInfo
/// </summary>
public ActionInfo()
{
Params = new Dictionary<string, (Type type, BindingSourceDefinition bs)>();
Params = new Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)>();
}
/// <summary>
/// Gets or sets the name of the action.
Expand Down Expand Up @@ -77,7 +77,7 @@ public int CustomGetHashCode()
/// <value>
/// The parameters.
/// </value>
public Dictionary<string, (Type type, BindingSourceDefinition bs)> Params { get; set; }
public Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)> Params { get; set; }

/// <summary>
/// Gets a value indicating whether this instance has parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BlocklyDefinitionGenerator
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateBlocklyDefinition(Type type)
public string GenerateBlocklyDefinition(TypeArgument type)
{

if (type.ConvertibleToBlocklyType())
Expand All @@ -32,7 +32,7 @@ public string GenerateBlocklyDefinition(Type type)
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateDefinitionString(Type type)
public string GenerateDefinitionString(TypeArgument type)
{
if (type.IsEnum)
{
Expand All @@ -42,7 +42,7 @@ public string GenerateDefinitionString(Type type)

var tooltipAndpropsDef = GenerateTooltipAndPropDef(type);
var blocklyTypeName = type.TranslateToNewTypeName();
var typeName = type.Name;
var typeName = type.TypeNameForBlockly;



Expand Down Expand Up @@ -74,23 +74,25 @@ public string GenerateDefinitionString(Type type)
return definitionString;
}

private string GenerateDefinitionStringForEnum(Type type)
private string GenerateDefinitionStringForEnum(TypeArgument type)
{
string typeName = type.TypeNameForBlockly;
if (!type.IsEnum)
throw new ArgumentException($"type {type.Name} is not enum!");


var names = Enum.GetNames(type);


var opt = string.Join(",",
names.Select(it => $"['{it}', '{ValueEnum(Enum.Parse(type, it))}']")
);
type.GetValuesForEnum().Select(it => $"['{it.Key}', '{it.Value}']")
) ;

var def = $@"{Environment.NewLine}
Blockly.Blocks['{type.TranslateToNewTypeName()}'] = {{
init: function () {{
this.appendDummyInput()
.appendField('{type.Name}')
.appendField(new Blockly.FieldDropdown([{opt}]), 'val_{type.Name}');
.appendField('{typeName}')
.appendField(new Blockly.FieldDropdown([{opt}]), 'val_{typeName}');
this.setOutput(true, 'Number');
this.setTooltip('Enumeration {type.Name}');
Expand All @@ -115,12 +117,12 @@ private long ValueEnum(object o)
}
throw new ArgumentException("there is an enum that is not valid");
}
internal (string tooltip, string propsDef) GenerateTooltipAndPropDef(Type type)
internal (string tooltip, string propsDef) GenerateTooltipAndPropDef(TypeArgument type)
{
string tooltip = $"{type.Name} with props:";
string propsDef = "";

var validProperties = type.GetProperties().Where(prop => prop.GetSetMethod() != null);
var validProperties = type.GetProperties();



Expand All @@ -145,15 +147,16 @@ private long ValueEnum(object o)
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateJSstring(Type type)
public string GenerateJSstring(TypeArgument type)
{
var str = typeof(string).FullName;
if (type.IsEnum)
{
string typeName = type.TypeNameForBlockly;
return $@"
Blockly.JavaScript['{type.TranslateToNewTypeName()}'] = function(block) {{
var dropdown_name = block.getFieldValue('val_{type.Name}');
var dropdown_name = block.getFieldValue('val_{typeName}');
code = dropdown_name;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
Expand All @@ -163,12 +166,7 @@ public string GenerateJSstring(Type type)

var arr = typeof(IEnumerable);
var props = type.GetProperties()
.Select(prop => (prop, isArray:
(!prop.PropertyType.IsValueType)
&&
(prop.PropertyType.FullName != str)
&&
arr.IsAssignableFrom(prop.PropertyType)))
.Select(prop => (prop, isArray:prop.IsArray))
//.Select(it => (prop: it.prop, filter: (!it.isArray) ? "" : "+'.filter(it=>it !=null)'" ))
//TODO: previous line can filter empty arrays, e.g. post with empty elements
// however , raises an error in acorn. For the moment, not implemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public class BlocklyToolBoxFunctionDefinitionsGenerator
/// <returns></returns>
public string GenerateBlocklyToolBoxFunctionDefinitions(List<ActionInfo> actionList)
{
actionList.Sort((a, b) => {
var res = a.ControllerName.CompareTo(b.ControllerName);
if (res != 0)
return res;
res = a.Verb.CompareTo(b.Verb);
if (res != 0)
return res;
return a.ActionName.CompareTo(b.ActionName);
});
string blockText = "var blockTextLocalSiteFunctions='';";
foreach (var actionsGroupedByController in actionList.GroupBy(it => it.ControllerName))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ namespace NetCore2Blockly.JavascriptGeneration
/// </summary>
public class BlocklyToolBoxJSGenerator
{
private string typeNameNoGeneric(Type type)
{
var t = Nullable.GetUnderlyingType(type) ?? type;
return t.Name;
}

/// <summary>
/// Generates the blockly tool box value.
/// </summary>
/// <param name="types">The types.</param>
/// <returns></returns>
public string GenerateBlocklyToolBoxValue(Type[] types)
public string GenerateBlocklyToolBoxValue(TypeArgument[] types)
{
string blockText = "";
var globalVars = "var glbVar=function(workspace){";
foreach (var type in types)
{

var typeName = typeNameNoGeneric(type);
var typeName = type.TypeNameForBlockly;
var newTypeName = type.TranslateToNewTypeName();

globalVars += $"workspace.createVariable('var_{typeName}', '{newTypeName}');";
Expand Down Expand Up @@ -67,20 +63,21 @@ public string GenerateBlocklyToolBoxValue(Type[] types)
/// <param name="blockText">The block text.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateToolBoxCodeForAllPropertiesOfAType(string blockText, Type type)
public string GenerateToolBoxCodeForAllPropertiesOfAType(string blockText, TypeArgument type)
{
var validProperties = type.GetProperties().Where(prop => prop.GetSetMethod() != null);
var validProperties = type.GetProperties();

foreach (var property in type.GetProperties())
{
var propertyType = property.PropertyType;
if (!propertyType.ConvertibleToBlocklyType())
continue;


var typeName = type.TypeNameForBlockly;

blockText += createBlockShadowDef(property.Name, propertyType.TranslateToBlocklyBlocksType());

blockText += $"blockText_{type.Name} += blockTextLocalSiteFunctions;";
blockText += $"blockText_{typeName} += blockTextLocalSiteFunctions;";
}

return blockText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JavascriptGenerator
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateBlocklyDefinition(Type type)
public string GenerateBlocklyDefinition(TypeArgument type)
{
return _definitionGenerator.GenerateBlocklyDefinition(type);
}
Expand All @@ -40,7 +40,7 @@ public string GenerateBlocklyDefinition(Type type)
/// </summary>
/// <param name="types">The types.</param>
/// <returns></returns>
public string GenerateBlocklyToolBoxValue(Type[] types)
public string GenerateBlocklyToolBoxValue(TypeArgument[] types)
{
return _toolBoxJSGenerator.GenerateBlocklyToolBoxValue(types);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NetCore2Blockly/NetCore2Blockly/NetCore2Blockly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>NetCore2Blockly.xml</DocumentationFile>
<DocumentationFile></DocumentationFile>
<WarningsAsErrors></WarningsAsErrors>
</PropertyGroup>

Expand Down
Loading

0 comments on commit ace64cd

Please sign in to comment.