none
Insert Import into project file at position 0 and save without prompt RRS feed

  • Question

  • I am building a Nuget package which has a tools folder with install.ps1. This Nuget package is dependent on NugetPowerTools package which provides some APIs to work with the project and its properties. I have pasted the code used in my install.ps1 below.

    param($installPath, $toolsPath, $package, $project)
    $buildProject = Get-MSBuildProject
    $import = $buildProject.Xml.AddImport("Custom.Targets")
    $buildProject.Save()

    I have 2 questions.

    1. The AddImport appends the target at the end (i.e., after other Import lines). I need to have my Import as the first line (since I have some functionality in this targets file which needs to be executed before others). Is it possible to insert the Import at first (i.e., my import with Custom.Targets should be the first item of Import in the project file)? I can't find an Insert method.

    2. Once the project file is modified by this install.ps1, there is a pop up /prompt in Visual Studio with 4 options namely, SaveAs, Discard, Overwrite and Ignore. I want to save & overwrite the project file without this prompt / pop up from my install.ps1. This is to avoid any accidental mistake by the user. To overcome, I found a link below, which has the following line of code, which helps me overcome this issue.

    https://stackoverflow.com/questions/12292577/how-can-i-reload-a-visual-studio-project-thru-a-nuget-powershell-script

    $(get-item $project.FullName).lastwritetime=get-date

    But now, I get another pop up / prompt with options to Reload, Reload All, Ignore, Ignore All. Is there a way to Reload this project without this prompt?

    Please help.


    Ven



    • Edited by Venh Tuesday, December 24, 2019 4:37 AM
    Tuesday, December 24, 2019 4:17 AM

All replies

  • Hi Venh,

    Welcome to MSDN forum.

    According to your description, I have learned about NugetPowerTool nuget package, which is third package and has not been not updated in a long time. 

    In addition, the thread is more related to this package, and the PS command line is more from the psm and psd files which are in tools folder. Maybe you could check them for your requirement.

    BTW, we recommend you could redirect to stack overflow with nuget, powershell and visual studio tag, then you could get dedicated support there.

    Thank you for understanding.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Wednesday, December 25, 2019 3:31 AM
  • Hi,

    Thanks for your reply. I am ok not using NugetPowerTools, but I am not getting enough examples to achieve my requirement. If you could please help me by providing the syntax without NugetPowerTools, I would be more than happy to use it. In fact, I don't want to use any dependency.


    Ven

    Wednesday, December 25, 2019 3:41 PM
  • Hi Venh,

    Sorry for delay in reply.

    For your requirement, we recommend you could create a console app to add custom target into your project using AddTarget() method which is under ProjectRootElement class

    After my research, it seems that there isn't any related interface to reload your project in VS IDE, so we suggest you could using msbuild tool without IDE to build your project.

    After that, you could combine them in a bat file, and run it developer command prompt VS2019, which is like:

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, December 27, 2019 7:38 AM