forked from apache/cordova-node-xcode
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure that PbxGroups are not duplicated (#17)
- Loading branch information
1 parent
77267e7
commit 99947af
Showing
5 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ node_modules/* | |
.DS_Store | ||
npm-debug.log | ||
package-lock.json | ||
|
||
.ns-build-pbxgroup-data.json | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function guidMapper(filePath) { | ||
this.filePath = filePath; | ||
this.data = this.loadFromFile(); | ||
} | ||
|
||
guidMapper.prototype.loadFromFile = function () { | ||
try { | ||
const rawData = fs.readFileSync(this.filePath, 'utf8'); | ||
return JSON.parse(rawData); | ||
} catch (error) { | ||
// If file doesn't exist or there's an error parsing it, initialize with an empty object. | ||
return {}; | ||
} | ||
}; | ||
|
||
guidMapper.prototype.writeFileSync = function () { | ||
const jsonData = JSON.stringify(this.data, null, 2); | ||
fs.writeFileSync(this.filePath, jsonData, 'utf8'); | ||
}; | ||
|
||
guidMapper.prototype.addEntry = function (guid, path, name) { | ||
if(!!guid && !! path && !!name){ | ||
this.data[guid] = { path: path, name: name }; | ||
} | ||
}; | ||
|
||
guidMapper.prototype.removeEntry = function (guid) { | ||
if (this.data[guid]) { | ||
delete this.data[guid]; | ||
} | ||
}; | ||
|
||
guidMapper.prototype.getEntries = function () { | ||
return this.data; | ||
}; | ||
|
||
guidMapper.prototype.findEntryGuid = function (name, path) { | ||
for (const guid in this.data) { | ||
if (this.data.hasOwnProperty(guid)) { | ||
const entry = this.data[guid]; | ||
if (entry.path === path && entry.name === name) { | ||
return guid; | ||
} | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
module.exports = guidMapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var guidMapper = require('../lib/guidMapper'); | ||
const fs = require('fs'); | ||
const $uuid = require('uuid'); | ||
const TEST_FILE_NAME = 'test/.ns-build-pbxgroup-data.json'; | ||
const goodGUID = $uuid.v4(); | ||
const goodName = "goodName"; | ||
const badName = 'badName'; | ||
const goodPath = "goodPath"; | ||
const badPath = "badPath"; | ||
exports.setUp = function(callback) { | ||
if(fs.existsSync(TEST_FILE_NAME)){ | ||
fs.rmSync(TEST_FILE_NAME); | ||
} | ||
callback(); | ||
} | ||
exports.tearDown = function(callback) { | ||
if(fs.existsSync(TEST_FILE_NAME)){ | ||
fs.rmSync(TEST_FILE_NAME); | ||
} | ||
callback(); | ||
} | ||
function addTestData(){ | ||
var mapper = new guidMapper(TEST_FILE_NAME); | ||
mapper.addEntry(goodGUID, goodPath, goodName); | ||
mapper.writeFileSync(); | ||
} | ||
exports.operations = { | ||
'should be able to add to map': function(test) { | ||
var mapper = new guidMapper(TEST_FILE_NAME); | ||
mapper.addEntry(goodGUID, goodPath, goodName); | ||
mapper.writeFileSync(); | ||
mapper = new guidMapper(TEST_FILE_NAME); | ||
const result = mapper.findEntryGuid(goodName, goodPath); | ||
|
||
test.ok(result === goodGUID) | ||
test.done(); | ||
}, | ||
'should not match only on name': function(test) { | ||
addTestData(); | ||
var mapper = new guidMapper(TEST_FILE_NAME); | ||
|
||
const result = mapper.findEntryGuid(goodName, badPath); | ||
|
||
test.ok(result === null) | ||
test.done(); | ||
}, | ||
'should not match only on path': function(test) { | ||
addTestData(); | ||
var mapper = new guidMapper(TEST_FILE_NAME); | ||
|
||
const result = mapper.findEntryGuid(badName, goodPath); | ||
|
||
test.ok(result === null) | ||
test.done(); | ||
}, | ||
'can remove': function(test) { | ||
addTestData(); | ||
var mapper = new guidMapper(TEST_FILE_NAME); | ||
mapper.removeEntry(goodGUID); | ||
var result = mapper.findEntryGuid(goodName, goodPath); | ||
|
||
test.ok(result === null); | ||
mapper.writeFileSync(); | ||
result = mapper.findEntryGuid(goodName, goodPath); | ||
test.ok(result === null) | ||
|
||
test.done(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters