Home > Software design >  How to make visual studio project use <ReferencePackage> or DLL reference And <ProjectRefer
How to make visual studio project use <ReferencePackage> or DLL reference And <ProjectRefer

Time:02-22

Please I have a problem. sorry if question title are not recognized well, I can't upload full-image during my reputations.

Edit: I need something like that maybe

Configuration Options

  • I guess the whole gape in main project .csproj file. I need to modify it to allow Configuration to switch between Nuget build that visible to other developers and Debug that only visible at me. When choose it the <ProjectReference> should load and should become visible at my solution.
  • You can ignore Github things I mentioned. A repo can be private/public wihtout problem.
  • Problem Short Description:

    1. I have main project in solution A. (Must be GIT public for other people)
    2. I have class library in solution B (Which maybe used in 2 main projects) (Must be GIT private repo for me only)
    3. I need the class library source only visible for me not other people. they just see Package or DLL.
    4. The main project are public Git repo, while class library are private Git repo.
    5. In my case I need to setup a 2 types of MSBuild configuration. (Debug/) and (Nuget/ )
    6. the other people only allowed to use which are private nuget package and they must not debug the class library.
    7. I need only me to use (Debug/<ProjectReference>) -> So I can change class library directly and build Nuget package for other people without PDB file,etc included.

    What I try to do? What topics I read?

    1. I Following topics I follow: Error Message Screenshot

      Do I need change something?

      CodePudding user response:

      Currently there's no official solution for that.

      So if anyone need what approach I collected and I modify some tags to prevent conflict PackageName with ProjectName, Here's final solution, Which original copied from https://github.com/dotnet/sdk/issues/1151#issuecomment-459275750 Many thanks to script author:

      Here's enhancement version of it:

      <PropertyGroup>
          <ReplacePackageReferences Condition="'$(ReplacePackageReferences)' == ''">true</ReplacePackageReferences>
          <ReplaceProjectReferences Condition="'$(ReplaceProjectReferences)' == ''">true</ReplaceProjectReferences>
      </PropertyGroup>
      <Choose>
          <When Condition="'$(SolutionPath)' != '' AND '$(SolutionPath)' != '*undefined*' AND Exists('$(SolutionPath)')">
              <PropertyGroup>
                  <SolutionFileContent>$([System.IO.File]::ReadAllText($(SolutionPath)))</SolutionFileContent>
                  <SmartSolutionDir>$([System.IO.Path]::GetDirectoryName( $(SolutionPath) ))</SmartSolutionDir>
                  <RegexPattern>(?&lt;="[PackageName]", ")(.*)(?=", ")</RegexPattern>
                  <HasSolution>true</HasSolution>
              </PropertyGroup>
          </When>
          <Otherwise>
              <PropertyGroup>
                  <HasSolution>false</HasSolution>
              </PropertyGroup>
          </Otherwise>
      </Choose>
      <Choose>
          <When Condition="$(ReplacePackageReferences) AND $(HasSolution)">
              <ItemGroup>
                  <!-- Keep the identity of the  packagereference -->
                  <SmartPackageReference Include="@(PackageReference)">
                      <InProject>false</InProject>
                      <PackageName>%(Identity)</PackageName>
                      <InSolution>$(SolutionFileContent.Contains('\%(Identity).csproj'))</InSolution>
                  </SmartPackageReference>
                  <!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
                  <PackageInSolution Include="@(SmartPackageReference -&gt; WithMetadataValue('InSolution', True) )">
                      <Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
                      <SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$(SolutionFileContent)', '%(Pattern)' ))</SmartPath>
                      <ProjName>'%(PackageName)'</ProjName>
                  </PackageInSolution>
                  <ProjectReference Include="@(PackageInSolution -> '$(SmartSolutionDir)\%(SmartPath)' )">
                      <Name>@(PackageInSolution -&gt; %(ProjName))</Name>
                  </ProjectReference>
                  <!-- Remove the package references that are now referenced as projects -->
                  <PackageReference Remove="@(PackageInSolution -> '%(PackageName)' )" />
              </ItemGroup>
          </When>
          <When Condition="$(ReplaceProjectReferences) AND '$(_RestoreSolutionFileUsed)' == ''">
              <ItemGroup>
                  <!-- Keep the identity of the  project reference (relative path), determine the project name and whether the project is contained in the current solution -->
                  <SmartProjectReference Include="@(ProjectReference)">
                      <OriginalIdentity>%(Identity)</OriginalIdentity>
                      <ProjectName>$([System.IO.Path]::GetFileNameWithoutExtension( $([System.IO.Path]::GetFullPath( '%(OriginalIdentity)' )) ))</ProjectName>
                      <InSolution>$(SolutionFileContent.Contains('\%(ProjectName).csproj'))</InSolution>
                  </SmartProjectReference>
                  <!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
                  <ProjectNotInSolution Include="@(SmartProjectReference -&gt; WithMetadataValue('InSolution', False) )">
                  </ProjectNotInSolution>
                  <!--Reference the latest version of the package (why not * ? > see https://github.com/NuGet/Home/issues/7328-->
                  <PackageReference Include="@(ProjectNotInSolution -> '%(ProjectName)' )" Version="*" />
                  <!-- Remove the project references that are now referenced as packages -->
                  <ProjectReference Remove="@(ProjectNotInSolution -> '%(OriginalIdentity)' )" />
              </ItemGroup>
          </When>
      </Choose>
      

      Note if you need to auto update nuget package then you must specify the package above the script provided above:

      <ItemGroup>
            <PackageReferen``ce Include="FooClassLibrary" Version="*"/>
        </ItemGroup>
      

      Feel free to modify or edit above script or edit the answer. If you found better answer please post it (Ex You create new extension for that or something).

    • Related