Skip to content

Commit

Permalink
Export nef and manifest as static
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Feb 25, 2024
1 parent 430947f commit 27520f6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Neo.Compiler.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private static int ProcessOutput(Options options, string folder, CompilationCont

if (options.GenerateArtifacts != Options.GenerateArtifactsKind.None)
{
var artifact = manifest.GetArtifactsSource(baseName);
var artifact = manifest.GetArtifactsSource(baseName, nef);

if (options.GenerateArtifacts == Options.GenerateArtifactsKind.All || options.GenerateArtifacts == Options.GenerateArtifactsKind.Source)
{
Expand Down
25 changes: 24 additions & 1 deletion src/Neo.SmartContract.Testing/Extensions/ArtifactExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Neo.IO;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Testing.Coverage;
using Neo.SmartContract.Testing.TestingStandards;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -32,9 +34,11 @@ public static class ArtifactExtensions
/// </summary>
/// <param name="manifest">Manifest</param>
/// <param name="name">Class name, by default is manifest.Name</param>
/// <param name="nef">Nef file</param>
/// <param name="debugInfo">Debug Info</param>
/// <param name="generateProperties">Generate properties</param>
/// <returns>Source</returns>
public static string GetArtifactsSource(this ContractManifest manifest, string? name = null, bool generateProperties = true)
public static string GetArtifactsSource(this ContractManifest manifest, string? name = null, NefFile? nef = null, NeoDebugInfo? debugInfo = null, bool generateProperties = true)
{
name ??= manifest.Name;

Expand Down Expand Up @@ -63,6 +67,25 @@ public static string GetArtifactsSource(this ContractManifest manifest, string?
sourceCode.WriteLine($"public abstract class {name} : " + string.Join(", ", inheritance));
sourceCode.WriteLine("{");

// Write compiled data

sourceCode.WriteLine(" #region Compiled data");
sourceCode.WriteLine();

var value = manifest.ToJson().ToString(false).Replace("\"", "\"\"");
sourceCode.WriteLine($" public static readonly {typeof(ContractManifest).FullName} ContractManifest = {typeof(ContractManifest).FullName}.Parse(@\"{value}\");");
sourceCode.WriteLine();

if (nef is not null)
{
value = Convert.ToBase64String(nef.ToArray()).Replace("\"", "\"\"");
sourceCode.WriteLine($" public static readonly {typeof(NefFile).FullName} NefFile = Neo.IO.Helper.AsSerializable<{typeof(NefFile).FullName}>(Convert.FromBase64String(@\"{value}\"));");
sourceCode.WriteLine();
}

sourceCode.WriteLine(" #endregion");
sourceCode.WriteLine();

// Crete events

if (manifest.Abi.Events.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,14 @@
</ItemGroup>

<ItemGroup>
<None Update="templates\neocontractnep17\Artifacts\Nep17Contract.manifest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="templates\neocontractnep17\Artifacts\Nep17Contract.nef">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="templates\neocontractnep17\Artifacts\Nep17Contract.nefdbgnfo">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="templates\neocontractowner\Artifacts\Ownable.manifest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="templates\neocontractowner\Artifacts\Ownable.nef">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="templates\neocontractowner\Artifacts\Ownable.nefdbgnfo">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="templates\neocontractowner\Artifacts\" />
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Message Text="Create and compile Nep17Contract.cs" Importance="high" />
<Exec Command="dotnet pack &quot;$(SolutionDir)src/Neo.SmartContract.Template/Neo.SmartContract.Template.csproj&quot;" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public class Nep17ContractTests : Nep17Tests<Nep17Contract>
/// <summary>
/// Initialize Test
/// </summary>
public Nep17ContractTests() :
base(
"templates/neocontractnep17/Artifacts/Nep17Contract.nef",
"templates/neocontractnep17/Artifacts/Nep17Contract.manifest.json"
)
{ }
public Nep17ContractTests() : base(Nep17Contract.NefFile, Nep17Contract.ContractManifest) { }

[TestMethod]
public void TestMyMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ public class OwnerContractTests : OwnableTests<Nep17Contract>
/// <summary>
/// Initialize Test
/// </summary>
public OwnerContractTests() :
base(
"templates/neocontractnep17/Artifacts/Nep17Contract.nef",
"templates/neocontractnep17/Artifacts/Nep17Contract.manifest.json"
)
{ }
public OwnerContractTests() : base(Nep17Contract.NefFile, Nep17Contract.ContractManifest) { }

[TestMethod]
public override void TestSetGetOwner()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public class OwnableContractTests : OwnableTests<Ownable>
/// <summary>
/// Initialize Test
/// </summary>
public OwnableContractTests() :
base(
"templates/neocontractowner/Artifacts/Ownable.nef",
"templates/neocontractowner/Artifacts/Ownable.manifest.json"
)
{ }
public OwnableContractTests() : base(Ownable.NefFile, Ownable.ContractManifest) { }

[TestMethod]
public override void TestSetGetOwner()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ namespace Neo.SmartContract.Testing;
public abstract class Contract1 : Neo.SmartContract.Testing.SmartContract, Neo.SmartContract.Testing.TestingStandards.INep17Standard, Neo.SmartContract.Testing.TestingStandards.IVerificable
{
#region Compiled data
public static readonly Neo.SmartContract.Manifest.ContractManifest ContractManifest = Neo.SmartContract.Manifest.ContractManifest.Parse(@""{""""name"""":""""Contract1"""",""""groups"""":[],""""features"""":{},""""supportedstandards"""":[""""NEP-17""""],""""abi"""":{""""methods"""":[{""""name"""":""""symbol"""",""""parameters"""":[],""""returntype"""":""""String"""",""""offset"""":1406,""""safe"""":true},{""""name"""":""""decimals"""",""""parameters"""":[],""""returntype"""":""""Integer"""",""""offset"""":1421,""""safe"""":true},{""""name"""":""""totalSupply"""",""""parameters"""":[],""""returntype"""":""""Integer"""",""""offset"""":43,""""safe"""":true},{""""name"""":""""balanceOf"""",""""parameters"""":[{""""name"""":""""owner"""",""""type"""":""""Hash160""""}],""""returntype"""":""""Integer"""",""""offset"""":85,""""safe"""":true},{""""name"""":""""transfer"""",""""parameters"""":[{""""name"""":""""from"""",""""type"""":""""Hash160""""},{""""name"""":""""to"""",""""type"""":""""Hash160""""},{""""name"""":""""amount"""",""""type"""":""""Integer""""},{""""name"""":""""data"""",""""type"""":""""Any""""}],""""returntype"""":""""Boolean"""",""""offset"""":281,""""safe"""":false},{""""name"""":""""getOwner"""",""""parameters"""":[],""""returntype"""":""""Hash160"""",""""offset"""":711,""""safe"""":true},{""""name"""":""""setOwner"""",""""parameters"""":[{""""name"""":""""newOwner"""",""""type"""":""""Hash160""""}],""""returntype"""":""""Void"""",""""offset"""":755,""""safe"""":false},{""""name"""":""""burn"""",""""parameters"""":[{""""name"""":""""account"""",""""type"""":""""Hash160""""},{""""name"""":""""amount"""",""""type"""":""""Integer""""}],""""returntype"""":""""Void"""",""""offset"""":873,""""safe"""":false},{""""name"""":""""mint"""",""""parameters"""":[{""""name"""":""""to"""",""""type"""":""""Hash160""""},{""""name"""":""""amount"""",""""type"""":""""Integer""""}],""""returntype"""":""""Void"""",""""offset"""":915,""""safe"""":false},{""""name"""":""""withdraw"""",""""parameters"""":[{""""name"""":""""token"""",""""type"""":""""Hash160""""},{""""name"""":""""to"""",""""type"""":""""Hash160""""},{""""name"""":""""amount"""",""""type"""":""""Integer""""}],""""returntype"""":""""Boolean"""",""""offset"""":957,""""safe"""":false},{""""name"""":""""onNEP17Payment"""",""""parameters"""":[{""""name"""":""""from"""",""""type"""":""""Hash160""""},{""""name"""":""""amount"""",""""type"""":""""Integer""""},{""""name"""":""""data"""",""""type"""":""""Any""""}],""""returntype"""":""""Void"""",""""offset"""":1139,""""safe"""":false},{""""name"""":""""verify"""",""""parameters"""":[],""""returntype"""":""""Boolean"""",""""offset"""":1203,""""safe"""":true},{""""name"""":""""myMethod"""",""""parameters"""":[],""""returntype"""":""""String"""",""""offset"""":1209,""""safe"""":false},{""""name"""":""""_deploy"""",""""parameters"""":[{""""name"""":""""data"""",""""type"""":""""Any""""},{""""name"""":""""update"""",""""type"""":""""Boolean""""}],""""returntype"""":""""Void"""",""""offset"""":1229,""""safe"""":false},{""""name"""":""""update"""",""""parameters"""":[{""""name"""":""""nefFile"""",""""type"""":""""ByteArray""""},{""""name"""":""""manifest"""",""""type"""":""""String""""}],""""returntype"""":""""Void"""",""""offset"""":1352,""""safe"""":false},{""""name"""":""""_initialize"""",""""parameters"""":[],""""returntype"""":""""Void"""",""""offset"""":1390,""""safe"""":false}],""""events"""":[{""""name"""":""""Transfer"""",""""parameters"""":[{""""name"""":""""from"""",""""type"""":""""Hash160""""},{""""name"""":""""to"""",""""type"""":""""Hash160""""},{""""name"""":""""amount"""",""""type"""":""""Integer""""}]},{""""name"""":""""SetOwner"""",""""parameters"""":[{""""name"""":""""newOwner"""",""""type"""":""""Hash160""""}]}]},""""permissions"""":[{""""contract"""":""""*"""",""""methods"""":""""*""""}],""""trusts"""":[],""""extra"""":{""""Author"""":""""\u003CYour Name Or Company Here\u003E"""",""""Description"""":""""\u003CDescription Here\u003E"""",""""Email"""":""""\u003CYour Public Email Here\u003E"""",""""Version"""":""""\u003CVersion String Here\u003E""""}}"");
#endregion
#region Events
public delegate void delSetOwner(UInt160? newOwner);
Expand Down

0 comments on commit 27520f6

Please sign in to comment.