<commit_before>﻿import path = require('path');
import tl = require('vsts-task-lib/task');
import sign = require('ios-signing-common/ios-signing-common');
import msbuildhelpers = require('msbuildhelpers/msbuildhelpers');

import { ToolRunner } from 'vsts-task-lib/toolrunner';

function expandSolutionWildcardPatterns(solutionPattern: string): string {
    const matchedSolutionFiles = tl.findMatch(null, solutionPattern, { followSymbolicLinks: false, followSpecifiedSymbolicLink: false });
    tl.debug(`Found ${matchedSolutionFiles ? matchedSolutionFiles.length : 0} solution files matching the pattern.`);

    if (matchedSolutionFiles && matchedSolutionFiles.length > 0) {
        const result = matchedSolutionFiles[0];
        if (matchedSolutionFiles.length > 1) {
            tl.warning(tl.loc('MultipleSolutionsFound', result));
        }

        return result;
    } else {
        throw tl.loc('SolutionDoesNotExist', solutionPattern);
    }
}

async function run() {
    let codesignKeychain: string;
    let profileToDelete: string;

    try {
        tl.setResourcePath(path.join(__dirname, 'task.json'));

        // Get build inputs
        const solutionInput: string = tl.getPathInput('solution', true, false);
        const configuration: string = tl.getInput('configuration', true);
        const clean: boolean = tl.getBoolInput('clean');
        const args: string = tl.getInput('args');
        const packageApp: boolean = tl.getBoolInput('packageApp');
        const buildForSimulator: boolean = tl.getBoolInput('forSimulator');
        const device: string = (buildForSimulator) ? 'iPhoneSimulator' : 'iPhone';
        tl.debug('device: ' + device);
        const cwd: string = tl.getInput('cwd');
        const runNugetRestore: boolean = tl.getBoolInput('runNugetRestore');

        // find the build tool path based on the build tool and location inputs
        const buildTool: string = tl.getInput('buildTool');
        const buildToolLocation: string = tl.getInput('mdtoolLocation', false);
        let buildToolPath: string;
        if (buildToolLocation) {
            // location is specified
            buildToolPath = buildToolLocation;
            if (buildTool === 'xbuild' && !buildToolLocation.toLowerCase().endsWith('xbuild')) {
                buildToolPath = path.join(buildToolLocation, 'xbuild');
            }
            if (buildTool === 'msbuild' && !buildToolLocation.toLowerCase().endsWith('msbuild')) {
                buildToolPath = path.join(buildToolLocation, 'msbuild');
            }
        } else {
            // no build tool path is supplied, check PATH
            if (buildTool === 'msbuild') {
                // check for msbuild 15 or higher, if not fall back to xbuild
                buildToolPath = await msbuildhelpers.getMSBuildPath('15.0');
            } else {
                buildToolPath = tl.which('xbuild', true);
            }
        }
        tl.checkPath(buildToolPath, 'build tool');
        tl.debug('Build tool path = ' + buildToolPath);

        const solutionPath = expandSolutionWildcardPatterns(solutionInput);

        if (clean) {
            const cleanBuildRunner: ToolRunner = tl.tool(buildToolPath);
            cleanBuildRunner.arg(solutionPath);
            cleanBuildRunner.argIf(configuration, '/p:Configuration=' + configuration);
            cleanBuildRunner.argIf(device, '/p:Platform=' + device);
            if (args) {
                cleanBuildRunner.line(args);
            }
            cleanBuildRunner.arg('/t:Clean');
            await cleanBuildRunner.exec();
        }

        if (runNugetRestore) {
            // Find location of nuget
            const nugetPath: string = tl.which('nuget', true);<commit_msg>Pass the task's cwd parameter for wildcard expansion<commit_after>﻿import path = require('path');
import tl = require('vsts-task-lib/task');
import sign = require('ios-signing-common/ios-signing-common');
import msbuildhelpers = require('msbuildhelpers/msbuildhelpers');

import { ToolRunner } from 'vsts-task-lib/toolrunner';

/**
 * Find all filenames starting from `rootDirectory` that match a wildcard pattern.
 * @param rootDirectory Directory to evaluate the pattern on. Falls back to System.DefaultWorkingDirectory and then `process.cwd()` if `null`.
 * @param solutionPattern A filename pattern to evaluate, possibly containing wildcards.
 */
function expandSolutionWildcardPatterns(rootDirectory: string | null, solutionPattern: string): string {
    const matchedSolutionFiles = tl.findMatch(rootDirectory, solutionPattern, { followSymbolicLinks: false, followSpecifiedSymbolicLink: false });
    tl.debug(`Found ${matchedSolutionFiles ? matchedSolutionFiles.length : 0} solution files matching the pattern.`);

    if (matchedSolutionFiles && matchedSolutionFiles.length > 0) {
        const result = matchedSolutionFiles[0];
        if (matchedSolutionFiles.length > 1) {
            tl.warning(tl.loc('MultipleSolutionsFound', result));
        }

        return result;
    } else {
        throw tl.loc('SolutionDoesNotExist', solutionPattern);
    }
}

async function run() {
    let codesignKeychain: string;
    let profileToDelete: string;

    try {
        tl.setResourcePath(path.join(__dirname, 'task.json'));

        // Get build inputs
        const solutionInput: string = tl.getPathInput('solution', true, false);
        const configuration: string = tl.getInput('configuration', true);
        const clean: boolean = tl.getBoolInput('clean');
        const args: string = tl.getInput('args');
        const packageApp: boolean = tl.getBoolInput('packageApp');
        const buildForSimulator: boolean = tl.getBoolInput('forSimulator');
        const device: string = (buildForSimulator) ? 'iPhoneSimulator' : 'iPhone';
        tl.debug('device: ' + device);
        const cwd: string = tl.getInput('cwd');
        const runNugetRestore: boolean = tl.getBoolInput('runNugetRestore');

        // find the build tool path based on the build tool and location inputs
        const buildTool: string = tl.getInput('buildTool');
        const buildToolLocation: string = tl.getInput('mdtoolLocation', false);
        let buildToolPath: string;
        if (buildToolLocation) {
            // location is specified
            buildToolPath = buildToolLocation;
            if (buildTool === 'xbuild' && !buildToolLocation.toLowerCase().endsWith('xbuild')) {
                buildToolPath = path.join(buildToolLocation, 'xbuild');
            }
            if (buildTool === 'msbuild' && !buildToolLocation.toLowerCase().endsWith('msbuild')) {
                buildToolPath = path.join(buildToolLocation, 'msbuild');
            }
        } else {
            // no build tool path is supplied, check PATH
            if (buildTool === 'msbuild') {
                // check for msbuild 15 or higher, if not fall back to xbuild
                buildToolPath = await msbuildhelpers.getMSBuildPath('15.0');
            } else {
                buildToolPath = tl.which('xbuild', true);
            }
        }
        tl.checkPath(buildToolPath, 'build tool');
        tl.debug('Build tool path = ' + buildToolPath);

        const solutionPath = expandSolutionWildcardPatterns(cwd, solutionInput);

        if (clean) {
            const cleanBuildRunner: ToolRunner = tl.tool(buildToolPath);
            cleanBuildRunner.arg(solutionPath);
            cleanBuildRunner.argIf(configuration, '/p:Configuration=' + configuration);
            cleanBuildRunner.argIf(device, '/p:Platform=' + device);
            if (args) {
                cleanBuildRunner.line(args);
            }
            cleanBuildRunner.arg('/t:Clean');
            await cleanBuildRunner.exec();
        }

        if (runNugetRestore) {
            // Find location of nuget
            const nugetPath: string = tl.which('nuget', true);
{'fraction_non_alphanumeric': 0.11320274739252098, 'fraction_numerical': 0.0022894937674891886, 'mean_word_length': 2.7658045977011496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1758240', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2075, 'n_words': 508}