Click here to Skip to main content
15,895,772 members
Articles / Web Development / ASP.NET

PLINQO - Supercharge LINQ to SQL

Rate me:
Please Sign up or sign in to vote.
3.79/5 (6 votes)
17 Aug 2007CPOL6 min read 51.7K   143   17  
PLINQO is a collection of CodeSmith templates that are meant to replace and extend the LINQ to SQL designers.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Build.BuildEngine;
using CodeSmith.Engine.Utility;

public class ProjectUpdater
{
    
    private string _projectDirectory;

    public ProjectUpdater(string projectFile)
    {
        _projectFile = projectFile;
        InitializeProject();
    }

    private Project _project;
    
    public Project Project
    {
        get { return _project; }
    }

    private string _projectFile;

    public string ProjectFile
    {
        get { return _projectFile; }
    }

    public void AddEmbeddedResource(string fileName)
    {
        AddItem(fileName, "EmbeddedResource", true);
    }

    public void AddCompileItem(string fileName)
    {
        AddItem(fileName, "Compile", true);
    }

    public void AddNoneItem(string fileName)
    {
        AddItem(fileName, "None", true);
    }
    
    public void AddCompileItem(string fileName, string dependentFile)
    {
        if (string.IsNullOrEmpty(this.ProjectFile) || _project == null)
            return;

        fileName = PathUtil.RelativePathTo(
            _projectDirectory, Path.GetFullPath(fileName));
        dependentFile = PathUtil.RelativePathTo(
            _projectDirectory, Path.GetFullPath(dependentFile));

        BuildItem classItem = null;
        BuildItem dependItem = null;

        BuildItemGroup group = _project.GetEvaluatedItemsByName("Compile");

        foreach (BuildItem item in group)
        {
            if (item.FinalItemSpec.Equals(fileName, StringComparison.OrdinalIgnoreCase))
                classItem = item;
            else if (item.FinalItemSpec.Equals(dependentFile, StringComparison.OrdinalIgnoreCase))
                dependItem = item;
        }

        if (classItem == null)
            classItem = _project.AddNewItem("Compile", fileName);
        if (dependItem == null)
            dependItem = _project.AddNewItem("Compile", dependentFile);

        if (!dependItem.HasMetadata("DependentUpon"))
        {
            dependItem.SetMetadata(
                "DependentUpon",
                Path.GetFileName(classItem.FinalItemSpec));
            _project.MarkProjectAsDirty();
        }

    }

    public void AddReferenceItem(string reference, bool searchProjectReferences)
    {
        if (searchProjectReferences)
        {
            BuildItemGroup group = _project.GetEvaluatedItemsByName("ProjectReference");
            foreach (BuildItem item in group)
            {
                if (item.Include.Contains(reference) ||
                    (item.HasMetadata("Name") && item.GetMetadata("Name").Contains(reference)))
                {
                    return;
                }
            }
        }
        AddItem(reference, "Reference", false);
    }

    public void AddItem(string fileName, string itemType, bool makeRelative)
    {
        if (string.IsNullOrEmpty(this.ProjectFile) || _project == null)
            return;

        if (makeRelative)
        {
            fileName = PathUtil.RelativePathTo(
                _projectDirectory, Path.GetFullPath(fileName));
        }

        BuildItem newItem = null;
        BuildItemGroup group = _project.GetEvaluatedItemsByName(itemType);

        foreach (BuildItem item in group)
        {
            if (fileName.Equals(item.FinalItemSpec,
                StringComparison.OrdinalIgnoreCase))
            {
                newItem = item;
                break;
            }
        }

        if (newItem == null)
        {
            newItem = _project.AddNewItem(itemType, fileName);
            _project.MarkProjectAsDirty();
        }
    }
    
    private void InitializeProject()
    {
        if (string.IsNullOrEmpty(this.ProjectFile)
            || !File.Exists(this.ProjectFile))
            return;

        _projectDirectory = Path.GetDirectoryName(
            Path.GetFullPath(_projectFile));

        Engine engine = new Engine(Environment.CurrentDirectory);
        _project = new Project(engine);
        _project.Load(_projectFile);
    }

    public void SaveProject()
    {
        if (string.IsNullOrEmpty(this.ProjectFile)
            || _project == null
            || !_project.IsDirty)
            return;

        _project.Save(this.ProjectFile);
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions