node-csproj-util
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "node-csproj-util", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Edit Visual Studio project files using Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,2 +26,4 @@ # node-csproj-util | ||
### Reading from a .proj file | ||
Project in a solution: | ||
```javascript | ||
@@ -33,3 +35,11 @@ ... | ||
``` | ||
Directly loading a project: | ||
```javascript | ||
... | ||
const proj = new Project({ filePath: 'examples/TestSolution/TestProject/TestProject.csproj' }); | ||
await proj.read(); | ||
... | ||
``` | ||
### Adding files to project | ||
@@ -44,2 +54,13 @@ ```javascript | ||
... | ||
``` | ||
``` | ||
### Adding to solution folders | ||
```javascript | ||
... | ||
// you can create or use an existing one | ||
const folder = sln.addFolder('MyFolder'); | ||
sln.addToFolder(folder, proj); | ||
await sln.save(); | ||
... | ||
``` | ||
You can create or use an existing one, since they are not actual folders, get them using the `Solution.projects` property. |
@@ -9,3 +9,2 @@ const fs = require('fs-extra'); | ||
function Project(data) { | ||
this.solution = data.solution; | ||
this.projectTypeGuid = data.projectTypeGuid; | ||
@@ -15,3 +14,8 @@ this.projectName = data.projectName; | ||
this.projectGuid = data.projectGuid; | ||
this.filePath = path.join(path.dirname(this.solution.path), path.normalize(this.relativePath)); | ||
this.filePath = data.filePath; | ||
if(data.solution) { | ||
this.solution = data.solution; | ||
this.filePath = this.filePath || path.join(path.dirname(this.solution.path), path.normalize(this.relativePath)); | ||
} | ||
} | ||
@@ -21,3 +25,3 @@ | ||
const buffer = await fs.readFile(this.filePath); | ||
this.xmlContent = await parser.parseStringPromise(buffer.toString());; | ||
this.xmlContent = await parser.parseStringPromise(buffer.toString()); | ||
@@ -35,2 +39,30 @@ this.itemGroup = { | ||
Project.prototype.getPropertyNode = function (key) { | ||
const propertyGroups = this.xmlContent.Project.PropertyGroup; | ||
for (let i = 0; i < propertyGroups.length; i++) { | ||
if (propertyGroups[i][key]) { | ||
return propertyGroups[i][key]; | ||
} | ||
} | ||
propertyGroups[1][key] = ['']; | ||
return propertyGroups[1][key]; | ||
} | ||
Project.prototype.getProperty = function (key) { | ||
const node = this.getPropertyNode(key); | ||
if(node) { | ||
return node[0]; | ||
} | ||
throw new Error(`PropertyGroup: Property "${key}" not found`); | ||
} | ||
Project.prototype.setProperty = function (key, value) { | ||
const node = this.getPropertyNode(key); | ||
if(node) { | ||
return node[0] = value; | ||
} | ||
throw new Error(`PropertyGroup: Property "${key}" not found`); | ||
} | ||
Project.prototype.add = function (filePath, opts = {}) { | ||
@@ -37,0 +69,0 @@ opts = { includeOutput: true, ...opts }; |
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const { SlnParser } = require('./parse'); | ||
@@ -20,2 +21,14 @@ const Project = require('./project'); | ||
Solution.prototype.addProject = function (project) { | ||
if (!project.projectTypeGuid) { | ||
const guids = project.getProperty('ProjectTypeGuids').split(';'); | ||
project.projectTypeGuid = guids[guids.length - 1] | ||
} | ||
project.projectName = project.projectName || project.getProperty('AssemblyName'); | ||
project.relativePath = project.relativePath || path.relative(path.dirname(this.path), project.filePath); | ||
project.projectGuid = project.projectGuid || project.getProperty('ProjectGuid'); | ||
this.projects.push(project); | ||
} | ||
Solution.prototype.save = async function () { | ||
@@ -31,2 +44,37 @@ const fileContent = this.parser.write({ | ||
Solution.prototype.addFolder = function (projectName) { | ||
const slnFolder = new Project({ | ||
solution: this, | ||
projectName: projectName, | ||
relativePath: projectName, | ||
projectGuid: `{${uuidv4().toUpperCase()}}`, | ||
projectTypeGuid: '{2150E333-8FDC-42A3-9474-1A3956D46DE8}' | ||
}); | ||
this.projects.push(slnFolder); | ||
return slnFolder; | ||
} | ||
Solution.prototype.addToFolder = function (slnFolder, project) { | ||
let globalSection = this.global.find(globalSection => globalSection.sectionName === 'NestedProjects'); | ||
if(!globalSection) { | ||
globalSection = { | ||
sectionName: 'NestedProjects', | ||
sectionValue: 'preSolution', | ||
config: [] | ||
}; | ||
this.global.push(globalSection); | ||
} | ||
globalSection.config.push({ | ||
configName: project.projectGuid || project.getProperty('ProjectGuid'), | ||
configValue: slnFolder.projectGuid | ||
}); | ||
} | ||
function uuidv4() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); | ||
return v.toString(16); | ||
}); | ||
} | ||
module.exports = Solution; |
17935
377
63