Skip to content

Commit

Permalink
close #12: add MsBuild task for running IronMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
chalcolith committed Jan 8, 2016
1 parent e124390 commit 056bd8b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
.vs
Source/IronMeta.snk
*.nupkg

# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
[Bb]in/
Expand Down
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -11,6 +11,13 @@ IronMeta is available on [NuGet](https://www.nuget.org/packages/IronMeta/). To
Once you have installed the NuGet package, add a grammar file with the extension .ironmeta to your project. Then generate a C# class from it. You can do this in two ways:

- You can install a [Visual Studio extension](https://visualstudiogallery.msdn.microsoft.com/73263c7c-319f-4f9e-a05a-b493094a4eb0) that provides a custom tool for generating C# code from IronMeta files. You must set the "Custom Tool" property of your IronMeta file to be `IronMetaGenerator`. Then the C# code will be generated whenever your grammer file changes. Syntax errors will appear in your Error List.
- `IronMeta.Library.dll` contains an MsBuild task called "IronMetaGenerate". A simple example of how to use this:

<UsingTask TaskName="IronMetaGenerate" AssemblyFile="path_to\IronMeta.Library.dll" />
<Target Name="BeforeBuild">
<IronMetaGenerate Input="MyParser.ironmeta" Output="MyParser.g.cs" Namespace="MyNamespace" Force="true" />
</Target>

- A command-line program `IronMeta.exe` is included in the NuGet package, in the `tools` directory. The program takes the following arguments:
- `-o {output}` (optional): Specify the output file name (defaults to `{input}_.g.cs`).
- `-n {namespace}` (optional): Specify the namespace to use for the generated parser (defaults to the name of the directory the input file is in).
Expand Down
2 changes: 1 addition & 1 deletion Source/CommonAssemblyInfo.cs
Expand Up @@ -8,4 +8,4 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("4.2.*")]
[assembly: AssemblyInformationalVersionAttribute("4.2.3")]
[assembly: AssemblyInformationalVersionAttribute("4.2.4")]
50 changes: 50 additions & 0 deletions Source/Library/IronMetaGenerate.cs
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using IronMeta.Generator;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace IronMeta
{
/// <summary>
/// An MsBuild task for generating parsers.
/// </summary>
public class IronMetaGenerate : Task
{
[Required]
public string Input { get; set; }

public string Namespace { get; set; }

public string Output { get; set; }

public bool Force { get; set; }

static readonly Regex IronMetaFileName = new Regex(@"^(.*).ironmeta\s*$", RegexOptions.Compiled);

This comment has been minimized.

Copy link
@JayBazuzi

JayBazuzi Jan 11, 2016

Why not use Path.GetExtension()?

This comment has been minimized.

Copy link
@chalcolith

chalcolith Jan 11, 2016

Author Owner

Because I tend to overgeneralize by default, I guess.


public override bool Execute()
{
if (string.IsNullOrWhiteSpace(Output))
{
var match = IronMetaFileName.Match(Input);
if (match.Success)
Output = match.Groups[1].Value + ".g.cs";
else
Output = Input + ".g.cs";
}

var result = CSharpShell.Process(Input, Output, Namespace, Force);
if (result.Success)
return true;

int num, offset;
result.MatchState.GetLine(result.ErrorIndex, out num, out offset);

Log.LogError("", "", "", Input, num, offset, num, offset, result.Error);
return false;
}
}
}
5 changes: 5 additions & 0 deletions Source/Library/Library.csproj
Expand Up @@ -42,6 +42,10 @@
<AssemblyOriginatorKeyFile>..\IronMeta.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Tasks.v4.0" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Verophyle.Regexp, Version=1.1.5684.13780, Culture=neutral, PublicKeyToken=74cddc3ca888ce35, processorArchitecture=MSIL">
Expand All @@ -59,6 +63,7 @@
<Compile Include="Generator\IGenerator.cs" />
<Compile Include="Generator\AstNode.cs" />
<Compile Include="Generator\Parser.g.cs" />
<Compile Include="IronMetaGenerate.cs" />
<Compile Include="Matcher\Matcher.cs" />
<Compile Include="Matcher\MatchItem.cs" />
<Compile Include="Matcher\MatchResult.cs" />
Expand Down

0 comments on commit 056bd8b

Please sign in to comment.