FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add a new option "--add-stubs-suffix" to add a "-stubs" suffix to each package by long0900 · Pull Request #1 · mlidbom/pythonnet-stub-generator · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 0 additions & 2 deletions csharp/PythonNetStubGenerator/StaticPythonTypes.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -215,7 +214,6 @@ public static string ToPythonType(this Type t, bool withGenericParams = true)
cleanName = "typing.Optional";
}


cleanName = $"{cleanName}[{pythonTypeArgs}]";
}
}
Expand Down
28 changes: 21 additions & 7 deletions csharp/PythonNetStubGenerator/StaticStubBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public static class StaticStubBuilder
private static HashSet<DirectoryInfo> SearchPaths { get; } = new HashSet<DirectoryInfo>();
private static HashSet<string> TargetAssemblyNames { get; } = new HashSet<string>();

public static DirectoryInfo BuildAssemblyStubs(DirectoryInfo destPath, FileInfo[] targetAssemblyPaths, DirectoryInfo[] searchPaths = null, bool onlyTargetTypes = false)
public static DirectoryInfo BuildAssemblyStubs(
DirectoryInfo destPath,
FileInfo[] targetAssemblyPaths,
DirectoryInfo[] searchPaths = null,
bool onlyTargetTypes = false,
bool addStubSuffix = false)
{
// prepare resolver
AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolve;
Expand Down Expand Up @@ -87,14 +92,18 @@ public static DirectoryInfo BuildAssemblyStubs(DirectoryInfo destPath, FileInfo[
typesToGenerate = types.ToList();
}

WriteStub(destPath, nameSpace, typesToGenerate);
WriteStub(destPath, nameSpace, typesToGenerate, addStubSuffix);
}


return destPath;
}

internal static void WriteStub(DirectoryInfo rootDirectory, string nameSpace, IEnumerable<Type> stubTypes)
internal static void WriteStub(
DirectoryInfo rootDirectory,
string nameSpace,
IEnumerable<Type> stubTypes,
bool addStubSuffix)
{
// sort the stub list so we get consistent output over time
var orderedTypes = stubTypes.OrderBy(it => it.Name);
Expand All @@ -107,14 +116,20 @@ internal static void WriteStub(DirectoryInfo rootDirectory, string nameSpace, IE
}
else
{
var split = nameSpace.Split('.');
var parts = nameSpace.Split('.');

if (split[0] == "global_")
if (parts[0] == "global_")
{
throw new InvalidDataException("The namespace \"global_\" is reserved.");
}

path = split.Aggregate(rootDirectory.FullName, Path.Combine);
// add "-stubs" suffix to module directory
if (addStubSuffix)
{
parts[0] += "-stubs";
}

path = parts.Aggregate(rootDirectory.FullName, Path.Combine);
}

if (!Directory.Exists(path))
Expand All @@ -126,7 +141,6 @@ internal static void WriteStub(DirectoryInfo rootDirectory, string nameSpace, IE

var stubText = StaticStubWriter.GetStub(nameSpace, orderedTypes);


File.WriteAllText(path, stubText);
}

Expand Down
7 changes: 1 addition & 6 deletions csharp/PythonNetStubGenerator/StaticStubWriter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static string GetStub(string nameSpace, IEnumerable<Type> stubTypes)
{
foreach (var typeGroup in typeGroups)
WriteTypeGroup(sb, typeGroup.Key, typeGroup);

}
var body = sb.ToString();

Expand Down Expand Up @@ -238,17 +237,13 @@ public static void WriteType(StringBuilder sb, Type type, string classNameOverri

var className = classNameOverride ?? type.CleanName();



var typeArguments = new List<Type>();

if (type.IsGenericTypeDefinition)
{
typeArguments.AddRange(type.GetGenericArguments());
}



using (var classScope = new StaticClassScope(className, typeArguments, typeArguments.Any()))
{

Expand Down Expand Up @@ -979,7 +974,7 @@ private static string ConvertOperatorName(string methodName)

private static string GetParameters(MethodBase method, bool includeSelf)
{
string GetParameter(ParameterInfo it)
static string GetParameter(ParameterInfo it)
{
var name = StaticPythonTypes.SafePythonName(it.Name);
var type = it.ParameterType.ToPythonType();
Expand Down
23 changes: 15 additions & 8 deletions csharp/PythonNetStubTool/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static int Main(string[] args)

Option<string> targetDllsOption = new("--target-dlls")
{
Description = "Target DLLs.",
Description = "Target DLLs, separated by commas.",
Required = true
};

Expand All @@ -33,20 +33,27 @@ static int Main(string[] args)
Required = false
};

Option<bool> addStubSuffixOption = new("--add-stubs-suffix")
{
Description = "Add a '-stubs' suffix to generated stub folders (for Pycharm).",
Required = false
};

RootCommand rootCommand = new("PythonNet Stub Generator Tool");
rootCommand.Options.Add(destPathOption);
rootCommand.Options.Add(targetDllsOption);
rootCommand.Options.Add(searchPathsOption);
rootCommand.Options.Add(onlyTargetTypesOption);
rootCommand.Options.Add(addStubSuffixOption);

rootCommand.SetAction(parseResult =>
{
DirectoryInfo destPath = parseResult.GetValue(destPathOption)!;
string targetDlls = parseResult.GetValue(targetDllsOption)!;
DirectoryInfo[]? searchPaths = parseResult.GetValue(searchPathsOption);
bool onlyTargetTypes = parseResult.GetValue(onlyTargetTypesOption);

return Run(destPath, targetDlls, searchPaths, onlyTargetTypes);
bool addStubSuffix = parseResult.GetValue(addStubSuffixOption);
return Run(destPath, targetDlls, searchPaths, onlyTargetTypes, addStubSuffix);
});

ParseResult parseResult = rootCommand.Parse(args);
Expand All @@ -58,12 +65,13 @@ static int Main(string[] args)
/// </summary>
/// <param name="destPath">Path to save the subs to.</param>
/// <param name="searchPaths">Path to search for referenced assemblies</param>
/// <param name="targetDlls">Target DLLsz</param>
/// <param name="targetDlls">Target DLLs, separated by commas.</param>
static int Run(
DirectoryInfo destPath,
string targetDlls,
DirectoryInfo[]? searchPaths = null,
bool onlyTargetTypes = false
bool onlyTargetTypes = false,
bool addStubSuffix = false
)
{
if (searchPaths != null)
Expand All @@ -75,21 +83,20 @@ static int Run(
var infos = new List<FileInfo>();
foreach (var pathStr in targetDlls.Split(','))
{
var assemblyPath = new FileInfo(pathStr);
var assemblyPath = new FileInfo(pathStr.Trim());
if (!assemblyPath.Exists)
{
Console.WriteLine($"error: can not find {assemblyPath}");
return -1;
}
infos.Add(assemblyPath);

}

Console.WriteLine($"building stubs...");

try
{
var dest = StaticStubBuilder.BuildAssemblyStubs(destPath, infos.ToArray(), searchPaths, onlyTargetTypes);
var dest = StaticStubBuilder.BuildAssemblyStubs(destPath, infos.ToArray(), searchPaths, onlyTargetTypes, addStubSuffix);
Console.WriteLine($"stubs saved to {dest}");
return 0;
}
Expand Down

Back | FazBrowse Home | New Git URL