Skip to content

Commit d081863

Browse files
authored
Fix: Detect changes correctly in conandata and conanfile (#244)
* fix check changes * fix guards * dont crash if conandata modified * more fixes * 2.0.6 version
1 parent aec456c commit d081863

File tree

4 files changed

+59
-30
lines changed

4 files changed

+59
-30
lines changed

ConanFileManager.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class ConanFileManager
1515
"# To keep your changes, remove these comment lines, but the plugin won't be able to modify your requirements"
1616
};
1717

18-
private static bool IsFileCommentGuarded(string path)
18+
public static bool IsFileCommentGuarded(string path)
1919
{
2020
if (!File.Exists(path)) return false;
2121

@@ -36,12 +36,13 @@ private static bool IsFileCommentGuarded(string path)
3636
public static string[] GetConandataRequirements(string projectDirectory)
3737
{
3838
string path = Path.Combine(projectDirectory, "conandata.yml");
39-
if (IsFileCommentGuarded(path))
39+
if (File.Exists(path))
4040
{
4141
string[] conandataContents = File.ReadAllLines(path);
4242

4343
var deserializer = new DeserializerBuilder()
4444
.WithNamingConvention(UnderscoredNamingConvention.Instance)
45+
.IgnoreUnmatchedProperties()
4546
.Build();
4647

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

57-
private static void WriteConanfileIfNecessary(string projectDirectory)
58+
public static bool ReCreateConanfile(string projectDirectory)
5859
{
5960
string conanfilePath = Path.Combine(projectDirectory, "conanfile.py");
60-
if (!IsFileCommentGuarded(conanfilePath))
61+
if (IsFileCommentGuarded(conanfilePath) || !File.Exists(conanfilePath))
6162
{
6263
string conanfileContents = string.Join(Environment.NewLine,
6364
_modifyCommentGuard.Concat(new string[]
@@ -84,26 +85,24 @@ private static void WriteConanfileIfNecessary(string projectDirectory)
8485
);
8586

8687
File.WriteAllText(conanfilePath, conanfileContents);
88+
return true;
8789
}
90+
return false;
8891
}
8992

90-
private static void WriteConandataIfNecessary(string projectDirectory)
93+
public static bool ReCreateConanData(string projectDirectory)
9194
{
9295
string conandataPath = Path.Combine(projectDirectory, "conandata.yml");
93-
if (!IsFileCommentGuarded(conandataPath))
96+
if (IsFileCommentGuarded(conandataPath) || !File.Exists(conandataPath))
9497
{
9598
string conandataContents = string.Join(Environment.NewLine,
9699
_modifyCommentGuard.Concat(new string[] { "requirements:" })
97100
);
98101

99102
File.WriteAllText(conandataPath, conandataContents);
103+
return true;
100104
}
101-
}
102-
103-
public static void WriteNecessaryConanGuardedFiles(string projectDirectory)
104-
{
105-
WriteConanfileIfNecessary(projectDirectory);
106-
WriteConandataIfNecessary(projectDirectory);
105+
return false;
107106
}
108107

109108
public static void WriteNewRequirement(string projectDirectory, string newRequirement)

ConanToolWindowControl.xaml.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,6 @@ private void InstallButton_Click(object sender, RoutedEventArgs e)
254254
var selectedLibrary = LibraryNameLabel.Content.ToString();
255255
var selectedVersion = VersionsComboBox.SelectedItem.ToString();
256256

257-
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} added to conandata.yml", "Conan C/C++ Package Manager");
258-
259-
InstallButton.Visibility = Visibility.Collapsed;
260-
RemoveButton.Visibility = Visibility.Visible;
261-
262257
ThreadHelper.ThrowIfNotOnUIThread();
263258

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

271-
ConanFileManager.WriteNecessaryConanGuardedFiles(projectDirectory);
272-
ConanFileManager.WriteNewRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
266+
ConanFileManager.ReCreateConanfile(projectDirectory);
267+
268+
string conandataPath = Path.Combine(projectDirectory, "conandata.yml");
269+
270+
if (!File.Exists(conandataPath)) {
271+
ConanFileManager.ReCreateConanData(projectDirectory);
272+
}
273+
274+
if (ConanFileManager.IsFileCommentGuarded(conandataPath)) {
275+
ConanFileManager.WriteNewRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
276+
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} added to conandata.yml", "Conan C/C++ Package Manager");
277+
278+
InstallButton.Visibility = Visibility.Collapsed;
279+
RemoveButton.Visibility = Visibility.Visible;
280+
VersionsComboBox.IsEnabled = false;
281+
}
282+
else {
283+
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} could not be added to conandata.yml because it was modified. Please, update the file manually.",
284+
"Conan C/C++ Package Manager",
285+
MessageBoxButton.OK,
286+
MessageBoxImage.Warning);
287+
}
273288

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

285-
MessageBox.Show($"Removing {selectedLibrary} version {selectedVersion}", "Conan C/C++ Package Manager");
286-
287-
InstallButton.Visibility = Visibility.Visible;
288-
RemoveButton.Visibility = Visibility.Collapsed;
289-
290299
ThreadHelper.ThrowIfNotOnUIThread();
291300

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

298-
ConanFileManager.RemoveRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
299-
VersionsComboBox.IsEnabled = true;
307+
string conandataPath = Path.Combine(projectDirectory, "conandata.yml");
308+
309+
if (ConanFileManager.IsFileCommentGuarded(conandataPath)) {
310+
ConanFileManager.RemoveRequirement(projectDirectory, selectedLibrary + "/" + selectedVersion);
311+
MessageBox.Show($"Removing {selectedLibrary} version {selectedVersion}", "Conan C/C++ Package Manager");
312+
313+
InstallButton.Visibility = Visibility.Visible;
314+
RemoveButton.Visibility = Visibility.Collapsed;
315+
VersionsComboBox.IsEnabled = true;
316+
}
317+
else {
318+
MessageBox.Show($"Requirement {selectedLibrary}/{selectedVersion} could not be removed from conandata.yml because it was modified. Please, update the file manually.",
319+
"Conan C/C++ Package Manager",
320+
MessageBoxButton.OK,
321+
MessageBoxImage.Warning);
322+
}
323+
300324
FilterListView(LibrarySearchTextBox.Text, ShowPackagesCheckbox.IsChecked ?? false);
301325
}
302326

ProjectConfigurationManager.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,19 @@ REM Initialize flags
123123
if exist "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" (
124124
echo Checking changes in conandata.yml
125125
fc ""conandata.yml"" "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" > nul
126-
if %errorlevel% equ 1 set performInstall=1
126+
if errorlevel 1 (
127+
set performInstall=1
128+
echo Changes detected in conandata.yml
129+
)
127130
)
128131
129132
if exist "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" (
130133
echo Checking changes in conanfile.py
131134
fc ""conanfile.py"" "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" > nul
132-
if %errorlevel% equ 1 set performInstall=1
135+
if errorlevel 1 (
136+
set performInstall=1
137+
echo Changes detected in conanfile.py
138+
)
133139
)
134140
135141
REM Check for the .runconan file that indicates changes in profiles

source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
33
<Metadata>
4-
<Identity Id="VSConanPackage.4d0379e2-2698-4e66-89de-6ead71165e9f" Version="2.0.5" Language="en-US" Publisher="Conan" />
4+
<Identity Id="VSConanPackage.4d0379e2-2698-4e66-89de-6ead71165e9f" Version="2.0.6" Language="en-US" Publisher="Conan" />
55
<DisplayName>Conan Extension for Visual Studio</DisplayName>
66
<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>
77
<MoreInfo>https://github.com/conan-io/conan-vs-extension</MoreInfo>

0 commit comments

Comments
 (0)