Giter Site home page Giter Site logo

csprojeditor's Introduction

CsProjEditor

Build Status codecov License: MIT

NuGet NuGet

This library offers csproj element/attribute operation without EnvDTE.

Load csproj file and create virtual dom, operate and write it out.

Motivation

When I want to edit csproj for dynamically generated csproj or vscproj, like Unity UWP generates, publish-store required me to operate with Visual Studio. This tools is for this dynamically operate csproj on CI or automation platform.

Limitation

  • Accept only utf8 and utf8bom encodings.
  • Accept only file path or stream. (no parsing string.)

Install

NuGet Library

dotnet add package CsProjEditor

CLI (.NET Global Tool)

dotnet tool install -g csprojcli

NuGet Usage

Load your csproj from path or Stream, then operate node and attribute!

// load csproj
var csproj = CsprojEditor.Load("your.csproj");

// edit
csproj.SetNodeValue("PropertyGroup", "PackageCertificateKeyFile", pfx);
csproj.InsertNode("PropertyGroup", "PackageCertificateThumbprint", thumbprint);
csproj.InsertNode("PropertyGroup", "GenerateAppInstallerFile", "False");
csproj.InsertNode("PropertyGroup", "AppxAutoIncrementPackageRevision", "True");
csproj.InsertNode("PropertyGroup", "AppxSymbolPackageEnabled", "False");
csproj.InsertNode("PropertyGroup", "AppxBundle", "Always");
csproj.InsertNode("PropertyGroup", "AppxBundlePlatforms", "x86");
csproj.InsertNode("PropertyGroup", "AppInstallerUpdateFrequency", "1");
csproj.InsertNode("PropertyGroup", "AppInstallerCheckForUpdateFrequency", "OnApplicationRun");
csproj.InsertAttribute("ItemGroup", "None", "Include", pfx, e => !e.HasAttributes);
csproj.InsertAttribute("ItemGroup", "None", "Include", "Package.StoreAssociation.xml", e => !e.HasAttributes);

// save
csproj.Save(path);

more examples? check examples and test.

CLI Usage

You can find help.

> csprojcli help

You can run for each operation for group.*, node.*, nodevalue.*, attrribute.* and attributevalue.*.

here's all commands and examples, you can reproduce with commands running at examples/csprojcli.

Usage: csprojcli [version] [help] [batch.loadandrun|batch.run|groups.get|group.get|group.exists|group.insert|group.replace|group.remove|nodes.get|node.get|node.exists|node.insert|node.replace|node.remove|nodevalue.get|nodevalue.exists|nodevalue.set|nodevalue.append|nodevalue.prepend|nodevalue.replace|nodevalue.remove|attribute.get|attribute.exists|attribute.insert|attribute.replace|attribute.remove|attributevalue.get|attributevalue.exists|attributevalue.set|attributevalue.append|attributevalue.prepend|attributevalue.replace|attributevalue.remove] [parameters]

E.g., run this for Batch execution. see JSON sample at https://raw.githubusercontent.com/guitarrapc/CsProjEditor/master/src/csprojcli/sample.json
--------------------------
$ csprojcli batch.loadandrun -jsonPath examples/csprojcli/uwp_storepublish.json
$ csprojcli batch.run -json JSON

E.g., run this for group execution.:
--------------------------
$ csprojcli groups.get -p SimpleNewCsProjUtf8_CRLF.csproj
$ csprojcli group.get -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup
$ csprojcli group.exists -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup
$ csprojcli group.insert -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli group.replace -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -pattern Property -replacement Foo -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli group.remove -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true

E.g., run this for node execution.:
--------------------------
$ csprojcli nodes.get -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup
$ csprojcli node.get -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n TargetFramework
$ csprojcli node.exists -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n TargetFramework
$ csprojcli node.insert -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n PackageCertificateThumbprint -v 1234567890ABCDEF -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli node.replace -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n LangVersion -pattern latest -replacement preview -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli node.remove -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n LangVersion -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true

E.g., run this for node value execution.:
--------------------------
$ csprojcli nodevalue.get -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n TargetFramework
$ csprojcli nodevalue.exists -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n TargetFramework -v netstandard2.0
$ csprojcli nodevalue.set -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n PackageCertificateKeyFile -v hogehoge.pfx -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli nodevalue.append -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n RootNamespace -v SimpleCsProj -append ect -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli nodevalue.prepend -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n RootNamespace -v SimpleCsProj -prepend Very -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli nodevalue.replace -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n OutputType -v Exe -pattern Exe -replacement AppContainer -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli nodevalue.remove -p SimpleNewCsProjUtf8_CRLF.csproj -g PropertyGroup -n AssemblyName -v SimpleCsProj -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true

E.g., run this for group attribute execution.:
--------------------------
$ csprojcli attribute.get -p SimpleNewCsProjUtf8_CRLF.csproj -g Import
$ csprojcli attribute.exists -p SimpleNewCsProjUtf8_CRLF.csproj -g Import -a Include
$ csprojcli attribute.insert -p SimpleNewCsProjUtf8_CRLF.csproj -g Import -a Include -v example.json -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attribute.insert -p SimpleNewCsProjUtf8_CRLF.csproj -g Import -a Project,Condition -v ".pack\\package.csproj","Exists('.pack\\package.csproj')" -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attribute.remove -p SimpleNewCsProjUtf8_CRLF.csproj -g Import -a Exclude -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true

E.g., run this for node attribute execution.:
--------------------------
$ csprojcli attribute.get -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None
$ csprojcli attribute.exists -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include
$ csprojcli attribute.insert -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include -v example.json -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attribute.replace -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include -v project.json -pattern None -replacement Content -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attribute.remove -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Exclude -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true

E.g., run this for attribute value execution.:
--------------------------
$ csprojcli attributevalue.get -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include
$ csprojcli attributevalue.exists -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n Compile -a Include -v App.cs
$ csprojcli attributevalue.set -p SimpleNewCsProjUtf8_CRLF.csproj -g Target -n Message -a Importance -v low - -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attributevalue.append -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include -v project.json -append ect -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attributevalue.prepend -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include -v project.json -prepend Very -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attributevalue.replace -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include -v project.json -pattern project -replacement example -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true
$ csprojcli attributevalue.remove -p SimpleNewCsProjUtf8_CRLF.csproj -g ItemGroup -n None -a Include -v project.json -output result_SimpleNewCsProjUtf8_CRLF.csproj -allowoverwrite true

Each command require save to overlap previous command execution result, you can do by passing -dry false and -allowoverwrite true.

csprojcli nodevalue.set -p "test.csproj" -g PropertyGroup -n PackageCertificateKeyFile -v hogehoge.pfx -output "result.csproj" -allowoverwrite true
csprojcli node.insert -p "result.csproj" -g PropertyGroup -n PackageCertificateThumbprint -v 1234567890ABCDEF -output "result.csproj" -allowoverwrite true

Also, you can define commands in JSON and batch execute it with batch.loadandrun. This will execute defined commands and save, or show dry-run, when everything goes fine.

csprojcli batch.loadandrun -jsonPath your_commands.json

json should follow to Scheme and here' all command sample JSON.

more examples? check examples.

Implementations

  • File: Load
  • File: ToString override.
  • File: Detect UTF8 Bom.
  • File: Detect EOL.
  • File: Save with original utf8 encoding, keep BOM.
  • File: Save will add empty line if last line is value.
  • XmlUtils: Get NameSpace (for old csproj, equivalent for new csproj)
  • XmlUtils: Get Declaration (for old csproj, equivalent for new csproj)
  • XmlUtils: Get space for each node
  • Filter: filter node, nodevalue and attribute and pass as XElement
  • Group: Get (Groups)
  • Group: Exists
  • Group: Insert
  • Group: Remove
  • Group: Replace
  • Node: Get (Nodes)
  • Node: Exists
  • Node: Insert
  • Node: Remove
  • Node: Replace
  • NodeValue: Get
  • NodeValue: Exists
  • NodeValue: Set
  • NodeValue: Append
  • NodeValue: Prepend
  • NodeValue: Remove
  • NodeValue: Replace
  • Attribute: Get (Group)
  • Attribute: Get (Node)
  • Attribute: Exists (Group)
  • Attribute: Exists (Node)
  • Attribute: Insert (Group)
  • Attribute: Insert (Node)
  • Attribute: Set (Group)
  • Attribute: Set (Node)
  • Attribute: Remove (Group)
  • Attribute: Remove (Node)
  • Attribute: Replace (Group)
  • Attribute: Replace (Node)
  • AttributeValue: Get (Group)
  • AttributeValue: Get (Node)
  • AttributeValue: Exists (Group)
  • AttributeValue: Exists (Node)
  • AttributeValue: Set (Group)
  • AttributeValue: Set (Node)
  • AttributeValue: Append (Group)
  • AttributeValue: Append (Node)
  • AttributeValue: Prepend (Group)
  • AttributeValue: Prepend (Node)
  • AttributeValue: Remove (Group)
  • AttributeValue: Remove (Node)
  • AttributeValue: Replace (Group)
  • AttributeValue: Replace (Node)
  • Tests: Node
  • Tests: NodeValue
  • Tests: Attribute (Group)
  • Tests: Attribute (Node)
  • Tests: AttributeValue
  • Tests: Save

csprojeditor's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar guitarrapc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

kkacer

csprojeditor's Issues

I would like to be able to pass groups with nodes and attributes already in.

This is an awesome piece of software! I've been waiting for someone to make it easier to edit projects.

I have some small requests: Make it a little bit more object oriented. I would love to load a project and get a list of sections with nodes and attributes populated, and when I create a fresh project, I create the sections, fill in the nodes/attributes and pass the section on to your to be able to update the xml.

A couple classes like this might do:

    using System.Collections.Generic;

    public partial class GroupWithNodes
    {
        public GroupWithNodes()
        {
            Nodes = new List<GroupNode>();
            Attributes = new List<GroupNode>();
        }

        public string Group { get; set; }

        public List<GroupNode> Nodes { get; set; }

        public List<GroupNode> Attributes { get; set; }
    }

    public partial class GroupNode
    {
        public GroupNode()
        {
            Attributes = new List<GroupNode>();
        }

        public string Name { get; set; }
        public string Value { get; set; }

        public List<GroupNode> Attributes { get; set; }
    }

Thanks,

Rick

Question: How to add some imports to a project via this tool

I want to add these 2 lines in the project (CSProj file),

Something like:

<Import Project=".pack\package.csproj" Condition="Exists('.pack\package.csproj')" />

This line would be under the first node (<project>)

Didn't find a sample on it, can you provide a sample showing how we would be able to do that via this library?

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.