Skip to content

Commit

Permalink
Add *.cs to ProjectItemsScheam. Add bindings folder to new solproj te…
Browse files Browse the repository at this point in the history
…mplate. Generate bindings on build.
  • Loading branch information
allisterb committed Dec 10, 2024
1 parent 9851237 commit 604ffa2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 12 deletions.
74 changes: 63 additions & 11 deletions src/Stratis.VS.SolidityProjectBuildTasks/CompileContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class CompileContracts : Task

public override bool Execute()
{

var solcpath = File.Exists(Path.Combine(ProjectDir, "node_modules", "solc", "solc.js")) ? Path.Combine(ProjectDir, "node_modules", "solc", "solc.js") :
Path.Combine(ExtDir, "node_modules", "solc", "solc.js");
var cmdline = "node \"" + solcpath + "\" --standard-json --base-path=\"" + ProjectDir + "\"" + " --include-path=\"" + Path.Combine(ProjectDir, "node_modules") + "\"";
Expand Down Expand Up @@ -183,16 +182,44 @@ public override bool Execute()
}
if (cs.evm.gasEstimates != null)
{
File.WriteAllText(Path.Combine(outputdir, c.Key + ".gas.txt"), Deserialize(cs.evm.gasEstimates));
File.WriteAllText(Path.Combine(outputdir, c.Key + ".gas.txt"), Serialize(cs.evm.gasEstimates));
}
if (cs.abi != null)
{
File.WriteAllText(Path.Combine(outputdir, c.Key + ".abi"), Deserialize(cs.abi));
File.WriteAllText(Path.Combine(outputdir, c.Key + ".abi"), Serialize(cs.abi));
}
}
}

if (!InstallNethereumGeneratorToolIfNotPresent())
{
Log.LogError("Cannot generate .NET bindings for Solidity project.");
return true;
}
if (!Directory.Exists(Path.Combine(ProjectDir, "bindings")))
{
Directory.CreateDirectory(Path.Combine(ProjectDir, "bindings"));
}

foreach (var c in output.contracts)
{
var cs = c.Value.Values.First();
if (Sources.ContainsKey(c.Key))
{
if (cs.evm.bytecode._object != null && cs.abi != null)
{
if (CheckRunCmdOutput(RunCmd("cmd.exe", $"/c dotnet Nethereum.Generator.Console generate from-abi -abi {Path.Combine(outputdir, c.Key + ".abi")} -bin {Path.Combine(outputdir, c.Key + ".bin")} -o bindings -ns Ethereum", ProjectDir), ""))
{
Log.LogMessage(MessageImportance.High, $"Created .NET bindings for {c.Key} contract at {Path.Combine(ProjectDir, "bindings", c.Key)}.");
}
else
{
Log.LogError($"Could not create .NET bindings for {c.Key} contract.");
}
}
}
}

InstallNethereumGeneratorToolIfNotPresent();
return true;
}

Expand Down Expand Up @@ -255,12 +282,29 @@ protected bool InstallSolidityLanguageServer()

protected bool InstallNethereumGeneratorToolIfNotPresent()
{
//Log.LogMessage(MessageImportance.High, $"Install Nethereum generator tool...");
//var output = RunCmd("cmd.exe", "dotnet tool install Nethereum.Generator.Console", ProjectDir);
Log.LogMessage(MessageImportance.High, $"Installing .NET tool manifest...");
if (!File.Exists(Path.Combine(ProjectDir, ".config", "dotnet-tools.json")))
{
if (!CheckRunCmdOutput(RunCmd("cmd.exe", "/c dotnet new tool-manifest", ProjectDir), "was created successfully"))
{
Log.LogError("Could not install .NET tools manifest in " + ProjectDir);
return false;
}
}
if (!CheckRunCmdOutput(RunCmd("cmd.exe", "/c dotnet tool list", ProjectDir), "nethereum.generator.console"))
{
Log.LogMessage(MessageImportance.High, $"Installing Nethereum generator .NET tool...");
if (!CheckRunCmdOutput(RunCmd("cmd.exe", "/c dotnet tool install Nethereum.Generator.Console", ProjectDir), "successfully installed"))
{
Log.LogError("Could not install Nethereum.Generator.Console .NET tool in " + ProjectDir);
return false;
}
}

return true;
}

public static Dictionary<string, object> RunCmd(string filename, string arguments, string workingdirectory)
public Dictionary<string, object> RunCmd(string filename, string arguments, string workingdirectory)
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = filename;
Expand All @@ -276,6 +320,7 @@ public static Dictionary<string, object> RunCmd(string filename, string argument
process.StartInfo = info;
try
{
Log.LogCommandLine($"{filename} {arguments}");
if (!process.Start())
{
output["error"] = ("Could not start {file} {args} in {dir}.", info.FileName, info.Arguments, info.WorkingDirectory);
Expand Down Expand Up @@ -325,7 +370,7 @@ public bool CheckRunCmdOutput(Dictionary<string, object> output, string checktex
if (output.ContainsKey("stderr"))
{
var stderr = (string)output["stderr"];
Log.LogMessage(MessageImportance.Low, stderr);
Log.LogMessage(MessageImportance.High, stderr);
}
if (output.ContainsKey("stdout"))
{
Expand All @@ -340,6 +385,10 @@ public bool CheckRunCmdOutput(Dictionary<string, object> output, string checktex
return false;
}
}
else if (checktext == "" && !output.ContainsKey("stdout"))
{
return true;
}
else
{
return false;
Expand Down Expand Up @@ -368,11 +417,14 @@ public static byte[] FromHexString(string input)
return dest;
}

public string Deserialize<T>(T obj)
public string Serialize<T>(T obj)
{
var sb = new StringBuilder();
CompactJson.Serializer.Write(obj, new StringWriter(sb), true);
return sb.ToString();
using (var sw = new StringWriter(sb))
{
CompactJson.Serializer.Write(obj, new StringWriter(sb), true);
return sb.ToString();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<None Include="Test.testproj" />
<Compile Remove="**\bindings\**" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<Contract Include="SmartContract1.sol" />
<None Include="$solidityconfigfile$" />
<None Include="package.json" />
<None Include="bindings\**" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Stratis.VS.SolidityProjectTemplate/SmartContract1.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: GPL-3.0
// SPDX-License-Identifier: GPL-3.0
pragma solidity $soliditycompilerversion$;

contract SimpleStorage {
Expand Down
1 change: 1 addition & 0 deletions src/Stratis.VS.SolidityProjectTemplate/Solidity.vstemplate
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ProjectItem ReplaceParameters="true" OpenInEditor="false">foundry.toml</ProjectItem>
<ProjectItem ReplaceParameters="true" OpenInEditor="false">package.json</ProjectItem>
<ProjectItem ReplaceParameters="true" OpenInEditor="true">SmartContract1.sol</ProjectItem>
<Folder Name="bindings" TargetFolderName="bindings" />
</Project>
</TemplateContent>
<WizardExtension>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<None Include="truffle-config.js" />
<None Include="hardhat.config.js" />
</ItemGroup>
<ItemGroup>
<Folder Include="bindings\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<ItemType Name="None" DisplayName="Does not participate in build"/>

<FileExtension Name=".sol" ContentType="Contract"/>
<FileExtension Name=".cs" ContentType="Text"/>
<FileExtension Name=".txt" ContentType="Text"/>
</ProjectSchemaDefinitions>

0 comments on commit 604ffa2

Please sign in to comment.