Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Custom CSPROJ #11

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/cscompiler/CSCompiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cscompiler;

#if (macro || cs_runtime)

import sys.io.File;

import haxe.io.Path;
import haxe.macro.Expr;
import haxe.macro.Type;

Expand All @@ -24,6 +27,7 @@ using reflaxe.helpers.TypeHelper;
// ---

import cscompiler.components.*;
import cscompiler.config.Define;

/**
The class that manages the generation of the C# code.
Expand Down Expand Up @@ -126,18 +130,31 @@ namespace Haxe {
}

/**
Generates the .csproj file.
Adds a .csproj file to the output directory.

If the Define `no-csproj` is specified, then nothing is added.

Otherwise, if the Define `csproj` specifies a path to an existing
.csproj file, then that is used.

Otherwise, a default .csproj is generated.
**/
function setupCsProj() {
if(!Context.defined("no-csproj")) {
appendToExtraFile("build.csproj", csProjContent());
if (D_NoCsproj.isDefined()) {
return;
}
if (!D_Csproj.isDefined()) {
appendToExtraFile("build.csproj", csProjDefaultContent());
return;
}
final path = new Path(Context.resolvePath(D_Csproj.getValue()));
appendToExtraFile('${path.file}.${path.ext}', File.getContent(path.toString()));
}

/**
Returns the content of the .csproj file.
Returns the default content of the .csproj file.
**/
function csProjContent() {
function csProjDefaultContent() {
return StringTools.trim('
<Project Sdk="Microsoft.NET.Sdk">

Expand Down
28 changes: 24 additions & 4 deletions src/cscompiler/config/Define.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,41 @@ package cscompiler.config;
import reflaxe.helpers.Context; // same as haxe.macro.Context

/**
Lists all the custom defines available for configuring Reflaxe/C#.
Lists all the custom defines available for configuring Reflaxe/C#.
**/
@:using(cscompiler.config.Define.DefineTools)
enum abstract Define(String) from String to String {
/**
-D csproj=[path to file]

When set, the csproj at the specified path will be used for C#
instead of an auto-generated csproj.

This will be ignored when the `Define` `no-csproj` is set.
**/
var D_Csproj = "csproj";

/**
-D namespace_style=[default|pascal]

Default value: `default`
Default value: `default`

Determines how namespace names are generated for C#.

If set to `pascal`, snake-case package names are converted
to pascal-case C# namespaces.
If set to `pascal`, snake-case package names are converted
to pascal-case C# namespaces.
**/
var D_NamespaceStyle = "namespace_style";

/**
-D no-csproj

When set, a csproj file will not be generated C#.

This also applies to any csproj file specified with
the Define `csproj`.
**/
var D_NoCsproj = "no-csproj";
}

/**
Expand Down
5 changes: 5 additions & 0 deletions test/tests/Define_Csproj/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
out
out-legacy-cs
intended/bin
intended/obj
3 changes: 3 additions & 0 deletions test/tests/Define_Csproj/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package;

function main() { }
10 changes: 10 additions & 0 deletions test/tests/Define_Csproj/Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- THIS IS NOT THE DEFAULT CSPROJ -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>Haxe.HaxeBoot</StartupObject>
</PropertyGroup>
</Project>
6 changes: 6 additions & 0 deletions test/tests/Define_Csproj/Test_Specified.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following are added automatically:
# -lib csharp
# --custom-target rcs=out

-main Main
-D csproj=Test.csproj
5 changes: 5 additions & 0 deletions test/tests/Define_Csproj/Test_Unspecified.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The following are added automatically:
# -lib csharp
# --custom-target rcs=out

-main Main
7 changes: 7 additions & 0 deletions test/tests/Define_Csproj/intended/Test_Specified/HaxeBoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Haxe {
class HaxeBoot {
static void Main(string[] args) {
haxe.root.Main_Fields_.main();
}
}
}
7 changes: 7 additions & 0 deletions test/tests/Define_Csproj/intended/Test_Specified/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace haxe.root {
class Main_Fields_ {
public static void main() {

}
}
}
10 changes: 10 additions & 0 deletions test/tests/Define_Csproj/intended/Test_Specified/Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- THIS IS NOT THE DEFAULT CSPROJ -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>Haxe.HaxeBoot</StartupObject>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Main.cs
Test.csproj
HaxeBoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Haxe {
class HaxeBoot {
static void Main(string[] args) {
haxe.root.Main_Fields_.main();
}
}
}
7 changes: 7 additions & 0 deletions test/tests/Define_Csproj/intended/Test_Unspecified/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace haxe.root {
class Main_Fields_ {
public static void main() {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Main.cs
build.csproj
HaxeBoot.cs
11 changes: 11 additions & 0 deletions test/tests/Define_Csproj/intended/Test_Unspecified/build.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>Haxe.HaxeBoot</StartupObject>
</PropertyGroup>

</Project>
Loading