@@ -35,8 +35,7 @@ const fs = __importStar(__nccwpck_require__(7147));
3535const path = __importStar(__nccwpck_require__(1017));
3636const core = __importStar(__nccwpck_require__(2186));
3737const github = __importStar(__nccwpck_require__(5438));
38- const xmlbuilder = __importStar(__nccwpck_require__(2958));
39- const xmlParser = __importStar(__nccwpck_require__(7448));
38+ const fast_xml_parser_1 = __nccwpck_require__(2603);
4039function configAuthentication(feedUrl, existingFileLocation = '', processRoot = process.cwd()) {
4140 const existingNuGetConfig = path.resolve(processRoot, existingFileLocation === ''
4241 ? getExistingNugetConfig(processRoot)
@@ -59,8 +58,8 @@ function getExistingNugetConfig(processRoot) {
5958 return defaultConfigName;
6059}
6160function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
61+ var _a, _b;
6262 core.info(`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`);
63- let xml;
6463 let sourceKeys = [];
6564 let owner = core.getInput('owner');
6665 let sourceUrl = feedUrl;
@@ -73,76 +72,117 @@ function writeFeedToFile(feedUrl, existingFileLocation, tempFileLocation) {
7372 if (fs.existsSync(existingFileLocation)) {
7473 // get key from existing NuGet.config so NuGet/dotnet can match credentials
7574 const curContents = fs.readFileSync(existingFileLocation, 'utf8');
76- const json = xmlParser.parse(curContents, { ignoreAttributes: false });
75+ const parserOptions = {
76+ ignoreAttributes: false
77+ };
78+ const parser = new fast_xml_parser_1.XMLParser(parserOptions);
79+ const json = parser.parse(curContents);
7780 if (typeof json.configuration === 'undefined') {
7881 throw new Error(`The provided NuGet.config seems invalid.`);
7982 }
80- if (typeof json.configuration.packageSources != 'undefined') {
81- if (typeof json.configuration.packageSources.add != 'undefined') {
82- // file has at least one <add>
83- if (typeof json.configuration.packageSources.add[0] === 'undefined') {
84- // file has only one <add>
85- if (json.configuration.packageSources.add['@_value']
86- .toLowerCase()
87- .includes(feedUrl.toLowerCase())) {
88- const key = json.configuration.packageSources.add['@_key'];
83+ if ((_b = (_a = json.configuration) === null || _a === void 0 ? void 0 : _a.packageSources) === null || _b === void 0 ? void 0 : _b.add) {
84+ const packageSources = json.configuration.packageSources.add;
85+ if (Array.isArray(packageSources)) {
86+ packageSources.forEach(source => {
87+ const value = source['@_value'];
88+ core.debug(`source '${value}'`);
89+ if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
90+ const key = source['@_key'];
8991 sourceKeys.push(key);
9092 core.debug(`Found a URL with key ${key}`);
9193 }
92- }
93- else {
94- // file has 2+ <add>
95- for (let i = 0; i < json.configuration.packageSources.add.length; i++) {
96- const source = json.configuration.packageSources.add[i];
97- const value = source['@_value'];
98- core.debug(`source '${value}'`);
99- if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
100- const key = source['@_key'];
101- sourceKeys.push(key);
102- core.debug(`Found a URL with key ${key}`);
103- }
104- }
94+ });
95+ }
96+ else {
97+ if (packageSources['@_value']
98+ .toLowerCase()
99+ .includes(feedUrl.toLowerCase())) {
100+ const key = packageSources['@_key'];
101+ sourceKeys.push(key);
102+ core.debug(`Found a URL with key ${key}`);
105103 }
106104 }
107105 }
108106 }
109- xml = xmlbuilder
110- .create('configuration')
111- .ele('config')
112- .ele('add', { key: 'defaultPushSource', value: sourceUrl })
113- .up()
114- .up();
107+ const xmlSource = [
108+ {
109+ '?xml': [
110+ {
111+ '#text': ''
112+ }
113+ ],
114+ ':@': {
115+ '@_version': '1.0'
116+ }
117+ },
118+ {
119+ configuration: [
120+ {
121+ config: [
122+ {
123+ add: [],
124+ ':@': {
125+ '@_key': 'defaultPushSource',
126+ '@_value': sourceUrl
127+ }
128+ }
129+ ]
130+ }
131+ ]
132+ }
133+ ];
115134 if (!sourceKeys.length) {
116135 let keystring = 'Source';
117- xml = xml
118- .ele('packageSources')
119- .ele('add', { key: keystring, value: sourceUrl })
120- .up()
121- .up();
136+ xmlSource[1].configuration.push({
137+ packageSources: [
138+ {
139+ add: [],
140+ ':@': {
141+ '@_key': keystring,
142+ '@_value': sourceUrl
143+ }
144+ }
145+ ]
146+ });
122147 sourceKeys.push(keystring);
123148 }
124- xml = xml.ele('packageSourceCredentials') ;
149+ const packageSourceCredentials = [] ;
125150 sourceKeys.forEach(key => {
126151 if (!isValidKey(key)) {
127152 throw new Error("Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again.");
128153 }
129- xml = xml
130- .ele(key)
131- .ele('add', { key: 'Username', value: owner })
132- .up()
133- .ele('add', {
134- key: 'ClearTextPassword',
135- value: process.env.NUGET_AUTH_TOKEN
136- })
137- .up()
138- .up();
154+ packageSourceCredentials.push({
155+ [key]: [
156+ {
157+ add: [],
158+ ':@': {
159+ '@_key': 'Username',
160+ '@_value': owner
161+ }
162+ },
163+ {
164+ add: [],
165+ ':@': {
166+ '@_key': 'ClearTextPassword',
167+ '@_value': process.env.NUGET_AUTH_TOKEN
168+ }
169+ }
170+ ]
171+ });
172+ });
173+ xmlSource[1].configuration.push({
174+ packageSourceCredentials
139175 });
140- // If NuGet fixes itself such that on Linux it can look for environment variables in the config file (it doesn't seem to work today),
141- // use this for the value above
142- // process.platform == 'win32'
143- // ? '%NUGET_AUTH_TOKEN%'
144- // : '$NUGET_AUTH_TOKEN'
145- const output = xml.end({ pretty: true });
176+ const xmlBuilderOptions = {
177+ format: true,
178+ ignoreAttributes: false,
179+ preserveOrder: true,
180+ allowBooleanAttributes: true,
181+ suppressBooleanAttributes: true,
182+ suppressEmptyNode: true
183+ };
184+ const builder = new fast_xml_parser_1.XMLBuilder(xmlBuilderOptions);
185+ const output = builder.build(xmlSource).trim();
146186 fs.writeFileSync(tempFileLocation, output);
147187}
148188
0 commit comments