如何获取.net版本的子字符串而不是直接在`Install.ps1`脚本中指向`net35`字符串?

时间:2016-07-21 12:18:51

标签: powershell nuget nuget-package

我需要为我的NuGet包的每个程序集设置CopyLocalfalse。 我在Install.ps1文件中执行此操作:

param($installPath, $toolsPath, $package, $project)

$asm_folder = [System.IO.Path]::Combine($installPath, "lib\\net35");

$files = [System.IO.Directory]::EnumerateFiles($asm_folder,"*.dll","TopDirectoryOnly");

$array = New-Object System.Collections.ArrayList;

foreach ($file in $files) {
    $file_name = [System.IO.Path]::GetFileNameWithoutExtension($file);
    $array.Add($file_name);
}

foreach ($reference in $project.Object.References) {

    if($array.Contains($reference.Name)) {

        if($reference.CopyLocal -eq $true) {
            $reference.CopyLocal = $false;
        }
        else {
            $reference.CopyLocal = $true;
        }
    }
}

如何获取(目标项目的).net版本的 substring ,而不是直接将net35字符串指向Install.ps1脚本?

2 个答案:

答案 0 :(得分:0)

您必须读取lib目录中的目录并查看其他版本。像这样:

Get-ChildItem $installPath -Recurse:$false -Filter 'net*' |?{ $_.PSIsContainer }

稍微更改了您的脚本:

param($installPath, $toolsPath, $package, $project)

#here you get the list of NET% folders
$net_folders = @(Get-ChildItem $installPath -Recurse:$false -Filter 'net*' |?{ $_.PSIsContainer })

#then you looking for every 'net' folder for .dll files
$files = @()
foreach ($net_folder in $net_folders){
   $files += @(Get-ChildItem $net_folders -Recurse:$false -Filter '*.dll' | Select @{l='FullName';e={[System.IO.Path]::GetFileNameWithoutExtension($_.FullName)}}| Select -Expand FullName)
}

foreach ($reference in $project.Object.References) {
    if($files.Contains($reference.Name)) {
        #since you are setting copylocal to true anyway you wont need if/else statement
        $reference.CopyLocal = $true;
    }
}

答案 1 :(得分:0)

我的决定对我来说并不愉快,但它有效:

<#
Install.ps1
AutoCAD-YYYY.Net.* NuGet package.

© Andrey Bushman, 2016
https://www.nuget.org/profiles/Bush

This PowerShell script will be launched by NuGet each time when
this package will be installed into the Visual Studio project.

This script sets `CopyLocal` to `false` for each AutoCAD assembly.
#>
param($installPath, $toolsPath, $package, $project)

$asm_root_folder_name = [System.IO.Path]::Combine($installPath,`
"lib");

$net_folders = [System.IO.Directory]::GetDirectories(`
$asm_root_folder_name, 'net*', 'TopDirectoryOnly');

$file_names = New-Object `
'System.Collections.Generic.HashSet[string]';

foreach ($net in $net_folders) {

    $files = [System.IO.Directory]::EnumerateFiles($net,"*.dll"`
    ,"TopDirectoryOnly");

        foreach ($file in $files) {

            $file_name = [System.IO.Path]::`
            GetFileNameWithoutExtension($file);

            $file_names.Add($file_name);
    }
}

foreach ($reference in $project.Object.References) {

    if($file_names.Contains($reference.Name)) {

        $reference.CopyLocal = $false;
    }
}
相关问题