Skip to content

Instantly share code, notes, and snippets.

@kzu
Last active August 28, 2018 07:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kzu/9274862 to your computer and use it in GitHub Desktop.
Save kzu/9274862 to your computer and use it in GitHub Desktop.
Generates class files from a text file and makes intellisense work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynamicIntellisense
{
public class Class1
{
public Class1()
{
Foo f;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{34991E38-315E-471F-8057-A915E0663A46}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DynamicIntellisense</RootNamespace>
<AssemblyName>DynamicIntellisense</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\$(Configuration)\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
</ItemGroup>
<ItemGroup>
<TxtClass Include="Foo.xyz" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildProjectDirectory)\InjectCode.targets" />
</Project>
<?xml version="1.0" encoding="UTF-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
NOTE: in order for the intellisense to update automatically,
the @TxtClass items MUST have a custom tool set to MSBuild:Compile,
like:
<TxtClass Include="Foo.xyz">
<Generator>MSBuild:Compile</Generator>
</TxtClass>
This is the mechanism used by XAML.
-->
<!-- By setting the custom tool in the item group definition, we don't require users to do it manually -->
<ItemDefinitionGroup>
<TxtClass>
<Generator>MSBuild:Compile</Generator>
</TxtClass>
<ItemDefinitionGroup>
<PropertyGroup>
<CompileDependsOn>
CreateDynamicCode;
$(CompileDependsOn);
</CompileDependsOn>
</PropertyGroup>
<ItemGroup>
<DynamicCodeFiles Include="@(TxtClass -> '$(IntermediateOutputPath)%(Filename)$(GeneratedFileExtension)')">
<InProject>false</InProject>
</DynamicCodeFiles>
</ItemGroup>
<Target Name="CreateDynamicCode" Condition="'@(TxtClass)' != ''"
Inputs="@(TxtClass)"
Outputs="@(DynamicCodeFiles)">
<!-- Let MSBuild batching do its job -->
<Transform Source="@(TxtClass)" Target="@(DynamicCodeFiles)" />
<ItemGroup>
<Compile Include="@(DynamicCodeFiles)" />
<FileWrites Include="@(DynamicCodeFiles)" />
</ItemGroup>
</Target>
<ItemGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<AvailableItemName Include="TxtClass" />
</ItemGroup>
<UsingTask TaskName="Transform" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Source Required="true" />
<Target Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text"/>
<Using Namespace="System.Collections.Generic"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var name = Path.GetFileNameWithoutExtension (Source);
var sb = new StringBuilder ();
sb.AppendFormat ("class {0}\r\n{{\r\n", name);
foreach (var line in File.ReadAllLines (Source)) {
sb.AppendFormat ("\tpublic string {0};\r\n", line.Trim ());
}
sb.AppendFormat ("}}\r\n");
File.WriteAllText (Target, sb.ToString ());
]]>
</Code>
</Task>
</UsingTask>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment