Skip to content

Commit

Permalink
Fix: Detect changes correctly in conandata and conanfile (#244)
Browse files Browse the repository at this point in the history
* fix check changes

* fix guards

* dont crash if conandata modified

* more fixes

* 2.0.6 version
  • Loading branch information
czoido authored Jun 24, 2024
1 parent aec456c commit d081863
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
23 changes: 11 additions & 12 deletions ConanFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class ConanFileManager
"# To keep your changes, remove these comment lines, but the plugin won't be able to modify your requirements"
};

private static bool IsFileCommentGuarded(string path)
public static bool IsFileCommentGuarded(string path)
{
if (!File.Exists(path)) return false;

Expand All @@ -36,12 +36,13 @@ private static bool IsFileCommentGuarded(string path)
public static string[] GetConandataRequirements(string projectDirectory)
{
string path = Path.Combine(projectDirectory, "conandata.yml");
if (IsFileCommentGuarded(path))
if (File.Exists(path))
{
string[] conandataContents = File.ReadAllLines(path);

var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();

var result = deserializer.Deserialize<Requirements>(string.Join(Environment.NewLine, conandataContents));
Expand All @@ -54,10 +55,10 @@ public static string[] GetConandataRequirements(string projectDirectory)
return new string[] { };
}

private static void WriteConanfileIfNecessary(string projectDirectory)
public static bool ReCreateConanfile(string projectDirectory)
{
string conanfilePath = Path.Combine(projectDirectory, "conanfile.py");
if (!IsFileCommentGuarded(conanfilePath))
if (IsFileCommentGuarded(conanfilePath) || !File.Exists(conanfilePath))
{
string conanfileContents = string.Join(Environment.NewLine,
_modifyCommentGuard.Concat(new string[]
Expand All @@ -84,26 +85,24 @@ private static void WriteConanfileIfNecessary(string projectDirectory)
);

File.WriteAllText(conanfilePath, conanfileContents);
return true;
}
return false;
}

private static void WriteConandataIfNecessary(string projectDirectory)
public static bool ReCreateConanData(string projectDirectory)
{
string conandataPath = Path.Combine(projectDirectory, "conandata.yml");
if (!IsFileCommentGuarded(conandataPath))
if (IsFileCommentGuarded(conandataPath) || !File.Exists(conandataPath))
{
string conandataContents = string.Join(Environment.NewLine,
_modifyCommentGuard.Concat(new string[] { "requirements:" })
);

File.WriteAllText(conandataPath, conandataContents);
return true;
}
}

public static void WriteNecessaryConanGuardedFiles(string projectDirectory)
{
WriteConanfileIfNecessary(projectDirectory);
WriteConandataIfNecessary(projectDirectory);
return false;
}

public static void WriteNewRequirement(string projectDirectory, string newRequirement)
Expand Down
54 changes: 39 additions & 15 deletions ConanToolWindowControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,6 @@ private void InstallButton_Click(object sender, RoutedEventArgs e)
var selectedLibrary = LibraryNameLabel.Content.ToString();
var selectedVersion = VersionsComboBox.SelectedItem.ToString();

MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} added to conandata.yml", "Conan C/C++ Package Manager");

InstallButton.Visibility = Visibility.Collapsed;
RemoveButton.Visibility = Visibility.Visible;

ThreadHelper.ThrowIfNotOnUIThread();

Project startupProject = ProjectConfigurationManager.GetStartupProject(_dte);
Expand All @@ -268,11 +263,30 @@ private void InstallButton_Click(object sender, RoutedEventArgs e)
string projectFilePath = startupProject.FullName;
string projectDirectory = Path.GetDirectoryName(projectFilePath);

ConanFileManager.WriteNecessaryConanGuardedFiles(projectDirectory);
ConanFileManager.WriteNewRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
ConanFileManager.ReCreateConanfile(projectDirectory);

string conandataPath = Path.Combine(projectDirectory, "conandata.yml");

if (!File.Exists(conandataPath)) {
ConanFileManager.ReCreateConanData(projectDirectory);
}

if (ConanFileManager.IsFileCommentGuarded(conandataPath)) {
ConanFileManager.WriteNewRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} added to conandata.yml", "Conan C/C++ Package Manager");

InstallButton.Visibility = Visibility.Collapsed;
RemoveButton.Visibility = Visibility.Visible;
VersionsComboBox.IsEnabled = false;
}
else {
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} could not be added to conandata.yml because it was modified. Please, update the file manually.",
"Conan C/C++ Package Manager",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}

_ = ProjectConfigurationManager.SaveConanPrebuildEventsAllConfigAsync(startupProject);
VersionsComboBox.IsEnabled = false;
FilterListView(LibrarySearchTextBox.Text, ShowPackagesCheckbox.IsChecked ?? false);
}
}
Expand All @@ -282,11 +296,6 @@ private void RemoveButton_Click(object sender, RoutedEventArgs e)
var selectedLibrary = LibraryNameLabel.Content.ToString();
var selectedVersion = VersionsComboBox.SelectedItem.ToString();

MessageBox.Show($"Removing {selectedLibrary} version {selectedVersion}", "Conan C/C++ Package Manager");

InstallButton.Visibility = Visibility.Visible;
RemoveButton.Visibility = Visibility.Collapsed;

ThreadHelper.ThrowIfNotOnUIThread();

Array activeSolutionProjects = _dte.ActiveSolutionProjects as Array;
Expand All @@ -295,8 +304,23 @@ private void RemoveButton_Click(object sender, RoutedEventArgs e)
string projectFilePath = activeProject.FullName;
string projectDirectory = Path.GetDirectoryName(projectFilePath);

ConanFileManager.RemoveRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
VersionsComboBox.IsEnabled = true;
string conandataPath = Path.Combine(projectDirectory, "conandata.yml");

if (ConanFileManager.IsFileCommentGuarded(conandataPath)) {
ConanFileManager.RemoveRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
MessageBox.Show($"Removing {selectedLibrary} version {selectedVersion}", "Conan C/C++ Package Manager");

InstallButton.Visibility = Visibility.Visible;
RemoveButton.Visibility = Visibility.Collapsed;
VersionsComboBox.IsEnabled = true;
}
else {
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} could not be removed from conandata.yml because it was modified. Please, update the file manually.",
"Conan C/C++ Package Manager",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}

FilterListView(LibrarySearchTextBox.Text, ShowPackagesCheckbox.IsChecked ?? false);
}

Expand Down
10 changes: 8 additions & 2 deletions ProjectConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,19 @@ REM Initialize flags
if exist "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" (
echo Checking changes in conandata.yml
fc ""conandata.yml"" "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" > nul
if %errorlevel% equ 1 set performInstall=1
if errorlevel 1 (
set performInstall=1
echo Changes detected in conandata.yml
)
)
if exist "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" (
echo Checking changes in conanfile.py
fc ""conanfile.py"" "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" > nul
if %errorlevel% equ 1 set performInstall=1
if errorlevel 1 (
set performInstall=1
echo Changes detected in conanfile.py
)
)
REM Check for the .runconan file that indicates changes in profiles
Expand Down
2 changes: 1 addition & 1 deletion source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="VSConanPackage.4d0379e2-2698-4e66-89de-6ead71165e9f" Version="2.0.5" Language="en-US" Publisher="Conan" />
<Identity Id="VSConanPackage.4d0379e2-2698-4e66-89de-6ead71165e9f" Version="2.0.6" Language="en-US" Publisher="Conan" />
<DisplayName>Conan Extension for Visual Studio</DisplayName>
<Description xml:space="preserve">Conan Extension for Visual Studio automates the use of the Conan C/C++ package manager for retrieving dependencies within Visual Studio projects.</Description>
<MoreInfo>https://github.com/conan-io/conan-vs-extension</MoreInfo>
Expand Down

0 comments on commit d081863

Please sign in to comment.