<commit_before>﻿import fs = require('fs');
import https = require('https');
import path = require('path');
import Q = require('q');
import tl = require('vsts-task-lib/task');
import url = require('url');

import * as auth from "./Authentication"
import * as ngToolRunner from "./NuGetToolRunner"
import * as util from 'util';
import * as os from 'os';

var xmlreader = require('xmlreader');

export interface IPackageSource {
    feedName: string;
    feedUri: string;
}

export class NuGetConfigHelper {

    private _nugetPath: string;
    private _nugetConfigPath: string;
    private _authInfo: auth.NuGetAuthInfo;
    private _environmentSettings: ngToolRunner.NuGetEnvironmentSettings;

    private tempNugetConfigBaseDir = tl.getVariable('Agent.BuildDirectory') || tl.getVariable('Agent.ReleaseDirectory') || process.cwd();
    private tempNugetConfigDir = path.join(this.tempNugetConfigBaseDir, 'Nuget');
    private tempNugetConfigFileName = 'tempNuGet_' + tl.getVariable('build.buildId') + '.config';
    public tempNugetConfigPath = path.join(this.tempNugetConfigDir, this.tempNugetConfigFileName);

    constructor(nugetPath: string, nugetConfigPath: string, authInfo: auth.NuGetAuthInfo, environmentSettings: ngToolRunner.NuGetEnvironmentSettings) {
        this._nugetPath = nugetPath;
        this._nugetConfigPath = nugetConfigPath;
        this._authInfo = authInfo;
        this._environmentSettings = environmentSettings;
    }

    public ensureTempConfigCreated() {
        // save nuget config file to agent build directory
        tl._writeLine('save nuget.config to temp config file');
        if (!(fs.existsSync(this.tempNugetConfigDir))) {
            fs.mkdirSync(this.tempNugetConfigDir);
        };

        if (this._nugetConfigPath) {
            tl.cp("-f", this._nugetConfigPath, this.tempNugetConfigPath);
        }
        else {
            // small file, use writeFileSync
            fs.writeFileSync(this.tempNugetConfigPath, "<configuration/>");
        }
    }

    public setSources(packageSources: IPackageSource[]): void {
        this.ensureTempConfigCreated();

        // remove sources
        tl._writeLine(tl.loc("NGCommon_RemovingSources"));
        this.removeSourcesInNugetConfig(packageSources);

        // add sources
        tl._writeLine(tl.loc("NGCommon_AddingSources"));
        this.addSourcesInNugetConfig(packageSources);
    }

    public getSourcesFromConfig(): Q.Promise<IPackageSource[]> {
        // load content of the user's nuget.config
        var xmlString = fs.readFileSync(this._nugetConfigPath).toString();

        // strip BOM; xml parser doesn't like it
        if (xmlString.charCodeAt(0) === 0xFEFF) {<commit_msg>Don't use tl.cp to copy nuget.config, as that breaks with tfvc<commit_after>﻿import fs = require('fs');
import https = require('https');
import path = require('path');
import Q = require('q');
import tl = require('vsts-task-lib/task');
import url = require('url');

import * as auth from "./Authentication"
import * as ngToolRunner from "./NuGetToolRunner"
import * as util from 'util';
import * as os from 'os';

var xmlreader = require('xmlreader');

export interface IPackageSource {
    feedName: string;
    feedUri: string;
}

export class NuGetConfigHelper {

    private _nugetPath: string;
    private _nugetConfigPath: string;
    private _authInfo: auth.NuGetAuthInfo;
    private _environmentSettings: ngToolRunner.NuGetEnvironmentSettings;

    private tempNugetConfigBaseDir = tl.getVariable('Agent.BuildDirectory') || tl.getVariable('Agent.ReleaseDirectory') || process.cwd();
    private tempNugetConfigDir = path.join(this.tempNugetConfigBaseDir, 'Nuget');
    private tempNugetConfigFileName = 'tempNuGet_' + tl.getVariable('build.buildId') + '.config';
    public tempNugetConfigPath = path.join(this.tempNugetConfigDir, this.tempNugetConfigFileName);

    constructor(nugetPath: string, nugetConfigPath: string, authInfo: auth.NuGetAuthInfo, environmentSettings: ngToolRunner.NuGetEnvironmentSettings) {
        this._nugetPath = nugetPath;
        this._nugetConfigPath = nugetConfigPath;
        this._authInfo = authInfo;
        this._environmentSettings = environmentSettings;
    }

    public ensureTempConfigCreated() {
        // save nuget config file to agent build directory
        tl._writeLine('save nuget.config to temp config file');
        if (!(fs.existsSync(this.tempNugetConfigDir))) {
            fs.mkdirSync(this.tempNugetConfigDir);
        };

        if (this._nugetConfigPath) {
            // don't use cp as that copies the read-only flag, and tfvc sets that on files
            let content = fs.readFileSync(this._nugetConfigPath);
            fs.writeFileSync(this.tempNugetConfigPath, content);
        }
        else {
            // small file, use writeFileSync
            fs.writeFileSync(this.tempNugetConfigPath, "<configuration/>");
        }
    }

    public setSources(packageSources: IPackageSource[]): void {
        this.ensureTempConfigCreated();

        // remove sources
        tl._writeLine(tl.loc("NGCommon_RemovingSources"));
        this.removeSourcesInNugetConfig(packageSources);

        // add sources
        tl._writeLine(tl.loc("NGCommon_AddingSources"));
        this.addSourcesInNugetConfig(packageSources);
    }

    public getSourcesFromConfig(): Q.Promise<IPackageSource[]> {
        // load content of the user's nuget.config
        var xmlString = fs.readFileSync(this._nugetConfigPath).toString();

        // strip BOM; xml parser doesn't like it
        if (xmlString.charCodeAt(0) === 0xFEFF) {
{'fraction_non_alphanumeric': 0.10954000357973868, 'fraction_numerical': 0.0007159477358152855, 'mean_word_length': 3.5028203062046734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6737648', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1562, 'n_words': 353}