Skip to content

Commit de08e95

Browse files
Jim8yajara87Wi1l-B0tshargon
authored
Fix: use local compiler tools in contract templates (#1906)
Co-authored-by: Alvaro <amjarag@gmail.com> Co-authored-by: Will <201105916+Wi1l-B0t@users.noreply.github.com> Co-authored-by: Shargon <shargon@gmail.com>
1 parent 193dfa8 commit de08e95

7 files changed

Lines changed: 92 additions & 4 deletions

File tree

src/Neo.SmartContract.Template/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Project templates generate:
8484

8585
```
8686
MyContract/
87+
├── .config/dotnet-tools.json
8788
├── MyContract.cs # Main contract file
8889
├── MyContract.csproj # Project file with proper references
8990
└── README.md # Template-specific documentation
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"neo.compiler.csharp": {
6+
"version": "TemplateNeoVersion",
7+
"commands": [
8+
"nccs"
9+
]
10+
}
11+
}
12+
}

src/Neo.SmartContract.Template/templates/neocontractnep11/Nep11Contract.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
2626
<Message Text="Start NeoContract converter, Source File: &quot;$(ProjectPath)&quot;" Importance="high">
2727
</Message>
28-
<Exec Command="nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" />
28+
<Exec Command="dotnet tool restore" WorkingDirectory="$(MSBuildProjectDirectory)" />
29+
<Exec Command="dotnet tool run nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" WorkingDirectory="$(MSBuildProjectDirectory)" />
2930
</Target>
3031

3132
</Project>

src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
2626
<Message Text="Start NeoContract converter, Source File: &quot;$(ProjectPath)&quot;" Importance="high">
2727
</Message>
28-
<Exec Command="nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" />
28+
<Exec Command="dotnet tool restore" WorkingDirectory="$(MSBuildProjectDirectory)" />
29+
<Exec Command="dotnet tool run nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" WorkingDirectory="$(MSBuildProjectDirectory)" />
2930
</Target>
3031

3132
</Project>

src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
2626
<Message Text="Start NeoContract converter, Source File: &quot;$(ProjectPath)&quot;" Importance="high">
2727
</Message>
28-
<Exec Command="nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" />
28+
<Exec Command="dotnet tool restore" WorkingDirectory="$(MSBuildProjectDirectory)" />
29+
<Exec Command="dotnet tool run nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" WorkingDirectory="$(MSBuildProjectDirectory)" />
2930
</Target>
3031

3132
</Project>

src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
2626
<Message Text="Start NeoContract converter, Source File: &quot;$(ProjectPath)&quot;" Importance="high">
2727
</Message>
28-
<Exec Command="nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" />
28+
<Exec Command="dotnet tool restore" WorkingDirectory="$(MSBuildProjectDirectory)" />
29+
<Exec Command="dotnet tool run nccs $(BaseNameArgument) $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(ProjectPath)&quot;" WorkingDirectory="$(MSBuildProjectDirectory)" />
2930
</Target>
3031

3132
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (C) 2015-2026 The Neo Project.
2+
//
3+
// ContractTemplateToolManifestTests.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using Microsoft.VisualStudio.TestTools.UnitTesting;
13+
using System;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Text.Json;
17+
using System.Xml.Linq;
18+
19+
namespace Neo.SmartContract.Template.UnitTests.templates;
20+
21+
[TestClass]
22+
public class ContractTemplateToolManifestTests
23+
{
24+
private static readonly string TemplateRoot = Path.GetFullPath(
25+
"../../../../../src/Neo.SmartContract.Template/templates");
26+
27+
[DataTestMethod]
28+
[DataRow("neocontractnep11")]
29+
[DataRow("neocontractnep17")]
30+
[DataRow("neocontractoracle")]
31+
[DataRow("neocontractowner")]
32+
[DataRow("neocontractsolution")]
33+
public void ContractTemplate_IncludesCompilerToolManifest(string templateName)
34+
{
35+
var manifestPath = Path.Combine(TemplateRoot, templateName, ".config", "dotnet-tools.json");
36+
Assert.IsTrue(File.Exists(manifestPath), $"{templateName} is missing .config/dotnet-tools.json.");
37+
38+
using var document = JsonDocument.Parse(File.ReadAllText(manifestPath));
39+
var root = document.RootElement;
40+
var compiler = root.GetProperty("tools").GetProperty("neo.compiler.csharp");
41+
42+
Assert.AreEqual(1, root.GetProperty("version").GetInt32());
43+
Assert.IsTrue(root.GetProperty("isRoot").GetBoolean());
44+
Assert.AreEqual("TemplateNeoVersion", compiler.GetProperty("version").GetString());
45+
46+
var commands = compiler.GetProperty("commands");
47+
Assert.AreEqual(1, commands.GetArrayLength());
48+
Assert.AreEqual("nccs", commands[0].GetString());
49+
}
50+
51+
[DataTestMethod]
52+
[DataRow("neocontractnep11", "Nep11Contract.csproj")]
53+
[DataRow("neocontractnep17", "Nep17Contract.csproj")]
54+
[DataRow("neocontractoracle", "OracleRequest.csproj")]
55+
[DataRow("neocontractowner", "Ownable.csproj")]
56+
public void StandaloneContractTemplate_UsesLocalCompilerTool(
57+
string templateName,
58+
string projectName)
59+
{
60+
var project = XDocument.Load(Path.Combine(TemplateRoot, templateName, projectName));
61+
var commands = project.Descendants("Exec")
62+
.Select(element => (string?)element.Attribute("Command"))
63+
.Where(command => command is not null)
64+
.ToArray();
65+
66+
CollectionAssert.Contains(commands, "dotnet tool restore");
67+
Assert.IsTrue(
68+
commands.Any(command => command!.StartsWith("dotnet tool run nccs ", StringComparison.Ordinal)),
69+
$"{projectName} must invoke nccs through the local tool manifest.");
70+
}
71+
}

0 commit comments

Comments
 (0)