guibranco / gstraccini-bot-service

🤖 :octocat: GStraccini-bot automates repository management, ensuring organization and health by handling pull requests, issues, comments, and commits.
https://gstraccini.bot
MIT License
2 stars 0 forks source link

[FEATURE] Add command to `FixCsproj` files based on `packages.config` #19

Open guibranco opened 11 months ago

guibranco commented 11 months ago

Description

We need to create a new command in the PHP Bot that will synchronize the version of NuGet packages specified in the packages.config file with the versions used in the .csproj files for .NET framework projects. Each directory in the project contains one packages.config and one .csproj file. The command should ensure that the versions of the packages in the .csproj file match those in the packages.config.

Solution

  1. Read the packages.config File:

    • Parse the packages.config file to extract package names and their versions.
    • Example format:
      <?xml version="1.0" encoding="utf-8"?>
      <packages>
      <package id="PagedList" version="1.17.0.0" targetFramework="net48" />
      <package id="PagedList.Mvc" version="4.5.0.0" targetFramework="net48" />
      </packages>
  2. Read and Update the .csproj File:

    • For each package listed in packages.config, find the corresponding <Reference> entry in the .csproj file.
    • Update the Version attribute of the <Reference> element to match the version specified in packages.config.
    • Example format:
      <Reference Include="PagedList, Version=1.17.0.0, Culture=neutral, PublicKeyToken=abbb863e9397c5e1, processorArchitecture=MSIL">
      <HintPath>..\..\packages\PagedList.1.17.0.0\lib\net40\PagedList.dll</HintPath>
      <Private>True</Private>
      </Reference>
      <Reference Include="PagedList.Mvc, Version=4.5.0.0, Culture=neutral, PublicKeyToken=abbb863e9397c5e1, processorArchitecture=MSIL">
      <HintPath>..\..\packages\PagedList.Mvc.4.5.0.0\lib\net40\PagedList.Mvc.dll</HintPath>
      <Private>True</Private>
      </Reference>
  3. Implementation Details:

    • Create a new command for the PHP Bot that:
      • Reads the packages.config file from the directory.
      • Updates the corresponding .csproj file based on the packages.config file.
      • Handles directories that contain both packages.config and .csproj files.
  4. Example PHP Code:

    use GuiBranco\Pancake\Request;
    
    function fixCsprojFiles($directory) {
       // Path to the packages.config and .csproj files
       $packagesConfigPath = $directory . '/packages.config';
       $csprojPath = $directory . '/project.csproj';
    
       // Load and parse the packages.config file
       $packages = simplexml_load_file($packagesConfigPath);
       $packageVersions = [];
       foreach ($packages->package as $package) {
           $packageVersions[(string)$package['id']] = (string)$package['version'];
       }
    
       // Load and parse the .csproj file
       $csprojContent = file_get_contents($csprojPath);
       foreach ($packageVersions as $packageId => $version) {
           $pattern = '/<Reference Include="' . preg_quote($packageId, '/') . ', Version=[^"]+"/';
           $replacement = '<Reference Include="' . $packageId . ', Version=' . $version . '">';
           $csprojContent = preg_replace($pattern, $replacement, $csprojContent);
       }
    
       // Save the updated .csproj file
       file_put_contents($csprojPath, $csprojContent);
    }
    
    // Example usage
    fixCsprojFiles('/path/to/directory');

Tech Notes

gitauto-ai[bot] commented 3 months ago

@guibranco Pull request completed! Check it out here https://github.com/guibranco/gstraccini-bot/pull/354 🚀

Note: I automatically create a pull request for an unassigned and open issue in order from oldest to newest once a day at 00:00 UTC, as long as you have remaining automation usage. Should you have any questions or wish to change settings or limits, please feel free to contact info@gitauto.ai or invite us to Slack Connect.