Giter Club home page Giter Club logo

nugetcontentgenerator's Introduction

NuGet Content Generator

Build status

An MSBuild task to insert replacement tokens in C# files.

What does it do?

This tool helps you in creating NuGet packages by automating the process of converting *.cs into *.cs.pp files. In the process, it creates the desired replacement tokens that can be used by NuGet when adding content files.

Here is an example

Given the following directory structure:

MySolution:
- MyProject:
  - content:
    - SomeClassToPackage.cs
  - MyProject.csproj
- MySolution.sln

And that you have installed NuGet Content Generator in the MyProject project, when you build it, a new file called SomeClassToPackage.cs.pp will be created at the same level of SomeClassToPackage.cs, resulting in the following structure:

MySolution:
- MyProject:
  - content:
    - SomeClassToPackage.cs
    - SomeClassToPackage.cs.pp
  - MyProject.csproj
- MySolution.sln

Important: the new *.pp files will NOT be added to the project, as they will only be created on your file system.

Content transformations

NuGet supports some replacement tokens that are evaluated at package installation time. The most common is rootnamespace, although many others are available. More on this can be found in the NuGet documentation.

Let's say that you have the following CategoryInfo.cs file:

/** @pp
 * rootnamespace: MySolution.SomeNamespace.Content
 */
namespace MySolution.SomeNamespace.Content.Models {
    public struct CategoryInfo {
        public string categoryid;
        public string description;
        public string htmlUrl;
        public string rssUrl;
        public string title;
    }
}

After the transformation, the symbols defined in the @pp comment section will be inserted where specified, resulting in the following CategoryInfo.cs.pp file:

namespace $rootnamespace$.Models {
    public struct CategoryInfo {
        public string categoryid;
        public string description;
        public string htmlUrl;
        public string rssUrl;
        public string title;
    }
}

You can define as many replacements as you would like, one in each line. The only restriction is that the comment section for defining these replacements must be the first thing in the file.

How do I install it?

You can install it from NuGet, by looking for the NuGetContentGenerator package or running the following command in the Package Manager Console:

PM> Install-Package NuGetContentGenerator

The package will then be installed and the project will automatically import the needed MSBuild targets. All that is left to do is build your project to see every content\**\*.cs be transformed to its NuGet content representation as content\**\*.cs.pp.

Don't forget to add your content files to the Nuspec. Ideally, given the .nuspec is at the same level as the project, the content should be added as follows:

<files>
  <file src="content\**\*.pp" target="content" />
</files>

nugetcontentgenerator's People

Contributors

rdumont avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nugetcontentgenerator's Issues

Assembly File Error?

When using nuget pack from the .csproj with .nuspec at the same level (to auto include dependency tree), I get this:

The result "" of evaluating the value "@(AssemblyFile)" of the "AssemblyFile" attribute in element <UsingTask> is not valid.  C:\Users\Patrick\Source\Repos\...\...\packages\NuGetContentGenerator.0.2.0\build\NuGetContentGenerator.targets

Any ideas?

Following the default content-layout breaks replacement

The following content will not be replaced, due to a missmatch in the layout (using-section vs namespace-definition):

using System;

/** @pp
 * rootnamespace: foobar
 */
namespace foobar
{
}

Following content gets replaced correctly:

/** @pp
 * rootnamespace: foobar
 */
namespace foobar
{
  using System;
}

Proposal for new directive standard

Instead of

/*
 * @pp
 * value */

use a simplified version:

//!library:directive=options[;directive2=options[;emptydirective[;directive3=options]]]]    <-- the basic format

//!contentgen:rootnamespace=text_to_replace

The main library can be respecified multiple times. Directives respecified is up to you... I would probably error out if I encounter two rootnamespace directives in the same file.

So in README.md we'd replace the sample like so:

//!contentgen:rootnamespace=MySolution.SomeNamespace.Content;projectfile=Something.csproj
or
//!contentgen:rootnamespace=MySolution.SomeNamespace.Content
//!contentgen:projectfile=Something.csproj

I mention this because I'm trying to generate a movement of postprocessor directives in our source files. Eventually goal to have bona fide PostprocessorDirectiveTrivia included in Roslyn codeanalysis libraries. I'm using it in my https://github.com/cpcontrib/cppacker project. (Note might rename to cppm)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.